Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from deprecated @azure/openai package to openai #3050

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 info
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved
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.10.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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the new library, apiVersion is a required field. In the old library, it wasn't required and the library had a default (see https://github.com/minhanh-phan/azure-sdk-for-js/blob/39b9c4251908c5eb60964cd8f7706fa2efd5e6b9/sdk/openai/openai/src/rest/openAIClient.ts#L22)

I copied this default and hope that it will continue to work. We can make the field required in v8

});
}

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.

Loading