|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using Microsoft.KernelMemory; |
| 4 | +using Microsoft.SemanticKernel; |
| 5 | +using Microsoft.SemanticKernel.Orchestration; |
| 6 | + |
| 7 | +public static class Program |
| 8 | +{ |
| 9 | + // ReSharper disable InconsistentNaming |
| 10 | + public static async Task Main() |
| 11 | + { |
| 12 | + const string Document = "mydocs-NASA-news.pdf"; |
| 13 | + const string Question1 = "any news about Orion?"; |
| 14 | + const string Question2 = "any news about Hubble telescope?"; |
| 15 | + const string Question3 = "what is a solar eclipse?"; |
| 16 | + |
| 17 | + // === PREPARE KERNEL === |
| 18 | + // Usual code to create an instance of SK, using Azure OpenAI. |
| 19 | + // You can use any LLM, replacing `WithAzureChatCompletionService` with other LLM options. |
| 20 | + |
| 21 | + var builder = new KernelBuilder(); |
| 22 | + builder.WithAzureChatCompletionService( |
| 23 | + deploymentName: Environment.GetEnvironmentVariable("AOAI_DEPLOYMENT"), |
| 24 | + endpoint: Environment.GetEnvironmentVariable("AOAI_ENDPOINT"), |
| 25 | + apiKey: Environment.GetEnvironmentVariable("AOAI_API_KEY")); |
| 26 | + |
| 27 | + var kernel = builder.Build(); |
| 28 | + |
| 29 | + // === PREPARE A SIMPLE SEMANTIC FUNCTION |
| 30 | + // A simple prompt showing how you can leverage the memory inside prompts and semantic functions. |
| 31 | + // See how "memory.ask" is used to pass the user question. At runtime the block is replaced with the |
| 32 | + // answer provided by the memory service. |
| 33 | + |
| 34 | + var skPrompt = """ |
| 35 | +Question to Kernel Memory: {{$input}} |
| 36 | +
|
| 37 | +Kernel Memory Answer: {{memory.ask $input}} |
| 38 | +
|
| 39 | +If the answer is empty say 'I don't know' otherwise reply with a preview of the answer, truncated to 15 words. |
| 40 | +"""; |
| 41 | + |
| 42 | + var doesItKnowFunction = kernel.CreateSemanticFunction(skPrompt); |
| 43 | + |
| 44 | + // === PREPARE MEMORY PLUGIN === |
| 45 | + // Load the Kernel Memory plugin into Semantic Kernel. |
| 46 | + // We're using a local instance here, so remember to start the service locally first, |
| 47 | + // otherwise change the URL pointing to your KM endpoint. |
| 48 | + |
| 49 | + var memory = new MemoryWebClient("http://127.0.0.1:9001/"); |
| 50 | + kernel.ImportFunctions(new MemoryPlugin(memory), "memory"); |
| 51 | + |
| 52 | + // === LOAD DOCUMENT INTO MEMORY === |
| 53 | + // Load some data in memory, in this case use a PDF file, though |
| 54 | + // you can also load web pages, Word docs, raw text, etc. |
| 55 | + |
| 56 | + await memory.ImportDocumentAsync(Document, documentId: "NASA001"); |
| 57 | + |
| 58 | + // === RUN SEMANTIC FUNCTION === |
| 59 | + // Run some example questions, showing how the answer is grounded on the document uploaded. |
| 60 | + // Only the first question can be answered, because the document uploaded doesn't contain any |
| 61 | + // information about Question2 and Question3. |
| 62 | + |
| 63 | + Console.WriteLine("---------"); |
| 64 | + KernelResult answer = await kernel.RunAsync(Question1, doesItKnowFunction); |
| 65 | + Console.WriteLine(Question1 + "\n"); |
| 66 | + Console.WriteLine("Answer: " + answer); |
| 67 | + |
| 68 | + Console.WriteLine("---------"); |
| 69 | + answer = await kernel.RunAsync(Question2, doesItKnowFunction); |
| 70 | + Console.WriteLine(Question2 + "\n"); |
| 71 | + Console.WriteLine("Answer: " + answer); |
| 72 | + |
| 73 | + Console.WriteLine("---------"); |
| 74 | + answer = await kernel.RunAsync(Question3, doesItKnowFunction); |
| 75 | + Console.WriteLine(Question3 + "\n"); |
| 76 | + Console.WriteLine("Answer: " + answer); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/* ===== OUTPUT ===== |
| 81 | +
|
| 82 | +--------- |
| 83 | +any news about Orion? |
| 84 | +
|
| 85 | +Answer: Yes, NASA has invited media to see the new test version of the Orion spacecraft... |
| 86 | +--------- |
| 87 | +any news about Hubble telescope? |
| 88 | +
|
| 89 | +Answer: I don't know. |
| 90 | +--------- |
| 91 | +what is a solar eclipse? |
| 92 | +
|
| 93 | +Answer: I don't know. |
| 94 | +
|
| 95 | +*/ |
0 commit comments