From 2ad857834ed230a9222f6d9737009c8419504995 Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Mon, 10 Apr 2023 15:55:32 -0500 Subject: [PATCH] Generated client --- sdk/cognitiveservices/azopenai/client.go | 127 +++++ sdk/cognitiveservices/azopenai/go.mod | 11 + sdk/cognitiveservices/azopenai/go.sum | 12 + sdk/cognitiveservices/azopenai/models.go | 207 ++++++++ .../azopenai/models_serde.go | 461 ++++++++++++++++++ .../azopenai/response_types.go | 22 + 6 files changed, 840 insertions(+) create mode 100644 sdk/cognitiveservices/azopenai/client.go create mode 100644 sdk/cognitiveservices/azopenai/go.mod create mode 100644 sdk/cognitiveservices/azopenai/go.sum create mode 100644 sdk/cognitiveservices/azopenai/models.go create mode 100644 sdk/cognitiveservices/azopenai/models_serde.go create mode 100644 sdk/cognitiveservices/azopenai/response_types.go diff --git a/sdk/cognitiveservices/azopenai/client.go b/sdk/cognitiveservices/azopenai/client.go new file mode 100644 index 000000000000..1571cf5ece44 --- /dev/null +++ b/sdk/cognitiveservices/azopenai/client.go @@ -0,0 +1,127 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// Code generated by Microsoft (R) AutoRest Code Generator. +// Changes may cause incorrect behavior and will be lost if the code is regenerated. +// DO NOT EDIT. + +package azopenai + +import ( + "context" + "errors" + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" + "net/http" + "net/url" + "strings" +) + +// Client contains the methods for the OpenAI group. +// Don't use this type directly, use a constructor function instead. +type Client struct { + internal *azcore.Client + endpoint string +} + +// GetCompletions - Creates a completion for the provided prompt, parameters and chosen model. +// If the operation fails it returns an *azcore.ResponseError type. +// +// Generated from API version 2022-12-01 +// - options - ClientGetCompletionsOptions contains the optional parameters for the Client.GetCompletions method. +func (client *Client) GetCompletions(ctx context.Context, deploymentID string, body CompletionRequest, options *ClientGetCompletionsOptions) (ClientGetCompletionsResponse, error) { + req, err := client.getCompletionsCreateRequest(ctx, deploymentID, body, options) + if err != nil { + return ClientGetCompletionsResponse{}, err + } + resp, err := client.internal.Pipeline().Do(req) + if err != nil { + return ClientGetCompletionsResponse{}, err + } + if !runtime.HasStatusCode(resp, http.StatusOK) { + return ClientGetCompletionsResponse{}, runtime.NewResponseError(resp) + } + return client.getCompletionsHandleResponse(resp) +} + +// getCompletionsCreateRequest creates the GetCompletions request. +func (client *Client) getCompletionsCreateRequest(ctx context.Context, deploymentID string, body CompletionRequest, options *ClientGetCompletionsOptions) (*policy.Request, error) { + urlPath := "/deployments/{deployment-id}/completions" + if deploymentID == "" { + return nil, errors.New("parameter deploymentID cannot be empty") + } + urlPath = strings.ReplaceAll(urlPath, "{deployment-id}", url.PathEscape(deploymentID)) + req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.endpoint, urlPath)) + if err != nil { + return nil, err + } + reqQP := req.Raw().URL.Query() + reqQP.Set("api-version", "2022-12-01") + req.Raw().URL.RawQuery = reqQP.Encode() + req.Raw().Header["Accept"] = []string{"application/json"} + return req, runtime.MarshalAsJSON(req, body) +} + +// getCompletionsHandleResponse handles the GetCompletions response. +func (client *Client) getCompletionsHandleResponse(resp *http.Response) (ClientGetCompletionsResponse, error) { + result := ClientGetCompletionsResponse{} + if val := resp.Header.Get("apim-request-id"); val != "" { + result.ApimRequestID = &val + } + if err := runtime.UnmarshalAsJSON(resp, &result.CompletionResponse); err != nil { + return ClientGetCompletionsResponse{}, err + } + return result, nil +} + +// GetEmbeddings - Get a vector representation of a given input that can be easily consumed by machine learning models and +// algorithms. +// If the operation fails it returns an *azcore.ResponseError type. +// +// Generated from API version 2022-12-01 +// - deploymentID - The deployment id of the model which was deployed. +// - options - ClientGetEmbeddingsOptions contains the optional parameters for the Client.GetEmbeddings method. +func (client *Client) GetEmbeddings(ctx context.Context, deploymentID string, body EmbeddingRequest, options *ClientGetEmbeddingsOptions) (ClientGetEmbeddingsResponse, error) { + req, err := client.getEmbeddingsCreateRequest(ctx, deploymentID, body, options) + if err != nil { + return ClientGetEmbeddingsResponse{}, err + } + resp, err := client.internal.Pipeline().Do(req) + if err != nil { + return ClientGetEmbeddingsResponse{}, err + } + if !runtime.HasStatusCode(resp, http.StatusOK) { + return ClientGetEmbeddingsResponse{}, runtime.NewResponseError(resp) + } + return client.getEmbeddingsHandleResponse(resp) +} + +// getEmbeddingsCreateRequest creates the GetEmbeddings request. +func (client *Client) getEmbeddingsCreateRequest(ctx context.Context, deploymentID string, body EmbeddingRequest, options *ClientGetEmbeddingsOptions) (*policy.Request, error) { + urlPath := "/deployments/{deployment-id}/embeddings" + if deploymentID == "" { + return nil, errors.New("parameter deploymentID cannot be empty") + } + urlPath = strings.ReplaceAll(urlPath, "{deployment-id}", url.PathEscape(deploymentID)) + req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.endpoint, urlPath)) + if err != nil { + return nil, err + } + reqQP := req.Raw().URL.Query() + reqQP.Set("api-version", "2022-12-01") + req.Raw().URL.RawQuery = reqQP.Encode() + req.Raw().Header["Accept"] = []string{"application/json"} + return req, runtime.MarshalAsJSON(req, body) +} + +// getEmbeddingsHandleResponse handles the GetEmbeddings response. +func (client *Client) getEmbeddingsHandleResponse(resp *http.Response) (ClientGetEmbeddingsResponse, error) { + result := ClientGetEmbeddingsResponse{} + if err := runtime.UnmarshalAsJSON(resp, &result.EmbeddingResponse); err != nil { + return ClientGetEmbeddingsResponse{}, err + } + return result, nil +} diff --git a/sdk/cognitiveservices/azopenai/go.mod b/sdk/cognitiveservices/azopenai/go.mod new file mode 100644 index 000000000000..e051c62950e0 --- /dev/null +++ b/sdk/cognitiveservices/azopenai/go.mod @@ -0,0 +1,11 @@ +module github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai + +go 1.18 + +require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 + +require ( + github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect + golang.org/x/net v0.7.0 // indirect + golang.org/x/text v0.7.0 // indirect +) diff --git a/sdk/cognitiveservices/azopenai/go.sum b/sdk/cognitiveservices/azopenai/go.sum new file mode 100644 index 000000000000..d39a720dafd8 --- /dev/null +++ b/sdk/cognitiveservices/azopenai/go.sum @@ -0,0 +1,12 @@ +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY= +github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 h1:+5VZ72z0Qan5Bog5C+ZkgSqUbeVUd9wgtHOrIKuc5b8= +github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/sdk/cognitiveservices/azopenai/models.go b/sdk/cognitiveservices/azopenai/models.go new file mode 100644 index 000000000000..c2c19b1df482 --- /dev/null +++ b/sdk/cognitiveservices/azopenai/models.go @@ -0,0 +1,207 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// Code generated by Microsoft (R) AutoRest Code Generator. +// Changes may cause incorrect behavior and will be lost if the code is regenerated. +// DO NOT EDIT. + +package azopenai + +// ClientGetCompletionsOptions contains the optional parameters for the Client.GetCompletions method. +type ClientGetCompletionsOptions struct { + // placeholder for future optional parameters +} + +// ClientGetEmbeddingsOptions contains the optional parameters for the Client.GetEmbeddings method. +type ClientGetEmbeddingsOptions struct { + // placeholder for future optional parameters +} + +// CompletionRequest - Request options to get completions for a prompt +type CompletionRequest struct { + // Generates bestof completions server-side and returns the "best" (the one with the highest log probability per token). Results + // cannot be streamed. When used with n, bestof controls the number of + // candidate completions and n specifies how many to return – bestof must be greater than n. Note: Because this parameter + // generates many completions, it can quickly consume your token quota. Use + // carefully and ensure that you have reasonable settings for maxtokens and stop. Has maximum value of 128. + BestOf *int32 + + // can be used to disable any server-side caching, 0=no cache, 1=prompt prefix enabled, 2=full cache + CacheLevel *int32 + CompletionConfig *string + + // Echo back the prompt in addition to the completion + Echo *bool + + // Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, + // decreasing the model's likelihood to repeat the same line verbatim. + FrequencyPenalty *float32 + + // Defaults to null. Modify the likelihood of specified tokens appearing in the completion. Accepts a json object that maps + // tokens (specified by their token ID in the GPT tokenizer) to an associated bias + // value from -100 to 100. You can use this tokenizer tool (which works for both GPT-2 and GPT-3) to convert text to token + // IDs. Mathematically, the bias is added to the logits generated by the model + // prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood + // of selection; values like -100 or 100 should result in a ban or exclusive + // selection of the relevant token. As an example, you can pass {"50256" : -100} to prevent the token from being generated. + LogitBias any + + // Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. For example, if logprobs is + // 5, the API will return a list of the 5 most likely tokens. The API will always + // return the logprob of the sampled token, so there may be up to logprobs+1 elements in the response. Minimum of 0 and maximum + // of 5 allowed. + Logprobs *int32 + + // The token count of your prompt plus max_tokens cannot exceed the model's context length. Most models have a context length + // of 2048 tokens (except for the newest models, which support 4096). Has + // minimum of 0. + MaxTokens *int32 + + // ID of the model to use. You can use the ModelsList operation to see all of your available models, or see our ModelsGet + // overview for descriptions of them. + Model *string + + // How many completions to generate for each prompt. Minimum of 1 and maximum of 128 allowed. Note: Because this parameter + // generates many completions, it can quickly consume your token quota. Use + // carefully and ensure that you have reasonable settings for max_tokens and stop. + N *int32 + + // Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing + // the model's likelihood to talk about new topics. + PresencePenalty *float32 + + // The prompt(s) to generate completions for, encoded as a string or array of strings. Note that is the document separator + // that the model sees during training, so if a prompt is not specified the model + // will generate as if from the beginning of a new document. Maximum allowed size of string list is 2048. + Prompt any + + // Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. + Stop any + + // Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, + // with the stream terminated by a data: [DONE] message. + Stream *bool + + // The suffix that comes after a completion of inserted text. + Suffix *string + + // What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, + // and 0 (argmax sampling) for ones with a well-defined answer. We generally + // recommend altering this or top_p but not both. + Temperature *float32 + + // An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens + // with top_p probability mass. So 0.1 means only the tokens comprising the top + // 10% probability mass are considered. We generally recommend altering this or temperature but not both. + TopP *float32 + + // A unique identifier representing your end-user, which can help monitoring and detecting abuse + User *string +} + +// CompletionResponse - Expected response schema to completion request +type CompletionResponse struct { + // REQUIRED + Choices []*Post200ApplicationJSONPropertiesItemsItem + + // REQUIRED + Created *int32 + + // REQUIRED + ID *string + + // REQUIRED + Model *string + + // REQUIRED + Object *string + + // Representation of the token counts processed for a completions request. + Usage *CompletionUsage +} + +// CompletionUsage - Representation of the token counts processed for a completions request. +type CompletionUsage struct { + // REQUIRED + CompletionTokens *int32 + + // REQUIRED + PromptTokens *int32 + + // REQUIRED + TotalTokens *int32 +} + +// EmbeddingItem - Embedding item. +type EmbeddingItem struct { + // REQUIRED + Embedding []*float32 + + // REQUIRED + Index *int32 + + // REQUIRED + Object *string +} + +// EmbeddingRequest - Request options to create an embedding from input text +type EmbeddingRequest struct { + // REQUIRED; Input text to get embeddings for, encoded as a string. To get embeddings for multiple inputs in a single request, + // pass an array of strings. Each input must not exceed 2048 tokens in length. Unless you + // are embedding code, we suggest replacing newlines (\n) in your input with a single space, as we have observed inferior + // results when newlines are present. + Input any + + // OPTIONAL; Contains additional key/value pairs not defined in the schema. + AdditionalProperties map[string]any + + // input type of embedding search to use + InputType *string + + // ID of the model to use. You can use the ModelsList operation to see all of your available models, or see our ModelsGet + // overview for descriptions of them. + Model *string + + // A unique identifier representing your end-user, which can help monitoring and detecting abuse. + User *string +} + +// EmbeddingResponse - Expected response schema to embedding request +type EmbeddingResponse struct { + // REQUIRED + Data []*EmbeddingItem + + // REQUIRED + Model *string + + // REQUIRED + Object *string + + // REQUIRED; Token counts processed for an embeddings request. + Usage *EmbeddingsUsage +} + +// EmbeddingsUsage - Token counts processed for an embeddings request. +type EmbeddingsUsage struct { + // REQUIRED + PromptTokens *int32 + + // REQUIRED + TotalTokens *int32 +} + +type Post200ApplicationJSONPropertiesItemsItem struct { + FinishReason *string + Index *int32 + Logprobs *PostResponses200ContentApplicationJSONSchemaChoicesItemLogprobs + Text *string +} + +type PostResponses200ContentApplicationJSONSchemaChoicesItemLogprobs struct { + TextOffset []*int32 + TokenLogprobs []*float32 + Tokens []*string + TopLogprobs []map[string]*float32 +} diff --git a/sdk/cognitiveservices/azopenai/models_serde.go b/sdk/cognitiveservices/azopenai/models_serde.go new file mode 100644 index 000000000000..3d4bef2cbcca --- /dev/null +++ b/sdk/cognitiveservices/azopenai/models_serde.go @@ -0,0 +1,461 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// Code generated by Microsoft (R) AutoRest Code Generator. +// Changes may cause incorrect behavior and will be lost if the code is regenerated. +// DO NOT EDIT. + +package azopenai + +import ( + "encoding/json" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "reflect" +) + +// MarshalJSON implements the json.Marshaller interface for type CompletionRequest. +func (c CompletionRequest) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "best_of", c.BestOf) + populate(objectMap, "cache_level", c.CacheLevel) + populate(objectMap, "completion_config", c.CompletionConfig) + populate(objectMap, "echo", c.Echo) + populate(objectMap, "frequency_penalty", c.FrequencyPenalty) + populateAny(objectMap, "logit_bias", c.LogitBias) + populate(objectMap, "logprobs", c.Logprobs) + populate(objectMap, "max_tokens", c.MaxTokens) + populate(objectMap, "model", c.Model) + populate(objectMap, "n", c.N) + populate(objectMap, "presence_penalty", c.PresencePenalty) + populateAny(objectMap, "prompt", c.Prompt) + populateAny(objectMap, "stop", c.Stop) + populate(objectMap, "stream", c.Stream) + populate(objectMap, "suffix", c.Suffix) + populate(objectMap, "temperature", c.Temperature) + populate(objectMap, "top_p", c.TopP) + populate(objectMap, "user", c.User) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type CompletionRequest. +func (c *CompletionRequest) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", c, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "best_of": + err = unpopulate(val, "BestOf", &c.BestOf) + delete(rawMsg, key) + case "cache_level": + err = unpopulate(val, "CacheLevel", &c.CacheLevel) + delete(rawMsg, key) + case "completion_config": + err = unpopulate(val, "CompletionConfig", &c.CompletionConfig) + delete(rawMsg, key) + case "echo": + err = unpopulate(val, "Echo", &c.Echo) + delete(rawMsg, key) + case "frequency_penalty": + err = unpopulate(val, "FrequencyPenalty", &c.FrequencyPenalty) + delete(rawMsg, key) + case "logit_bias": + err = unpopulate(val, "LogitBias", &c.LogitBias) + delete(rawMsg, key) + case "logprobs": + err = unpopulate(val, "Logprobs", &c.Logprobs) + delete(rawMsg, key) + case "max_tokens": + err = unpopulate(val, "MaxTokens", &c.MaxTokens) + delete(rawMsg, key) + case "model": + err = unpopulate(val, "Model", &c.Model) + delete(rawMsg, key) + case "n": + err = unpopulate(val, "N", &c.N) + delete(rawMsg, key) + case "presence_penalty": + err = unpopulate(val, "PresencePenalty", &c.PresencePenalty) + delete(rawMsg, key) + case "prompt": + err = unpopulate(val, "Prompt", &c.Prompt) + delete(rawMsg, key) + case "stop": + err = unpopulate(val, "Stop", &c.Stop) + delete(rawMsg, key) + case "stream": + err = unpopulate(val, "Stream", &c.Stream) + delete(rawMsg, key) + case "suffix": + err = unpopulate(val, "Suffix", &c.Suffix) + delete(rawMsg, key) + case "temperature": + err = unpopulate(val, "Temperature", &c.Temperature) + delete(rawMsg, key) + case "top_p": + err = unpopulate(val, "TopP", &c.TopP) + delete(rawMsg, key) + case "user": + err = unpopulate(val, "User", &c.User) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", c, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type CompletionResponse. +func (c CompletionResponse) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "choices", c.Choices) + populate(objectMap, "created", c.Created) + populate(objectMap, "id", c.ID) + populate(objectMap, "model", c.Model) + populate(objectMap, "object", c.Object) + populate(objectMap, "usage", c.Usage) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type CompletionResponse. +func (c *CompletionResponse) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", c, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "choices": + err = unpopulate(val, "Choices", &c.Choices) + delete(rawMsg, key) + case "created": + err = unpopulate(val, "Created", &c.Created) + delete(rawMsg, key) + case "id": + err = unpopulate(val, "ID", &c.ID) + delete(rawMsg, key) + case "model": + err = unpopulate(val, "Model", &c.Model) + delete(rawMsg, key) + case "object": + err = unpopulate(val, "Object", &c.Object) + delete(rawMsg, key) + case "usage": + err = unpopulate(val, "Usage", &c.Usage) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", c, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type CompletionUsage. +func (c CompletionUsage) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "completion_tokens", c.CompletionTokens) + populate(objectMap, "prompt_tokens", c.PromptTokens) + populate(objectMap, "total_tokens", c.TotalTokens) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type CompletionUsage. +func (c *CompletionUsage) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", c, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "completion_tokens": + err = unpopulate(val, "CompletionTokens", &c.CompletionTokens) + delete(rawMsg, key) + case "prompt_tokens": + err = unpopulate(val, "PromptTokens", &c.PromptTokens) + delete(rawMsg, key) + case "total_tokens": + err = unpopulate(val, "TotalTokens", &c.TotalTokens) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", c, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type EmbeddingItem. +func (e EmbeddingItem) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "embedding", e.Embedding) + populate(objectMap, "index", e.Index) + populate(objectMap, "object", e.Object) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingItem. +func (e *EmbeddingItem) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "embedding": + err = unpopulate(val, "Embedding", &e.Embedding) + delete(rawMsg, key) + case "index": + err = unpopulate(val, "Index", &e.Index) + delete(rawMsg, key) + case "object": + err = unpopulate(val, "Object", &e.Object) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type EmbeddingRequest. +func (e EmbeddingRequest) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populateAny(objectMap, "input", e.Input) + populate(objectMap, "input_type", e.InputType) + populate(objectMap, "model", e.Model) + populate(objectMap, "user", e.User) + if e.AdditionalProperties != nil { + for key, val := range e.AdditionalProperties { + objectMap[key] = val + } + } + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingRequest. +func (e *EmbeddingRequest) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "input": + err = unpopulate(val, "Input", &e.Input) + delete(rawMsg, key) + case "input_type": + err = unpopulate(val, "InputType", &e.InputType) + delete(rawMsg, key) + case "model": + err = unpopulate(val, "Model", &e.Model) + delete(rawMsg, key) + case "user": + err = unpopulate(val, "User", &e.User) + delete(rawMsg, key) + default: + if e.AdditionalProperties == nil { + e.AdditionalProperties = map[string]any{} + } + if val != nil { + var aux any + err = json.Unmarshal(val, &aux) + e.AdditionalProperties[key] = aux + } + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type EmbeddingResponse. +func (e EmbeddingResponse) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "data", e.Data) + populate(objectMap, "model", e.Model) + populate(objectMap, "object", e.Object) + populate(objectMap, "usage", e.Usage) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingResponse. +func (e *EmbeddingResponse) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "data": + err = unpopulate(val, "Data", &e.Data) + delete(rawMsg, key) + case "model": + err = unpopulate(val, "Model", &e.Model) + delete(rawMsg, key) + case "object": + err = unpopulate(val, "Object", &e.Object) + delete(rawMsg, key) + case "usage": + err = unpopulate(val, "Usage", &e.Usage) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type EmbeddingsUsage. +func (e EmbeddingsUsage) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "prompt_tokens", e.PromptTokens) + populate(objectMap, "total_tokens", e.TotalTokens) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingsUsage. +func (e *EmbeddingsUsage) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "prompt_tokens": + err = unpopulate(val, "PromptTokens", &e.PromptTokens) + delete(rawMsg, key) + case "total_tokens": + err = unpopulate(val, "TotalTokens", &e.TotalTokens) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", e, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type Post200ApplicationJSONPropertiesItemsItem. +func (p Post200ApplicationJSONPropertiesItemsItem) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "finish_reason", p.FinishReason) + populate(objectMap, "index", p.Index) + populate(objectMap, "logprobs", p.Logprobs) + populate(objectMap, "text", p.Text) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type Post200ApplicationJSONPropertiesItemsItem. +func (p *Post200ApplicationJSONPropertiesItemsItem) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", p, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "finish_reason": + err = unpopulate(val, "FinishReason", &p.FinishReason) + delete(rawMsg, key) + case "index": + err = unpopulate(val, "Index", &p.Index) + delete(rawMsg, key) + case "logprobs": + err = unpopulate(val, "Logprobs", &p.Logprobs) + delete(rawMsg, key) + case "text": + err = unpopulate(val, "Text", &p.Text) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", p, err) + } + } + return nil +} + +// MarshalJSON implements the json.Marshaller interface for type PostResponses200ContentApplicationJSONSchemaChoicesItemLogprobs. +func (p PostResponses200ContentApplicationJSONSchemaChoicesItemLogprobs) MarshalJSON() ([]byte, error) { + objectMap := make(map[string]any) + populate(objectMap, "text_offset", p.TextOffset) + populate(objectMap, "token_logprobs", p.TokenLogprobs) + populate(objectMap, "tokens", p.Tokens) + populate(objectMap, "top_logprobs", p.TopLogprobs) + return json.Marshal(objectMap) +} + +// UnmarshalJSON implements the json.Unmarshaller interface for type PostResponses200ContentApplicationJSONSchemaChoicesItemLogprobs. +func (p *PostResponses200ContentApplicationJSONSchemaChoicesItemLogprobs) UnmarshalJSON(data []byte) error { + var rawMsg map[string]json.RawMessage + if err := json.Unmarshal(data, &rawMsg); err != nil { + return fmt.Errorf("unmarshalling type %T: %v", p, err) + } + for key, val := range rawMsg { + var err error + switch key { + case "text_offset": + err = unpopulate(val, "TextOffset", &p.TextOffset) + delete(rawMsg, key) + case "token_logprobs": + err = unpopulate(val, "TokenLogprobs", &p.TokenLogprobs) + delete(rawMsg, key) + case "tokens": + err = unpopulate(val, "Tokens", &p.Tokens) + delete(rawMsg, key) + case "top_logprobs": + err = unpopulate(val, "TopLogprobs", &p.TopLogprobs) + delete(rawMsg, key) + } + if err != nil { + return fmt.Errorf("unmarshalling type %T: %v", p, err) + } + } + return nil +} + +func populate(m map[string]any, k string, v any) { + if v == nil { + return + } else if azcore.IsNullValue(v) { + m[k] = nil + } else if !reflect.ValueOf(v).IsNil() { + m[k] = v + } +} + +func populateAny(m map[string]any, k string, v any) { + if v == nil { + return + } else if azcore.IsNullValue(v) { + m[k] = nil + } else { + m[k] = v + } +} + +func unpopulate(data json.RawMessage, fn string, v any) error { + if data == nil { + return nil + } + if err := json.Unmarshal(data, v); err != nil { + return fmt.Errorf("struct field %s: %v", fn, err) + } + return nil +} diff --git a/sdk/cognitiveservices/azopenai/response_types.go b/sdk/cognitiveservices/azopenai/response_types.go new file mode 100644 index 000000000000..b38874cb00ed --- /dev/null +++ b/sdk/cognitiveservices/azopenai/response_types.go @@ -0,0 +1,22 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// Code generated by Microsoft (R) AutoRest Code Generator. +// Changes may cause incorrect behavior and will be lost if the code is regenerated. +// DO NOT EDIT. + +package azopenai + +// ClientGetCompletionsResponse contains the response from method Client.GetCompletions. +type ClientGetCompletionsResponse struct { + CompletionResponse + // ApimRequestID contains the information returned from the apim-request-id header response. + ApimRequestID *string +} + +// ClientGetEmbeddingsResponse contains the response from method Client.GetEmbeddings. +type ClientGetEmbeddingsResponse struct { + EmbeddingResponse +}