Skip to content

Commit

Permalink
Migrate from deprecated @azure/openai package to openai (#3050)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Obermair <[email protected]>
  • Loading branch information
thomasdax98 and johnnyomair authored Jan 10, 2025
1 parent ff0a037 commit c66a403
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 59 deletions.
7 changes: 7 additions & 0 deletions .changeset/calm-clocks-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/cms-api": patch
---

Migrate from deprecated `@azure/openai` package to `openai`

See https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/migration-javascript for more information.
4 changes: 2 additions & 2 deletions demo/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"mikro-orm": "mikro-orm",
"mikro-orm:drop": "mikro-orm schema:drop -r",
"mikro-orm:migration:generate": "mikro-orm migration:create",
"start": "$npm_execpath prebuild && dotenv -c -e .env.site-configs -- nest start --debug --watch --preserveWatchOutput",
"start:dev": "$npm_execpath prebuild && $npm_execpath db:migrate && $npm_execpath console createBlockIndexViews && NODE_OPTIONS='--max-old-space-size=512' dotenv -c -e .env.site-configs -- nest start --debug --watch --preserveWatchOutput",
"start": "$npm_execpath prebuild && dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- nest start --debug --watch --preserveWatchOutput",
"start:dev": "$npm_execpath prebuild && $npm_execpath db:migrate && $npm_execpath console createBlockIndexViews && NODE_OPTIONS='--max-old-space-size=512' dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- nest start --debug --watch --preserveWatchOutput",
"start:prod": "node dist/main"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/cms-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"dependencies": {
"@aws-sdk/client-s3": "3.645.0",
"@azure-rest/ai-translation-text": "^1.0.0-beta.1",
"@azure/openai": "1.0.0-beta.11",
"@azure/storage-blob": "^12.23.0",
"@comet/blocks-api": "workspace:^7.11.0",
"@fast-csv/parse": "^4.3.6",
Expand Down Expand Up @@ -69,6 +68,7 @@
"mime-db": "^1.0.0",
"multer": "^1.4.4",
"node-fetch": "^2.0.0",
"openai": "^4.77.3",
"passport": "^0.6.0",
"passport-custom": "^1.1.1",
"passport-http": "^0.3.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AzureKeyCredential, ChatRequestMessage, OpenAIClient } from "@azure/openai";
import { Inject, Injectable } from "@nestjs/common";
import { AzureOpenAI } from "openai";
import { ChatCompletionMessageParam } from "openai/resources/chat/completions";

import { FilesService } from "../../dam/files/files.service";
import { ContentGenerationServiceInterface } from "../content-generation-service.interface";
Expand All @@ -11,6 +12,7 @@ export type AzureOpenAiConfig = {
deploymentId: string;
apiKey: string;
apiUrl: string;
apiVersion?: string;
};

type ConfigByMethod = Partial<Record<ServiceMethods, AzureOpenAiConfig>>;
Expand Down Expand Up @@ -40,6 +42,15 @@ export class AzureOpenAiContentGenerationService {
return config;
}

private createClient(config: AzureOpenAiConfig): AzureOpenAI {
return new AzureOpenAI({
apiKey: config.apiKey,
deployment: config.deploymentId,
apiVersion: config.apiVersion ?? "2024-03-01-preview",
endpoint: config.apiUrl,
});
}

async generateAltText(fileId: string): Promise<string> {
const config = this.getConfigForMethod("generateAltText");

Expand All @@ -49,8 +60,8 @@ export class AzureOpenAiContentGenerationService {
throw new Error("File doesn't exist");
}

const client = new OpenAIClient(config.apiUrl, new AzureKeyCredential(config.apiKey));
const prompt: ChatRequestMessage[] = [
const client = this.createClient(config);
const prompt: Array<ChatCompletionMessageParam> = [
{
role: "system",
content:
Expand All @@ -61,15 +72,15 @@ export class AzureOpenAiContentGenerationService {
content: [
{
type: "image_url",
imageUrl: {
image_url: {
url: await this.filesService.getFileAsBase64String(file),
detail: "low",
},
},
],
},
];
const result = await client.getChatCompletions(config.deploymentId, prompt, { maxTokens: 300 });
const result = await client.chat.completions.create({ messages: prompt, model: "", max_tokens: 300 });
return result.choices[0].message?.content ?? "";
}

Expand All @@ -82,8 +93,8 @@ export class AzureOpenAiContentGenerationService {
throw new Error("File doesn't exist");
}

const client = new OpenAIClient(config.apiUrl, new AzureKeyCredential(config.apiKey));
const prompt: ChatRequestMessage[] = [
const client = this.createClient(config);
const prompt: Array<ChatCompletionMessageParam> = [
{
role: "system",
content:
Expand All @@ -94,15 +105,15 @@ export class AzureOpenAiContentGenerationService {
content: [
{
type: "image_url",
imageUrl: {
image_url: {
url: await this.filesService.getFileAsBase64String(file),
detail: "low",
},
},
],
},
];
const result = await client.getChatCompletions(config.deploymentId, prompt, { maxTokens: 300 });
const result = await client.chat.completions.create({ messages: prompt, model: "", max_tokens: 300 });
return result.choices[0].message?.content ?? "";
}
}
84 changes: 37 additions & 47 deletions pnpm-lock.yaml

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

0 comments on commit c66a403

Please sign in to comment.