Skip to content

Commit

Permalink
🍎 Chore: openai v4둜 버전업
Browse files Browse the repository at this point in the history
#
  • Loading branch information
ks1ksi committed Sep 7, 2023
1 parent f5ee35e commit d817b6a
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 48 deletions.
204 changes: 170 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"class-validator": "^0.14.0",
"multer": "^1.4.4",
"nodemailer": "^6.9.4",
"openai": "^3.3.0",
"openai": "^4.5.0",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"reflect-metadata": "^0.1.13",
Expand Down
27 changes: 14 additions & 13 deletions src/openai/open-ai.service.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Configuration, OpenAIApi } from 'openai';
import OpenAI from 'openai';

@Injectable()
export class OpenAiService {
private readonly openAiConfig: Configuration;
private readonly openAi: OpenAIApi;
private readonly openai: OpenAI;
private logger = new Logger(OpenAiService.name);

constructor(private readonly configService: ConfigService) {
this.openAiConfig = new Configuration({
this.openai = new OpenAI({
apiKey: configService.get<string>('OPENAI_API_KEY'),
});
this.openAi = new OpenAIApi(this.openAiConfig);
}

async getEmbedding(text: string): Promise<number[]> {
try {
const result = await this.openAi.createEmbedding({
const result = await this.openai.embeddings.create({
model: 'text-embedding-ada-002',
input: text,
});
return result.data.data[0].embedding;
} catch (e) {
if (e.response) {
this.logger.error(e.response.status);
this.logger.error(e.response.data);

return result.data[0].embedding;
} catch (error) {
if (error.response) {
this.logger.error(error.response.status); // e.g. 401
this.logger.error(error.response.data.message); // e.g. The authentication token you passed was invalid...
this.logger.error(error.response.data.code); // e.g. 'invalid_api_key'
this.logger.error(error.response.data.type); // e.g. 'invalid_request_error'
} else {
this.logger.error(e);
this.logger.error(error);
}
throw e;
throw error;
}
}
}

0 comments on commit d817b6a

Please sign in to comment.