Skip to content

Commit a7aadee

Browse files
[azopenai] Adding in image generation, as exported by the current .tsp (#21054)
Adding in image generation for both Azure OpenAI and OpenAI. - Automated the remaining pieces so we can generate from a .tsp file. - Updated from the latest tsp from Travis - Adding a workaround for the bug in Azure OpenAI with streaming. Working on a fix as it needs to be done service side.
1 parent 934612c commit a7aadee

28 files changed

+2191
-156
lines changed

eng/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
{
4040
"Name": "azopenai",
41-
"CoverageGoal": 0.45
41+
"CoverageGoal": 0.39
4242
},
4343
{
4444
"Name": "aztemplate",
@@ -102,4 +102,4 @@
102102
"CoverageGoal": 0.80
103103
}
104104
]
105-
}
105+
}

sdk/cognitiveservices/azopenai/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "go",
44
"TagPrefix": "go/cognitiveservices/azopenai",
5-
"Tag": "go/cognitiveservices/azopenai_bf5b07347b"
5+
"Tag": "go/cognitiveservices/azopenai_25f5951837"
66
}

sdk/cognitiveservices/azopenai/autorest.md

Lines changed: 125 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ These settings apply only when `--go` is specified on the command line.
44

55
``` yaml
66
input-file:
7-
- https://github.com/mikekistler/azure-rest-api-specs/blob/baed660fd853b4a387ca9f0b9491fd1414b66e9e/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2023-03-15-preview/inference.json
7+
#- https://raw.githubusercontent.com/Azure/azure-rest-api-specs/13a645b66b741e3cc2ef378cb81974b30e6a7a86/specification/cognitiveservices/AzureOpenAI/inference/2023-06-01-preview/generated.json
8+
- ./testdata/generated/openapi3.json
9+
810
output-folder: ../azopenai
911
clear-output-folder: false
1012
module: github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai
1113
license-header: MICROSOFT_MIT_NO_VERSION
1214
openapi-type: data-plane
1315
go: true
14-
use: "@autorest/[email protected]"
16+
use: "@autorest/[email protected]"
17+
title: "OpenAI"
18+
slice-elements-byval: true
19+
remove-non-reference-schema: true
1520
```
1621
1722
## Transformations
@@ -30,25 +35,18 @@ directive:
3035
where: $.paths..parameters..[?(@.name=='deploymentId')]
3136
transform: $["x-ms-parameter-location"] = "client";
3237

33-
# Update operationIds to combine all operations into a single client
34-
- rename-operation:
35-
from: getCompletions
36-
to: OpenAI_GetCompletions
37-
- rename-operation:
38-
from: getEmbeddings
39-
to: OpenAI_GetEmbeddings
40-
- rename-operation:
41-
from: getChatCompletions
42-
to: OpenAI_GetChatCompletions
43-
44-
# Mark request bodies as required (TypeSpec issue #1838)
4538
- from: openapi-document
46-
where: $.paths["/deployments/{deploymentId}/completions"].post.requestBody
39+
where: $..paths["/deployments/{deploymentId}/completions"].post.requestBody
4740
transform: $["required"] = true;
4841
- from: openapi-document
4942
where: $.paths["/deployments/{deploymentId}/embeddings"].post.requestBody
5043
transform: $["required"] = true;
5144

45+
# get rid of these auto-generated LRO status methods that aren't exposed.
46+
- from: openapi-document
47+
where: $.paths
48+
transform: delete $["/operations/images/{operationId}"]
49+
5250
# Remove stream property from CompletionsOptions and ChatCompletionsOptions
5351
- from: openapi-document
5452
where: $.components.schemas["CompletionsOptions"]
@@ -62,25 +60,44 @@ directive:
6260
where: '$.components.schemas["EmbeddingsOptions"].properties["input"]'
6361
transform: delete $.anyOf;
6462

