@@ -16,9 +16,9 @@ import (
1616)
1717
1818type ChatApp struct {
19- configProvider * azureappconfiguration.AzureAppConfiguration
20- openAIClient openai.Client
21- aiConfig AIConfig
19+ configProvider * azureappconfiguration.AzureAppConfiguration
20+ openAIClient openai.Client
21+ aiConfig AIConfig
2222}
2323
2424type AIConfig struct {
@@ -37,7 +37,7 @@ type ChatCompletion struct {
3737type AzureOpenAI struct {
3838 Endpoint string
3939 APIVersion string
40- APIKey string
40+ APIKey string
4141}
4242
4343type Message struct {
@@ -57,15 +57,15 @@ func loadAzureAppConfiguration(ctx context.Context) (*azureappconfiguration.Azur
5757 }
5858
5959 authOptions := azureappconfiguration.AuthenticationOptions {
60- Endpoint : endpoint ,
60+ Endpoint : endpoint ,
6161 Credential : credential ,
6262 }
6363
6464 options := & azureappconfiguration.Options {
6565 Selectors : []azureappconfiguration.Selector {
6666 // Load all keys that start with "ChatApp:" and have no label
6767 {
68- KeyFilter : "ChatApp:*" ,
68+ KeyFilter : "ChatApp:*" ,
6969 },
7070 },
7171 TrimKeyPrefixes : []string {"ChatApp:" },
@@ -113,25 +113,26 @@ func (app *ChatApp) createAzureOpenAIClient() error {
113113 return nil
114114}
115115
116- func (app * ChatApp ) callAzureOpenAI (userMessage string ) (string , error ) {
117- messages := []openai.ChatCompletionMessageParamUnion {}
116+ func (app * ChatApp ) callAzureOpenAI (chatConversation []openai.ChatCompletionMessageParamUnion ) (string , error ) {
117+ var completionMessages []openai.ChatCompletionMessageParamUnion
118+
118119 for _ , msg := range app .aiConfig .ChatCompletion .Messages {
119120 switch msg .Role {
120121 case "system" :
121- messages = append (messages , openai .SystemMessage (msg .Content ))
122+ completionMessages = append (completionMessages , openai .SystemMessage (msg .Content ))
122123 case "user" :
123- messages = append (messages , openai .UserMessage (msg .Content ))
124+ completionMessages = append (completionMessages , openai .UserMessage (msg .Content ))
124125 case "assistant" :
125- messages = append (messages , openai .AssistantMessage (msg .Content ))
126+ completionMessages = append (completionMessages , openai .AssistantMessage (msg .Content ))
126127 }
127128 }
128129
129- // Add the user's input message
130- messages = append (messages , openai . UserMessage ( userMessage ) )
130+ // Add the chat conversation history
131+ completionMessages = append (completionMessages , chatConversation ... )
131132
132133 // Create chat completion parameters
133134 params := openai.ChatCompletionNewParams {
134- Messages : messages ,
135+ Messages : completionMessages ,
135136 Model : app .aiConfig .ChatCompletion .Model ,
136137 MaxTokens : openai .Int (app .aiConfig .ChatCompletion .MaxTokens ),
137138 Temperature : openai .Float (app .aiConfig .ChatCompletion .Temperature ),
@@ -152,10 +153,19 @@ func (app *ChatApp) callAzureOpenAI(userMessage string) (string, error) {
152153}
153154
154155func (app * ChatApp ) runInteractiveChat () {
155- fmt .Println ("Chat started! What's on your mind?" )
156+ // Initialize chat conversation
157+ var chatConversation []openai.ChatCompletionMessageParamUnion
158+ fmt .Println ("Chat started! What's on your mind?" )
156159 reader := bufio .NewReader (os .Stdin )
157160
158161 for {
162+ // Refresh the configuration from Azure App Configuration
163+ ctx := context .Background ()
164+ if err := app .configProvider .Refresh (ctx ); err != nil {
165+ log .Printf ("Error refreshing configuration: %v" , err )
166+ }
167+
168+ // Get user input
159169 fmt .Print ("You: " )
160170 userInput , err := reader .ReadString ('\n' )
161171 if err != nil {
@@ -169,22 +179,20 @@ func (app *ChatApp) runInteractiveChat() {
169179 break
170180 }
171181
172- // Refresh configuration
173- ctx := context .Background ()
174- if err := app .configProvider .Refresh (ctx ); err != nil {
175- log .Printf ("Error refreshing configuration: %v" , err )
176- }
182+ // Add user message to chat conversation
183+ chatConversation = append (chatConversation , openai .UserMessage (userInput ))
177184
178- // Get AI response
179- fmt .Print ("AI: " )
180- response , err := app .callAzureOpenAI (userInput )
185+ // Get AI response and add it to chat conversation
186+ response , err := app .callAzureOpenAI (chatConversation )
181187 if err != nil {
182188 log .Printf ("Error calling OpenAI: %v" , err )
183189 fmt .Println ("Sorry, I encountered an error. Please try again." )
184190 continue
185191 }
186192
187- fmt .Println (response )
193+ fmt .Printf ("AI: %s\n " , response )
194+ chatConversation = append (chatConversation , openai .AssistantMessage (response ))
195+
188196 fmt .Println ()
189197 }
190198}
@@ -196,11 +204,11 @@ func main() {
196204 log .Fatal ("Error loading Azure App Configuration:" , err )
197205 return
198206 }
199-
200- // Load AI configuration from Azure App Configuration
207+
208+ // Configure chat completion with AI configuration
201209 var aiConfig AIConfig
202210 if err := configProvider .Unmarshal (& aiConfig , & azureappconfiguration.ConstructionOptions {Separator : ":" }); err != nil {
203- log .Fatal ("Error loading AI configuration: " , err )
211+ log .Fatal ("Error unmarshaling AI configuration" , err )
204212 }
205213
206214 // Register a callback to refresh AI configuration on changes
@@ -212,7 +220,7 @@ func main() {
212220
213221 app := & ChatApp {
214222 configProvider : configProvider ,
215- aiConfig : aiConfig ,
223+ aiConfig : aiConfig ,
216224 }
217225
218226 // Initialize Azure OpenAI client
0 commit comments