-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (100 loc) · 3.99 KB
/
Program.cs
File metadata and controls
116 lines (100 loc) · 3.99 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
using System.ClientModel;
using System.Text;
using CardMaker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents.OpenAI;
using OpenAI.Files;
using OpenAI.VectorStores;
// Set the console encoding to UTF-8 to support Unicode characters
Console.OutputEncoding = Encoding.UTF8;
// Load embedded resources
string cardMaker, toolsList, yamlPersona;
cardMaker = ContextUtils.EmbeddedResource("Agent_Prompt_v2.md");
toolsList = ContextUtils.EmbeddedResource("Tools.txt");
yamlPersona = ContextUtils.EmbeddedResource("Persona_Prompt.yaml");
// Load configuration from environment variables or user secrets.
Settings settings = new();
// prerequisites for the OpenAI Assistant
OpenAIClientProvider clientProvider =
OpenAIClientProvider.ForAzureOpenAI(
new ApiKeyCredential(settings.AzureOpenAI.ApiKey),
new Uri(settings.AzureOpenAI.Endpoint));
Console.WriteLine("Creating store...");
VectorStoreClient storeClient = clientProvider.Client.GetVectorStoreClient();
CreateVectorStoreOperation operation = await storeClient.CreateVectorStoreAsync(waitUntilCompleted: true);
string storeId = operation.VectorStoreId;
/* uncomment if you want to upload a file to the vector store
Dictionary<string, OpenAIFile> fileReferences = [];
OpenAIFileClient fileClient = clientProvider.Client.GetOpenAIFileClient();
Stream stream = ContextUtils.EmbeddedResourceStream("platform-engineering.pdf");
OpenAIFile fileInfo = await fileClient.UploadFileAsync(stream, "platform-engineering.pdf", FileUploadPurpose.Assistants);
await storeClient.AddFileToVectorStoreAsync(storeId, fileInfo.Id, waitUntilCompleted: true);
fileReferences.Add(fileInfo.Id, fileInfo);
*/
var builder = Kernel.CreateBuilder();
//builder.Services.AddSingleton(ContextUtils.getLoggerFactory());
builder.AddAzureOpenAIChatCompletion(
deploymentName: "gpt-4o-2",
apiKey: settings.AzureOpenAI.ApiKey,
endpoint: settings.AzureOpenAI.Endpoint,
modelId: "gpt-4o" // Optional name of the underlying model if the deployment name doesn't match the model name
);
var kernel = builder.Build();
Tools tools = new();
kernel.Plugins.AddFromObject(tools);
var function = kernel.CreateFunctionFromPromptYaml(yamlPersona);
kernel.Plugins.AddFromFunctions("quote_tool", [function]);
OpenAIAssistantAgent agent =
await OpenAIAssistantAgent.CreateAsync(
clientProvider,
new OpenAIAssistantDefinition(settings.AzureOpenAI.ChatModelDeployment)
{
Name = "Card Maker",
Instructions = cardMaker,
Temperature = 0.4f,
TopP = 0.9f,
EnableFileSearch = true,
VectorStoreId = storeId
},
kernel);
string threadId = await agent.CreateThreadAsync();
var utils = new ContextUtils(agent, threadId);
if (!Console.IsOutputRedirected)
{
Console.Clear();
}
try
{
// provide additional grounding before starting the conversation
await utils.backchannel("here are the tools we will use exclusively for this mapping exercise");
await utils.backchannel(toolsList);
await utils.AddChatMessageAsync("Hello! What are you good at and why are we here?");
string input;
while ((input = Console.ReadLine()) is not null)
{
if (string.IsNullOrWhiteSpace(input))
{
Console.Write(">");
continue;
}
if (input.Trim().Equals("EXIT", StringComparison.OrdinalIgnoreCase))
{
break;
}
input += utils.formatPrompt();
await utils.AddChatMessageAsync(input);
}
}
finally
{
Console.WriteLine();
Console.WriteLine("Cleaning-up...");
await Task.WhenAll(
[
agent.DeleteThreadAsync(threadId),
agent.DeleteAsync(),
storeClient.DeleteVectorStoreAsync(storeId),
//..fileReferences.Select(fileReference => fileClient.DeleteFileAsync(fileReference.Key))
]);
}