-
Notifications
You must be signed in to change notification settings - Fork 853
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
OpenAI client #20617
Merged
Merged
OpenAI client #20617
Changes from all commits
Commits
Show all changes
65 commits
Select commit
Hold shift + click to select a range
c5660e6
Add autorest.md file
mikekistler 182a3b9
Add build.go
mikekistler 216b15c
Generated client
mikekistler a8d3e71
Add custom_client.go and supporting files
mikekistler 3db25b0
Add LICENSE.txt, README.md, CHANGELOG.md.
mikekistler e90a278
Add tests -- WIP
mikekistler 8b446ad
Load an env file for the key and endpoint
2f8401c
starting to add in recordings
8314e07
Adding in recordings
3bd833c
ci.yml
9c97e7f
Use latest autorest-go
797fdd1
Update sdk/cognitiveservices/azopenai/custom_client.go
richardpark-msft 2fb1fea
Update sdk/cognitiveservices/azopenai/custom_client.go
richardpark-msft 9e5c6aa
Update sdk/cognitiveservices/azopenai/custom_client.go
richardpark-msft 2c10176
Strip out deploymentID formatting from the individual client function…
5938ac5
splice out the auto-generated `deploymentID` field from the client
6535eca
splice out the auto-generated `deploymentID` field from the client
b3df143
No en-us links
afcf521
Added in another recorded test
a4f5a23
Remove check, we'll let the service handle it.
ec5978c
Package isn't published yet, temp link for now.
6131717
Whoops, uri.JoinPaths is a new API.
8f64745
Missed another dead link.
3d97237
Missing doc comment and rename suggested by Jeff.
e30c767
Docs for EventReader
2d42c9f
Adding more docs, unexporting most of policy
90e12c8
Unexport serviceAPIVersions
518e9b0
Unexport serviceAPIVersions
88fcc73
Update recordings and tag
6bd8da6
Merge remote-tracking branch 'upstream/main' into openai-client
18dfcf3
Merge remote-tracking branch 'upstream/main' into openai-client
dd70e9d
Hm...
a029a80
Oops, wrong path
5691319
Fixed generation issue
a8d9d23
Adding in a test that shows error parsing.
c5c1bb6
Updated to use require
ac088e1
New function test
8c1403b
Scrub the unused error types (error, innnerror, blah)
5b886e7
New functions, new problems.
5e05b7f
Add in a token credential test as well.
d94ce3b
Remove unused serviceversions enum.
0c44586
Remove the entire "Client<blah>" prefix. WE ARE THE CLIENT.
94a3fd1
Dang it, boolean logic.
b54d9b2
Updating tests.
a8f319a
Adding current coverage
0e26d92
Updated test, skip in playback for now.
15ed993
Hit the OpenAI endpoint for chat completions.
4bfc4b2
Test for getting back an error from OpenAI directly.
08e85f4
Some more tests.
77f56cc
Upated doc comment and field name from Jeff's suggestion.
69eb508
Adding client creation examples
7f6cf78
Update sdk/cognitiveservices/azopenai/CHANGELOG.md
richardpark-msft a3f5932
Update eng/config.json
richardpark-msft 8417772
Update sdk/cognitiveservices/azopenai/README.md
richardpark-msft 97c14ae
Update sdk/cognitiveservices/azopenai/README.md
richardpark-msft a57e1a6
Update sdk/cognitiveservices/azopenai/README.md
richardpark-msft 6c50f68
Examples update
64edff8
Merge branch 'openai-client' of https://github.com/mikekistler/azure-…
7986b4d
Added in a streaming completions example (with a test!)
3b3f549
Don't fail the example test if there's no key actually configured.
aa90d20
Updating some more, use the same env variable for the streaming deplo…
176af41
Rerecording.
4250ac8
Don't let the test run for now (output is conditional since the env v…
8b3ec9f
Needed to fill in another fake variable.
432cdfb
rerecord...
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,5 @@ | ||
# Release History | ||
|
||
## 0.1.0 (unreleased) | ||
|
||
* Initial release of the `azopenai` library |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE |
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,107 @@ | ||
# Azure OpenAI client module for Go | ||
|
||
Azure OpenAI is a managed service that allows developers to deploy, tune, and generate content from OpenAI models on Azure resources. | ||
|
||
The Azure OpenAI client library for GO is an adaptation of OpenAI's REST APIs that provides an idiomatic interface and rich integration with the rest of the Azure SDK ecosystem. | ||
|
||
[Source code][azopenai_repo] | [Package (pkg.go.dev)][azopenai_pkg_go] | [REST API documentation][openai_rest_docs] | [Product documentation][openai_docs] | ||
|
||
## Getting started | ||
|
||
### Prerequisites | ||
|
||
* Go, version 1.18 or higher - [Install Go](https://go.dev/doc/install) | ||
* [Azure subscription][azure_sub] | ||
* [Azure OpenAI access][azure_openai_access] | ||
|
||
### Install the packages | ||
|
||
Install the `azopenai` and `azidentity` modules with `go get`: | ||
|
||
```bash | ||
go get github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai | ||
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity | ||
``` | ||
|
||
The [azidentity][azure_identity] module is used for authentication during client construction. | ||
|
||
### Authentication | ||
|
||
<!-- TODO: Add api-key authentication instructions --> | ||
|
||
#### Create a client | ||
|
||
Constructing the client requires your vault's URL, which you can get from the Azure CLI or the Azure Portal. | ||
|
||
```go | ||
import ( | ||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai" | ||
) | ||
|
||
func main() { | ||
endpoint := "https://<TODO: OpenAI endpoint>" | ||
apiKey := "<TODO: OpenAI API key>" | ||
|
||
var err error | ||
cred := azopenai.KeyCredential{APIKey: apiKey} | ||
client, err := azopenai.NewClientWithKeyCredential(endpoint, cred, &options) | ||
if err != nil { | ||
// TODO: handle error | ||
} | ||
} | ||
``` | ||
|
||
## Key concepts | ||
|
||
See [Key concepts][openai_key_concepts] in the product documentation for more details about general concepts. | ||
|
||
## Troubleshooting | ||
|
||
### Error Handling | ||
|
||
All methods that send HTTP requests return `*azcore.ResponseError` when these requests fail. `ResponseError` has error details and the raw response from the service. | ||
|
||
### Logging | ||
|
||
This module uses the logging implementation in `azcore`. To turn on logging for all Azure SDK modules, set `AZURE_SDK_GO_LOGGING` to `all`. By default, the logger writes to stderr. Use the `azcore/log` package to control log output. For example, logging only HTTP request and response events, and printing them to stdout: | ||
|
||
```go | ||
import azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log" | ||
|
||
// Print log events to stdout | ||
azlog.SetListener(func(cls azlog.Event, msg string) { | ||
fmt.Println(msg) | ||
}) | ||
|
||
// Includes only requests and responses in credential logs | ||
azlog.SetEvents(azlog.EventRequest, azlog.EventResponse) | ||
``` | ||
|
||
## Contributing | ||
|
||
This project welcomes contributions and suggestions. Most contributions require you to agree to a [Contributor License Agreement (CLA)][cla] declaring that you have the right to, and actually do, grant us the rights to use your contribution. | ||
|
||
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate | ||
the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to | ||
do this once across all repos using our CLA. | ||
|
||
This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information, see | ||
the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or | ||
comments. | ||
|
||
<!-- LINKS --> | ||
<!-- https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/cognitiveservices/azopenai -> | ||
<!-- https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai --> | ||
[azure_openai_access]: https://learn.microsoft.com/azure/cognitive-services/openai/overview#how-do-i-get-access-to-azure-openai | ||
[azopenai_repo]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk | ||
richardpark-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[azopenai_pkg_go]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk | ||
[azure_identity]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity | ||
[azure_sub]: https://azure.microsoft.com/free/ | ||
[openai_docs]: https://learn.microsoft.com/azure/cognitive-services/openai | ||
[openai_key_concepts]: https://learn.microsoft.com/azure/cognitive-services/openai/overview#key-concepts | ||
[openai_rest_docs]: https://learn.microsoft.com/azure/cognitive-services/openai/reference | ||
[cla]: https://cla.microsoft.com | ||
[coc]: https://opensource.microsoft.com/codeofconduct/ | ||
[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ | ||
[coc_contact]: mailto:opencode@microsoft.com |
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,6 @@ | ||
{ | ||
"AssetsRepo": "Azure/azure-sdk-assets", | ||
"AssetsRepoPrefixPath": "go", | ||
"TagPrefix": "go/cognitiveservices/azopenai", | ||
"Tag": "go/cognitiveservices/azopenai_0bc6dc4171" | ||
} |
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,150 @@ | ||
# Go | ||
|
||
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 | ||
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/go@4.0.0-preview.50" | ||
``` | ||
|
||
## Transformations | ||
|
||
``` yaml | ||
directive: | ||
# Add x-ms-parameter-location to parameters in x-ms-parameterized-host | ||
- from: openapi-document | ||
where: $.servers.0.variables.endpoint | ||
debug: true | ||
transform: $["x-ms-parameter-location"] = "client"; | ||
|
||
# Make deploymentId a client parameter | ||
# This must be done in each operation as the parameter is not defined in the components section | ||
- from: openapi-document | ||
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 | ||
transform: $["required"] = true; | ||
- from: openapi-document | ||
where: $.paths["/deployments/{deploymentId}/embeddings"].post.requestBody | ||
transform: $["required"] = true; | ||
|
||
# Remove stream property from CompletionsOptions and ChatCompletionsOptions | ||
- from: openapi-document | ||
where: $.components.schemas["CompletionsOptions"] | ||
transform: delete $.properties.stream; | ||
- from: openapi-document | ||
where: $.components.schemas["ChatCompletionsOptions"] | ||
transform: delete $.properties.stream; | ||
|
||
# Replace anyOf schemas with an empty schema (no type) to get an "any" type generated | ||
- from: openapi-document | ||
where: '$.components.schemas["EmbeddingsOptions"].properties["input"]' | ||
transform: delete $.anyOf; | ||
|
||
# Fix autorest bug | ||
- from: openapi-document | ||
where: $.components.schemas["ChatMessage"].properties.role | ||
transform: > | ||
delete $.allOf; | ||
$["$ref"] = "#/components/schemas/ChatRole"; | ||
|
||
# Fix another autorest bug | ||
- from: openapi-document | ||
where: $.components.schemas["Choice"].properties.finish_reason | ||
transform: > | ||
delete $.oneOf; | ||
$["$ref"] = "#/components/schemas/CompletionsFinishReason"; | ||
- 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 | ||
transform: > | ||
delete $.allOf; | ||
$["$ref"] = "#/components/schemas/CompletionsUsage"; | ||
- from: openapi-document | ||
where: $.components.schemas["Completions"].properties.usage | ||
transform: > | ||
delete $.allOf; | ||
$["$ref"] = "#/components/schemas/CompletionsUsage"; | ||
|
||
# | ||
# strip out the deploymentID validation code - we absorbed this into the endpoint. | ||
# | ||
# urlPath := "/deployments/{deploymentId}/embeddings" | ||
# if client.deploymentID == "" { | ||
# return nil, errors.New("parameter client.deploymentID cannot be empty") | ||
# } | ||
# urlPath = strings.ReplaceAll(urlPath, "{deploymentId}", url.PathEscape(client.deploymentID)) | ||
- from: client.go | ||
where: $ | ||
transform: >- | ||
return $.replace( | ||
/(\s+)urlPath\s*:=\s*"\/deployments\/\{deploymentId\}\/([^"]+)".+?url\.PathEscape.+?\n/gs, | ||
"$1urlPath := \"$2\"\n") | ||
|
||
# splice out the auto-generated `deploymentID` field from the client | ||
- from: client.go | ||
where: $ | ||
transform: >- | ||
return $.replace( | ||
/(type Client struct.+?)deploymentID string([^}]+})/s, | ||
"$1$2") | ||
|
||
# delete unused error models | ||
- from: 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 | ||
where: $ | ||
transform: return $.replace(/(?:\/\/.*\s)?func \(\w \*?(?:ErrorResponse|ErrorResponseError|InnerError|InnerErrorInnererror)\).*\{\s(?:.+\s)+\}\s/g, ""); | ||
|
||
- from: constants.go | ||
where: $ | ||
transform: >- | ||
return $.replace( | ||
/type ServiceAPIVersions string.+PossibleServiceAPIVersionsValues.+?\n}/gs, | ||
"") | ||
|
||
# delete client name prefix from method options and response types | ||
- from: | ||
- client.go | ||
- models.go | ||
- response_types.go | ||
where: $ | ||
transform: return $.replace(/Client(\w+)((?:Options|Response))/g, "$1$2"); | ||
``` |
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 | ||
|
||
//go:generate autorest ./autorest.md | ||
//go:generate go mod tidy | ||
//go:generate goimports -w . | ||
|
||
// 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 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,28 @@ | ||
# NOTE: Please refer to https://aka.ms/azsdk/engsys/ci-yaml before editing this file. | ||
trigger: | ||
branches: | ||
include: | ||
- main | ||
- feature/* | ||
- hotfix/* | ||
- release/* | ||
paths: | ||
include: | ||
- sdk/cognitiveservices/azopenai | ||
- eng/ | ||
|
||
pr: | ||
branches: | ||
include: | ||
- main | ||
- feature/* | ||
- hotfix/* | ||
- release/* | ||
paths: | ||
include: | ||
- sdk/cognitiveservices/azopenai | ||
|
||
stages: | ||
- template: /eng/pipelines/templates/jobs/archetype-sdk-client.yml | ||
parameters: | ||
ServiceDirectory: "cognitiveservices/azopenai" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
vault's URL
is correct in this context.Copy/paste typo, likely from
azure-sdk-for-go/sdk/security/keyvault/azsecrets/README.md
Line 37 in 1e4fb7e