-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom_client.go and supporting files
- Loading branch information
1 parent
2ad8578
commit b9f65fc
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//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. | ||
|
||
package azopenai | ||
|
||
// this file contains handwritten additions to the generated code | ||
|
||
import ( | ||
"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" | ||
) | ||
|
||
const ( | ||
clientName = "azopenai.Client" | ||
apiVersion = "2022-12-01" | ||
tokenScope = "https://cognitiveservices.azure.com/.default" | ||
) | ||
|
||
// Clients | ||
|
||
// ClientOptions contains optional settings for Client. | ||
type ClientOptions struct { | ||
azcore.ClientOptions | ||
} | ||
|
||
// NewClient creates a new instance of Client with the specified values. | ||
// - endpoint - OpenAI service endpoint | ||
// - credential - used to authorize requests. Usually a credential from azidentity. | ||
// - options - client options, pass nil to accept the default values. | ||
func NewClient(endpoint string, credential azcore.TokenCredential, options *ClientOptions) (*Client, error) { | ||
if options == nil { | ||
options = &ClientOptions{} | ||
} | ||
authPolicy := runtime.NewBearerTokenPolicy(credential, []string{tokenScope}, nil) | ||
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Client{endpoint: endpoint + "/openai", internal: azcoreClient}, nil | ||
} | ||
|
||
// NewClient creates a client that accesses Azure Monitor logs data. | ||
func NewClientWithKeyCredential(endpoint string, credential KeyCredential, options *ClientOptions) (*Client, error) { | ||
if options == nil { | ||
options = &ClientOptions{} | ||
} | ||
authPolicy := NewAPIKeyPolicy(credential, "api-key") | ||
azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Client{endpoint: endpoint + "/openai", internal: azcoreClient}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//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. | ||
|
||
package azopenai | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
) | ||
|
||
// KeyCredential | ||
|
||
type KeyCredential struct { | ||
APIKey string | ||
} | ||
|
||
// APIKeyPolicy authorizes requests with an API key acquired from a KeyCredential. | ||
type APIKeyPolicy struct { | ||
header string | ||
cred KeyCredential | ||
} | ||
|
||
// NewAPIKeyPolicy creates a policy object that authorizes requests with an API Key. | ||
// cred: a KeyCredential implementation. | ||
func NewAPIKeyPolicy(cred KeyCredential, header string) *APIKeyPolicy { | ||
return &APIKeyPolicy{ | ||
header: header, | ||
cred: cred, | ||
} | ||
} | ||
|
||
// Do returns a function which authorizes req with a token from the policy's credential | ||
func (b *APIKeyPolicy) Do(req *policy.Request) (*http.Response, error) { | ||
req.Raw().Header.Set(b.header, b.cred.APIKey) | ||
return req.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//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. | ||
|
||
package azopenai | ||
|
||
const ( | ||
version = "v0.1.0" | ||
) |