63+
- from: openapi-document
64+
where: $.paths["/images/generations:submit"].post
65+
transform: $["x-ms-long-running-operation"] = true;
66+
6567
# Fix autorest bug
68+
- from: openapi-document
69+
where: $.components.schemas["BatchImageGenerationOperationResponse"].properties
70+
transform: |
71+
$.result["$ref"] = "#/components/schemas/ImageGenerations"; delete $.allOf;
72+
$.status["$ref"] = "#/components/schemas/AzureOpenAIOperationState"; delete $.allOf;
73+
$.error["$ref"] = "#/components/schemas/Azure.Core.Foundations.Error"; delete $.allOf;
6674
- from: openapi-document
6775
where: $.components.schemas["ChatMessage"].properties.role
68-
transform: >
69-
delete $.allOf;
70-
$["$ref"] = "#/components/schemas/ChatRole";
71-
72-
# Fix another autorest bug
76+
transform: $["$ref"] = "#/components/schemas/ChatRole"; delete $.oneOf;
7377
- from: openapi-document
7478
where: $.components.schemas["Choice"].properties.finish_reason
75-
transform: >
76-
delete $.oneOf;
77-
$["$ref"] = "#/components/schemas/CompletionsFinishReason";
79+
transform: $["$ref"] = "#/components/schemas/CompletionsFinishReason"; delete $.oneOf;
80+
- from: openapi-document
81+
where: $.components.schemas["ImageOperation"].properties.status
82+
transform: $["$ref"] = $.anyOf[0]["$ref"];delete $.anyOf;
83+
- from: openapi-document
84+
where: $.components.schemas["ImageGenerationOptions"].properties
85+
transform: |
86+
$.size["$ref"] = "#/components/schemas/ImageSize"; delete $.allOf;
87+
$.response_format["$ref"] = "#/components/schemas/ImageGenerationResponseFormat"; delete $.allOf;
88+
- from: openapi-document
89+
where: $.components.schemas["ImageOperationResponse"].properties
90+
transform: |
91+
$.status["$ref"] = "#/components/schemas/State"; delete $.status.allOf;
92+
$.result["$ref"] = "#/components/schemas/ImageResponse"; delete $.status.allOf;
93+
- from: openapi-document
94+
where: $.components.schemas["ImageOperationStatus"].properties.status
95+
transform: $["$ref"] = "#/components/schemas/State"; delete $.allOf;
7896
- from: openapi-document
7997
where: $.components.schemas["ChatChoice"].properties.finish_reason
8098
transform: >
8199
delete $.oneOf;
82100
$["$ref"] = "#/components/schemas/CompletionsFinishReason";
83-
84101
# Fix "AutoGenerated" models
85102
- from: openapi-document
86103
where: $.components.schemas["ChatCompletions"].properties.usage
@@ -108,28 +125,29 @@ directive:
108125
/(\s+)urlPath\s*:=\s*"\/deployments\/\{deploymentId\}\/([^"]+)".+?url\.PathEscape.+?\n/gs,
109126
"$1urlPath := \"$2\"\n")
110127
128+
# Unexport the the poller state enum.
129+
- from:
130+
- constants.go
131+
- models.go
132+
where: $
133+
transform: return $.replace(/AzureOpenAIOperationState/g, "azureOpenAIOperationState");
134+
111135
# splice out the auto-generated `deploymentID` field from the client
112136
- from: client.go
113137
where: $
114138
transform: >-
115139
return $.replace(
116-
/(type Client struct.+?)deploymentID string([^}]+})/s,
117-
"$1$2")
140+
/(type Client struct[^}]+})/s,
141+
"type Client struct {\ninternal *azcore.Client; clientData;\n}")
118142
119-
# delete unused error models
120-
- from: models.go
121-
where: $
122-
transform: >-
123-
return $.replace(
124-
/\/\/ AzureCoreFoundations.*?type AzureCoreFoundations(Error|ErrorResponse|ErrorResponseError|InnerError|InnerErrorInnererror|ErrorInnererror) struct \{[^}]+\}/gs,
125-
"")
126-
- from: models_serde.go
143+
- from:
144+
- models_serde.go
145+
- models.go
127146
where: $
128-
transform: >-
129-
return $.replace(
130-
/\/\/ (UnmarshalJSON|MarshalJSON) implements.*?AzureCoreFoundations.*?func.+?\n}/gs,
131-
"")
132-
- from: models_serde.go
147+
transform: return $.replace(/AzureCoreFoundations/g, "azureCoreFoundations");
148+
- from:
149+
- models_serde.go
150+
- models.go
133151
where: $
134152
transform: return $.replace(/(?:\/\/.*\s)?func \(\w \*?(?:ErrorResponse|ErrorResponseError|InnerError|InnerErrorInnererror)\).*\{\s(?:.+\s)+\}\s/g, "");
135153

