Skip to content
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 65 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
c5660e6
Add autorest.md file
mikekistler Apr 10, 2023
182a3b9
Add build.go
mikekistler Apr 10, 2023
216b15c
Generated client
mikekistler Apr 10, 2023
a8d3e71
Add custom_client.go and supporting files
mikekistler Apr 11, 2023
3db25b0
Add LICENSE.txt, README.md, CHANGELOG.md.
mikekistler Apr 19, 2023
e90a278
Add tests -- WIP
mikekistler Jun 4, 2023
8b446ad
Load an env file for the key and endpoint
Jun 13, 2023
2f8401c
starting to add in recordings
Jun 13, 2023
8314e07
Adding in recordings
Jun 13, 2023
3bd833c
ci.yml
Jun 13, 2023
9c97e7f
Use latest autorest-go
Jun 13, 2023
797fdd1
Update sdk/cognitiveservices/azopenai/custom_client.go
richardpark-msft Jun 13, 2023
2fb1fea
Update sdk/cognitiveservices/azopenai/custom_client.go
richardpark-msft Jun 13, 2023
9e5c6aa
Update sdk/cognitiveservices/azopenai/custom_client.go
richardpark-msft Jun 13, 2023
2c10176
Strip out deploymentID formatting from the individual client function…
Jun 13, 2023
5938ac5
splice out the auto-generated `deploymentID` field from the client
Jun 13, 2023
6535eca
splice out the auto-generated `deploymentID` field from the client
Jun 13, 2023
b3df143
No en-us links
Jun 13, 2023
afcf521
Added in another recorded test
Jun 13, 2023
a4f5a23
Remove check, we'll let the service handle it.
Jun 13, 2023
ec5978c
Package isn't published yet, temp link for now.
Jun 13, 2023
6131717
Whoops, uri.JoinPaths is a new API.
Jun 13, 2023
8f64745
Missed another dead link.
Jun 13, 2023
3d97237
Missing doc comment and rename suggested by Jeff.
Jun 13, 2023
e30c767
Docs for EventReader
Jun 14, 2023
2d42c9f
Adding more docs, unexporting most of policy
Jun 14, 2023
90e12c8
Unexport serviceAPIVersions
Jun 14, 2023
518e9b0
Unexport serviceAPIVersions
Jun 14, 2023
88fcc73
Update recordings and tag
Jun 14, 2023
6bd8da6
Merge remote-tracking branch 'upstream/main' into openai-client
Jun 14, 2023
18dfcf3
Merge remote-tracking branch 'upstream/main' into openai-client
Jun 14, 2023
dd70e9d
Hm...
Jun 14, 2023
a029a80
Oops, wrong path
Jun 14, 2023
5691319
Fixed generation issue
Jun 14, 2023
a8d9d23
Adding in a test that shows error parsing.
Jun 14, 2023
c5c1bb6
Updated to use require
Jun 14, 2023
ac088e1
New function test
Jun 14, 2023
8c1403b
Scrub the unused error types (error, innnerror, blah)
Jun 14, 2023
5b886e7
New functions, new problems.
Jun 14, 2023
5e05b7f
Add in a token credential test as well.
Jun 14, 2023
d94ce3b
Remove unused serviceversions enum.
Jun 14, 2023
0c44586
Remove the entire "Client<blah>" prefix. WE ARE THE CLIENT.
Jun 14, 2023
94a3fd1
Dang it, boolean logic.
Jun 14, 2023
b54d9b2
Updating tests.
Jun 14, 2023
a8f319a
Adding current coverage
Jun 14, 2023
0e26d92
Updated test, skip in playback for now.
Jun 14, 2023
15ed993
Hit the OpenAI endpoint for chat completions.
Jun 15, 2023
4bfc4b2
Test for getting back an error from OpenAI directly.
Jun 15, 2023
08e85f4
Some more tests.
Jun 15, 2023
77f56cc
Upated doc comment and field name from Jeff's suggestion.
Jun 15, 2023
69eb508
Adding client creation examples
Jun 15, 2023
7f6cf78
Update sdk/cognitiveservices/azopenai/CHANGELOG.md
richardpark-msft Jun 15, 2023
a3f5932
Update eng/config.json
richardpark-msft Jun 15, 2023
8417772
Update sdk/cognitiveservices/azopenai/README.md
richardpark-msft Jun 15, 2023
97c14ae
Update sdk/cognitiveservices/azopenai/README.md
richardpark-msft Jun 15, 2023
a57e1a6
Update sdk/cognitiveservices/azopenai/README.md
richardpark-msft Jun 15, 2023
6c50f68
Examples update
Jun 15, 2023
64edff8
Merge branch 'openai-client' of https://github.com/mikekistler/azure-…
Jun 15, 2023
7986b4d
Added in a streaming completions example (with a test!)
Jun 19, 2023
3b3f549
Don't fail the example test if there's no key actually configured.
Jun 19, 2023
aa90d20
Updating some more, use the same env variable for the streaming deplo…
Jun 19, 2023
176af41
Rerecording.
Jun 20, 2023
4250ac8
Don't let the test run for now (output is conditional since the env v…
Jun 20, 2023
8b3ec9f
Needed to fill in another fake variable.
Jun 20, 2023
432cdfb
rerecord...
Jun 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sdk/cognitiveservices/azopenai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Release History

## 0.1.0 (unreleased)

* This is the initial release of the `azopenai` library
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved
21 changes: 21 additions & 0 deletions sdk/cognitiveservices/azopenai/LICENSE.txt
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
101 changes: 101 additions & 0 deletions sdk/cognitiveservices/azopenai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Azure OpenAI client module for Go

The Azure OpenAI client module is used to ...
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved

[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 - [Create a free account][azure_sub]
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved

### 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.
Copy link
Member

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

Constructing the client also 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 [[email protected]][coc_contact] with any additional questions or
comments.

<!-- LINKS -->
[azopenai_repo]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/cognitiveservices/azopenai
[azopenai_pkg_go]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai
[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/en-us/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:[email protected]
6 changes: 6 additions & 0 deletions sdk/cognitiveservices/azopenai/assets.json
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_e2a2d2dbef"
}
121 changes: 121 additions & 0 deletions sdk/cognitiveservices/azopenai/autorest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# 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/[email protected]"
```

## 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";

# Remove deploymentId from path when not set
- from: client.go
where: $
transform: return $.replace(/\sreturn.*parameter client.deploymentID cannot be empty"\)\s/g, "urlPath = strings.ReplaceAll(urlPath, \"/deployments/{deploymentId}/\", \"/v1/\")");
# Drop error import
- from: client.go
where: $
transform: return $.replace(/(import \(\n(\s\"\w+\"\n)+)\s\"errors\"\n/g, "$1");

# delete unused error models
- from: models.go
where: $
transform: return $.replace(/(?:\/\/.*\s)?type (?:ErrorResponse|ErrorResponseError).+\{(?:\s.+\s)+\}\s/g, "");
- from: models_serde.go
where: $
transform: return $.replace(/(?:\/\/.*\s)?func \(\w \*?(?:ErrorResponse|ErrorResponseError)\).*\{\s(?:.+\s)+\}\s/g, "");

# rename the "AzureCoreFoundation" prefix from models
- from:
- client.go
- models.go
- models_serde.go
- response_types.go
where: $
transform: return $.replace(/AzureCoreFoundations/g, "");
```
11 changes: 11 additions & 0 deletions sdk/cognitiveservices/azopenai/build.go
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 gofmt -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
28 changes: 28 additions & 0 deletions sdk/cognitiveservices/azopenai/ci.yml
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"
Loading