bolt.diy/app/lib/.server/llm/model.ts

135 lines
3.3 KiB
TypeScript
Raw Normal View History

// @ts-nocheck
// Preventing TS checks with files presented in the video for a better presentation.
import { getAPIKey, getBaseURL } from '~/lib/.server/llm/api-key';
2024-07-10 16:44:39 +00:00
import { createAnthropic } from '@ai-sdk/anthropic';
import { createOpenAI } from '@ai-sdk/openai';
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { ollama } from 'ollama-ai-provider';
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
2024-10-22 03:52:36 +00:00
import { createMistral } from '@ai-sdk/mistral';
2024-07-10 16:44:39 +00:00
export const DEFAULT_NUM_CTX = process.env.DEFAULT_NUM_CTX ?
parseInt(process.env.DEFAULT_NUM_CTX, 10) :
32768;
export function getAnthropicModel(apiKey: string, model: string) {
2024-07-10 16:44:39 +00:00
const anthropic = createAnthropic({
apiKey,
});
return anthropic(model);
}
export function getOpenAILikeModel(baseURL:string,apiKey: string, model: string) {
const openai = createOpenAI({
baseURL,
apiKey,
});
return openai(model);
}
export function getOpenAIModel(apiKey: string, model: string) {
const openai = createOpenAI({
apiKey,
});
return openai(model);
}
2024-10-22 03:52:36 +00:00
export function getMistralModel(apiKey: string, model: string) {
const mistral = createMistral({
apiKey
});
return mistral(model);
}
export function getGoogleModel(apiKey: string, model: string) {
2024-11-13 20:22:08 +00:00
const google = createGoogleGenerativeAI({
apiKey,
2024-11-13 20:22:08 +00:00
});
return google(model);
}
export function getGroqModel(apiKey: string, model: string) {
const openai = createOpenAI({
baseURL: 'https://api.groq.com/openai/v1',
apiKey,
});
return openai(model);
}
export function getOllamaModel(baseURL: string, model: string) {
2024-11-07 23:51:12 +00:00
let Ollama = ollama(model, {
numCtx: DEFAULT_NUM_CTX,
2024-11-07 23:51:12 +00:00
});
Ollama.config.baseURL = `${baseURL}/api`;
return Ollama;
}
2024-10-22 22:19:27 +00:00
export function getDeepseekModel(apiKey: string, model: string){
const openai = createOpenAI({
baseURL: 'https://api.deepseek.com/beta',
apiKey,
});
return openai(model);
}
export function getOpenRouterModel(apiKey: string, model: string) {
const openRouter = createOpenRouter({
apiKey
});
return openRouter.chat(model);
}
2024-10-28 03:16:07 +00:00
export function getLMStudioModel(baseURL: string, model: string) {
const lmstudio = createOpenAI({
baseUrl: `${baseURL}/v1`,
2024-11-11 02:08:01 +00:00
apiKey: "",
2024-10-28 03:16:07 +00:00
});
return lmstudio(model);
}
2024-11-07 00:03:37 +00:00
export function getXAIModel(apiKey: string, model: string) {
const openai = createOpenAI({
baseURL: 'https://api.x.ai/v1',
apiKey,
});
return openai(model);
}
2024-10-29 03:19:30 +00:00
export function getModel(provider: string, model: string, env: Env, apiKeys?: Record<string, string>) {
const apiKey = getAPIKey(env, provider, apiKeys);
const baseURL = getBaseURL(env, provider);
switch (provider) {
case 'Anthropic':
return getAnthropicModel(apiKey, model);
case 'OpenAI':
return getOpenAIModel(apiKey, model);
case 'Groq':
return getGroqModel(apiKey, model);
case 'OpenRouter':
return getOpenRouterModel(apiKey, model);
case 'Google':
2024-10-28 03:16:07 +00:00
return getGoogleModel(apiKey, model);
case 'OpenAILike':
return getOpenAILikeModel(baseURL,apiKey, model);
2024-10-22 22:19:27 +00:00
case 'Deepseek':
2024-10-28 03:16:07 +00:00
return getDeepseekModel(apiKey, model);
2024-10-22 03:52:36 +00:00
case 'Mistral':
return getMistralModel(apiKey, model);
2024-10-28 03:16:07 +00:00
case 'LMStudio':
return getLMStudioModel(baseURL, model);
2024-11-07 00:03:37 +00:00
case 'xAI':
return getXAIModel(apiKey, model);
default:
return getOllamaModel(baseURL, model);
}
2024-07-10 16:44:39 +00:00
}