Преглед изворни кода

✨ feat(ai): update openai dependency and service implementation

- add openai@5.20.2 package to dependencies
- update AI service to use new OpenAI v5+ API syntax
- replace Configuration and OpenAIApi with new OpenAI class initialization
- update chat completion method from createChatCompletion to chat.completions.create
- adjust response data access path from response.data.choices to response.choices
yourname пре 2 месеци
родитељ
комит
54f4603a33
3 измењених фајлова са 25 додато и 6 уклоњено
  1. 1 0
      package.json
  2. 19 0
      pnpm-lock.yaml
  3. 5 6
      src/server/services/ai.service.ts

+ 1 - 0
package.json

@@ -72,6 +72,7 @@
     "mysql2": "^3.14.2",
     "next-themes": "^0.4.6",
     "open-docxtemplater-image-module-2": "^1.0.2",
+    "openai": "^5.20.2",
     "pdf-lib": "^1.17.1",
     "pdf-merger-js": "^4.3.0",
     "pizzip": "^3.2.0",

+ 19 - 0
pnpm-lock.yaml

@@ -194,6 +194,9 @@ importers:
       open-docxtemplater-image-module-2:
         specifier: ^1.0.2
         version: 1.0.2
+      openai:
+        specifier: ^5.20.2
+        version: 5.20.2(zod@4.0.15)
       pdf-lib:
         specifier: ^1.17.1
         version: 1.17.1
@@ -2719,6 +2722,18 @@ packages:
   open-docxtemplater-image-module-2@1.0.2:
     resolution: {integrity: sha512-Qj7ofO0/0sxobwPMPxflq87oVBoUdhKQ9L0+CmZBvsTrmPEhYKoGEUftMEkSWqhMW2fKgL3GaDVjbySzZ5OG3g==}
 
+  openai@5.20.2:
+    resolution: {integrity: sha512-ZeBIEazj6W/XOulMw92L8iU5FblQeM5IxjPRCZNVArNXsoF+bLPnS7B2X4O0JSmpCM+F5Gdajr6bn4nAUj4LFQ==}
+    hasBin: true
+    peerDependencies:
+      ws: ^8.18.0
+      zod: ^3.23.8
+    peerDependenciesMeta:
+      ws:
+        optional: true
+      zod:
+        optional: true
+
   openapi3-ts@4.5.0:
     resolution: {integrity: sha512-jaL+HgTq2Gj5jRcfdutgRGLosCy/hT8sQf6VOy+P+g36cZOjI1iukdPnijC+4CmeRzg/jEllJUboEic2FhxhtQ==}
 
@@ -6045,6 +6060,10 @@ snapshots:
     dependencies:
       xmldom: 0.1.31
 
+  openai@5.20.2(zod@4.0.15):
+    optionalDependencies:
+      zod: 4.0.15
+
   openapi3-ts@4.5.0:
     dependencies:
       yaml: 2.8.0

+ 5 - 6
src/server/services/ai.service.ts

@@ -1,4 +1,4 @@
-import { OpenAIApi, Configuration } from 'openai';
+import OpenAI from 'openai';
 import * as dotenv from 'dotenv';
 
 dotenv.config();
@@ -10,7 +10,7 @@ export interface AIGenerationOptions {
 }
 
 export class AIService {
-  private openai: OpenAIApi | null = null;
+  private openai: OpenAI | null = null;
 
   constructor() {
     this.initializeOpenAI();
@@ -24,10 +24,9 @@ export class AIService {
     }
 
     try {
-      const configuration = new Configuration({
+      this.openai = new OpenAI({
         apiKey: apiKey,
       });
-      this.openai = new OpenAIApi(configuration);
       console.log('OpenAI service initialized successfully');
     } catch (error) {
       console.error('Failed to initialize OpenAI service:', error);
@@ -52,7 +51,7 @@ export class AIService {
     } = options;
 
     try {
-      const response = await this.openai.createChatCompletion({
+      const response = await this.openai.chat.completions.create({
         model,
         messages: [
           {
@@ -68,7 +67,7 @@ export class AIService {
         max_tokens: maxTokens,
       });
 
-      const result = response.data.choices[0]?.message?.content;
+      const result = response.choices[0]?.message?.content;
       if (!result) {
         throw new Error('No response from AI service');
       }