UNPKG

@directus/api

Version:

Directus is a real-time API and App dashboard for managing SQL database content

66 lines (65 loc) 2.38 kB
import { createAnthropic } from '@ai-sdk/anthropic'; import { createGoogleGenerativeAI } from '@ai-sdk/google'; import { createOpenAI } from '@ai-sdk/openai'; import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; import { createProviderRegistry } from 'ai'; export function buildProviderConfigs(settings) { const configs = []; if (settings.openaiApiKey) { configs.push({ type: 'openai', apiKey: settings.openaiApiKey, }); } if (settings.anthropicApiKey) { configs.push({ type: 'anthropic', apiKey: settings.anthropicApiKey, }); } if (settings.googleApiKey) { configs.push({ type: 'google', apiKey: settings.googleApiKey, }); } if (settings.openaiCompatibleApiKey && settings.openaiCompatibleBaseUrl) { configs.push({ type: 'openai-compatible', apiKey: settings.openaiCompatibleApiKey, baseUrl: settings.openaiCompatibleBaseUrl, }); } return configs; } export function createAIProviderRegistry(configs, settings) { const providers = {}; for (const config of configs) { switch (config.type) { case 'openai': providers['openai'] = createOpenAI({ apiKey: config.apiKey }); break; case 'anthropic': providers['anthropic'] = createAnthropic({ apiKey: config.apiKey }); break; case 'google': providers['google'] = createGoogleGenerativeAI({ apiKey: config.apiKey }); break; case 'openai-compatible': if (config.baseUrl) { const customHeaders = settings?.openaiCompatibleHeaders?.reduce((acc, { header, value }) => { acc[header] = value; return acc; }, {}) ?? {}; providers['openai-compatible'] = createOpenAICompatible({ name: settings?.openaiCompatibleName ?? 'openai-compatible', apiKey: config.apiKey, baseURL: config.baseUrl, headers: customHeaders, }); } break; } } return createProviderRegistry(providers); }