sitcod3 commited on
Commit
882d2b8
·
verified ·
1 Parent(s): 16cf865

Upload src/lib/aiServiceServer.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/lib/aiServiceServer.ts +42 -0
src/lib/aiServiceServer.ts ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import axios from 'axios';
2
+
3
+ const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';
4
+
5
+ const FREE_MISTRAL_MODELS = [
6
+ 'mistralai/mistral-7b-instruct',
7
+ 'mistralai/mixtral-8x7b-instruct',
8
+ 'mistralai/mistral-nemo'
9
+ ];
10
+
11
+ let currentModelIndex = 0;
12
+
13
+ export async function getAiCompletionServer(prompt: string): Promise<string> {
14
+ let attempts = 0;
15
+
16
+ while (attempts < FREE_MISTRAL_MODELS.length) {
17
+ const model = FREE_MISTRAL_MODELS[currentModelIndex];
18
+ try {
19
+ const response = await axios.post(
20
+ OPENROUTER_API_URL,
21
+ {
22
+ model: model,
23
+ messages: [{ role: 'user', content: prompt }],
24
+ },
25
+ {
26
+ headers: {
27
+ 'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
28
+ 'HTTP-Referer': process.env.APP_URL || 'http://localhost:3000',
29
+ 'X-Title': 'BRIDGE ModulS Dashboard',
30
+ },
31
+ }
32
+ );
33
+ return response.data.choices[0].message.content;
34
+ } catch (error) {
35
+ console.error(`Error with model ${model}:`, error);
36
+ attempts++;
37
+ currentModelIndex = (currentModelIndex + 1) % FREE_MISTRAL_MODELS.length;
38
+ }
39
+ }
40
+
41
+ throw new Error('All Mistral models failed.');
42
+ }