@@ -2,6 +2,7 @@ const { getAIModel } = require("../models/model");
22const logger = require ( "../utils/logger" ) ;
33const { FALLBACK_AI_MODEL , SCHEMA_PROMPT_BUDGET_CHARS } = require ( "../core/constants" ) ;
44const { buildTableCatalog, buildSchemaDSL } = require ( "../utils/schemaCompressor" ) ;
5+ const { inferProviderFromModel, PROVIDER_API_ENV_KEYS } = require ( "../core/env" ) ;
56
67const argv = require ( "minimist" ) ( process . argv . slice ( 2 ) ) ;
78
@@ -46,14 +47,26 @@ class LLMService {
4647 process . env . AI_MODEL ||
4748 FALLBACK_AI_MODEL
4849 ) . toLowerCase ( ) ;
49- const key = apiKey || argv . apikey || process . env . AI_API_KEY || null ;
50+
51+ let key = apiKey || argv . apikey || null ;
52+ if ( ! key ) {
53+ const provider = inferProviderFromModel ( aiModel ) ;
54+ const providerEnvKey = PROVIDER_API_ENV_KEYS [ provider ] ;
55+ key = providerEnvKey ? process . env [ providerEnvKey ] : null ;
56+ // Fallback to generic AI_API_KEY if provider-specific key not found
57+ if ( ! key ) {
58+ key = process . env . AI_API_KEY || null ;
59+ }
60+ }
5061
5162 // Only reinitialize if config changed
5263 if ( this . config . model !== aiModel || this . config . apiKey !== key ) {
5364 this . config . model = aiModel ;
5465 this . config . apiKey = key ;
5566 this . llm = getAIModel ( aiModel , key ) ;
56- logger . info ( `LLM initialized with model: ${ aiModel } ` ) ;
67+ logger . info (
68+ `LLM initialized with model: ${ aiModel } , provider: ${ inferProviderFromModel ( aiModel ) } ` ,
69+ ) ;
5770 }
5871
5972 return this . llm ;
0 commit comments