forked from Gerome-Elassaad/CodingIT
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodels.ts
More file actions
114 lines (104 loc) · 3.64 KB
/
models.ts
File metadata and controls
114 lines (104 loc) · 3.64 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
import { createAnthropic } from '@ai-sdk/anthropic'
import { createFireworks } from '@ai-sdk/fireworks'
import { createGoogleGenerativeAI } from '@ai-sdk/google'
import { createVertex } from '@ai-sdk/google-vertex'
import { createMistral } from '@ai-sdk/mistral'
import { createOpenAI } from '@ai-sdk/openai'
import { createOllama } from 'ollama-ai-provider'
import { createOpenRouter } from '@openrouter/ai-sdk-provider'
export type LLMModel = {
id: string
name: string
provider: string
providerId: string
isBeta?: boolean
}
export type LLMModelConfig = {
model?: string
apiKey?: string
baseURL?: string
temperature?: number
topP?: number
topK?: number
frequencyPenalty?: number
presencePenalty?: number
maxTokens?: number
}
export function getModelClient(model: LLMModel, config: LLMModelConfig) {
const { id: modelNameString, providerId } = model
const { apiKey, baseURL } = config
const providerConfigs = {
anthropic: () => createAnthropic({ apiKey, baseURL })(modelNameString),
openai: () => createOpenAI({ apiKey, baseURL })(modelNameString),
google: () =>
createGoogleGenerativeAI({ apiKey, baseURL })(modelNameString),
mistral: () => createMistral({ apiKey, baseURL })(modelNameString),
groq: () =>
createOpenAI({
apiKey: apiKey || process.env.GROQ_API_KEY,
baseURL: baseURL || 'https://api.groq.com/openai/v1',
})(modelNameString),
togetherai: () =>
createOpenAI({
apiKey: apiKey || process.env.TOGETHER_API_KEY,
baseURL: baseURL || 'https://api.together.xyz/v1',
})(modelNameString),
ollama: () => createOllama({ baseURL })(modelNameString),
fireworks: () =>
createFireworks({
apiKey: apiKey || process.env.FIREWORKS_API_KEY,
baseURL: baseURL || 'https://api.fireworks.ai/inference/v1',
})(modelNameString),
vertex: () => {
const vertexCredentials = process.env.GOOGLE_VERTEX_CREDENTIALS;
// Handle both API key and JSON credentials
if (!vertexCredentials) {
// Fallback to Google AI SDK if no Vertex credentials
return createGoogleGenerativeAI({
apiKey: apiKey || process.env.GOOGLE_AI_API_KEY
})(modelNameString);
}
// Try to parse as JSON first (service account credentials)
try {
const credentials = JSON.parse(vertexCredentials);
return createVertex({
googleAuthOptions: { credentials },
})(modelNameString);
} catch {
// If not JSON, treat as API key and use Google AI SDK instead
return createGoogleGenerativeAI({
apiKey: vertexCredentials || apiKey || process.env.GOOGLE_AI_API_KEY
})(modelNameString);
}
},
xai: () =>
createOpenAI({
apiKey: apiKey || process.env.XAI_API_KEY,
baseURL: baseURL || 'https://api.x.ai/v1',
})(modelNameString),
deepseek: () =>
createOpenAI({
apiKey: apiKey || process.env.DEEPSEEK_API_KEY,
baseURL: baseURL || 'https://api.deepseek.com/v1',
})(modelNameString),
openrouter: () =>
createOpenRouter({
apiKey: apiKey || process.env.OPENROUTER_API_KEY,
baseURL: baseURL || 'https://openrouter.ai/api/v1',
})(modelNameString),
}
const createClient =
providerConfigs[providerId as keyof typeof providerConfigs]
if (!createClient) {
throw new Error(`Unsupported provider: ${providerId}`)
}
return createClient()
}
export function getDefaultModelParams(model: LLMModel) {
// Return default parameters for the model
// This can be customized per provider/model if needed
return {
temperature: 0.7,
maxTokens: 4096,
}
}