1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
using System . ComponentModel ;
3
3
using Microsoft . SemanticKernel ;
4
+ using Microsoft . SemanticKernel . Agents ;
4
5
using Microsoft . SemanticKernel . Agents . OpenAI ;
5
6
using Microsoft . SemanticKernel . ChatCompletion ;
6
7
using OpenAI . Assistants ;
@@ -27,15 +28,15 @@ await this.AssistantClient.CreateAssistantAsync(
27
28
OpenAIAssistantAgent agent = new ( assistant , this . AssistantClient ) ;
28
29
29
30
// Create a thread for the agent conversation.
30
- string threadId = await this . AssistantClient . CreateThreadAsync ( metadata : SampleMetadata ) ;
31
+ OpenAIAssistantAgentThread agentThread = new ( this . AssistantClient , metadata : SampleMetadata ) ;
31
32
32
33
// Respond to user input
33
- await InvokeAgentAsync ( agent , threadId , "Fortune favors the bold." ) ;
34
- await InvokeAgentAsync ( agent , threadId , "I came, I saw, I conquered." ) ;
35
- await InvokeAgentAsync ( agent , threadId , "Practice makes perfect." ) ;
34
+ await InvokeAgentAsync ( agent , agentThread , "Fortune favors the bold." ) ;
35
+ await InvokeAgentAsync ( agent , agentThread , "I came, I saw, I conquered." ) ;
36
+ await InvokeAgentAsync ( agent , agentThread , "Practice makes perfect." ) ;
36
37
37
38
// Output the entire chat history
38
- await DisplayChatHistoryAsync ( agent , threadId ) ;
39
+ await DisplayChatHistoryAsync ( agentThread ) ;
39
40
}
40
41
41
42
[ Fact ]
@@ -54,14 +55,14 @@ await this.AssistantClient.CreateAssistantAsync(
54
55
OpenAIAssistantAgent agent = new ( assistant , this . AssistantClient , [ plugin ] ) ;
55
56
56
57
// Create a thread for the agent conversation.
57
- string threadId = await this . AssistantClient . CreateThreadAsync ( metadata : SampleMetadata ) ;
58
+ OpenAIAssistantAgentThread agentThread = new ( this . AssistantClient , metadata : SampleMetadata ) ;
58
59
59
60
// Respond to user input
60
- await InvokeAgentAsync ( agent , threadId , "What is the special soup and its price?" ) ;
61
- await InvokeAgentAsync ( agent , threadId , "What is the special drink and its price?" ) ;
61
+ await InvokeAgentAsync ( agent , agentThread , "What is the special soup and its price?" ) ;
62
+ await InvokeAgentAsync ( agent , agentThread , "What is the special drink and its price?" ) ;
62
63
63
64
// Output the entire chat history
64
- await DisplayChatHistoryAsync ( agent , threadId ) ;
65
+ await DisplayChatHistoryAsync ( agentThread ) ;
65
66
}
66
67
67
68
[ Fact ]
@@ -80,28 +81,33 @@ await this.AssistantClient.CreateAssistantAsync(
80
81
OpenAIAssistantAgent agent = new ( assistant , this . AssistantClient ) ;
81
82
82
83
// Create a thread for the agent conversation.
83
- string threadId = await this . AssistantClient . CreateThreadAsync ( metadata : SampleMetadata ) ;
84
+ OpenAIAssistantAgentThread agentThread = new ( this . AssistantClient , metadata : SampleMetadata ) ;
84
85
85
86
// Respond to user input
86
- await InvokeAgentAsync ( agent , threadId , "Is 191 a prime number?" ) ;
87
- await InvokeAgentAsync ( agent , threadId , "Determine the values in the Fibonacci sequence that that are less then the value of 101" ) ;
87
+ await InvokeAgentAsync ( agent , agentThread , "Is 191 a prime number?" ) ;
88
+ await InvokeAgentAsync ( agent , agentThread , "Determine the values in the Fibonacci sequence that that are less then the value of 101" ) ;
88
89
89
90
// Output the entire chat history
90
- await DisplayChatHistoryAsync ( agent , threadId ) ;
91
+ await DisplayChatHistoryAsync ( agentThread ) ;
91
92
}
92
93
93
94
// Local function to invoke agent and display the conversation messages.
94
- private async Task InvokeAgentAsync ( OpenAIAssistantAgent agent , string threadId , string input )
95
+ private async Task InvokeAgentAsync ( OpenAIAssistantAgent agent , AgentThread agentThread , string input )
95
96
{
96
97
ChatMessageContent message = new ( AuthorRole . User , input ) ;
97
- await agent . AddChatMessageAsync ( threadId , message ) ;
98
98
this . WriteAgentChatMessage ( message ) ;
99
99
100
+ // For this sample, also capture fully formed messages so we can display them later.
100
101
ChatHistory history = [ ] ;
102
+ Task OnNewMessage ( ChatMessageContent message )
103
+ {
104
+ history . Add ( message ) ;
105
+ return Task . CompletedTask ;
106
+ }
101
107
102
108
bool isFirst = false ;
103
109
bool isCode = false ;
104
- await foreach ( StreamingChatMessageContent response in agent . InvokeStreamingAsync ( threadId , messages : history ) )
110
+ await foreach ( StreamingChatMessageContent response in agent . InvokeStreamingAsync ( message , agentThread , new ( ) { OnNewMessage = OnNewMessage } ) )
105
111
{
106
112
if ( string . IsNullOrEmpty ( response . Content ) )
107
113
{
@@ -136,13 +142,13 @@ private async Task InvokeAgentAsync(OpenAIAssistantAgent agent, string threadId,
136
142
}
137
143
}
138
144
139
- private async Task DisplayChatHistoryAsync ( OpenAIAssistantAgent agent , string threadId )
145
+ private async Task DisplayChatHistoryAsync ( OpenAIAssistantAgentThread agentThread )
140
146
{
141
147
Console . WriteLine ( "================================" ) ;
142
148
Console . WriteLine ( "CHAT HISTORY" ) ;
143
149
Console . WriteLine ( "================================" ) ;
144
150
145
- ChatMessageContent [ ] messages = await agent . GetThreadMessagesAsync ( threadId ) . ToArrayAsync ( ) ;
151
+ ChatMessageContent [ ] messages = await agentThread . GetMessagesAsync ( ) . ToArrayAsync ( ) ;
146
152
for ( int index = messages . Length - 1 ; index >= 0 ; -- index )
147
153
{
148
154
this . WriteAgentChatMessage ( messages [ index ] ) ;
0 commit comments