fix: make openai client initialization conditional

Co-Authored-By: Han Xiao <han.xiao@jina.ai>
This commit is contained in:
Devin AI 2025-02-05 10:29:51 +00:00
parent 8293464127
commit c81eb0e32a

View File

@ -17,17 +17,19 @@ export class LLMClient {
constructor() {
this.geminiClient = new GoogleGenerativeAI(GEMINI_API_KEY);
this.openaiClient = new OpenAI({
apiKey: OPENAI_API_KEY,
baseURL: llmConfig.baseURL
});
if (llmConfig.provider === 'openai') {
this.openaiClient = new OpenAI({
apiKey: OPENAI_API_KEY,
baseURL: llmConfig.baseURL
});
}
}
async generateContent(model: any, prompt: string) {
if (llmConfig.provider === 'gemini') {
const result = await model.generateContent(prompt);
return result;
} else {
} else if (this.openaiClient) {
const completion = await model.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }],
@ -43,18 +45,20 @@ export class LLMClient {
}
};
}
throw new Error('OpenAI client not initialized. Set OPENAI_API_KEY and provider="openai" to use OpenAI.');
}
getModel(options: LLMClientOptions) {
if (llmConfig.provider === 'gemini') {
return this.geminiClient.getGenerativeModel(options);
} else {
} else if (this.openaiClient) {
return {
...this.openaiClient.chat.completions,
temperature: options.temperature,
generateContent: (prompt: string) => this.generateContent(this.openaiClient.chat.completions, prompt)
};
}
throw new Error('OpenAI client not initialized. Set OPENAI_API_KEY and provider="openai" to use OpenAI.');
}
}