modul-s / src /lib /aiServiceServer.ts
sitcod3's picture
Upload src/lib/aiServiceServer.ts with huggingface_hub
882d2b8 verified
Raw History Blame Contribute Delete
1.21 kB
import axios from 'axios';
const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';
const FREE_MISTRAL_MODELS = [
'mistralai/mistral-7b-instruct',
'mistralai/mixtral-8x7b-instruct',
'mistralai/mistral-nemo'
];
let currentModelIndex = 0;
export async function getAiCompletionServer(prompt: string): Promise<string> {
let attempts = 0;
while (attempts < FREE_MISTRAL_MODELS.length) {
const model = FREE_MISTRAL_MODELS[currentModelIndex];
try {
const response = await axios.post(
OPENROUTER_API_URL,
{
model: model,
messages: [{ role: 'user', content: prompt }],
},
{
headers: {
'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
'HTTP-Referer': process.env.APP_URL || 'http://localhost:3000',
'X-Title': 'BRIDGE ModulS Dashboard',
},
}
);
return response.data.choices[0].message.content;
} catch (error) {
console.error(`Error with model ${model}:`, error);
attempts++;
currentModelIndex = (currentModelIndex + 1) % FREE_MISTRAL_MODELS.length;
}
}
throw new Error('All Mistral models failed.');
}