@@ -144,7 +162,74 @@ directive:
144162
- from:
145163
- client.go
146164
- models.go
147-
- response_types.go
165+
- options.go
166+
- response_types.go
148167
where: $
149168
transform: return $.replace(/Client(\w+)((?:Options|Response))/g, "$1$2");
169+
170+
# allow interception of formatting the URL path
171+
- from: client.go
172+
where: $
173+
transform: return $.replace(/runtime\.JoinPaths\(client.endpoint, urlPath\)/g, "client.formatURL(urlPath)");
174+
175+
- from: models.go
176+
where: $
177+
transform: |
178+
return $.replace(/type ImageGenerationsDataItem struct {[^}]+}/, "// ImageGenerationsDataItem represents an image URL or payload\ntype ImageGenerationsDataItem struct{\nImageLocation\nImagePayload\n}");
179+
180+
# delete the auto-generated ImageGenerationsDataItem, we handle that custom
181+
- from: models.go
182+
where: $
183+
transform: return $.replace(/\/\/ ImageGenerationsDataItem represents[^}]+}/s, "");
184+
185+
# rename the image constants
186+
- from: constants.go
187+
where: $
188+
transform: |
189+
return $.replace(/ImageSizeFiveHundredTwelveX512/g, "ImageSize512x512")
190+
.replace(/ImageSizeOneThousandTwentyFourX1024/g, "ImageSize1024x1024")
191+
.replace(/ImageSizeTwoHundredFiftySixX256/g, "ImageSize256x256");
192+
193+
# scrub the Image(Payload|Location) deserializers.
194+
- from: models_serde.go
195+
where: $
196+
transform: |
197+
return $.replace(/\/\/ UnmarshalJSON implements the json.Unmarshaller interface for type ImagePayload.+?\n}/s, "")
198+
.replace(/\/\/ MarshalJSON implements the json.Marshaller interface for type ImagePayload.+?\n}/s, "")
199+
.replace(/\/\/ UnmarshalJSON implements the json.Unmarshaller interface for type ImageLocation.+?\n}/s, "")
200+
.replace(/\/\/ MarshalJSON implements the json.Marshaller interface for type ImageLocation.+?\n}/s, "");
201+
202+
# hide the image generation pollers.
203+
- rename-operation:
204+
from: beginAzureBatchImageGeneration
205+
to: azureBatchImageGenerationInternal
206+
- from:
207+
- client.go
208+
- models.go
209+
- models_serde.go
210+
- options.go
211+
- response_types.go
212+
where: $
213+
transform: |
214+
return $.replace(/GetAzureBatchImageGenerationOperationStatusResponse/g, "getAzureBatchImageGenerationOperationStatusResponse")
215+
.replace(/AzureBatchImageGenerationInternalResponse/g, "azureBatchImageGenerationInternalResponse")
216+
.replace(/GetAzureBatchImageGenerationOperationStatusOptions/g, "getAzureBatchImageGenerationOperationStatusOptions")
217+
.replace(/GetAzureBatchImageGenerationOperationStatus/g, "getAzureBatchImageGenerationOperationStatus")
218+
.replace(/BeginAzureBatchImageGenerationInternal/g, "beginAzureBatchImageGeneration")
219+
.replace(/BatchImageGenerationOperationResponse/g, "batchImageGenerationOperationResponse");
220+
221+
# fix some casing
222+
- from:
223+
- client.go
224+
- models.go
225+
- models_serde.go
226+
- options.go
227+
- response_types.go
228+
where: $
229+
transform: return $.replace(/Logprobs/g, "LogProbs")
230+
231+
# remove PossibleazureOpenAIOperationStateValues, since we don't expose the poller
232+
- from: constants.go
233+
where: $
234+
transform: return $.replace(/\/\/ PossibleazureOpenAIOperationStateValues returns.+?\n}/s, "");
150235
```

sdk/cognitiveservices/azopenai/build.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
13
//go:build go1.18
24
// +build go1.18
35

4-
//go:generate autorest ./autorest.md
6+
//go:generate pwsh ./genopenapi3.ps1
7+
//go:generate autorest ./autorest.md
58
//go:generate go mod tidy
69
//go:generate goimports -w .
710

0 commit comments

Comments
 (0)