Skip to content

Commit b980243

Browse files
authored
[openai] Migrate openai projects to use snippets extraction (#33264)
### Packages impacted by this PR - @azure/openai ### Issues associated with this PR - #32416 ### Describe the problem that is addressed by this PR Updates all projects under `openai` to use snippets extraction. ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary)
1 parent 13605d6 commit b980243

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+443
-295
lines changed

common/tools/dev-tool/src/commands/admin/list/snippets-migrations.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function setMigrationResult(
9494
if (
9595
packageJson.scripts &&
9696
packageJson.scripts["update-snippets"] &&
97-
packageJson.scripts["update-snippets"] === "dev-tool run update-snippets"
97+
packageJson.scripts["update-snippets"].includes("dev-tool run update-snippets")
9898
) {
9999
results.snippets[project.packageName] = project.projectFolder;
100100
results.totalSnippets++;

common/tools/dev-tool/src/commands/admin/migrate-package.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,13 @@ async function fixApiExtractorConfig(apiExtractorJsonPath: string): Promise<void
219219
}
220220
const apiExtractorJson = JSON.parse(await readFile(apiExtractorJsonPath, "utf-8"));
221221

222-
const oldPath = apiExtractorJson.dtsRollup.publicTrimmedFilePath;
223-
const projectName = basename(oldPath, ".d.ts");
222+
if (apiExtractorJson.dtsRollup.publicTrimmedFilePath) {
223+
const oldPath = apiExtractorJson.dtsRollup.publicTrimmedFilePath;
224+
const projectName = basename(oldPath, ".d.ts");
225+
apiExtractorJson.dtsRollup.publicTrimmedFilePath = `dist/${projectName}.d.ts`;
226+
}
224227

225228
apiExtractorJson.mainEntryPointFilePath = "dist/esm/index.d.ts";
226-
apiExtractorJson.dtsRollup.publicTrimmedFilePath = `dist/${projectName}.d.ts`;
227229

228230
// TODO: Clean up the betaTrimmedFilePath
229231
delete apiExtractorJson.dtsRollup.betaTrimmedFilePath;
@@ -233,7 +235,7 @@ async function fixApiExtractorConfig(apiExtractorJsonPath: string): Promise<void
233235

234236
async function cleanupFiles(projectFolder: string): Promise<void> {
235237
// Remove the old test files
236-
const filesToRemove = ["karma.conf.js", "karma.conf.cjs", ".nycrc"];
238+
const filesToRemove = ["karma.conf.js", "karma.conf.cjs", ".nycrc", ".mocharc.json"];
237239
for (const file of filesToRemove) {
238240
try {
239241
await unlink(resolve(projectFolder, file));

sdk/openai/openai/.mocharc.json

-5
This file was deleted.

sdk/openai/openai/CHANGELOG.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- dev-tool snippets ignore -->
2+
13
# Release History
24

35
## 2.1.0-beta.1 (Unreleased)
@@ -71,17 +73,18 @@ This release adds types for Azure features supported in Azure OpenAI Service API
7173
- Adds a new property `logprobs` in `ChatChoice` to support log probabilities for this chat choice
7274
- Adds new properties `logprobs` and `topLogprobs` in `ChatCompletionsOptions` class to support log probabilities for chat completions
7375
- Adds `dimensions` in `GetEmbeddingsOptions`.
74-
when using Azure OpenAI, specifies the input type to use for embedding search.
76+
when using Azure OpenAI, specifies the input type to use for embedding search.
7577
- Updates the default service API version to `2024-03-01-preview`
7678
- Returns content filter results and prompt filter results for image generations through `contentFilterResults` and `promptFilterResults` properties
7779

7880
### Breaking Changes
7981

8082
- `AzureChatExtensionConfiguration`, `OnYourDataAuthenticationOptions`, `OnYourDataVectorizationSource`, `OnYourDataVectorizationSourceType`, `ChatCompletionsNamedToolSelection`, `ChatCompletionsToolDefinition`, `ChatCompletionsToolCall`, `ChatMessageContentItem`, `ChatRequestMessage`, `ChatFinishDetails` are renamed with `Union` postfix.
81-
- `AzureCognitiveSearchQueryType`, `ChatMessageImageDetailLevel`, `ElasticsearchQueryType`, `FunctionCallPreset`, `ImageGenerationQuality`, `ImageGenerationResponseFormat`, `ImageSize`, `ImageGenerationStyle`, `OnYourDataAuthenticationType`, `OnYourDataVectorizationSourceType` union types no longer have fixed values.
83+
- `AzureCognitiveSearchQueryType`, `ChatMessageImageDetailLevel`, `ElasticsearchQueryType`, `FunctionCallPreset`, `ImageGenerationQuality`, `ImageGenerationResponseFormat`, `ImageSize`, `ImageGenerationStyle`, `OnYourDataAuthenticationType`, `OnYourDataVectorizationSourceType` union types no longer have fixed values.
8284
- `prompFilterResults` property in `ChatCompletions`, `prompFilterResults` property in `Choice`, `toolCalls` in `ChatResponseMessage` are now optional
8385

8486
Changes to "bring your own data" features:
87+
8588
- Introduces a new type: `AzureChatExtensionDataSourceResponseCitation`
8689
- For `AzureChatExtensionsMessageContext`, replaced `messages` property with `citations` and added `intent` as a string
8790
- Rename `AzureCognitiveSearch` to `AzureSearch`
@@ -118,10 +121,10 @@ Changes to "bring your own data" features:
118121
- `listChatCompletions` and `listCompletions` are renamed to `streamChatCompletions` and `streamCompletions` respectively and their return types are updated to be a `ReadableStream`. For example, `streamChatCompletions` can be used as follows:
119122

120123
```js
121-
const events = await client.streamChatCompletions(deploymentId, messages);
122-
for await (const event of events) {
123-
// use event ...
124-
}
124+
const events = await client.streamChatCompletions(deploymentId, messages);
125+
for await (const event of events) {
126+
// use event ...
127+
}
125128
```
126129

127130
## 1.0.0-beta.8 (2023-12-07)
@@ -142,18 +145,18 @@ features and changes to the client library.
142145
`ChatMessage` changes:
143146

144147
- The singular `ChatMessage` type has been replaced by `ChatRequestMessage` and `ChatResponseMessage`, the former of
145-
which is a union of special message structures such as `ChatRequestSystemMessage` and
146-
`ChatRequestUserMessage`.
148+
which is a union of special message structures such as `ChatRequestSystemMessage` and
149+
`ChatRequestUserMessage`.
147150

148151
Dall-e-3:
149152

150153
- Azure OpenAI now uses `dall-e-3` model deployments for its image generation API and such a valid deployment must
151-
be provided to the `GetImageGenerations` method.
154+
be provided to the `GetImageGenerations` method.
152155

153156
On Your Data:
154157

155158
- The `AzureExtensionChatConfiguration` type has been updated to inline the parameters of the extension into the
156-
configuration object itself.
159+
configuration object itself.
157160

158161
## 1.0.0-beta.7 (2023-10-25)
159162

@@ -194,7 +197,7 @@ On Your Data:
194197

195198
### Breaking Changes
196199

197-
- Remove `beginAzureBatchImageGeneration` and `getAzureBatchImageGenerationOperationStatus` methods.
200+
- Remove `beginAzureBatchImageGeneration` and `getAzureBatchImageGenerationOperationStatus` methods.
198201
- `getImages` has been updated to return the image URLs/payloads directly, rather than requiring the user to call `getAzureBatchImageGenerationOperationStatus` to retrieve them.
199202

200203
## 1.0.0-beta.4 (2023-08-09)

sdk/openai/openai/MIGRATION.md

+40-35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<!-- dev-tool snippets ignore -->
2+
13
# Migrating to `openai`
24

35
**Note ⚠️**: This document is a work-in-progress and may change to reflect updates to the package. We value your feedback, please [create an issue](https://github.com/Azure/azure-sdk-for-js/issues/new/choose) to suggest any improvements or report any problems with this guide or with the package itself.
@@ -70,7 +72,7 @@ Migrated code:
7072
import { AzureOpenAI } from "openai";
7173
const deployment = "Your Azure OpenAI deployment";
7274
const apiVersion = "2024-11-01-preview";
73-
const options = { azureADTokenProvider, deployment, apiVersion }
75+
const options = { azureADTokenProvider, deployment, apiVersion };
7476
const client = new AzureOpenAI(options);
7577
```
7678

@@ -101,7 +103,7 @@ const result = await client.getChatCompletions(deploymentName, messages, { maxTo
101103
Migrated code:
102104

103105
```typescript
104-
const result = await client.chat.completions.create({ messages, model: '', max_tokens: 100 });
106+
const result = await client.chat.completions.create({ messages, model: "", max_tokens: 100 });
105107
```
106108

107109
Notice the following:
@@ -124,7 +126,12 @@ const stream = await client.streamChatCompletions(deploymentName, messages, { ma
124126
Migrated code:
125127

126128
```typescript
127-
const stream = await client.chat.completions.create({ model: '', messages, max_tokens: 100, stream: true });
129+
const stream = await client.chat.completions.create({
130+
model: "",
131+
messages,
132+
max_tokens: 100,
133+
stream: true,
134+
});
128135
```
129136

130137
### Azure On Your Data
@@ -136,16 +143,19 @@ Original code:
136143
```typescript
137144
const azureSearchEndpoint = "Your Azure Search resource endpoint";
138145
const azureSearchIndexName = "Your Azure Search index name";
139-
const result = await client.getChatCompletions(deploymentName, messages, { azureExtensionOptions: {
140-
data_sources: [{
141-
type: "azure_search",
142-
endpoint: azureSearchEndpoint,
143-
indexName: azureSearchIndexName,
144-
authentication: {
145-
type: "system_assigned_managed_identity",
146-
}
147-
}]
148-
}
146+
const result = await client.getChatCompletions(deploymentName, messages, {
147+
azureExtensionOptions: {
148+
data_sources: [
149+
{
150+
type: "azure_search",
151+
endpoint: azureSearchEndpoint,
152+
indexName: azureSearchIndexName,
153+
authentication: {
154+
type: "system_assigned_managed_identity",
155+
},
156+
},
157+
],
158+
},
149159
});
150160
```
151161

@@ -157,18 +167,20 @@ import "@azure/openai/types";
157167
const azureSearchEndpoint = "Your Azure Search resource endpoint";
158168
const azureSearchIndexName = "Your Azure Search index name";
159169
const result = await client.chat.completions.create({
160-
model: '',
170+
model: "",
161171
messages,
162-
data_sources: [{
172+
data_sources: [
173+
{
163174
type: "azure_search",
164175
parameters: {
165176
endpoint: azureSearchEndpoint,
166177
index_name: azureSearchIndexName,
167178
authentication: {
168179
type: "system_assigned_managed_identity",
169-
}
170-
}
171-
}]
180+
},
181+
},
182+
},
183+
],
172184
});
173185
```
174186

@@ -199,7 +211,7 @@ Migrated code:
199211
import { createReadStream } from "fs";
200212

201213
const result = await client.audio.transcriptions.create({
202-
model: '',
214+
model: "",
203215
file: createReadStream(audioFilePath),
204216
});
205217
```
@@ -231,7 +243,7 @@ Migrated code:
231243
import { createReadStream } from "fs";
232244

233245
const result = await client.audio.translations.create({
234-
model: '',
246+
model: "",
235247
file: createReadStream(audioFilePath),
236248
});
237249
```
@@ -300,23 +312,16 @@ The following example shows how to migrate the `createMessage` method call.
300312
Original code:
301313

302314
```typescript
303-
const threadResponse = await assistantsClient.createMessage(
304-
assistantThread.id,
305-
role,
306-
message
307-
);
315+
const threadResponse = await assistantsClient.createMessage(assistantThread.id, role, message);
308316
```
309317

310318
Migration code:
311319

312320
```typescript
313-
const threadResponse = await assistantsClient.beta.threads.messages.create(
314-
assistantThread.id,
315-
{
316-
role,
317-
content: message,
318-
}
319-
);
321+
const threadResponse = await assistantsClient.beta.threads.messages.create(assistantThread.id, {
322+
role,
323+
content: message,
324+
});
320325
```
321326

322327
Notice that:
@@ -356,7 +361,7 @@ const runResponse = await assistantsClient.beta.threads.runs.createAndPoll(
356361
{
357362
assistant_id: assistantResponse.id,
358363
},
359-
{ pollIntervalMs: 500 }
364+
{ pollIntervalMs: 500 },
360365
);
361366
```
362367
@@ -404,7 +409,7 @@ const embeddings = await client.getEmbeddings(deploymentName, input);
404409
Migrated code:
405410
406411
```typescript
407-
const embeddings = await client.embeddings.create({ input, model: '' });
412+
const embeddings = await client.embeddings.create({ input, model: "" });
408413
```
409414
410415
Notice that:
@@ -426,7 +431,7 @@ const results = await client.getImages(deploymentName, prompt, { n, size });
426431
Migrated code:
427432
428433
```typescript
429-
const results = await client.images.generate({ prompt, model: '', n, size });
434+
const results = await client.images.generate({ prompt, model: "", n, size });
430435
```
431436
432437
Notice that:

0 commit comments

Comments
 (0)