File size: 1,212 Bytes
882d2b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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.');
}