From ef058afb1790f4a64675f9467592443027b6f67d Mon Sep 17 00:00:00 2001 From: Neko Date: Wed, 22 Nov 2023 16:08:46 +0800 Subject: [PATCH] fix(next/api): initialize openai when api key provided --- next/api/src/service/openai.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/next/api/src/service/openai.ts b/next/api/src/service/openai.ts index faf536c2a..fb8ff69cc 100644 --- a/next/api/src/service/openai.ts +++ b/next/api/src/service/openai.ts @@ -28,18 +28,15 @@ const OpenAIOutputSchema = z.object({ export type TicketClassifyResult = z.infer; export class OpenAIService { - active: boolean; - instance: InstanceType; + instance?: InstanceType; agent?: InstanceType; constructor() { const apiKey = process.env.OPENAI_API_KEY; const httpProxy = process.env.http_proxy; - this.instance = new OpenAI({ apiKey }); if (!apiKey) { console.warn('OPENAI_API_KEY not provided, disabling openAIService...'); - this.active = false; return; } @@ -47,11 +44,11 @@ export class OpenAIService { this.agent = new HttpsProxyAgent(httpProxy); } - this.active = true; + this.instance = new OpenAI({ apiKey }); } async classify(content: string, categories: Category[]) { - if (!this.active) { + if (!this.instance) { return undefined; } @@ -64,7 +61,7 @@ export class OpenAIService { const res = await (async () => { try { const res = ( - await this.instance.chat.completions.create( + await this.instance!.chat.completions.create( { response_format: { type: 'json_object',