Skip to content

Commit d646597

Browse files
committed
update
1 parent c143eb7 commit d646597

File tree

1 file changed

+52
-24
lines changed

1 file changed

+52
-24
lines changed

articles/ai-services/openai/includes/chat-go.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,71 +40,99 @@ import (
4040
"os"
4141

4242
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
43+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
4344
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
4445
)
4546

4647
func main() {
47-
azureOpenAIKey := os.Getenv("AZURE_OPENAI_API_KEY")
48-
//modelDeploymentID = deployment name, if model name and deployment name do not match change this value to name chosen when you deployed the model.
49-
modelDeploymentID := "gpt-35-turbo"
48+
azureOpenAIKey := os.Getenv("AOAI_CHAT_COMPLETIONS_API_KEY")
49+
modelDeploymentID := os.Getenv("AOAI_CHAT_COMPLETIONS_MODEL")
5050

5151
// Ex: "https://<your-azure-openai-host>.openai.azure.com"
52-
azureOpenAIEndpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
52+
azureOpenAIEndpoint := os.Getenv("AOAI_CHAT_COMPLETIONS_ENDPOINT")
5353

5454
if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
5555
fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
5656
return
5757
}
5858

59-
keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)
60-
61-
if err != nil {
62-
// TODO: Update the following line with your application specific error handling logic
63-
log.Fatalf("ERROR: %s", err)
64-
}
59+
keyCredential := azcore.NewKeyCredential(azureOpenAIKey)
6560

61+
// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
62+
// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
6663
client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)
6764

6865
if err != nil {
69-
// TODO: Update the following line with your application specific error handling logic
70-
log.Fatalf("ERROR: %s", err)
66+
// TODO: Update the following line with your application specific error handling logic
67+
log.Printf("ERROR: %s", err)
68+
return
7169
}
7270

7371
// This is a conversation in progress.
7472
// NOTE: all messages, regardless of role, count against token usage for this API.
75-
messages := []azopenai.ChatMessage{
73+
messages := []azopenai.ChatRequestMessageClassification{
7674
// You set the tone and rules of the conversation with a prompt as the system role.
77-
{Role: to.Ptr(azopenai.ChatRoleSystem), Content: to.Ptr("You are a helpful assistant.")},
75+
&azopenai.ChatRequestSystemMessage{Content: to.Ptr("You are a helpful assistant. You will talk like a pirate.")},
7876

7977
// The user asks a question
80-
{Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("Does Azure OpenAI support customer managed keys?")},
78+
&azopenai.ChatRequestUserMessage{Content: azopenai.NewChatRequestUserMessageContent("Can you help me?")},
8179

82-
// The reply would come back from the Azure OpenAI model. You'd add it to the conversation so we can maintain context.
83-
{Role: to.Ptr(azopenai.ChatRoleAssistant), Content: to.Ptr("Yes, customer managed keys are supported by Azure OpenAI")},
80+
// The reply would come back from the ChatGPT. You'd add it to the conversation so we can maintain context.
81+
&azopenai.ChatRequestAssistantMessage{Content: to.Ptr("Arrrr! Of course, me hearty! What can I do for ye?")},
8482

8583
// The user answers the question based on the latest reply.
86-
{Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("Do other Azure AI services support this too?")},
84+
&azopenai.ChatRequestUserMessage{Content: azopenai.NewChatRequestUserMessageContent("What's the best way to train a parrot?")},
8785

88-
// from here you'd keep iterating, sending responses back from the chat completions API
86+
// from here you'd keep iterating, sending responses back from ChatGPT
8987
}
9088

89+
gotReply := false
90+
9191
resp, err := client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{
9292
// This is a conversation in progress.
9393
// NOTE: all messages count against token usage for this API.
94-
Messages: messages,
95-
Deployment: modelDeploymentID,
94+
Messages: messages,
95+
DeploymentName: &modelDeploymentID,
9696
}, nil)
9797

9898
if err != nil {
99-
// TODO: Update the following line with your application specific error handling logic
100-
log.Fatalf("ERROR: %s", err)
99+
// TODO: Update the following line with your application specific error handling logic
100+
log.Printf("ERROR: %s", err)
101+
return
101102
}
102103

103104
for _, choice := range resp.Choices {
104-
fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", *choice.Index, *choice.Message.Content)
105+
gotReply = true
106+
107+
if choice.ContentFilterResults != nil {
108+
fmt.Fprintf(os.Stderr, "Content filter results\n")
109+
110+
if choice.ContentFilterResults.Error != nil {
111+
fmt.Fprintf(os.Stderr, " Error:%v\n", choice.ContentFilterResults.Error)
112+
}
113+
114+
fmt.Fprintf(os.Stderr, " Hate: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Hate.Severity, *choice.ContentFilterResults.Hate.Filtered)
115+
fmt.Fprintf(os.Stderr, " SelfHarm: sev: %v, filtered: %v\n", *choice.ContentFilterResults.SelfHarm.Severity, *choice.ContentFilterResults.SelfHarm.Filtered)
116+
fmt.Fprintf(os.Stderr, " Sexual: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Sexual.Severity, *choice.ContentFilterResults.Sexual.Filtered)
117+
fmt.Fprintf(os.Stderr, " Violence: sev: %v, filtered: %v\n", *choice.ContentFilterResults.Violence.Severity, *choice.ContentFilterResults.Violence.Filtered)
118+
}
119+
120+
if choice.Message != nil && choice.Message.Content != nil {
121+
fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", *choice.Index, *choice.Message.Content)
122+
}
123+
124+
if choice.FinishReason != nil {
125+
// this choice's conversation is complete.
126+
fmt.Fprintf(os.Stderr, "Finish reason[%d]: %s\n", *choice.Index, *choice.FinishReason)
127+
}
128+
}
129+
130+
if gotReply {
131+
fmt.Fprintf(os.Stderr, "Got chat completions reply\n")
105132
}
106133

107134
}
135+
108136
```
109137

110138
> [!IMPORTANT]

0 commit comments

Comments
 (0)