Skip to content

Commit

Permalink
[OpenAI] Rename prompt_annotations to prompt_filter_results (Azure#27186
Browse files Browse the repository at this point in the history
)

### Packages impacted by this PR
@azure/openai

### Issues associated with this PR
https://portal.microsofticm.com/imp/v3/incidents/details/424851016/home

### Describe the problem that is addressed by this PR
The API renamed this property last minute

### 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?
N/A

### Are there test cases added in this PR? _(If not, why?)_
Yes

### Provide a list of related PRs _(if any)_
N/A

### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [x] 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)
  • Loading branch information
deyaaeldeen authored Sep 21, 2023
1 parent f621795 commit 2b7a680
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 51 deletions.
52 changes: 27 additions & 25 deletions sdk/openai/openai/sources/customizations/api/deserializers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ChatMessage, ChatRole, Completions } from "../../generated/src/models/models.js";
import {
ChatMessage,
ChatRole,
Completions,
PromptFilterResult,
} from "../../generated/src/models/models.js";
import {
ChatChoiceOutput,
ChatMessageOutput,
Expand All @@ -11,22 +16,30 @@ import {
} from "../../generated/src/rest/outputModels.js";
import { ChatCompletions } from "../models/models.js";
import { ContentFilterResults } from "./models.js";

function getPromptFilterResult(body: Record<string, any>): {
promptFilterResults?: PromptFilterResult[];
} {
const res = body["prompt_annotations"] ?? body["prompt_filter_results"];
return !res
? {}
: {
promptFilterResults: res.map((p: PromptFilterResultOutput) => ({
promptIndex: p["prompt_index"],
...(!p.content_filter_results
? {}
: {
contentFilterResults: deserializeContentFilter(p.content_filter_results),
}),
})),
};
}

export function getCompletionsResult(body: Record<string, any>): Omit<Completions, "usage"> {
return {
id: body["id"],
created: new Date(body["created"]),
...(!body["prompt_annotations"]
? {}
: {
promptFilterResults: body["prompt_annotations"].map((p: PromptFilterResultOutput) => ({
promptIndex: p["prompt_index"],
...(!p.content_filter_results
? {}
: {
contentFilterResults: deserializeContentFilter(p.content_filter_results),
}),
})),
}),
...getPromptFilterResult(body),
choices: (body["choices"] ?? []).map((p: ChoiceOutput) => ({
text: p["text"],
index: p["index"],
Expand Down Expand Up @@ -62,18 +75,7 @@ export function getChatCompletionsResult(body: Record<string, any>): ChatComplet
? {}
: { contentFilterResults: deserializeContentFilter(p.content_filter_results) }),
})),
...(!body["prompt_annotations"]
? {}
: {
promptFilterResults: body["prompt_annotations"].map((p: PromptFilterResultOutput) => ({
promptIndex: p["prompt_index"],
...(!p.content_filter_results
? {}
: {
contentFilterResults: deserializeContentFilter(p.content_filter_results),
}),
})),
}),
...getPromptFilterResult(body),
...(!body["usage"]
? {}
: {
Expand Down
46 changes: 21 additions & 25 deletions sdk/openai/openai/src/api/deserializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* If you need to make changes, please do so in the original source file, \{project-root\}/sources/custom
*/

import { ChatMessage, ChatRole, Completions } from "../models/models.js";
import { ChatMessage, ChatRole, Completions, PromptFilterResult } from "../models/models.js";
import {
ChatChoiceOutput,
ChatMessageOutput,
Expand All @@ -20,22 +20,29 @@ import {
import { ChatCompletions } from "../models/models.js";
import { ContentFilterResults } from "./models.js";

function getPromptFilterResult(body: Record<string, any>): {
promptFilterResults?: PromptFilterResult[];
} {
const res = body["prompt_annotations"] ?? body["prompt_filter_results"];
return !res
? {}
: {
promptFilterResults: res.map((p: PromptFilterResultOutput) => ({
promptIndex: p["prompt_index"],
...(!p.content_filter_results
? {}
: {
contentFilterResults: deserializeContentFilter(p.content_filter_results),
}),
})),
};
}

export function getCompletionsResult(body: Record<string, any>): Omit<Completions, "usage"> {
return {
id: body["id"],
created: new Date(body["created"]),
...(!body["prompt_annotations"]
? {}
: {
promptFilterResults: body["prompt_annotations"].map((p: PromptFilterResultOutput) => ({
promptIndex: p["prompt_index"],
...(!p.content_filter_results
? {}
: {
contentFilterResults: deserializeContentFilter(p.content_filter_results),
}),
})),
}),
...getPromptFilterResult(body),
choices: (body["choices"] ?? []).map((p: ChoiceOutput) => ({
text: p["text"],
index: p["index"],
Expand Down Expand Up @@ -71,18 +78,7 @@ export function getChatCompletionsResult(body: Record<string, any>): ChatComplet
? {}
: { contentFilterResults: deserializeContentFilter(p.content_filter_results) }),
})),
...(!body["prompt_annotations"]
? {}
: {
promptFilterResults: body["prompt_annotations"].map((p: PromptFilterResultOutput) => ({
promptIndex: p["prompt_index"],
...(!p.content_filter_results
? {}
: {
contentFilterResults: deserializeContentFilter(p.content_filter_results),
}),
})),
}),
...getPromptFilterResult(body),
...(!body["usage"]
? {}
: {
Expand Down
Loading

0 comments on commit 2b7a680

Please sign in to comment.