feat: switch chatbot from Gemini to Groq (llama-3.3-70b, 14400 RPD free)
This commit is contained in:
1 parent
6727361c6e
commit
0c0977a220
4 files changed
+64
-78
No files matched your search
+28
-29
@@ -75,41 +75,40 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
console.warn("Backend API call failed, attempting client-side fallback. Details:", error);
|
||||
}
|
||||
|
||||
// CLIENT-SIDE FALLBACK (e.g. for InfinityFree or local cert errors)
|
||||
// CLIENT-SIDE FALLBACK: use Groq directly (OpenAI-compatible, 14,400 RPD free)
|
||||
if (!replySuccess) {
|
||||
const config = window.chatbotConfig;
|
||||
if (config && config.apiKey && config.systemPrompt) {
|
||||
if (config && config.groqApiKey && config.systemPrompt) {
|
||||
try {
|
||||
// Structure payload for Gemini client-side and inject systemInstructions in first user message
|
||||
let firstMsgPrepended = false;
|
||||
const contents = chatHistory.map(msg => {
|
||||
let text = msg.text;
|
||||
const role = msg.role === 'user' ? 'user' : 'model';
|
||||
if (!firstMsgPrepended && role === 'user') {
|
||||
text = `System Context & Instructions:\n${config.systemPrompt}\n\nUser Message: ${text}`;
|
||||
firstMsgPrepended = true;
|
||||
}
|
||||
return {
|
||||
role: role,
|
||||
parts: [{ text: text }]
|
||||
};
|
||||
// Build OpenAI-compatible messages array
|
||||
const messages = [
|
||||
{ role: "system", content: config.systemPrompt }
|
||||
];
|
||||
|
||||
// Append history (excluding the latest user message which we add separately)
|
||||
chatHistory.slice(0, -1).forEach(msg => {
|
||||
messages.push({
|
||||
role: msg.role === "user" ? "user" : "assistant",
|
||||
content: msg.text
|
||||
});
|
||||
});
|
||||
|
||||
// Add current user message
|
||||
messages.push({ role: "user", content: message });
|
||||
|
||||
const requestPayload = {
|
||||
contents: contents,
|
||||
generationConfig: {
|
||||
temperature: 0.7,
|
||||
maxOutputTokens: 800
|
||||
}
|
||||
model: "llama-3.3-70b-versatile", // 14,400 RPD free
|
||||
messages: messages,
|
||||
temperature: 0.7,
|
||||
max_tokens: 800,
|
||||
stream: false
|
||||
};
|
||||
|
||||
// gemini-2.0-flash-lite: highest free-tier quota (1,500 req/day)
|
||||
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-lite:generateContent?key=${encodeURIComponent(config.apiKey)}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
const response = await fetch("https://api.groq.com/openai/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${config.groqApiKey}`
|
||||
},
|
||||
body: JSON.stringify(requestPayload)
|
||||
});
|
||||
@@ -117,8 +116,8 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
const data = await response.json();
|
||||
removeTypingIndicator(typingIndicator);
|
||||
|
||||
if (response.ok && data.candidates && data.candidates[0].content.parts[0].text) {
|
||||
const botReply = data.candidates[0].content.parts[0].text;
|
||||
if (response.ok && data.choices && data.choices[0].message.content) {
|
||||
const botReply = data.choices[0].message.content;
|
||||
addMessage(botReply, "bot-msg");
|
||||
chatHistory.push({ role: "model", text: botReply });
|
||||
replySuccess = true;
|
||||
@@ -129,11 +128,11 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||
} catch (fallbackError) {
|
||||
removeTypingIndicator(typingIndicator);
|
||||
addMessage("Unable to connect to assistant. Please check your internet connection.", "bot-msg error-msg");
|
||||
console.error("Direct API Fallback Error:", fallbackError);
|
||||
console.error("Groq Fallback Error:", fallbackError);
|
||||
}
|
||||
} else {
|
||||
removeTypingIndicator(typingIndicator);
|
||||
addMessage("API request blocked by host firewall and fallback config is missing.", "bot-msg error-msg");
|
||||
addMessage("Chatbot is temporarily unavailable. Please try again later.", "bot-msg error-msg");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user