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

[azopenai] Adding in image generation, as exported by the current .tsp #21054

Merged
merged 15 commits into from
Jul 6, 2023
2 changes: 1 addition & 1 deletion sdk/cognitiveservices/azopenai/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/cognitiveservices/azopenai",
"Tag": "go/cognitiveservices/azopenai_bf5b07347b"
"Tag": "go/cognitiveservices/azopenai_2ea5c89a3b"
}
129 changes: 90 additions & 39 deletions sdk/cognitiveservices/azopenai/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ These settings apply only when `--go` is specified on the command line.

``` yaml
input-file:
- https://github.com/mikekistler/azure-rest-api-specs/blob/baed660fd853b4a387ca9f0b9491fd1414b66e9e/specification/cognitiveservices/data-plane/AzureOpenAI/inference/preview/2023-03-15-preview/inference.json
#- https://raw.githubusercontent.com/Azure/azure-rest-api-specs/13a645b66b741e3cc2ef378cb81974b30e6a7a86/specification/cognitiveservices/AzureOpenAI/inference/2023-06-01-preview/generated.json
- ./testdata/generated/openapi3.json

output-folder: ../azopenai
clear-output-folder: false
module: github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai
license-header: MICROSOFT_MIT_NO_VERSION
openapi-type: data-plane
go: true
use: "@autorest/[email protected]"
title: "OpenAI"
```

## Transformations
Expand All @@ -30,20 +33,8 @@ directive:
where: $.paths..parameters..[?(@.name=='deploymentId')]
transform: $["x-ms-parameter-location"] = "client";

# Update operationIds to combine all operations into a single client
- rename-operation:
from: getCompletions
to: OpenAI_GetCompletions
- rename-operation:
from: getEmbeddings
to: OpenAI_GetEmbeddings
- rename-operation:
from: getChatCompletions
to: OpenAI_GetChatCompletions

# Mark request bodies as required (TypeSpec issue #1838)
- from: openapi-document
where: $.paths["/deployments/{deploymentId}/completions"].post.requestBody
where: $..paths["/deployments/{deploymentId}/completions"].post.requestBody
transform: $["required"] = true;
- from: openapi-document
where: $.paths["/deployments/{deploymentId}/embeddings"].post.requestBody
Expand All @@ -62,25 +53,44 @@ directive:
where: '$.components.schemas["EmbeddingsOptions"].properties["input"]'
transform: delete $.anyOf;

- from: openapi-document
where: $.paths["/images/generations:submit"].post
transform: $["x-ms-long-running-operation"] = true;

# Fix autorest bug
- from: openapi-document
where: $.components.schemas["BatchImageGenerationOperationResponse"].properties
transform: |
$.result["$ref"] = "#/components/schemas/ImageGenerations"; delete $.allOf;
$.status["$ref"] = "#/components/schemas/AzureOpenAIOperationState"; delete $.allOf;
$.error["$ref"] = "#/components/schemas/Azure.Core.Foundations.Error"; delete $.allOf;
- from: openapi-document
where: $.components.schemas["ChatMessage"].properties.role
transform: >
delete $.allOf;
$["$ref"] = "#/components/schemas/ChatRole";

# Fix another autorest bug
transform: $["$ref"] = "#/components/schemas/ChatRole"; delete $.oneOf;
- from: openapi-document
where: $.components.schemas["Choice"].properties.finish_reason
transform: >
delete $.oneOf;
$["$ref"] = "#/components/schemas/CompletionsFinishReason";
transform: $["$ref"] = "#/components/schemas/CompletionsFinishReason"; delete $.oneOf;
- from: openapi-document
where: $.components.schemas["ImageOperation"].properties.status
transform: $["$ref"] = $.anyOf[0]["$ref"];delete $.anyOf;
- from: openapi-document
where: $.components.schemas["ImageGenerationOptions"].properties
transform: |
$.size["$ref"] = "#/components/schemas/ImageSize"; delete $.allOf;
$.response_format["$ref"] = "#/components/schemas/ImageGenerationResponseFormat"; delete $.allOf;
- from: openapi-document
where: $.components.schemas["ImageOperationResponse"].properties
transform: |
$.status["$ref"] = "#/components/schemas/State"; delete $.status.allOf;
$.result["$ref"] = "#/components/schemas/ImageResponse"; delete $.status.allOf;
- from: openapi-document
where: $.components.schemas["ImageOperationStatus"].properties.status
transform: $["$ref"] = "#/components/schemas/State"; delete $.allOf;
- from: openapi-document
where: $.components.schemas["ChatChoice"].properties.finish_reason
transform: >
delete $.oneOf;
$["$ref"] = "#/components/schemas/CompletionsFinishReason";

