-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodelConfig.ts
More file actions
315 lines (288 loc) · 9.66 KB
/
Copy pathmodelConfig.ts
File metadata and controls
315 lines (288 loc) · 9.66 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { ChatMessage } from 'llamaindex';
import { Gemini, GEMINI_MODEL } from '@llamaindex/google';
import { Anthropic } from '@llamaindex/anthropic';
import { OpenAI } from '@llamaindex/openai'
import { GoogleGenAI } from '@google/genai';
type Provider =
| 'openai'
| 'anthropic'
| 'gemini'
| 'google'
| 'publicai'
| 'swiss-ai'
| 'aisingapore'
| 'BSC-LT';
type LLMInstance = OpenAI | Anthropic | Gemini;
const getApiKey = (provider: Provider): string => {
switch (provider) {
case 'openai':
return process.env.OPENAI_API_KEY || '';
case 'anthropic':
return process.env.ANTHROPIC_API_KEY || '';
case 'gemini':
case 'google':
return process.env.GOOGLE_API_KEY || '';
case 'publicai':
case 'swiss-ai':
case 'aisingapore':
case 'BSC-LT':
return process.env.PUBLICAI_API_KEY || '';
default:
throw new Error(`Unsupported provider: ${provider}`);
}
};
const getGeminiModel = (modelName: string) => {
const model = GEMINI_MODEL[modelName as keyof typeof GEMINI_MODEL];
if (!model) {
throw new Error(
`Invalid Gemini model: ${modelName}. Must be one of: ${Object.values(GEMINI_MODEL).join(', ')}.\n\nPossibly this list is outdated, in which case you may need to update @llamaindex/gemini`,
);
}
console.log(`[i] Using Gemini model: ${model}`);
return model;
};
interface ChatInterface {
chat(params: { messages: ChatMessage[] }): Promise<any>;
}
/**
* Lists available Gemini models using the Google GenAI SDK
* @returns Promise<string[]> Array of available model names
*/
async function listAvailableGeminiModels(): Promise<string[]> {
try {
const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) {
console.warn('No GOOGLE_API_KEY found for listing models');
return [];
}
const genAI = new GoogleGenAI({ apiKey });
const models = await genAI.models.list();
const modelNames: string[] = [];
for await (const model of models) {
if (model.name) {
modelNames.push(model.name);
}
}
console.log('Available Gemini models:', modelNames);
return modelNames;
} catch (error) {
console.error('Error listing Gemini models:', error);
return [];
}
}
import { getPostHogClient } from './posthog-server';
// A wrapper class that normalizes the chat interface
// (each LLM's chat & response is unfortunately slightly different,
// so creating a class that ensures they're handled properly and return a consistent response)
export class LLM {
private llm: LLMInstance & ChatInterface;
private model: string;
private provider: string;
constructor(llm: LLMInstance, model: string, provider: string) {
this.llm = llm as LLMInstance & ChatInterface;
this.model = model;
this.provider = provider;
}
async chat(params: {
messages: ChatMessage[];
distinctId?: string;
tag?: string;
sessionIds?: string[];
hostIds?: string[];
}): Promise<string> {
const startTime = Date.now();
// All LLMs actually accept the same message format, even though they specify it differently.
// In TS we have to have the ChatInterface to prevent type errors.
let responseString = '';
try {
console.log(
`[i] Sending chat request to LLM:\n${JSON.stringify(this.llm.metadata, null, 2)}
with params:\n\n`,
JSON.stringify(params, null, 2)
);
const response = await this.llm.chat(params);
console.log(
'[i] Chat response content:',
JSON.stringify(response.message.content)
);
// Normalize response format based on provider type
if (response.message.content instanceof Array) {
responseString = response.message.content
.map((c: any) => c.text)
.join('');
} else {
responseString = response.message.content.toString();
}
const endTime = Date.now();
const client = getPostHogClient();
if (client) {
// Extract usage stats if available (structure varies by provider/library version)
const rawResponse = response.raw;
let inputTokens = 0;
let outputTokens = 0;
// Attempt to parse common usage formats
if (rawResponse && typeof rawResponse === 'object') {
const raw = rawResponse as any;
// 1. OpenAI style (often used by LlamaIndex OpenAI provider)
// Structure: { usage: { prompt_tokens: 10, completion_tokens: 20, ... } }
if (raw.usage) {
inputTokens =
raw.usage.prompt_tokens || raw.usage.input_tokens || 0;
outputTokens =
raw.usage.completion_tokens || raw.usage.output_tokens || 0;
}
// 2. Anthropic style
// Structure: { usage: { input_tokens: 10, output_tokens: 20 } }
// Note: LlamaIndex might map this to the standard 'usage' object above, but checking raw keys safely
else if (
raw.input_tokens !== undefined &&
raw.output_tokens !== undefined
) {
inputTokens = raw.input_tokens;
outputTokens = raw.output_tokens;
}
// 3. Google Gemini style
// Structure: { usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 20 } }
else if (raw.usageMetadata) {
inputTokens = raw.usageMetadata.promptTokenCount || 0;
outputTokens = raw.usageMetadata.candidatesTokenCount || 0;
}
}
// Map specific internal model names to OpenRouter standard names for PostHog cost calculation
// https://openrouter.ai/models
let reportedModel = this.model;
if (this.model.includes('2_0_FLASH')) {
reportedModel = 'google/gemini-2.0-flash-exp';
}
client.capture({
distinctId: params.distinctId || 'anonymous_server_user',
event: '$ai_generation',
properties: {
$ai_model: reportedModel,
$ai_provider: this.provider,
$ai_input: params.messages,
$ai_output_choices: [
{
message: {
role: 'assistant',
content: responseString,
},
},
],
$ai_latency: (endTime - startTime) / 1000,
$ai_input_tokens: inputTokens,
$ai_output_tokens: outputTokens,
$ai_status: 'success',
$ai_trace_id: crypto.randomUUID(),
tag: params.tag,
session_ids: params.sessionIds,
host_ids: params.hostIds,
},
});
}
} catch (error) {
console.error('Error in LLM chat:', error);
const isError = (err: unknown): err is Error => {
return err instanceof Error;
};
// Handle specific Gemini model availability issues
if (isError(error) && error.message.includes('ListModels')) {
console.warn(
'Model may not be available. Attempting to list available models...'
);
// List available models to help diagnose the issue
const availableModels = await listAvailableGeminiModels();
if (availableModels.length > 0) {
console.error(
`Please update the model to use one of the following:\nAvailable Gemini models:`,
availableModels
);
} else {
console.error(
'No available Gemini models found. Please check your API key and permissions.'
);
}
const userFacingError = new Error(
`Apologies, the backend API provider has changed their models.\nPlease contact support with this error message and where it occurred.`
);
throw userFacingError;
}
const endTime = Date.now();
const client = getPostHogClient();
if (client) {
client.capture({
distinctId: params.distinctId || 'anonymous_server_user',
event: '$ai_generation',
properties: {
$ai_model: this.model,
$ai_provider: this.provider,
$ai_input: params.messages,
$ai_latency: (endTime - startTime) / 1000,
$ai_status: 'error',
$ai_error: error instanceof Error ? error.message : String(error),
$ai_trace_id: crypto.randomUUID(),
tag: params.tag,
session_ids: params.sessionIds,
host_ids: params.hostIds,
},
});
}
throw error;
}
return responseString;
}
}
export const getLLM = (
type: 'SMALL' | 'MAIN' | 'LARGE',
temperature = 0.7
): LLM => {
const model = process.env[`${type}_LLM_MODEL`];
const provider = process.env[`${type}_LLM_PROVIDER`] as Provider;
if (!model || !provider) {
throw new Error(`Missing configuration for ${type}_LLM`);
}
const apiKey = getApiKey(provider);
let llm: LLMInstance;
switch (provider) {
case 'openai':
llm = new OpenAI({
model,
apiKey,
temperature,
});
break;
case 'anthropic':
llm = new Anthropic({
model,
apiKey,
temperature,
});
break;
case 'gemini':
llm = new Gemini({
model: getGeminiModel(model),
temperature,
});
break;
case 'publicai':
case 'swiss-ai':
case 'aisingapore':
case 'BSC-LT':
console.log(`Preparing PublicAI model ${provider}/${model}`);
llm = new OpenAI({
model: `${provider}/${model}`,
apiKey,
temperature,
baseURL: 'https://api.publicai.co/v1',
additionalSessionOptions: {
defaultHeaders: {
'User-Agent': 'Harmonica/1.0',
},
},
});
break;
default:
throw new Error(`Provider ${provider} not implemented`);
}
return new LLM(llm, model, provider);
};