- Try NanoGPT first, fall back to z.ai GLM-4.7-Flash on failure - Add ZAI_API_KEY environment variable - Update deploy workflow with new secret
This commit is contained in:
@@ -42,6 +42,7 @@ jobs:
|
|||||||
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
|
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
|
||||||
CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}
|
CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}
|
||||||
NANOGPT_API_KEY=${{ secrets.NANOGPT_API_KEY }}
|
NANOGPT_API_KEY=${{ secrets.NANOGPT_API_KEY }}
|
||||||
|
ZAI_API_KEY=${{ secrets.ZAI_API_KEY }}
|
||||||
NANO_MODEL=${{ secrets.NANO_MODEL }}
|
NANO_MODEL=${{ secrets.NANO_MODEL }}
|
||||||
NODE_ENV=production
|
NODE_ENV=production
|
||||||
EOF
|
EOF
|
||||||
|
|||||||
+75
-15
@@ -1,9 +1,11 @@
|
|||||||
// ai.js - NanoGPT API wrapper for kekbot
|
// ai.js - NanoGPT API wrapper for kekbot with fallback support
|
||||||
// Copyright (C) 2025 Luis Bauza
|
// Copyright (C) 2025 Luis Bauza
|
||||||
|
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
const DEFAULT_MODEL = 'GLM-4.5-Air-Derestricted-Steam-ReExtract';
|
const DEFAULT_MODEL = 'GLM-4.5-Air-Derestricted-Steam-ReExtract';
|
||||||
|
const FALLBACK_MODEL = 'GLM-4.7-Flash';
|
||||||
|
const FALLBACK_API_URL = 'https://api.z.ai/api/coding/paas/v4/chat/completions';
|
||||||
|
|
||||||
const SASSY_SYSTEM_PROMPT = `You are kekbot, a sassy Discord chatter with a big personality.
|
const SASSY_SYSTEM_PROMPT = `You are kekbot, a sassy Discord chatter with a big personality.
|
||||||
|
|
||||||
@@ -41,12 +43,14 @@ CONTEXT: You're chatting in a Discord server. Multiple people might be talking.
|
|||||||
const conversationHistory = new Map();
|
const conversationHistory = new Map();
|
||||||
const MAX_HISTORY_PER_CHANNEL = 10;
|
const MAX_HISTORY_PER_CHANNEL = 10;
|
||||||
|
|
||||||
export async function getAIResponse(prompt, channelId, mentionedUsername) {
|
// Estimate token count (rough approximation: ~4 chars per token)
|
||||||
const history = conversationHistory.get(channelId) || [];
|
function estimateTokens(text) {
|
||||||
|
return Math.ceil(text.length / 4);
|
||||||
|
}
|
||||||
|
|
||||||
// Build messages array with system prompt and history
|
function buildMessages(history, prompt, mentionedUsername, systemPrompt) {
|
||||||
const messages = [
|
const messages = [
|
||||||
{ role: 'system', content: SASSY_SYSTEM_PROMPT },
|
{ role: 'system', content: systemPrompt },
|
||||||
...history,
|
...history,
|
||||||
{
|
{
|
||||||
role: 'user',
|
role: 'user',
|
||||||
@@ -55,10 +59,10 @@ export async function getAIResponse(prompt, channelId, mentionedUsername) {
|
|||||||
: prompt
|
: prompt
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
const model = process.env.NANO_MODEL || DEFAULT_MODEL;
|
async function callNanoGPT(messages, model) {
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
'https://nano-gpt.com/api/v1/chat/completions',
|
'https://nano-gpt.com/api/v1/chat/completions',
|
||||||
{
|
{
|
||||||
@@ -75,34 +79,90 @@ export async function getAIResponse(prompt, channelId, mentionedUsername) {
|
|||||||
timeout: 60000,
|
timeout: 60000,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
return response.data.choices[0].message.content;
|
||||||
|
}
|
||||||
|
|
||||||
const aiResponse = response.data.choices[0].message.content;
|
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',
|
||||||
|
},
|
||||||
|
timeout: 60000,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return response.data.choices[0].message.content;
|
||||||
|
}
|
||||||
|
|
||||||
// Add to conversation history
|
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;
|
||||||
|
|
||||||
|
// 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 = [
|
const newHistory = [
|
||||||
...history,
|
...history,
|
||||||
{ role: 'user', content: prompt },
|
{ role: 'user', content: prompt },
|
||||||
{ role: 'assistant', content: aiResponse }
|
{ role: 'assistant', content: aiResponse }
|
||||||
];
|
];
|
||||||
|
|
||||||
// Trim history if too long
|
|
||||||
if (newHistory.length > MAX_HISTORY_PER_CHANNEL) {
|
if (newHistory.length > MAX_HISTORY_PER_CHANNEL) {
|
||||||
newHistory.splice(0, newHistory.length - MAX_HISTORY_PER_CHANNEL);
|
newHistory.splice(0, newHistory.length - MAX_HISTORY_PER_CHANNEL);
|
||||||
}
|
}
|
||||||
|
|
||||||
conversationHistory.set(channelId, newHistory);
|
conversationHistory.set(channelId, newHistory);
|
||||||
|
|
||||||
return aiResponse;
|
return aiResponse;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('NanoGPT API error:', error.response?.data || error.message);
|
console.error('NanoGPT failed:', error.response?.data || error.message);
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
return aiResponse;
|
||||||
|
} catch (fallbackError) {
|
||||||
|
console.error('Fallback API also failed:', fallbackError.response?.data || fallbackError.message);
|
||||||
|
throw new Error('All AI providers failed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error('No AI provider configured');
|
||||||
|
}
|
||||||
|
|
||||||
export function clearHistory(channelId) {
|
export function clearHistory(channelId) {
|
||||||
conversationHistory.delete(channelId);
|
conversationHistory.delete(channelId);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isAIEnabled() {
|
export function isAIEnabled() {
|
||||||
return !!process.env.NANOGPT_API_KEY;
|
return !!process.env.NANOGPT_API_KEY || !!process.env.ZAI_API_KEY;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user