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