Skip to content

Commit

Permalink
Update OpenAI package
Browse files Browse the repository at this point in the history
  • Loading branch information
floscher committed Jun 20, 2024
1 parent 9b9f379 commit f3333db
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 56 deletions.
169 changes: 131 additions & 38 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
"express": "^4.18.2",
"jose": "^4.13.1",
"multer": "^1.4.5-lts.1",
"openai": "^3.2.1",
"openai": "^4.52.0",
"pg": "^8.4.0",
"reflect-metadata": "^0.1.13",
"reflect-metadata": "^0.2.2",
"typeorm": "^0.3.15",
"winston": "^3.8.2",
"winston-daily-rotate-file": "^4.7.1"
"winston-daily-rotate-file": "^5.0.0"
},
"devDependencies": {
"@faker-js/faker": "^8.0.1",
Expand Down
28 changes: 13 additions & 15 deletions server/src/service/openai.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { AiSummaryData, base64ToBuffer, determineMimeType, SupportedImageMimeType } from "@fumix/fu-blog-common";
import console from "console";
import { Configuration, OpenAIApi } from "openai";
import OpenAI from "openai";
import logger from "../logger.js";
import { OpenAISettings } from "../settings.js";

const openai = new OpenAIApi(
new Configuration({
apiKey: OpenAISettings.API_KEY,
}),
);
const openai = new OpenAI({
apiKey: OpenAISettings.API_KEY,
});

export async function dallEGenerateImage(prompt: string): Promise<[SupportedImageMimeType, Buffer]> {
console.log("Create image");
return openai
.createImage({
return openai.images
.generate({
prompt,
size: "512x512",
n: 1,
response_format: "b64_json",
})
.then(({ data, status }) => {
const base64 = data.data[0]?.b64_json;
.then((it) => {
const base64 = it.data[0]?.b64_json;
if (base64) {
const buffer = base64ToBuffer(base64);
const mimeType = determineMimeType(buffer);
Expand All @@ -34,8 +32,8 @@ export async function dallEGenerateImage(prompt: string): Promise<[SupportedImag
}

export async function chatGptSummarize(text: string): Promise<AiSummaryData> {
return openai
.createChatCompletion({
return openai.chat.completions
.create({
model: "gpt-3.5-turbo",
messages: [
{
Expand All @@ -56,15 +54,15 @@ Es enthält eine knappe für Endnutzer verständliche Beschreibung, was das Prob
},
],
})
.then<AiSummaryData>(({ data, status }) => {
const answer = data.choices?.[0]?.message?.content;
.then<AiSummaryData>((it) => {
const answer = it.choices?.[0]?.message?.content;
if (answer) {
const dto = JSON.parse(answer) as AiSummaryData;
if (dto) {
return dto;
}
}
logger.error(`Could not decode ChatGPT response ${JSON.stringify(data)} (${status})`);
logger.error(`Could not decode ChatGPT response ${JSON.stringify(it)}`);
return { error: `Could not decode ChatGPT response (${status})` };
})
.catch((error) => {
Expand Down

0 comments on commit f3333db

Please sign in to comment.