import OpenAI from 'openai'; import { env } from '$env/dynamic/private'; export const createLLM = () => { // Detect if we are using Ollama or OpenAI based on URL const baseURL = env.OPENAI_BASE_URL; const apiKey = env.OPENAI_API_KEY; const model = env.LLM_MODEL || 'gpt-4o'; console.log('[LLM] Initializing client...'); console.log('[LLM] Base URL:', baseURL); console.log('[LLM] Model:', model); if (!baseURL) { throw new Error('OPENAI_BASE_URL environment variable is not set'); } if (!apiKey) { throw new Error('OPENAI_API_KEY environment variable is not set'); } const client = new OpenAI({ apiKey, baseURL }); return { client, model }; }; /** * Health check for LLM service */ export async function checkLLMHealth(): Promise { try { const { client } = createLLM(); await client.models.list(); console.log('[LLM] Health check passed'); return true; } catch (e) { console.error('[LLM] Health check failed:', e); return false; } }