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

177 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-11-21 21:05:35 +00:00
/*
* @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';
2024-11-21 21:05:35 +00:00
import { createOpenRouter } from '@openrouter/ai-sdk-provider';
2024-10-22 03:52:36 +00:00
import { createMistral } from '@ai-sdk/mistral';
2024-11-21 21:05:35 +00:00
import { createCohere } from '@ai-sdk/cohere';
import type { LanguageModelV1 } from 'ai';
2024-12-11 08:32:21 +00:00
import type { IProviderSetting } from '~/types/model';
2024-07-10 16:44:39 +00:00
2024-11-22 16:48:46 +00:00
export const DEFAULT_NUM_CTX = process.env.DEFAULT_NUM_CTX ? parseInt(process.env.DEFAULT_NUM_CTX, 10) : 32768;
2024-07-10 16:44:39 +00:00
type OptionalApiKey = string | undefined;
2024-11-22 16:48:46 +00:00
export function getAnthropicModel(apiKey: OptionalApiKey, model: string) {
2024-07-10 16:44:39 +00:00
const anthropic = createAnthropic({
apiKey,
});
return anthropic(model);
}
2024-11-22 16:48:46 +00:00
export function getOpenAILikeModel(baseURL: string, apiKey: OptionalApiKey, model: string) {
const openai = createOpenAI({
baseURL,
apiKey,
});
return openai(model);
}
2024-11-20 15:08:42 +00:00
2024-11-22 16:48:46 +00:00
export function getCohereAIModel(apiKey: OptionalApiKey, model: string) {
2024-11-20 15:08:42 +00:00
const cohere = createCohere({
apiKey,
});
return cohere(model);
}
2024-11-21 23:09:49 +00:00
2024-11-22 16:48:46 +00:00
export function getOpenAIModel(apiKey: OptionalApiKey, model: string) {
const openai = createOpenAI({
apiKey,
});
return openai(model);
}
2024-11-22 16:48:46 +00:00
export function getMistralModel(apiKey: OptionalApiKey, model: string) {
2024-10-22 03:52:36 +00:00
const mistral = createMistral({
2024-11-21 21:05:35 +00:00
apiKey,
2024-10-22 03:52:36 +00:00
});
return mistral(model);
}
2024-11-22 16:48:46 +00:00
export function getGoogleModel(apiKey: OptionalApiKey, 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);
}
2024-11-22 16:48:46 +00:00
export function getGroqModel(apiKey: OptionalApiKey, model: string) {
const openai = createOpenAI({
baseURL: 'https://api.groq.com/openai/v1',
apiKey,
});
return openai(model);
}
2024-11-22 16:48:46 +00:00
export function getHuggingFaceModel(apiKey: OptionalApiKey, model: string) {
2024-11-17 23:49:02 +00:00
const openai = createOpenAI({
baseURL: 'https://api-inference.huggingface.co/v1/',
apiKey,
});
return openai(model);
}
export function getOllamaModel(baseURL: string, model: string) {
2024-11-22 16:48:46 +00:00
const ollamaInstance = ollama(model, {
numCtx: DEFAULT_NUM_CTX,
}) as LanguageModelV1 & { config: any };
2024-11-07 23:51:12 +00:00
2024-11-21 21:27:29 +00:00
ollamaInstance.config.baseURL = `${baseURL}/api`;
2024-11-21 21:05:35 +00:00
2024-11-21 21:27:29 +00:00
return ollamaInstance;
}
2024-11-22 16:48:46 +00:00
export function getDeepseekModel(apiKey: OptionalApiKey, model: string) {
2024-10-22 22:19:27 +00:00
const openai = createOpenAI({
baseURL: 'https://api.deepseek.com/beta',
apiKey,
});
return openai(model);
}
2024-11-22 16:48:46 +00:00
export function getOpenRouterModel(apiKey: OptionalApiKey, model: string) {
const openRouter = createOpenRouter({
2024-11-21 21:05:35 +00:00
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-21 21:05:35 +00:00
apiKey: '',
2024-10-28 03:16:07 +00:00
});
return lmstudio(model);
}
2024-11-22 16:48:46 +00:00
export function getXAIModel(apiKey: OptionalApiKey, model: string) {
2024-11-07 00:03:37 +00:00
const openai = createOpenAI({
baseURL: 'https://api.x.ai/v1',
apiKey,
});
return openai(model);
}
2024-11-21 23:09:49 +00:00
2024-12-11 08:32:21 +00:00
export function getModel(
provider: string,
model: string,
env: Env,
apiKeys?: Record<string, string>,
providerSettings?: Record<string, IProviderSetting>,
) {
2024-11-30 03:02:35 +00:00
/*
* let apiKey; // Declare first
* let baseURL;
*/
2024-11-21 23:09:49 +00:00
2024-11-30 03:02:35 +00:00
const apiKey = getAPIKey(env, provider, apiKeys); // Then assign
2024-12-11 08:32:21 +00:00
const baseURL = providerSettings?.[provider].baseUrl || getBaseURL(env, provider);
2024-11-21 23:09:49 +00:00
switch (provider) {
case 'Anthropic':
return getAnthropicModel(apiKey, model);
case 'OpenAI':
return getOpenAIModel(apiKey, model);
case 'Groq':
return getGroqModel(apiKey, model);
2024-11-17 23:49:02 +00:00
case 'HuggingFace':
return getHuggingFaceModel(apiKey, model);
case 'OpenRouter':
return getOpenRouterModel(apiKey, model);
case 'Google':
2024-10-28 03:16:07 +00:00
return getGoogleModel(apiKey, model);
case 'OpenAILike':
2024-11-21 23:09:49 +00:00
return getOpenAILikeModel(baseURL, apiKey, model);
case 'Together':
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':
2024-11-21 23:09:49 +00:00
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);
2024-11-20 15:08:42 +00:00
case 'Cohere':
return getCohereAIModel(apiKey, model);
default:
return getOllamaModel(baseURL, model);
}
2024-07-10 16:44:39 +00:00
}