# Fix "AutoGenerated" models
- from: openapi-document
where: $.components.schemas["ChatCompletions"].properties.usage
Expand Down Expand Up @@ -113,23 +123,23 @@ directive:
where: $
transform: >-
return $.replace(
/(type Client struct.+?)deploymentID string([^}]+})/s,
"$1$2")

# delete unused error models
- from: models.go
/(type Client struct[^}]+})/s,
"type Client struct {\ninternal *azcore.Client; clientData;\n}")

# - from: models_serde.go
# where: $
# transform: >-
# return $.replace(
# /\/\/ (UnmarshalJSON|MarshalJSON) implements.*?AzureCoreFoundations.*?func.+?\n}/gs,
# "")
- from:
- models_serde.go
- models.go
where: $
transform: >-
return $.replace(
/\/\/ AzureCoreFoundations.*?type AzureCoreFoundations(Error|ErrorResponse|ErrorResponseError|InnerError|InnerErrorInnererror|ErrorInnererror) struct \{[^}]+\}/gs,
"")
- from: models_serde.go
where: $
transform: >-
return $.replace(
/\/\/ (UnmarshalJSON|MarshalJSON) implements.*?AzureCoreFoundations.*?func.+?\n}/gs,
"")
- from: models_serde.go
transform: return $.replace(/AzureCoreFoundations/g, "azureCoreFoundations");
- from:
- models_serde.go
- models.go
where: $
transform: return $.replace(/(?:\/\/.*\s)?func \(\w \*?(?:ErrorResponse|ErrorResponseError|InnerError|InnerErrorInnererror)\).*\{\s(?:.+\s)+\}\s/g, "");

Expand All @@ -147,4 +157,45 @@ directive:
- response_types.go
where: $
transform: return $.replace(/Client(\w+)((?:Options|Response))/g, "$1$2");

# allow interception of formatting the URL path
- from: client.go
where: $
transform: return $.replace(/runtime\.JoinPaths\(client.endpoint, urlPath\)/g, "client.formatURL(urlPath)");

- from: models.go
where: $
transform: |
return $.replace(/type ImageGenerationsDataItem struct {[^}]+}/, "// ImageGenerationsDataItem represents an image URL or payload\ntype ImageGenerationsDataItem struct{\nImageLocation\nImagePayload\n}");

# delete the auto-generated ImageGenerationsDataItem, we handle that custom
- from: models.go
where: $
transform: return $.replace(/\/\/ ImageGenerationsDataItem represents[^}]+}/s, "");

# rename the image constants
- from: constants.go
where: $
transform: |
return $.replace(/ImageSizeFiveHundredTwelveX512/g, "ImageSize512x512")
.replace(/ImageSizeOneThousandTwentyFourX1024/g, "ImageSize1024x1024")
.replace(/ImageSizeTwoHundredFiftySixX256/g, "ImageSize256x256");

# hide the image generation pollers.
- rename-operation:
from: beginAzureBatchImageGeneration
to: azureBatchImageGenerationInternal
- from:
- client.go
- models.go
- models_serde.go
- response_types.go
where: $
transform: |
return $.replace(/GetAzureBatchImageGenerationOperationStatusResponse/g, "getAzureBatchImageGenerationOperationStatusResponse")
.replace(/AzureBatchImageGenerationInternalResponse/g, "azureBatchImageGenerationInternalResponse")
.replace(/GetAzureBatchImageGenerationOperationStatusOptions/g, "getAzureBatchImageGenerationOperationStatusOptions")
.replace(/GetAzureBatchImageGenerationOperationStatus/g, "getAzureBatchImageGenerationOperationStatus")
.replace(/BeginAzureBatchImageGenerationInternal/g, "beginAzureBatchImageGeneration")
.replace(/BatchImageGenerationOperationResponse/g, "batchImageGenerationOperationResponse");
```
5 changes: 4 additions & 1 deletion sdk/cognitiveservices/azopenai/build.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
//go:build go1.18
// +build go1.18

//go:generate autorest ./autorest.md
//go:generate pwsh ./genopenapi3.ps1
//go:generate autorest ./autorest.md
//go:generate go mod tidy
//go:generate goimports -w .

Expand Down
132 changes: 122 additions & 10 deletions sdk/cognitiveservices/azopenai/client.go

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

Loading