Download src/lib/aiServiceServer.ts from sitcod3/modul-s: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/sitcod3/modul-s/resolve/main/src/lib/aiServiceServer.ts
- Command line
-
hf download hf://spaces/sitcod3/modul-s/src/lib/aiServiceServer.ts
-
curl -L -o aiServiceServer.ts https://huggingface.co/spaces/sitcod3/modul-s/resolve/main/src/lib/aiServiceServer.ts
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.'); | |
| } | |