-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
242 lines (210 loc) · 9.59 KB
/
Program.cs
File metadata and controls
242 lines (210 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using FirstPlugin;
using Microsoft.SemanticKernel.ChatCompletion;
using OpenAI.Assistants;
using OpenAI.Chat;
using Microsoft.SemanticKernel.Plugins.Web.Brave;
using Microsoft.SemanticKernel.Plugins.Web;
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_ApiKey");
var endpoint = Environment.GetEnvironmentVariable("OPENAI_API_Endpoint");
var apiKeyBrave = Environment.GetEnvironmentVariable("BRAVE_API_KEY");
// Create and configure the kernel
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(deploymentName: "gpt-4o-mini",
endpoint: endpoint!,
apiKey: apiKey!);
var kernel = builder.Build();
var braveConnector = new BraveConnector(apiKeyBrave!);
var bravePlugin = new WebSearchEnginePlugin(braveConnector);
ChatCompletionAgent agent = CreateAgentWithPlugin(
plugins: [
KernelPluginFactory.CreateFromType<LightsPlugin>(),
KernelPluginFactory.CreateFromType<SoftwareBuilderPlugin>(),
KernelPluginFactory.CreateFromType<GoogleKeepPlugin>(),
KernelPluginFactory.CreateFromType<GoogleFitnessPlugin>(),
KernelPluginFactory.CreateFromObject(bravePlugin),
],
instructions: "Respond to user questions as an assistant",
name: "Main-Assistant");
AgentThread thread = new ChatHistoryAgentThread();
// Respond to user input, invoking functions where appropriate.
while (true)
{
await InvokeAgentAsync(agent, thread);
}
bool FunctionCallsPresent(Microsoft.SemanticKernel.ChatMessageContent message)
{
// Get function calls from the chat message content and quit the chat loop if no function calls are found.
IEnumerable<FunctionCallContent> functionCalls = FunctionCallContent.GetFunctionCalls(message);
if (functionCalls.Any())
{
return true;
}
return false;
}
static IEnumerable<FunctionResultContent> GetFunctionCalls(Microsoft.SemanticKernel.ChatMessageContent messageContent)
{
return messageContent.Items.OfType<FunctionResultContent>();
}
ChatCompletionAgent CreateAgentWithPlugin(
IEnumerable<KernelPlugin> plugins,
string? instructions = null,
string? name = null)
{
ChatCompletionAgent agent =
new()
{
Instructions = instructions,
Name = name,
Kernel = kernel,
Arguments = new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }),
};
// Initialize plugin and add to the agent's Kernel (same as direct Kernel usage).
agent.Kernel.Plugins.AddRange(plugins);
return agent;
}
async Task InvokeAgentAsync(ChatCompletionAgent agent, AgentThread thread)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"User input:");
var input = Console.ReadLine();
Console.ResetColor();
// Check if user wants to clear context
if (input?.ToLower() == "clear-context")
{
thread = new ChatHistoryAgentThread();
Console.WriteLine("Conversation context has been cleared.");
}
Microsoft.SemanticKernel.ChatMessageContent message = new(AuthorRole.User, input);
try
{
await foreach (var response in agent.InvokeAsync(message, thread))
{
WriteFunctionCalls(response);
WriteAgentChatMessage(response);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Error invoking chat completion: {ex.Message}");
Console.ResetColor();
return;
}
}
void WriteFunctionCalls(AgentResponseItem<Microsoft.SemanticKernel.ChatMessageContent> response)
{
var functionCallResults = GetFunctionCalls(response.Message);
foreach (var functionResult in functionCallResults)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Function call: {functionResult?.PluginName} - {functionResult?.FunctionName}");
// Display function call details
Console.WriteLine($" Call ID: {functionResult?.CallId}");
Console.WriteLine($" Plugin Name: {functionResult?.PluginName}");
Console.WriteLine($" Function Name: {functionResult?.FunctionName}");
Console.ResetColor();
// Check if the function call has a result and display it with more context
if (functionResult?.Result != null)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($" Function result: {functionResult.Result}");
// Try to extract more detailed information about the result
try
{
var resultType = functionResult.Result.GetType().Name;
Console.WriteLine($" Result Type: {resultType}");
// For complex objects like collections, provide more detail
if (functionResult.Result is System.Collections.IEnumerable collection &&
!(functionResult.Result is string))
{
int itemCount = 0;
foreach (var item in collection)
{
itemCount++;
if (itemCount <= 3) // Limit to first 3 items to avoid excessive output
{
Console.WriteLine($" Item {itemCount}: {item}");
}
}
if (itemCount > 3)
{
Console.WriteLine($" ... and {itemCount - 3} more items");
}
}
}
catch (Exception ex)
{
Console.WriteLine($" (Error parsing result details: {ex.Message})");
}
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(" No result returned");
Console.ResetColor();
}
Console.WriteLine(); // Add spacing between different function calls
}
if (!functionCallResults.Any())
{
Console.WriteLine("No function calls found.");
}
}
void WriteAgentChatMessage(Microsoft.SemanticKernel.ChatMessageContent message)
{
Console.ForegroundColor = ConsoleColor.Green;
// Include ChatMessageContent.AuthorName in output, if present.
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
string authorExpression = message.Role == AuthorRole.User ? string.Empty : $" - {message.AuthorName ?? "*"}";
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
string contentExpression = string.IsNullOrWhiteSpace(message.Content) ? string.Empty : message.Content;
Console.WriteLine($"\n# {message.Role}{authorExpression}: {contentExpression}");
// Provide visibility for inner content (that isn't TextContent).
foreach (KernelContent item in message.Items)
{
#pragma warning disable SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
if (item is AnnotationContent annotation)
{
Console.WriteLine($" [{item.GetType().Name}] {annotation.Quote}: File #{annotation.FileId}");
}
else if (item is FileReferenceContent fileReference)
{
Console.WriteLine($" [{item.GetType().Name}] File #{fileReference.FileId}");
}
else if (item is ImageContent image)
{
Console.WriteLine($" [{item.GetType().Name}] {image.Uri?.ToString() ?? image.DataUri ?? $"{image.Data?.Length} bytes"}");
}
else if (item is FunctionCallContent functionCall)
{
Console.WriteLine($" Function call content: [{item.GetType().Name}] {functionCall.Id}");
}
else if (item is FunctionResultContent functionResult)
{
Console.WriteLine($" Function result content: [{item.GetType().Name}] {functionResult.CallId} - {functionResult.Result?.ToString() ?? "*"}");
}
#pragma warning restore SKEXP0110 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
if (message.Metadata?.TryGetValue("Usage", out object? usage) ?? false)
{
#pragma warning disable OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
if (usage is RunStepTokenUsage assistantUsage)
{
WriteUsage(assistantUsage.TotalTokenCount, assistantUsage.InputTokenCount, assistantUsage.OutputTokenCount);
}
else if (usage is ChatTokenUsage chatUsage)
{
WriteUsage(chatUsage.TotalTokenCount, chatUsage.InputTokenCount, chatUsage.OutputTokenCount);
}
#pragma warning restore OPENAI001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
Console.ResetColor();
void WriteUsage(long totalTokens, long inputTokens, long outputTokens)
{
Console.WriteLine($" [Usage] Tokens: {totalTokens}, Input: {inputTokens}, Output: {outputTokens}");
}
}