forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
316 lines (251 loc) · 14.3 KB
/
Program.cs
File metadata and controls
316 lines (251 loc) · 14.3 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.AI.Llama;
using Microsoft.KernelMemory.AI.Tokenizers;
using Microsoft.KernelMemory.DataFormats.Image.AzureAIDocIntel;
/* Use MemoryServerlessClient to run the default import pipeline
* in the same process, without distributed queues.
*
* The pipeline might use settings in appsettings.json, but uses
* 'InProcessPipelineOrchestrator' explicitly.
*
* Note: no web service required, each file is processed in this process. */
var openAIConfig = new OpenAIConfig();
var azureOpenAITextConfig = new AzureOpenAIConfig();
var azureOpenAIEmbeddingConfig = new AzureOpenAIConfig();
var llamaConfig = new LlamaSharpConfig();
var searchClientConfig = new SearchClientConfig();
var azDocIntelConfig = new AzureAIDocIntelConfig();
new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.Development.json", optional: true)
.Build()
.BindSection("KernelMemory:Services:OpenAI", openAIConfig)
.BindSection("KernelMemory:Services:LlamaSharp", llamaConfig)
.BindSection("KernelMemory:Services:AzureOpenAIText", azureOpenAITextConfig)
.BindSection("KernelMemory:Services:AzureOpenAIEmbedding", azureOpenAIEmbeddingConfig)
.BindSection("KernelMemory:Services:AzureAIDocIntel", azDocIntelConfig)
.BindSection("KernelMemory:Retrieval:SearchClient", searchClientConfig);
var memory = new KernelMemoryBuilder()
// .WithOpenAIDefaults(Env.Var("OPENAI_API_KEY"))
// .WithOpenAI(openAICfg)
// .WithLlamaTextGeneration(llamaConfig)
.WithAzureOpenAITextGeneration(azureOpenAITextConfig, new DefaultGPTTokenizer())
.WithAzureOpenAITextEmbeddingGeneration(azureOpenAIEmbeddingConfig, new DefaultGPTTokenizer())
// .WithAzureAIDocIntel(azDocIntelConfig) // => use Azure AI Document Intelligence OCR
// .WithAzureBlobsStorage(new AzureBlobsConfig {...}) // => use Azure Blobs
// .WithAzureAISearch(Env.Var("AZSEARCH_ENDPOINT"), Env.Var("AZSEARCH_API_KEY")) // => use Azure AI Search
// .WithQdrant("http://127.0.0.1:6333") // => use Qdrant to store memories
.Build<MemoryServerless>();
// Use these boolean to enable/disable parts of the examples below
bool ingestion = true;
bool useImages = false; // Deploy Azure AI Document Intelligence to use this
bool retrieval = true;
bool purge = true;
// =======================
// === INGESTION =========
// =======================
var toDelete = new List<string>();
if (ingestion)
{
Console.WriteLine("\n====================================\n");
// Uploading some text, without using files. Hold a copy of the ID to delete it later.
Console.WriteLine("Uploading text about E=mc^2");
var docId = await memory.ImportTextAsync("In physics, mass–energy equivalence is the relationship between mass and energy " +
"in a system's rest frame, where the two quantities differ only by a multiplicative " +
"constant and the units of measurement. The principle is described by the physicist " +
"Albert Einstein's formula: E = m*c^2");
toDelete.Add(docId);
// Simple file upload, with document ID
toDelete.Add("doc001");
Console.WriteLine("Uploading article file about Carbon");
await memory.ImportDocumentAsync("file1-Wikipedia-Carbon.txt", documentId: "doc001");
// Extract memory from images (if OCR enabled)
if (useImages)
{
toDelete.Add("img001");
Console.WriteLine("Uploading Image file with a news about a conference sponsored by Microsoft");
await memory.ImportDocumentAsync(new Document("img001").AddFiles(new[] { "file6-ANWC-image.jpg" }));
}
// Uploading multiple files and adding a user tag, checking if the document already exists
toDelete.Add("doc002");
if (!await memory.IsDocumentReadyAsync(documentId: "doc002"))
{
Console.WriteLine("Uploading a text file, a Word doc, and a PDF about Semantic Kernel");
await memory.ImportDocumentAsync(new Document("doc002")
.AddFiles(new[] { "file2-Wikipedia-Moon.txt", "file3-lorem-ipsum.docx", "file4-SK-Readme.pdf" })
.AddTag("user", "Blake"));
}
else
{
Console.WriteLine("doc002 already uploaded.");
}
// Categorizing files with several tags
toDelete.Add("doc003");
if (!await memory.IsDocumentReadyAsync(documentId: "doc003"))
{
Console.WriteLine("Uploading a PDF with a news about NASA and Orion");
await memory.ImportDocumentAsync(new Document("doc003")
.AddFile("file5-NASA-news.pdf")
.AddTag("user", "Taylor")
.AddTag("collection", "meetings")
.AddTag("collection", "NASA")
.AddTag("collection", "space")
.AddTag("type", "news"));
}
else
{
Console.WriteLine("doc003 already uploaded.");
}
// Downloading web pages
toDelete.Add("webPage1");
if (!await memory.IsDocumentReadyAsync("webPage1"))
{
Console.WriteLine("Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md");
await memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md", documentId: "webPage1");
}
else
{
Console.WriteLine("webPage1 already uploaded.");
}
// Custom pipelines, e.g. excluding summarization
toDelete.Add("webPage2");
if (!await memory.IsDocumentReadyAsync("webPage2"))
{
Console.WriteLine("Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/SECURITY_FILTERS.md");
await memory.ImportWebPageAsync("https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/SECURITY_FILTERS.md",
documentId: "webPage2",
steps: Constants.PipelineWithoutSummary);
}
else
{
Console.WriteLine("webPage2 already uploaded.");
}
}
// =======================
// === RETRIEVAL =========
// =======================
if (retrieval)
{
Console.WriteLine("\n====================================\n");
// Question without filters
var question = "What's E = m*c^2?";
Console.WriteLine($"Question: {question}");
var answer = await memory.AskAsync(question, minRelevance: 0.76);
Console.WriteLine($"\nAnswer: {answer.Result}");
Console.WriteLine("\n====================================\n");
// Another question without filters
question = "What's Semantic Kernel?";
Console.WriteLine($"Question: {question}");
answer = await memory.AskAsync(question, minRelevance: 0.76);
Console.WriteLine($"\nAnswer: {answer.Result}\n\n Sources:\n");
// Show sources / citations
foreach (var x in answer.RelevantSources)
{
Console.WriteLine(x.SourceUrl != null
? $" - {x.SourceUrl} [{x.Partitions.First().LastUpdate:D}]"
: $" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]");
}
if (useImages)
{
Console.WriteLine("\n====================================\n");
question = "Which conference is Microsoft sponsoring?";
Console.WriteLine($"Question: {question}");
answer = await memory.AskAsync(question, minRelevance: 0.76);
Console.WriteLine($"\nAnswer: {answer.Result}\n\n Sources:\n");
// Show sources / citations
foreach (var x in answer.RelevantSources)
{
Console.WriteLine(x.SourceUrl != null
? $" - {x.SourceUrl} [{x.Partitions.First().LastUpdate:D}]"
: $" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]");
}
}
Console.WriteLine("\n====================================\n");
// Filter question by "user" tag
question = "Any news from NASA about Orion?";
Console.WriteLine($"Question: {question}");
// Blake doesn't know
answer = await memory.AskAsync(question, filter: MemoryFilters.ByTag("user", "Blake"));
Console.WriteLine($"\nBlake Answer (none expected): {answer.Result}");
// Taylor knows
answer = await memory.AskAsync(question, filter: MemoryFilters.ByTag("user", "Taylor"));
Console.WriteLine($"\nTaylor Answer: {answer.Result}\n Sources:\n");
// Show sources / citations
foreach (var x in answer.RelevantSources)
{
Console.WriteLine(x.SourceUrl != null
? $" - {x.SourceUrl} [{x.Partitions.First().LastUpdate:D}]"
: $" - {x.SourceName} - {x.Link} [{x.Partitions.First().LastUpdate:D}]");
}
Console.WriteLine("\n====================================\n");
// Filter question by "type" tag, there are news but no articles
question = "What is Orion?";
Console.WriteLine($"Question: {question}");
answer = await memory.AskAsync(question, filter: MemoryFilters.ByTag("type", "article"));
Console.WriteLine($"\nArticles (none expected): {answer.Result}");
answer = await memory.AskAsync(question, filter: MemoryFilters.ByTag("type", "news"));
Console.WriteLine($"\nNews: {answer.Result}");
}
// =======================
// === PURGE =============
// =======================
if (purge)
{
Console.WriteLine("====================================");
foreach (var docId in toDelete)
{
Console.WriteLine($"Deleting memories derived from {docId}");
await memory.DeleteDocumentAsync(docId);
}
}
// ReSharper disable CommentTypo
/* ==== OUTPUT ====
====================================
Uploading text about E=mc^2
Uploading article file about Carbon
Uploading Image file with a news about a conference sponsored by Microsoft
Uploading a text file, a Word doc, and a PDF about Semantic Kernel
Uploading a PDF with a news about NASA and Orion
Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/README.md
Uploading https://raw.githubusercontent.com/microsoft/kernel-memory/main/docs/SECURITY_FILTERS.md
====================================
Question: What's E = m*c^2?
Answer: E = m*c^2 is a formula in physics that describes the mass–energy equivalence. This principle, proposed by Albert Einstein, states that the energy of an object (E) is equal to the mass (m) of that object times the speed of light (c) squared. This relationship is observed in a system's rest frame, where mass and energy differ only by a multiplicative constant and the units of measurement.
====================================
Question: What's Semantic Kernel?
Answer: Semantic Kernel (SK) is a lightweight Software Development Kit (SDK) that enables the integration of AI Large Language Models (LLMs) with conventional programming languages. It combines natural language semantic functions, traditional code native functions, and embeddings-based memory to unlock new potential and add value to applications with AI.
SK supports prompt templating, function chaining, vectorized memory, and intelligent planning capabilities. It encapsulates several design patterns from the latest AI research, allowing developers to infuse their applications with plugins like prompt chaining, recursive reasoning, summarization, zero/few-shot learning, contextual memory, long-term memory, embeddings, semantic indexing, planning, retrieval-augmented generation, and accessing external knowledge stores as well as your own data.
Semantic Kernel is available for use with C# and Python and can be explored and used to build AI-first apps. It is an open-source project, inviting developers to contribute and join in its development.
Sources:
- file4-SK-Readme.pdf - doc002/a166fd04b91a44cd919a300e84931bdf [Friday, December 8, 2023]
- content.url - webPage1/fbcb60da9d5a4ba1a390e108941fc7ad [Friday, December 8, 2023]
- content.url - webPage2/79a67b4f470b43549fce1b9a3de21c95 [Friday, December 8, 2023]
====================================
Question: Which conference is Microsoft sponsoring?
Answer: Microsoft is sponsoring the Automotive News World Congress 2023 event, which is taking place in Detroit, Michigan on September 12, 2023.
Sources:
- file6-ANWC-image.jpg - img001/ac7d8bc0051945a689aa23d1fa9092b2 [Friday, December 8, 2023]
- file5-NASA-news.pdf - doc003/be2411fdc3e84c5995a7753beb927ecd [Friday, December 8, 2023]
- content.url - webPage1/fbcb60da9d5a4ba1a390e108941fc7ad [Friday, December 8, 2023]
- file4-SK-Readme.pdf - doc002/a166fd04b91a44cd919a300e84931bdf [Friday, December 8, 2023]
- file3-lorem-ipsum.docx - doc002/a1269887842d4748980cbdd7e1aabc12 [Friday, December 8, 2023]
====================================
Question: Any news from NASA about Orion?
Blake Answer (none expected): INFO NOT FOUND
Taylor Answer: Yes, NASA has invited media to see the new test version of the Orion spacecraft and the hardware teams will use to recover the capsule and astronauts upon their return from space during the Artemis II mission. The event is scheduled to take place at 11 a.m. PDT on Wednesday, Aug. 2, at Naval Base San Diego. Teams are currently conducting the first in a series of tests in the Pacific Ocean to demonstrate and evaluate the processes, procedures, and hardware for recovery operations for crewed Artemis missions. The tests will help prepare the team for Artemis II, NASA’s first crewed mission under Artemis that will send four astronauts in Orion around the Moon to checkout systems ahead of future lunar missions. The Artemis II crew – NASA astronauts Reid Wiseman, Victor Glover, and Christina Koch, and CSA (Canadian Space Agency) astronaut Jeremy Hansen – will participate in recovery testing at sea next year.
Sources:
- file5-NASA-news.pdf - doc003/be2411fdc3e84c5995a7753beb927ecd [Friday, December 8, 2023]
====================================
Question: What is Orion?
Articles (none expected): INFO NOT FOUND
News: Orion is a spacecraft developed by NASA. It is being used in the Artemis II mission, which is NASA's first crewed mission under the Artemis program. The mission will send four astronauts in the Orion spacecraft around the Moon to check out systems ahead of future lunar missions.
====================================
Deleting memories derived from d421ecd8e79747ec8ed5f1db49baba2c202312070451357022920
Deleting memories derived from doc001
Deleting memories derived from img001
Deleting memories derived from doc002
Deleting memories derived from doc003
Deleting memories derived from webPage1
Deleting memories derived from webPage2
*/