|
| 1 | +# Go |
| 2 | + |
| 3 | +These settings apply only when `--go` is specified on the command line. |
| 4 | + |
| 5 | +``` yaml |
| 6 | +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 |
| 8 | +output-folder: ../azopenai |
| 9 | +clear-output-folder: false |
| 10 | +module: github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai |
| 11 | +license-header: MICROSOFT_MIT_NO_VERSION |
| 12 | +openapi-type: data-plane |
| 13 | +go: true |
| 14 | +use: "@autorest/go@4.0.0-preview.50" |
| 15 | +``` |
| 16 | +
|
| 17 | +## Transformations |
| 18 | +
|
| 19 | +``` yaml |
| 20 | +directive: |
| 21 | + # Add x-ms-parameter-location to parameters in x-ms-parameterized-host |
| 22 | + - from: openapi-document |
| 23 | + where: $.servers.0.variables.endpoint |
| 24 | + debug: true |
| 25 | + transform: $["x-ms-parameter-location"] = "client"; |
| 26 | + |
| 27 | + # Make deploymentId a client parameter |
| 28 | + # This must be done in each operation as the parameter is not defined in the components section |
| 29 | + - from: openapi-document |
| 30 | + where: $.paths..parameters..[?(@.name=='deploymentId')] |
| 31 | + transform: $["x-ms-parameter-location"] = "client"; |
| 32 | + |
| 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) |
| 45 | + - from: openapi-document |
| 46 | + where: $.paths["/deployments/{deploymentId}/completions"].post.requestBody |
| 47 | + transform: $["required"] = true; |
| 48 | + - from: openapi-document |
| 49 | + where: $.paths["/deployments/{deploymentId}/embeddings"].post.requestBody |
| 50 | + transform: $["required"] = true; |
| 51 | + |
| 52 | + # Remove stream property from CompletionsOptions and ChatCompletionsOptions |
| 53 | + - from: openapi-document |
| 54 | + where: $.components.schemas["CompletionsOptions"] |
| 55 | + transform: delete $.properties.stream; |
| 56 | + - from: openapi-document |
| 57 | + where: $.components.schemas["ChatCompletionsOptions"] |
| 58 | + transform: delete $.properties.stream; |
| 59 | + |
| 60 | + # Replace anyOf schemas with an empty schema (no type) to get an "any" type generated |
| 61 | + - from: openapi-document |
| 62 | + where: '$.components.schemas["EmbeddingsOptions"].properties["input"]' |
| 63 | + transform: delete $.anyOf; |
| 64 | + |
| 65 | + # Fix autorest bug |
| 66 | + - from: openapi-document |
| 67 | + where: $.components.schemas["ChatMessage"].properties.role |
| 68 | + transform: > |
| 69 | + delete $.allOf; |
| 70 | + $["$ref"] = "#/components/schemas/ChatRole"; |
| 71 | +
|
| 72 | + # Fix another autorest bug |
| 73 | + - from: openapi-document |
| 74 | + where: $.components.schemas["Choice"].properties.finish_reason |
| 75 | + transform: > |
| 76 | + delete $.oneOf; |
| 77 | + $["$ref"] = "#/components/schemas/CompletionsFinishReason"; |
| 78 | + - from: openapi-document |
| 79 | + where: $.components.schemas["ChatChoice"].properties.finish_reason |
| 80 | + transform: > |
| 81 | + delete $.oneOf; |
| 82 | + $["$ref"] = "#/components/schemas/CompletionsFinishReason"; |
| 83 | +
|
| 84 | + # Fix "AutoGenerated" models |
| 85 | + - from: openapi-document |
| 86 | + where: $.components.schemas["ChatCompletions"].properties.usage |
| 87 | + transform: > |
| 88 | + delete $.allOf; |
| 89 | + $["$ref"] = "#/components/schemas/CompletionsUsage"; |
| 90 | + - from: openapi-document |
| 91 | + where: $.components.schemas["Completions"].properties.usage |
| 92 | + transform: > |
| 93 | + delete $.allOf; |
| 94 | + $["$ref"] = "#/components/schemas/CompletionsUsage"; |
| 95 | +
|
| 96 | + # |
| 97 | + # strip out the deploymentID validation code - we absorbed this into the endpoint. |
| 98 | + # |
| 99 | + # urlPath := "/deployments/{deploymentId}/embeddings" |
| 100 | + # if client.deploymentID == "" { |
| 101 | + # return nil, errors.New("parameter client.deploymentID cannot be empty") |
| 102 | + # } |
| 103 | + # urlPath = strings.ReplaceAll(urlPath, "{deploymentId}", url.PathEscape(client.deploymentID)) |
| 104 | + - from: client.go |
| 105 | + where: $ |
| 106 | + transform: >- |
| 107 | + return $.replace( |
| 108 | + /(\s+)urlPath\s*:=\s*"\/deployments\/\{deploymentId\}\/([^"]+)".+?url\.PathEscape.+?\n/gs, |
| 109 | + "$1urlPath := \"$2\"\n") |
| 110 | +
|
| 111 | + # splice out the auto-generated `deploymentID` field from the client |
| 112 | + - from: client.go |
| 113 | + where: $ |
| 114 | + transform: >- |
| 115 | + return $.replace( |
| 116 | + /(type Client struct.+?)deploymentID string([^}]+})/s, |
| 117 | + "$1$2") |
| 118 | +
|
| 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 |
| 127 | + where: $ |
| 128 | + transform: >- |
| 129 | + return $.replace( |
| 130 | + /\/\/ (UnmarshalJSON|MarshalJSON) implements.*?AzureCoreFoundations.*?func.+?\n}/gs, |
| 131 | + "") |
| 132 | + - from: models_serde.go |
| 133 | + where: $ |
| 134 | + transform: return $.replace(/(?:\/\/.*\s)?func \(\w \*?(?:ErrorResponse|ErrorResponseError|InnerError|InnerErrorInnererror)\).*\{\s(?:.+\s)+\}\s/g, ""); |
| 135 | + |
| 136 | + - from: constants.go |
| 137 | + where: $ |
| 138 | + transform: >- |
| 139 | + return $.replace( |
| 140 | + /type ServiceAPIVersions string.+PossibleServiceAPIVersionsValues.+?\n}/gs, |
| 141 | + "") |
| 142 | +
|
| 143 | + # delete client name prefix from method options and response types |
| 144 | + - from: |
| 145 | + - client.go |
| 146 | + - models.go |
| 147 | + - response_types.go |
| 148 | + where: $ |
| 149 | + transform: return $.replace(/Client(\w+)((?:Options|Response))/g, "$1$2"); |
| 150 | +``` |
0 commit comments