@@ -37,12 +37,14 @@ public static async Task RunAsync()
3737 // Set FunctionCall to the name of a specific function to force the model to use that function.
3838 requestSettings . FunctionCall = "TimePlugin-Date" ;
3939 await CompleteChatWithFunctionsAsync ( "What day is today?" , chatHistory , chatCompletion , kernel , requestSettings ) ;
40- await StreamingCompleteChatWithFunctionsAsync ( "What day is today?" , chatHistory , chatCompletion , kernel , requestSettings ) ;
40+
41+ // Uncomment the samples and run them one at a time
42+ //await StreamingCompleteChatWithFunctionsAsync("What day is today?", chatHistory, chatCompletion, kernel, requestSettings);
4143
4244 // Set FunctionCall to auto to let the model choose the best function to use.
4345 requestSettings . FunctionCall = OpenAIRequestSettings . FunctionCallAuto ;
44- await CompleteChatWithFunctionsAsync ( "What computer tablets are available for under $200?" , chatHistory , chatCompletion , kernel , requestSettings ) ;
45- await StreamingCompleteChatWithFunctionsAsync ( "What computer tablets are available for under $200?" , chatHistory , chatCompletion , kernel , requestSettings ) ;
46+ // await CompleteChatWithFunctionsAsync("What computer tablets are available for under $200?", chatHistory, chatCompletion, kernel, requestSettings);
47+ // await StreamingCompleteChatWithFunctionsAsync("What computer tablets are available for under $200?", chatHistory, chatCompletion, kernel, requestSettings);
4648 }
4749
4850 private static async Task < IKernel > InitializeKernelAsync ( )
@@ -66,56 +68,43 @@ private static async Task CompleteChatWithFunctionsAsync(string ask, ChatHistory
6668 Console . WriteLine ( $ "User message: { ask } ") ;
6769 chatHistory . AddUserMessage ( ask ) ;
6870
69- // Send request
71+ // Send request and add response to chat history
7072 var chatResult = ( await chatCompletion . GetChatCompletionsAsync ( chatHistory , requestSettings ) ) [ 0 ] ;
73+ chatHistory . AddAssistantMessage ( chatResult ) ;
7174
72- // Check for message response
73- var chatMessage = await chatResult . GetChatMessageAsync ( ) ;
74- if ( ! string . IsNullOrEmpty ( chatMessage . Content ) )
75- {
76- Console . WriteLine ( chatMessage . Content ) ;
77-
78- // Add the response to chat history
79- chatHistory . AddAssistantMessage ( chatMessage . Content ) ;
80- }
75+ await PrintChatResultAsync ( chatResult ) ;
8176
8277 // Check for function response
8378 OpenAIFunctionResponse ? functionResponse = chatResult . GetOpenAIFunctionResponse ( ) ;
8479 if ( functionResponse is not null )
8580 {
86- // Print function response details
87- Console . WriteLine ( "Function name: " + functionResponse . FunctionName ) ;
88- Console . WriteLine ( "Plugin name: " + functionResponse . PluginName ) ;
89- Console . WriteLine ( "Arguments: " ) ;
90- foreach ( var parameter in functionResponse . Parameters )
91- {
92- Console . WriteLine ( $ "- { parameter . Key } : { parameter . Value } ") ;
93- }
94-
9581 // If the function returned by OpenAI is an SKFunction registered with the kernel,
9682 // you can invoke it using the following code.
9783 if ( kernel . Functions . TryGetFunctionAndContext ( functionResponse , out ISKFunction ? func , out ContextVariables ? context ) )
9884 {
99- var kernelResult = await kernel . RunAsync ( func , context ) ;
85+ var result = ( await kernel . RunAsync ( func , context ) ) . GetValue < object > ( ) ;
10086
101- var result = kernelResult . GetValue < object > ( ) ;
102-
103- string ? resultMessage = null ;
87+ string ? resultContent = null ;
10488 if ( result is RestApiOperationResponse apiResponse )
10589 {
106- resultMessage = apiResponse . Content ? . ToString ( ) ;
90+ resultContent = apiResponse . Content ? . ToString ( ) ;
10791 }
10892 else if ( result is string str )
10993 {
110- resultMessage = str ;
94+ resultContent = str ;
11195 }
11296
113- if ( ! string . IsNullOrEmpty ( resultMessage ) )
97+ if ( ! string . IsNullOrEmpty ( resultContent ) )
11498 {
115- Console . WriteLine ( resultMessage ) ;
116-
11799 // Add the function result to chat history
118- chatHistory . AddAssistantMessage ( resultMessage ) ;
100+ chatHistory . AddFunctionMessage ( resultContent , functionResponse . FullyQualifiedName ) ;
101+
102+ // Get another completion
103+ requestSettings . FunctionCall = OpenAIRequestSettings . FunctionCallNone ;
104+ chatResult = ( await chatCompletion . GetChatCompletionsAsync ( chatHistory , requestSettings ) ) [ 0 ] ;
105+ chatHistory . AddAssistantMessage ( chatResult ) ;
106+
107+ await PrintChatResultAsync ( chatResult ) ;
119108 }
120109 }
121110 else
@@ -125,6 +114,30 @@ private static async Task CompleteChatWithFunctionsAsync(string ask, ChatHistory
125114 }
126115 }
127116
117+ private static async Task PrintChatResultAsync ( IChatResult chatResult )
118+ {
119+ // Check for message response
120+ var chatMessage = await chatResult . GetChatMessageAsync ( ) ;
121+ if ( ! string . IsNullOrEmpty ( chatMessage . Content ) )
122+ {
123+ Console . WriteLine ( $ "Assistant response: { chatMessage . Content } ") ;
124+ }
125+
126+ // Check for function response
127+ OpenAIFunctionResponse ? functionResponse = chatResult . GetOpenAIFunctionResponse ( ) ;
128+ if ( functionResponse is not null )
129+ {
130+ // Print function response details
131+ Console . WriteLine ( "Function name: " + functionResponse . FunctionName ) ;
132+ Console . WriteLine ( "Plugin name: " + functionResponse . PluginName ) ;
133+ Console . WriteLine ( "Arguments: " ) ;
134+ foreach ( var parameter in functionResponse . Parameters )
135+ {
136+ Console . WriteLine ( $ "- { parameter . Key } : { parameter . Value } ") ;
137+ }
138+ }
139+ }
140+
128141 private static async Task StreamingCompleteChatWithFunctionsAsync ( string ask , ChatHistory chatHistory , IChatCompletion chatCompletion , IKernel kernel , OpenAIRequestSettings requestSettings )
129142 {
130143 Console . WriteLine ( $ "User message: { ask } ") ;
0 commit comments