This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completions_client.go
137 lines (125 loc) · 5.57 KB
/
completions_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//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.
package azopenai
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)
// CompletionsClient contains the methods for the Completions group.
// Don't use this type directly, use NewCompletionsClient() instead.
type CompletionsClient struct {
endpoint string
apiKey *APIKeyClient
internal *arm.Client
}
// NewCompletionsClient creates a new instance of CompletionsClient with the specified values.
// - credential - used to authorize requests. Usually a credential from azidentity.
// - options - pass nil to accept the default values.
func NewCompletionsClient(endpoint string, credential azcore.TokenCredential, options *arm.ClientOptions) (*CompletionsClient, error) {
cl, err := arm.NewClient(moduleName+".CompletionsClient", moduleVersion, credential, options)
if err != nil {
return nil, err
}
client := &CompletionsClient{
endpoint: endpoint,
internal: cl,
}
return client, nil
}
// NewCompletionsClientFromAPIKey creates a new instance of CompletionsClient with the specified values.
// - apiKey - used to authorize requests.
func NewCompletionsClientFromAPIKey(endpoint string, apiKey string, options *policy.ClientOptions) (*CompletionsClient, error) {
return &CompletionsClient{
endpoint: endpoint,
apiKey: NewAPIKeyClient(apiKey, options),
}, nil
}
// Create - 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 2023-03-15-preview
// - options - CompletionsClientCreateOptions contains the optional parameters for the CompletionsClient.Create method.
func (client *CompletionsClient) Create(ctx context.Context, deploymentID string, body CompletionsCreateParameters, options *CompletionsClientCreateOptions) (CompletionsClientCreateResponse, error) {
req, err := client.createCreateRequest(ctx, deploymentID, body, options)
if err != nil {
return CompletionsClientCreateResponse{}, err
}
resp, err := getPipeline(client.apiKey, client.internal).Do(req)
if err != nil {
return CompletionsClientCreateResponse{}, err
}
if !runtime.HasStatusCode(resp, http.StatusOK) {
return CompletionsClientCreateResponse{}, runtime.NewResponseError(resp)
}
return client.createHandleResponse(resp)
}
// CreateStream - Creates a completion for the chat message in stream
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2023-03-15-preview
// - options - CompletionsClientCreateOptions contains the optional parameters for the CompletionsClient.CreateStream method.
func (client *CompletionsClient) CreateStream(ctx context.Context, deploymentID string, body CompletionsCreateParameters, options *CompletionsClientCreateOptions) (CompletionsClientCreateStreamResponse, error) {
body.Stream = to.Ptr(true) // ensure the request has { "stream": true }
req, err := streamRequest(client.createCreateRequest(ctx, deploymentID, body, options))
if err != nil {
return CompletionsClientCreateStreamResponse{}, err
}
resp, err := getPipeline(client.apiKey, client.internal).Do(req)
if err != nil {
return CompletionsClientCreateStreamResponse{}, err
}
if !runtime.HasStatusCode(resp, http.StatusOK) {
return CompletionsClientCreateStreamResponse{}, runtime.NewResponseError(resp)
}
return client.createHandlerResponseStream(resp)
}
// createCreateRequest creates the Create request.
func (client *CompletionsClient) createCreateRequest(ctx context.Context, deploymentID string, body CompletionsCreateParameters, options *CompletionsClientCreateOptions) (*policy.Request, error) {
host := "https://{endpoint}/openai"
host = strings.ReplaceAll(host, "{endpoint}", client.endpoint)
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(host, urlPath))
if err != nil {
return nil, err
}
reqQP := req.Raw().URL.Query()
reqQP.Set("api-version", "2023-03-15-preview")
req.Raw().URL.RawQuery = reqQP.Encode()
req.Raw().Header["Accept"] = []string{"application/json"}
return req, runtime.MarshalAsJSON(req, body)
}
// createHandleResponse handles the Create response.
func (client *CompletionsClient) createHandleResponse(resp *http.Response) (CompletionsClientCreateResponse, error) {
result := CompletionsClientCreateResponse{}
if val := resp.Header.Get("apim-request-id"); val != "" {
result.ApimRequestID = &val
}
if err := runtime.UnmarshalAsJSON(resp, &result.Completions); err != nil {
return CompletionsClientCreateResponse{}, err
}
return result, nil
}
// createHandlerResponseStream handles the CreateStream response.
func (client *CompletionsClient) createHandlerResponseStream(resp *http.Response) (CompletionsClientCreateStreamResponse, error) {
return CompletionsClientCreateStreamResponse{
StreamReader: &StreamReader[Completions]{
Reader: resp.Body,
},
}, nil
}