-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
168 lines (135 loc) · 5.16 KB
/
Program.cs
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
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using System.ComponentModel;
// Disable warnings related to Semantic Kernel
#pragma warning disable SKEXP0003
#pragma warning disable SKEXP0010
#pragma warning disable SKEXP0011
#pragma warning disable SKEXP0021
#pragma warning disable SKEXP0052
#pragma warning disable SKEXP0060
#pragma warning disable SKEXP0061
// Azure OpenAI Variables
var apiKey = "";
var deploymentChatName = "";
var deploymentEmbeddingName = "";
var endpoint = "";
// Azure AI Search Variables
string searchApiKey = "";
string searchEndpoint = "";
var builder = Kernel.CreateBuilder();
builder
.AddAzureOpenAIChatCompletion(
deploymentChatName,
endpoint,
apiKey);
var kernel = builder.Build();
var embeddingConfig = new AzureOpenAIConfig
{
APIKey = apiKey,
Deployment = deploymentEmbeddingName,
Endpoint = endpoint,
APIType = AzureOpenAIConfig.APITypes.EmbeddingGeneration,
Auth = AzureOpenAIConfig.AuthTypes.APIKey
};
var chatConfig = new AzureOpenAIConfig
{
APIKey = apiKey,
Deployment = deploymentChatName,
Endpoint = endpoint,
APIType = AzureOpenAIConfig.APITypes.ChatCompletion,
Auth = AzureOpenAIConfig.AuthTypes.APIKey
};
var kernelMemory = new KernelMemoryBuilder()
.WithAzureOpenAITextGeneration(chatConfig)
.WithAzureOpenAITextEmbeddingGeneration(embeddingConfig)
.WithAzureAISearchMemoryDb(searchEndpoint, searchApiKey)
.Build<MemoryServerless>();
// Import a document to the memory, e.g. Expense Policy PDF document
await kernelMemory.ImportDocumentAsync("CHANGE_ME: FILE_LOCATION.pdf", documentId: "doc001");
// Import the memory plugin
var plugin = new MemoryPlugin(kernelMemory, waitForIngestionToComplete: true);
kernel.ImportPluginFromObject(plugin, "memory");
// Import the VCard plugin
kernel.ImportPluginFromType<VCardPlugin>();
ChatHistory history = [];
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
Console.WriteLine("Hello! This is a chat console.");
Console.WriteLine(string.Empty);
var system = $@"
CHANGE_ME: WRITE YOUR ASSISTANT SYSTEM PROMPT HERE
";
history.AddMessage(AuthorRole.System, system);
// Enable auto function calling
OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
};
while (true)
{
Console.WriteLine(string.Empty);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("Input > ");
var message = Console.ReadLine();
Console.ResetColor();
// Prompt the message to the kernel memory
var prompt = $@"
Question to Kernel Memory: {message}
Kernel Memory Answer: {{memory.ask}}
";
history.AddMessage(AuthorRole.User, prompt);
var result = await chatCompletionService.GetChatMessageContentAsync(history, openAIPromptExecutionSettings, kernel);
Console.WriteLine(string.Empty);
Console.WriteLine(string.Empty);
Console.Write("Assistant > ");
Console.WriteLine(result.Content);
history.AddMessage(AuthorRole.Assistant, result.Content);
}
public class VCardPlugin
{
const string FunctionModelDescription = @"
- This function creates a request for a Virtual Card.
- You must always ask for the user approval before creating a virtual card.
- You must always ask for the user policy details before creating a virtual card.
- You must always ask for the number of days of travel before creating a virtual card.
- You must always request the ammount to be created.
- If the ammount is bigger than the allowed from the policy deny the request.
";
[KernelFunction, Description(FunctionModelDescription)]
public string CreateCard(int ammount, int days)
{
Random random = new Random();
string cardNumber = "5" + random.Next(1, 6).ToString() + string.Concat(Enumerable.Range(0, 14).Select(n => random.Next(0, 10).ToString()));
// Calculate the Luhn check digit
int sum = 0;
for (int i = 0; i < 15; i++)
{
int digit = int.Parse(cardNumber[i].ToString());
if (i % 2 == 0)
{
digit *= 2;
if (digit > 9)
{
digit -= 9;
}
}
sum += digit;
}
int checkDigit = (10 - (sum % 10)) % 10;
cardNumber += checkDigit.ToString();
// Generate a random expiry date in the future
int expiryMonth = random.Next(1, 13); // Month is between 1 and 12
int expiryYear = DateTime.Now.Year + random.Next(1, 6); // Year is between now and 5 years from now
// Generate a random 3-digit CVC
int cvc = random.Next(100, 1000); // CVC is a 3-digit number
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine($"[Card created with ammount {ammount} for {days} days]");
Console.WriteLine($"Card Number: {cardNumber}");
Console.WriteLine($"Expiry Date: {expiryMonth:D2}/{expiryYear}");
Console.WriteLine($"CVC: {cvc}");
Console.ResetColor();
return "Virtual Card created successfully.";
}
}