-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[azopenai] Readme and examples (#21192)
Creating examples and a readme for azopenai. Fixes #21038
- Loading branch information
1 parent
ceca085
commit ba64496
Showing
12 changed files
with
634 additions
and
219 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
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
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
66 changes: 66 additions & 0 deletions
66
sdk/cognitiveservices/azopenai/example_client_createimage_test.go
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,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: | ||
} |
55 changes: 55 additions & 0 deletions
55
sdk/cognitiveservices/azopenai/example_client_embeddings_test.go
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,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.