diff --git a/src/lib/ai-client.ts b/src/lib/ai-client.ts index e0ad683..4f82cff 100644 --- a/src/lib/ai-client.ts +++ b/src/lib/ai-client.ts @@ -1,6 +1,19 @@ import { OpenAI } from 'openai'; -export const aiClient = new OpenAI({ - baseURL: 'https://api.tokenfactory.nebius.com/v1/', - apiKey: process.env.NEBIUS_API_KEY, -}); +/** + * Creates a Nebius AI client using the provided API key. + * This is wrapped in a function to avoid top-level initialization errors + * if the environment variables are not yet loaded or are missing. + */ +export function getNebiusClient() { + const apiKey = process.env.NEBIUS_API_KEY; + + if (!apiKey) { + throw new Error('NEBIUS_API_KEY is not configured.'); + } + + return new OpenAI({ + baseURL: 'https://api.tokenfactory.nebius.com/v1/', + apiKey: apiKey, + }); +} diff --git a/src/services/analyze-bottle-nebius.ts b/src/services/analyze-bottle-nebius.ts index d73304b..1721c53 100644 --- a/src/services/analyze-bottle-nebius.ts +++ b/src/services/analyze-bottle-nebius.ts @@ -1,6 +1,6 @@ 'use server'; -import { aiClient } from '@/lib/ai-client'; +import { getNebiusClient } from '@/lib/ai-client'; import { SYSTEM_INSTRUCTION as GEMINI_SYSTEM_INSTRUCTION } from '@/lib/gemini'; import { BottleMetadataSchema, AnalysisResponse, BottleMetadata } from '@/types/whisky'; import { createServerActionClient } from '@supabase/auth-helpers-nextjs'; @@ -56,7 +56,8 @@ export async function analyzeBottleNebius(base64Image: string, tags?: string[], console.log(`[analyzeBottleNebius] Instruction prepared for AI: ${instruction.substring(0, 100)}...`); - const response = await aiClient.chat.completions.create({ + const client = getNebiusClient(); + const response = await client.chat.completions.create({ model: "Qwen/Qwen2.5-VL-72B-Instruct", messages: [ {