Skip to content

Commit

Permalink
fix(next/api): initialize openai when api key provided
Browse files Browse the repository at this point in the history
  • Loading branch information
NotEvenANeko committed Nov 22, 2023
1 parent 0acefbd commit ef058af
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions next/api/src/service/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,27 @@ const OpenAIOutputSchema = z.object({
export type TicketClassifyResult = z.infer<typeof OpenAIOutputSchema>;

export class OpenAIService {
active: boolean;
instance: InstanceType<typeof OpenAI>;
instance?: InstanceType<typeof OpenAI>;
agent?: InstanceType<typeof HttpsProxyAgent>;

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;
}

if (httpProxy) {
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;
}

Expand All @@ -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',
Expand Down

0 comments on commit ef058af

Please sign in to comment.