@@ -40,71 +40,99 @@ import (
40
40
" os"
41
41
42
42
" github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
43
+ " github.com/Azure/azure-sdk-for-go/sdk/azcore"
43
44
" github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
44
45
)
45
46
46
47
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" )
50
50
51
51
// Ex: "https://<your-azure-openai-host>.openai.azure.com"
52
- azureOpenAIEndpoint := os.Getenv (" AZURE_OPENAI_ENDPOINT " )
52
+ azureOpenAIEndpoint := os.Getenv (" AOAI_CHAT_COMPLETIONS_ENDPOINT " )
53
53
54
54
if azureOpenAIKey == " " || modelDeploymentID == " " || azureOpenAIEndpoint == " " {
55
55
fmt.Fprintf (os.Stderr , " Skipping example, environment variables missing\n " )
56
56
return
57
57
}
58
58
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)
65
60
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
66
63
client , err := azopenai.NewClientWithKeyCredential (azureOpenAIEndpoint, keyCredential, nil )
67
64
68
65
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
71
69
}
72
70
73
71
// This is a conversation in progress.
74
72
// NOTE: all messages, regardless of role, count against token usage for this API.
75
- messages := []azopenai.ChatMessage {
73
+ messages := []azopenai.ChatRequestMessageClassification {
76
74
// 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 ." )},
78
76
79
77
// 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 ?" )},
81
79
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? " )},
84
82
85
83
// 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 ?" )},
87
85
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
89
87
}
90
88
89
+ gotReply := false
90
+
91
91
resp , err := client.GetChatCompletions (context.TODO (), azopenai.ChatCompletionsOptions {
92
92
// This is a conversation in progress.
93
93
// NOTE: all messages count against token usage for this API.
94
- Messages: messages,
95
- Deployment: modelDeploymentID,
94
+ Messages: messages,
95
+ DeploymentName: & modelDeploymentID,
96
96
}, nil )
97
97
98
98
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
101
102
}
102
103
103
104
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 " )
105
132
}
106
133
107
134
}
135
+
108
136
```
109
137
110
138
> [ !IMPORTANT]
0 commit comments