### Setup KM Service Configuration Wizard Source: https://github.com/microsoft/kernel-memory/blob/main/tools/README.md This script starts the KM service configuration wizard and creates an appsettings.Development.json file. ```bash ./setup-km-service.sh ``` -------------------------------- ### Setup Kernel Memory Service Configuration Source: https://github.com/microsoft/kernel-memory/blob/main/service/Service/README.md This command initiates the setup wizard for the Kernel Memory service, which helps create a default configuration file. ```bash dotnet run setup ``` -------------------------------- ### Start KM Service from Source Source: https://github.com/microsoft/kernel-memory/blob/main/tools/README.md This script starts the Kernel Memory service from source code, using KM nuget packages where configured. ```bash ./run-km-service.sh ``` -------------------------------- ### Elasticsearch Configuration Example Source: https://github.com/microsoft/kernel-memory/blob/main/extensions/Elasticsearch/README.md Example of how to configure the Elasticsearch settings in `appsettings.json`. Ensure to fill in your specific endpoint, username, certificate fingerprint, and password. ```json // ... "Elasticsearch": { "Endpoint": "", "UserName": "", "CertificateFingerPrint": "", "Password": "", }, // ... ``` -------------------------------- ### Install AZD on Windows Source: https://github.com/microsoft/kernel-memory/blob/main/infra/AZD.md Installs the Azure Developer CLI on Windows using winget. Ensure AZD is installed before proceeding with deployment. ```bash winget install microsoft.azd ``` -------------------------------- ### Handling Configuration Errors Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/errors.md Example of catching ConfigurationException during Kernel Memory setup and exiting if configuration is invalid. ```csharp try { var memory = new KernelMemoryBuilder() .WithOpenAIDefaults(apiKey) .Build(); } catch (ConfigurationException ex) { Console.WriteLine($"Configuration invalid: {ex.Message}"); Environment.Exit(1); } ``` -------------------------------- ### Example: Development Environment Configuration Loading Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/configuration.md Demonstrates setting the ASPNETCORE_ENVIRONMENT to Development and running the application. This example illustrates the sequence of configuration file loading: appsettings.json, then appsettings.Development.json, followed by User Secrets, and finally environment variables. ```bash # Set environment export ASPNETCORE_ENVIRONMENT=Development # Start application dotnet run # Loads: appsettings.json → appsettings.Development.json → User Secrets → Env Vars ``` -------------------------------- ### Search Client Usage Example Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/storage-search-interfaces.md Demonstrates initializing and using the ISearchClient for searching and asking questions. This example shows how to instantiate the client and perform basic search and question-answering operations, including iterating through results and sources. ```csharp var searchClient = new SearchClient( memoryDb: memoryDb, embeddingGenerator: embeddingGen, textGenerator: textGen ); // Search var searchResults = await searchClient.SearchAsync( index: "documents", query: "project timeline", minRelevance: 0.7, limit: 10 ); foreach (var citation in searchResults.Results) { Console.WriteLine($"Found: {citation.SourceName}"); } // Ask question var answer = await searchClient.AskAsync( index: "documents", question: "What is the project timeline?" ); Console.WriteLine(answer.Result); foreach (var source in answer.RelevantSources) { Console.WriteLine($"Source: {source.SourceName}"); } ``` -------------------------------- ### Populated Elasticsearch Configuration Example Source: https://github.com/microsoft/kernel-memory/blob/main/extensions/Elasticsearch/README.md An example of a populated `appsettings.Development.json` file after running the Docker command and obtaining the necessary credentials and fingerprint. Use these values to connect to your local Elasticsearch instance. ```json // ... "Elasticsearch": { "Endpoint": "https://localhost:9200", "UserName": "elastic", "CertificateFingerPrint": "b2ffe859bde01ece5734526a29b1ce7646b36030835cbbe81424a26151f5f2c5", "Password": "defg...." }, // ... ``` -------------------------------- ### Custom Memory Ingestion Pipeline Setup Source: https://github.com/microsoft/kernel-memory/blob/main/README.md Configure Kernel Memory to use custom .NET handlers for document ingestion. This example shows how to disable default handlers and add custom steps for processing documents. ```csharp var memoryBuilder = new KernelMemoryBuilder() .WithoutDefaultHandlers() .WithOpenAIDefaults(Environment.GetEnvironmentVariable("OPENAI_API_KEY")); var memory = memoryBuilder.Build(); // Plug in custom .NET handlers memory.Orchestrator.AddHandler("step1"); memory.Orchestrator.AddHandler("step2"); memory.Orchestrator.AddHandler("step3"); // Use the custom handlers with the memory object await memory.ImportDocumentAsync( new Document("mytest001") .AddFile("file1.docx") .AddFile("file2.pdf"), steps: new[] { "step1", "step2", "step3" }); ``` -------------------------------- ### Start Qdrant with Docker Source: https://github.com/microsoft/kernel-memory/blob/main/tools/README.md This script starts Qdrant using Docker for local development and debugging. Qdrant is used to store and search vectors as an alternative to Azure AI Search. ```bash ./run-qdrant.sh ``` -------------------------------- ### Example cURL for POST /ask Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/rest-api.md This example shows how to use cURL to send a question to the /ask endpoint, specifying the question and the index to search within. ```bash curl -X POST http://localhost:9001/ask \ -H "Content-Type: application/json" \ -d '{ "question": "What were the main results from Q1?", "index": "quarterly-reports" }' ``` -------------------------------- ### Start KM Service from Local Source (Ignoring Nugets) Source: https://github.com/microsoft/kernel-memory/blob/main/tools/README.md This script starts the Kernel Memory service from local source code, ignoring KM nuget packages. ```bash ./run-km-service-from-source.sh ``` -------------------------------- ### Start Kernel Memory Service from Source (WSL/Linux/MacOS) Source: https://github.com/microsoft/kernel-memory/blob/main/service/Service/README.md Navigate to the service directory and execute the run script to start the Kernel Memory service from source on Unix-like systems. This uses development settings. ```shell cd service/Service ./run.sh ``` -------------------------------- ### Example Retrieval Configuration Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/configuration.md Configure the embedding generator and text generator services to be used for retrieval and question-answering operations. This example sets both to use OpenAI. ```json { "KernelMemory": { "Retrieval": { "EmbeddingGeneratorType": "OpenAI", "TextGeneratorType": "OpenAI" } } } ``` -------------------------------- ### Example cURL for POST /search Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/rest-api.md This example demonstrates how to use cURL to send a search request to the /search endpoint, specifying the query, index, minimum relevance, and limit. ```bash curl -X POST http://localhost:9001/search \ -H "Content-Type: application/json" \ -d '{ "query": "project timeline", "index": "company-docs", "minRelevance": 0.7, "limit": 10 }' ``` -------------------------------- ### Start Kernel Memory Service with Docker (Custom Config - Windows) Source: https://github.com/microsoft/kernel-memory/blob/main/README.md Command to start the Kernel Memory service using Docker on Windows, with a custom configuration file mounted. The appsettings.Development.json file should be in the current directory. ```shell docker run --volume .\appsettings.Development.json:/app/appsettings.Production.json -it --rm -p 9001:9001 kernelmemory/service ``` -------------------------------- ### Start Kernel Memory Service from Source (Windows) Source: https://github.com/microsoft/kernel-memory/blob/main/service/Service/README.md Navigate to the service directory and execute the run script to start the Kernel Memory service from source on Windows. This uses development settings. ```batch cd service\Service run.cmd ``` -------------------------------- ### Start Kernel Memory Service with Docker (Custom Config - Linux/macOS) Source: https://github.com/microsoft/kernel-memory/blob/main/README.md Command to start the Kernel Memory service using Docker on Linux or macOS, with a custom configuration file mounted. The appsettings.Development.json file should be in the current directory. ```shell docker run --volume ./appsettings.Development.json:/app/appsettings.Production.json -it --rm -p 9001:9001 kernelmemory/service ``` -------------------------------- ### Create a Document Instance with Tags and Files Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/types.md Demonstrates how to instantiate and configure a Document object using its fluent API for adding tags and files. ```csharp var doc = new Document("doc-001") .AddTag("user", "alice@example.com") .AddFile("/path/to/file1.pdf") .AddFile("/path/to/file2.docx"); ``` -------------------------------- ### Example Service Configuration Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/configuration.md This JSON configuration demonstrates how to set service-level parameters like enabling the web service, handlers, and OpenAPI UI. It also shows how to configure the maximum upload size and SSE done message. ```json { "KernelMemory": { "Service": { "RunWebService": true, "RunHandlers": true, "OpenApiEnabled": true, "MaxUploadSizeMb": 100, "SendSSEDoneMessage": true } } } ``` -------------------------------- ### Install Kernel Memory Dependency Source: https://github.com/microsoft/kernel-memory/blob/main/examples/000-notebooks/001-upload-and-ask.ipynb Installs the Microsoft.KernelMemory.Core NuGet package. This is the first step to using Kernel Memory. ```csharp #r "nuget: Microsoft.KernelMemory.Core, 0.29.240219.2" using Microsoft.KernelMemory; ``` -------------------------------- ### Start Redis with Docker Source: https://github.com/microsoft/kernel-memory/blob/main/tools/README.md This script starts Redis using Docker for local development and debugging, running on port 6379. It also starts RedisInsight on port 8001. Redis is used to store and search vectors as an alternative to Azure AI Search. ```bash ./run-redis.sh ``` -------------------------------- ### Kernel Memory OpenAPI Response Example Source: https://github.com/microsoft/kernel-memory/blob/main/README.md Example JSON response from the Kernel Memory service's OpenAPI 'ask' endpoint. ```json { "Query": "Any news from NASA about Orion?", "Text": "Yes, there is news from NASA about the Orion spacecraft. NASA has invited the media to see a new test version [......] For more information about the Artemis program, you can visit the NASA website.", "RelevantSources": [ { "Link": "...", "SourceContentType": "application/pdf", "SourceName": "file5-NASA-news.pdf", "Partitions": [ { "Text": "Skip to main content\nJul 28, 2023\nMEDIA ADVISORY M23-095\nNASA Invites Media to See Recovery Craft for\nArtemis Moon Mission\n(/sites/default/files/thumbnails/image/ksc-20230725-ph-fmx01_0003orig.jpg)\nAboard the [......] to Mars (/topics/moon-to-\nmars/),Orion Spacecraft (/exploration/systems/orion/index.html)\nNASA Invites Media to See Recovery Craft for Artemis Moon Miss... https://www.nasa.gov/press-release/nasa-invites-media-to-see-recov...\n2 of 3 7/28/23, 4:51 PM", "Relevance": 0.8430657, "SizeInTokens": 863, "LastUpdate": "2023-08-01T08:15:02-07:00" } ] } ] } ``` -------------------------------- ### Document Storage Usage Example Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/storage-search-interfaces.md Demonstrates how to use the IDocumentStorage interface to manage document files, including creating partitions, writing, reading, listing, and deleting files. ```csharp var storage = new AzureBlobDocumentStorage(blobConfig); // Create document partition await storage.CreatePartitionAsync("doc-001", index: "documents"); // Write file using var fileStream = File.OpenRead("report.pdf"); await storage.WriteAsync( "doc-001", "report.pdf", index: "documents", fileContent: fileStream ); // Read file var file = await storage.ReadAsync("doc-001", "report.pdf", index: "documents"); using var readStream = file.Content; // List files var files = await storage.ListFilesAsync("doc-001", index: "documents"); foreach (var f in files) { Console.WriteLine($"File: {f.FileName}"); } // Delete await storage.DeleteAsync("doc-001", "report.pdf", index: "documents"); ``` -------------------------------- ### IMemoryDb Usage Example Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/storage-search-interfaces.md Demonstrates how to use the IMemoryDb interface to create an index, upsert a record with metadata, and search for similar items. Assumes a concrete implementation like QdrantMemoryDb is available. ```csharp var memoryDb = new QdrantMemoryDb(qdrantConfig); // Create index await memoryDb.CreateIndexAsync("documents", vectorSize: 1536); // Insert record var record = new MemoryRecord { Id = "doc-001", Vector = new Embedding { Vector = new[] { 0.1f, 0.2f, /* ... */ } }, Payload = "Document content here", Tags = new Dictionary> { { "user", new[] { "alice@example.com" } }, { "project", new[] { "Q1-planning" } } } }; string recordId = await memoryDb.UpsertAsync("documents", record); // Search similar await foreach (var (similar, relevance) in memoryDb.GetSimilarListAsync( "documents", "query text", minRelevance: 0.7, limit: 10 )) { Console.WriteLine($"Match: {similar.Payload}, Relevance: {relevance}"); } // Delete index await memoryDb.DeleteIndexAsync("documents"); ``` -------------------------------- ### Example Service Authorization Configuration Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/configuration.md This JSON configuration shows how to enable service authorization and set two access keys for key rotation. Ensure these keys are kept secure. ```json { "KernelMemory": { "ServiceAuthorization": { "Enabled": true, "AccessKey1": "sk-1234567890abcdef", "AccessKey2": "sk-abcdef1234567890" } } } ``` -------------------------------- ### Install Kernel Memory SK Plugin and Dependencies Source: https://github.com/microsoft/kernel-memory/blob/main/examples/000-notebooks/002-semantic-kernel-plugin.ipynb Installs the necessary NuGet packages for Kernel Memory and Semantic Kernel. Ensure you have the correct versions. ```python #r "nuget: Microsoft.SemanticKernel, 1.5.0" #r "nuget: Microsoft.KernelMemory.SemanticKernelPlugin, 0.29.240219.2" using Microsoft.SemanticKernel; using Microsoft.KernelMemory; ``` -------------------------------- ### List Indexes with GET /indexes Source: https://github.com/microsoft/kernel-memory/blob/main/_autodocs/rest-api.md Use the GET /indexes endpoint to retrieve a list of all available indexes in the system. This is useful for understanding the available data sources. ```json { "results": [ { "name": "company-docs" }, { "name": "quarterly-reports" } ] } ``` -------------------------------- ### KM Service Setup with SQL Server Source: https://github.com/microsoft/kernel-memory/blob/main/extensions/SQLServer/README.md Integrate SQL Server memory storage into the Kernel Memory service by adding the nuget package and configuring the KernelMemoryBuilder. ```csharp IKernelMemory memory = new KernelMemoryBuilder(appBuilder.Services) .FromAppSettings() .WithSqlServerMemoryDb(...) .Build(); ``` -------------------------------- ### Import Documents and Ask Questions with WebClient Source: https://github.com/microsoft/kernel-memory/blob/main/examples/001-dotnet-WebClient/README.md Demonstrates importing multiple files into memory and asking a question using the MemoryWebClient. Ensure the Kernel Memory Service is running locally before execution. ```csharp var memory = new MemoryWebClient("http://127.0.0.1:9001/"); await memory.ImportDocumentAsync(new Document("doc012") .AddFiles([ "file2.txt", "file3.docx", "file4.pdf" ]) .AddTag("user", "Blake")); while (!await memory.IsDocumentReadyAsync(documentId: "doc012")) { Console.WriteLine("Waiting for memory ingestion to complete..."); await Task.Delay(TimeSpan.FromSeconds(2)); string answer = await memory.AskAsync("What's Semantic Kernel?"); ``` -------------------------------- ### Start RabbitMQ with Docker Source: https://github.com/microsoft/kernel-memory/blob/main/tools/README.md This script starts RabbitMQ using Docker for local development and debugging. RabbitMQ provides queues for asynchronous pipelines, as an alternative to Azure Queues. ```bash ./run-rabbitmq.sh ```