2024-10-13 18:53:43 +00:00
|
|
|
// @ts-nocheck
|
|
|
|
// Preventing TS checks with files presented in the video for a better presentation.
|
2024-10-23 15:36:12 +00:00
|
|
|
import { getAPIKey, getBaseURL } from '~/lib/.server/llm/api-key';
|
2024-07-10 16:44:39 +00:00
|
|
|
import { createAnthropic } from '@ai-sdk/anthropic';
|
2024-10-13 18:53:43 +00:00
|
|
|
import { createOpenAI } from '@ai-sdk/openai';
|
2024-10-15 21:47:35 +00:00
|
|
|
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
2024-10-13 18:53:43 +00:00
|
|
|
import { ollama } from 'ollama-ai-provider';
|
2024-10-15 00:40:21 +00:00
|
|
|
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
|
|
|
|
2024-10-13 18:53:43 +00:00
|
|
|
export function getAnthropicModel(apiKey: string, model: string) {
|
2024-07-10 16:44:39 +00:00
|
|
|
const anthropic = createAnthropic({
|
|
|
|
apiKey,
|
|
|
|
});
|
|
|
|
|
2024-10-13 18:53:43 +00:00
|
|
|
return anthropic(model);
|
|
|
|
}
|
2024-11-21 23:09:49 +00:00
|
|
|
|
|
|
|
export function getOpenAILikeModel(baseURL: string, apiKey: string, model: string) {
|
|
|
|
// console.log('OpenAILike config:', { baseURL, hasApiKey: !!apiKey, model });
|
2024-10-23 15:36:12 +00:00
|
|
|
const openai = createOpenAI({
|
|
|
|
baseURL,
|
|
|
|
apiKey,
|
|
|
|
});
|
2024-11-21 23:09:49 +00:00
|
|
|
// console.log('OpenAI client created:', !!openai);
|
|
|
|
const client = openai(model);
|
|
|
|
// console.log('OpenAI model client:', !!client);
|
|
|
|
return client;
|
|
|
|
// return {
|
|
|
|
// model: client,
|
|
|
|
// provider: 'OpenAILike' // Correctly identifying the actual provider
|
|
|
|
// };
|
2024-10-23 15:36:12 +00:00
|
|
|
}
|
2024-11-21 23:09:49 +00:00
|
|
|
|
2024-10-13 18:53:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-10-15 21:47:35 +00:00
|
|
|
export function getGoogleModel(apiKey: string, model: string) {
|
2024-11-13 20:22:08 +00:00
|
|
|
const google = createGoogleGenerativeAI({
|
2024-10-15 21:47:35 +00:00
|
|
|
apiKey,
|
2024-11-13 20:22:08 +00:00
|
|
|
});
|
2024-10-15 21:47:35 +00:00
|
|
|
|
|
|
|
return google(model);
|
|
|
|
}
|
|
|
|
|
2024-10-13 18:53:43 +00:00
|
|
|
export function getGroqModel(apiKey: string, model: string) {
|
|
|
|
const openai = createOpenAI({
|
|
|
|
baseURL: 'https://api.groq.com/openai/v1',
|
|
|
|
apiKey,
|
|
|
|
});
|
|
|
|
|
|
|
|
return openai(model);
|
|
|
|
}
|
|
|
|
|
2024-11-17 23:49:02 +00:00
|
|
|
export function getHuggingFaceModel(apiKey: string, model: string) {
|
|
|
|
const openai = createOpenAI({
|
|
|
|
baseURL: 'https://api-inference.huggingface.co/v1/',
|
|
|
|
apiKey,
|
|
|
|
});
|
|
|
|
|
|
|
|
return openai(model);
|
|
|
|
}
|
|
|
|
|
2024-10-24 08:49:11 +00:00
|
|
|
export function getOllamaModel(baseURL: string, model: string) {
|
2024-11-07 23:51:12 +00:00
|
|
|
let Ollama = ollama(model, {
|
|
|
|
numCtx: 32768,
|
|
|
|
});
|
|
|
|
|
2024-10-24 08:49:11 +00:00
|
|
|
Ollama.config.baseURL = `${baseURL}/api`;
|
|
|
|
return Ollama;
|
2024-10-13 18:53:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-21 23:09:49 +00:00
|
|
|
export function getDeepseekModel(apiKey: string, model: string) {
|
2024-10-22 22:19:27 +00:00
|
|
|
const openai = createOpenAI({
|
|
|
|
baseURL: 'https://api.deepseek.com/beta',
|
|
|
|
apiKey,
|
|
|
|
});
|
|
|
|
|
|
|
|
return openai(model);
|
|
|
|
}
|
2024-10-13 18:53:43 +00:00
|
|
|
|
2024-10-15 00:40:21 +00:00
|
|
|
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({
|
2024-10-30 17:05:57 +00:00
|
|
|
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-11-21 23:09:49 +00:00
|
|
|
|
2024-10-29 03:19:30 +00:00
|
|
|
export function getModel(provider: string, model: string, env: Env, apiKeys?: Record<string, string>) {
|
2024-11-21 23:09:49 +00:00
|
|
|
let apiKey; // Declare first
|
|
|
|
let baseURL;
|
|
|
|
|
|
|
|
apiKey = getAPIKey(env, provider, apiKeys); // Then assign
|
|
|
|
baseURL = getBaseURL(env, provider);
|
|
|
|
|
|
|
|
// console.log('getModel inputs:', { provider, model, baseURL, hasApiKey: !!apiKey });
|
2024-10-15 21:47:35 +00:00
|
|
|
|
2024-10-13 18:53:43 +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);
|
2024-10-15 00:40:21 +00:00
|
|
|
case 'OpenRouter':
|
|
|
|
return getOpenRouterModel(apiKey, model);
|
2024-10-15 21:47:35 +00:00
|
|
|
case 'Google':
|
2024-10-28 03:16:07 +00:00
|
|
|
return getGoogleModel(apiKey, model);
|
2024-10-23 15:36:12 +00:00
|
|
|
case 'OpenAILike':
|
2024-11-21 23:09:49 +00:00
|
|
|
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-10-13 18:53:43 +00:00
|
|
|
default:
|
2024-10-24 08:49:11 +00:00
|
|
|
return getOllamaModel(baseURL, model);
|
2024-10-13 18:53:43 +00:00
|
|
|
}
|
2024-11-21 23:09:49 +00:00
|
|
|
}
|