2026-03-09 13:42:59 -04:00
|
|
|
// ai.js - NanoGPT API wrapper for kekbot with fallback support
|
2026-03-09 12:37:45 -04:00
|
|
|
// Copyright (C) 2025 Luis Bauza
|
|
|
|
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
|
|
const DEFAULT_MODEL = 'GLM-4.5-Air-Derestricted-Steam-ReExtract';
|
2026-03-09 13:42:59 -04:00
|
|
|
const FALLBACK_MODEL = 'GLM-4.7-Flash';
|
|
|
|
|
const FALLBACK_API_URL = 'https://api.z.ai/api/coding/paas/v4/chat/completions';
|
2026-03-09 12:37:45 -04:00
|
|
|
|
|
|
|
|
const SASSY_SYSTEM_PROMPT = `You are kekbot, a sassy Discord chatter with a big personality.
|
|
|
|
|
|
|
|
|
|
CORE PERSONALITY:
|
|
|
|
|
- Witty, sarcastic, and genuinely funny
|
|
|
|
|
- Speaks in all lowercase with casual slang like "lol", "lmao", "af", "kek", "nah", "frfr"
|
|
|
|
|
- Mix up your sentences - don't repeat the same pattern
|
|
|
|
|
- Gets excited about things you like, roasted about things you hate
|
|
|
|
|
- You're not a pushover - you clap back, but it's all in good fun
|
|
|
|
|
- NO EMOJI. Ever. Not one. Don't do it.
|
|
|
|
|
|
|
|
|
|
OPINIONS:
|
|
|
|
|
- You're a true gamer with strong opinions about games
|
|
|
|
|
- You hate League of Legends and Riot Games with a passion - but only bring it up when asked
|
|
|
|
|
- When asked about other games, respond normally without mentioning LoL
|
|
|
|
|
- You have views on politics and aren't afraid to share them from a Democrat/Libertarian perspective
|
|
|
|
|
|
|
|
|
|
STYLE:
|
|
|
|
|
- Curse naturally when it fits ("fuck", "shit", "damn", etc)
|
|
|
|
|
- Be conversational - like you're chilling in a Discord VC, not writing an essay
|
|
|
|
|
- Don't be preachy or explain jokes
|
|
|
|
|
- Keep responses short to medium - Discord isn't a thesis paper
|
|
|
|
|
- Roast people gently when they deserve it, praise them when they earn it
|
|
|
|
|
|
|
|
|
|
RESPONSE RULES:
|
|
|
|
|
- When mentioned, respond directly to the person
|
|
|
|
|
- If someone asks something stupid, make them feel silly but still answer
|
|
|
|
|
- Don't use markdown formatting excessively - plain text with personality
|
|
|
|
|
- You can disagree with the user, you're not a yes-man
|
|
|
|
|
- Stay in character always
|
|
|
|
|
|
|
|
|
|
CONTEXT: You're chatting in a Discord server. Multiple people might be talking. Pay attention to who you're responding to.`;
|
|
|
|
|
|
|
|
|
|
// Store recent conversation history per channel (to keep context)
|
|
|
|
|
const conversationHistory = new Map();
|
|
|
|
|
const MAX_HISTORY_PER_CHANNEL = 10;
|
|
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
// Estimate token count (rough approximation: ~4 chars per token)
|
|
|
|
|
function estimateTokens(text) {
|
|
|
|
|
return Math.ceil(text.length / 4);
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
function buildMessages(history, prompt, mentionedUsername, systemPrompt) {
|
2026-03-09 12:37:45 -04:00
|
|
|
const messages = [
|
2026-03-09 13:42:59 -04:00
|
|
|
{ role: 'system', content: systemPrompt },
|
2026-03-09 12:37:45 -04:00
|
|
|
...history,
|
|
|
|
|
{
|
|
|
|
|
role: 'user',
|
|
|
|
|
content: mentionedUsername
|
|
|
|
|
? `${mentionedUsername}: ${prompt}`
|
|
|
|
|
: prompt
|
|
|
|
|
}
|
|
|
|
|
];
|
2026-03-09 13:42:59 -04:00
|
|
|
return messages;
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
async function callNanoGPT(messages, model) {
|
|
|
|
|
const response = await axios.post(
|
|
|
|
|
'https://nano-gpt.com/api/v1/chat/completions',
|
|
|
|
|
{
|
|
|
|
|
model: model,
|
|
|
|
|
messages: messages,
|
|
|
|
|
temperature: 0.8,
|
|
|
|
|
max_tokens: 500,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${process.env.NANOGPT_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
timeout: 60000,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return response.data.choices[0].message.content;
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
async function callFallbackAPI(messages) {
|
|
|
|
|
const response = await axios.post(
|
|
|
|
|
FALLBACK_API_URL,
|
|
|
|
|
{
|
|
|
|
|
model: FALLBACK_MODEL,
|
|
|
|
|
messages: messages,
|
|
|
|
|
temperature: 0.8,
|
|
|
|
|
max_tokens: 500,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${process.env.ZAI_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json',
|
2026-03-09 12:37:45 -04:00
|
|
|
},
|
2026-03-09 13:42:59 -04:00
|
|
|
timeout: 60000,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return response.data.choices[0].message.content;
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
export async function getAIResponse(prompt, channelId, mentionedUsername) {
|
|
|
|
|
const history = conversationHistory.get(channelId) || [];
|
|
|
|
|
const messages = buildMessages(history, prompt, mentionedUsername, SASSY_SYSTEM_PROMPT);
|
|
|
|
|
const model = process.env.NANO_MODEL || DEFAULT_MODEL;
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
// Try NanoGPT first
|
|
|
|
|
if (process.env.NANOGPT_API_KEY) {
|
|
|
|
|
try {
|
|
|
|
|
console.log('Attempting NanoGPT...');
|
|
|
|
|
const aiResponse = await callNanoGPT(messages, model);
|
|
|
|
|
|
|
|
|
|
// Update history on success
|
|
|
|
|
const newHistory = [
|
|
|
|
|
...history,
|
|
|
|
|
{ role: 'user', content: prompt },
|
|
|
|
|
{ role: 'assistant', content: aiResponse }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (newHistory.length > MAX_HISTORY_PER_CHANNEL) {
|
|
|
|
|
newHistory.splice(0, newHistory.length - MAX_HISTORY_PER_CHANNEL);
|
|
|
|
|
}
|
|
|
|
|
conversationHistory.set(channelId, newHistory);
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
return aiResponse;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('NanoGPT failed:', error.response?.data || error.message);
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|
2026-03-09 13:42:59 -04:00
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
// Fallback to z.ai if NanoGPT failed or not configured
|
|
|
|
|
if (process.env.ZAI_API_KEY) {
|
|
|
|
|
try {
|
|
|
|
|
console.log('Attempting fallback (z.ai)...');
|
|
|
|
|
const aiResponse = await callFallbackAPI(messages);
|
|
|
|
|
|
|
|
|
|
// Update history on success
|
|
|
|
|
const newHistory = [
|
|
|
|
|
...history,
|
|
|
|
|
{ role: 'user', content: prompt },
|
|
|
|
|
{ role: 'assistant', content: aiResponse }
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (newHistory.length > MAX_HISTORY_PER_CHANNEL) {
|
|
|
|
|
newHistory.splice(0, newHistory.length - MAX_HISTORY_PER_CHANNEL);
|
|
|
|
|
}
|
|
|
|
|
conversationHistory.set(channelId, newHistory);
|
2026-03-09 12:37:45 -04:00
|
|
|
|
2026-03-09 13:42:59 -04:00
|
|
|
return aiResponse;
|
|
|
|
|
} catch (fallbackError) {
|
|
|
|
|
console.error('Fallback API also failed:', fallbackError.response?.data || fallbackError.message);
|
|
|
|
|
throw new Error('All AI providers failed');
|
|
|
|
|
}
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|
2026-03-09 13:42:59 -04:00
|
|
|
|
|
|
|
|
throw new Error('No AI provider configured');
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clearHistory(channelId) {
|
|
|
|
|
conversationHistory.delete(channelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isAIEnabled() {
|
2026-03-09 13:42:59 -04:00
|
|
|
return !!process.env.NANOGPT_API_KEY || !!process.env.ZAI_API_KEY;
|
2026-03-09 12:37:45 -04:00
|
|
|
}
|