Skip to content

Commit

Permalink
feat: switch to other model if ratelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
RedNotSus committed Sep 15, 2024
1 parent 0fe3bf0 commit 5802426
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const cors = require("cors");

const axios = require("axios");
const dotenv = require("dotenv");
const tokenLimit = 100;
let tokenUsage = 0;
const modelPrimary = "llama-3.1-70b-versatile";
const modelBackup = "llama3-groq-70b-8192-tool-use-preview";
dotenv.config();

const server = http.createServer();
Expand All @@ -27,17 +31,18 @@ app.post("/api/chat", async (req, res) => {
const { message, userId } = req.body;

if (!userId) {
return res.status(400).json({ error: "User ID is required" });
return res.status(400).json({ error: "Not enough arguments" });
}

let conversation = activeConversations.get(userId) || [];
conversation.push({ role: "user", content: message });

try {
const modelToUse = tokenUsage < tokenLimit ? modelPrimary : modelBackup;
const response = await axios.post(
"https://api.groq.com/openai/v1/chat/completions",
{
model: "llama-3.1-70b-versatile",
model: modelToUse,
messages: conversation,
temperature: 0.7,
max_tokens: 1024,
Expand All @@ -47,12 +52,12 @@ app.post("/api/chat", async (req, res) => {
Authorization: `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
},
}
);

const aiResponse = response.data.choices[0].message.content;
conversation.push({ role: "assistant", content: aiResponse });

tokenUsage += response.data.usage.total_tokens;
if (conversation.length > 10) {
conversation = conversation.slice(-10);
}
Expand Down Expand Up @@ -99,7 +104,7 @@ async function fetchDataFromGithub(
res,
next,
baseUrl,
secondaryUrl = null,
secondaryUrl = null
) {
function isAFile(urlString) {
return urlString.trim().split("/").pop().length !== 0;
Expand Down

0 comments on commit 5802426

Please sign in to comment.