mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-01-23 11:17:02 +00:00
76cc7a8139
- Create detailed provider implementation guide with: - Architecture overview and implementation steps - Configuration patterns and best practices - Testing checklist and Docker integration guide - Example using Together AI implementation - Add Together AI as new provider with: - Environment variables and Docker configuration - Support for Qwen, Llama, and Mixtral models - API key and base URL management - OpenAI-compatible API integration
165 lines
4.2 KiB
TypeScript
165 lines
4.2 KiB
TypeScript
/*
|
|
* @ts-nocheck
|
|
* Preventing TS checks with files presented in the video for a better presentation.
|
|
*/
|
|
import { getAPIKey, getBaseURL } from '~/lib/.server/llm/api-key';
|
|
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';
|
|
import { createMistral } from '@ai-sdk/mistral';
|
|
import { createCohere } from '@ai-sdk/cohere';
|
|
import type { LanguageModelV1 } from 'ai';
|
|
|
|
export const DEFAULT_NUM_CTX = process.env.DEFAULT_NUM_CTX ? parseInt(process.env.DEFAULT_NUM_CTX, 10) : 32768;
|
|
|
|
type OptionalApiKey = string | undefined;
|
|
|
|
export function getAnthropicModel(apiKey: OptionalApiKey, model: string) {
|
|
const anthropic = createAnthropic({
|
|
apiKey,
|
|
});
|
|
|
|
return anthropic(model);
|
|
}
|
|
export function getOpenAILikeModel(baseURL: string, apiKey: OptionalApiKey, model: string) {
|
|
const openai = createOpenAI({
|
|
baseURL,
|
|
apiKey,
|
|
});
|
|
|
|
return openai(model);
|
|
}
|
|
|
|
export function getCohereAIModel(apiKey: OptionalApiKey, model: string) {
|
|
const cohere = createCohere({
|
|
apiKey,
|
|
});
|
|
|
|
return cohere(model);
|
|
}
|
|
|
|
export function getOpenAIModel(apiKey: OptionalApiKey, model: string) {
|
|
const openai = createOpenAI({
|
|
apiKey,
|
|
});
|
|
|
|
return openai(model);
|
|
}
|
|
|
|
export function getMistralModel(apiKey: OptionalApiKey, model: string) {
|
|
const mistral = createMistral({
|
|
apiKey,
|
|
});
|
|
|
|
return mistral(model);
|
|
}
|
|
|
|
export function getGoogleModel(apiKey: OptionalApiKey, model: string) {
|
|
const google = createGoogleGenerativeAI({
|
|
apiKey,
|
|
});
|
|
|
|
return google(model);
|
|
}
|
|
|
|
export function getGroqModel(apiKey: OptionalApiKey, model: string) {
|
|
const openai = createOpenAI({
|
|
baseURL: 'https://api.groq.com/openai/v1',
|
|
apiKey,
|
|
});
|
|
|
|
return openai(model);
|
|
}
|
|
|
|
export function getHuggingFaceModel(apiKey: OptionalApiKey, model: string) {
|
|
const openai = createOpenAI({
|
|
baseURL: 'https://api-inference.huggingface.co/v1/',
|
|
apiKey,
|
|
});
|
|
|
|
return openai(model);
|
|
}
|
|
|
|
export function getOllamaModel(baseURL: string, model: string) {
|
|
const ollamaInstance = ollama(model, {
|
|
numCtx: DEFAULT_NUM_CTX,
|
|
}) as LanguageModelV1 & { config: any };
|
|
|
|
ollamaInstance.config.baseURL = `${baseURL}/api`;
|
|
|
|
return ollamaInstance;
|
|
}
|
|
|
|
export function getDeepseekModel(apiKey: OptionalApiKey, model: string) {
|
|
const openai = createOpenAI({
|
|
baseURL: 'https://api.deepseek.com/beta',
|
|
apiKey,
|
|
});
|
|
|
|
return openai(model);
|
|
}
|
|
|
|
export function getOpenRouterModel(apiKey: OptionalApiKey, model: string) {
|
|
const openRouter = createOpenRouter({
|
|
apiKey,
|
|
});
|
|
|
|
return openRouter.chat(model);
|
|
}
|
|
|
|
export function getLMStudioModel(baseURL: string, model: string) {
|
|
const lmstudio = createOpenAI({
|
|
baseUrl: `${baseURL}/v1`,
|
|
apiKey: '',
|
|
});
|
|
|
|
return lmstudio(model);
|
|
}
|
|
|
|
export function getXAIModel(apiKey: OptionalApiKey, model: string) {
|
|
const openai = createOpenAI({
|
|
baseURL: 'https://api.x.ai/v1',
|
|
apiKey,
|
|
});
|
|
|
|
return openai(model);
|
|
}
|
|
|
|
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 'HuggingFace':
|
|
return getHuggingFaceModel(apiKey, model);
|
|
case 'OpenRouter':
|
|
return getOpenRouterModel(apiKey, model);
|
|
case 'Google':
|
|
return getGoogleModel(apiKey, model);
|
|
case 'OpenAILike':
|
|
return getOpenAILikeModel(baseURL, apiKey, model);
|
|
case 'Together':
|
|
return getOpenAILikeModel(baseURL, apiKey, model);
|
|
case 'Deepseek':
|
|
return getDeepseekModel(apiKey, model);
|
|
case 'Mistral':
|
|
return getMistralModel(apiKey, model);
|
|
case 'LMStudio':
|
|
return getLMStudioModel(baseURL, model);
|
|
case 'xAI':
|
|
return getXAIModel(apiKey, model);
|
|
case 'Cohere':
|
|
return getCohereAIModel(apiKey, model);
|
|
default:
|
|
return getOllamaModel(baseURL, model);
|
|
}
|
|
}
|