-
Notifications
You must be signed in to change notification settings - Fork 971
[azopenai] Readme and examples #21192
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2402051
Readme and examples
bfc67a3
Update topline
7e1706c
Updating readme and custom_client.go's package comment
39fbda4
Adding in a functions example, rerecord.
aa27e23
Oops, no space!
33f0462
C'mon man, it's OpenAI, not Open AI! (thanks Charles)
richardpark-msft aef3df4
go:build needs to be there.
richardpark-msft f8933fa
Merge branch 'ai-readme' of github.com:richardpark-msft/azure-sdk-for…
80599fc
Use TODO instead of panic in examples
0e23531
Use Azure OpenAI as the endpoint.
2c48cfa
Moving comment into the right spot
richardpark-msft 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
66 changes: 66 additions & 0 deletions
66
sdk/cognitiveservices/azopenai/example_client_createimage_test.go
This file contains hidden or 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,66 @@ | ||
| //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_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore/to" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai" | ||
| ) | ||
|
|
||
| func ExampleClient_CreateImage() { | ||
| azureOpenAIKey := os.Getenv("AOAI_API_KEY") | ||
|
|
||
| // Ex: "https://<your-azure-openai-host>.openai.azure.com" | ||
| azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT") | ||
|
|
||
| if azureOpenAIKey == "" || azureOpenAIEndpoint == "" { | ||
| fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n") | ||
| return | ||
| } | ||
|
|
||
| keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, "", nil) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| resp, err := client.CreateImage(context.TODO(), azopenai.ImageGenerationOptions{ | ||
| Prompt: to.Ptr("a cat"), | ||
| ResponseFormat: to.Ptr(azopenai.ImageGenerationResponseFormatURL), | ||
| }, nil) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| for _, generatedImage := range resp.Data { | ||
| // the underlying type for the generatedImage is dictated by the value of | ||
| // ImageGenerationOptions.ResponseFormat. In this example we used `azopenai.ImageGenerationResponseFormatURL`, | ||
| // so the underlying type will be ImageLocation. | ||
|
|
||
| resp, err := http.Head(*generatedImage.URL) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| fmt.Fprintf(os.Stderr, "Image generated, HEAD request on URL returned %d\n", resp.StatusCode) | ||
| } | ||
|
|
||
| // Output: | ||
|
richardpark-msft marked this conversation as resolved.
|
||
| } | ||
55 changes: 55 additions & 0 deletions
55
sdk/cognitiveservices/azopenai/example_client_embeddings_test.go
This file contains hidden or 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,55 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| package azopenai_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/cognitiveservices/azopenai" | ||
| ) | ||
|
|
||
| func ExampleClient_GetEmbeddings() { | ||
| azureOpenAIKey := os.Getenv("AOAI_API_KEY") | ||
| modelDeploymentID := os.Getenv("AOAI_EMBEDDINGS_MODEL_DEPLOYMENT") | ||
|
|
||
| // Ex: "https://<your-azure-openai-host>.openai.azure.com" | ||
| azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT") | ||
|
|
||
| if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" { | ||
| fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n") | ||
| return | ||
| } | ||
|
|
||
| keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| // In Azure OpenAI you must deploy a model before you can use it in your client. For more information | ||
| // see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource | ||
| client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, modelDeploymentID, nil) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| resp, err := client.GetEmbeddings(context.TODO(), azopenai.EmbeddingsOptions{ | ||
| Input: []string{"The food was delicious and the waiter..."}, | ||
| Model: &modelDeploymentID, | ||
| }, nil) | ||
|
|
||
| if err != nil { | ||
| // TODO: handle error | ||
| } | ||
|
|
||
| for _, embed := range resp.Data { | ||
| // embed.Embedding contains the embeddings for this input index. | ||
| fmt.Fprintf(os.Stderr, "Got embeddings for input %d\n", *embed.Index) | ||
| } | ||
|
|
||
| // Output: | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.