Announcing Semantic Kernel integration with Azure Cognitive Search

Nilesh Acharya

Image skpatternlarge

We’re excited to announce integration of Azure Cognitive Search Vector Search with Semantic Kernel, available in both C# and Python. This integration follows the existing Semantic Memory architecture, making it incredibly easy for developers to add memory to prompts and plugins. This integration unlocks the following key benefits.

  1. Access to Vector Search: Utilize the capabilities of Azure Cognitive Services Vector Search to index datastores including Cosmos DB, Azure SQL Server and blob storage to perform vectors searches across a various data types including image, audio, text and video. Vector search compares the vector representation of the query and content to find relevant results for users with high efficiency and accuracy.
  2. Managed Service on Azure: Say goodbye to spinning up VMs and storing your data outside Azure. With Azure Cognitive Search, everything is managed within the platform on multiple Azure regions with high availability and low latency. This simplifies your search implementation and reduces overall complexity.
  3. Bring Your Own Embedding Vectors: Developers can now integrate their own embedding vectors, e.g. generated by OpenAI, for an even more precise similarity search using cosine similarity. This allows for more accurate and efficient search experiences within the Semantic Kernel framework, and it’s particularly useful for grounding AI responses with relevant information, enhancing the overall value of AI-generated content.
  4. Integration with Azure Active Directory: enhance the security of your authentication process by integrating with Azure Active Directory, ensuring a secure and seamless user experience.
  5. Simplified Memory Management: with the integration of Azure Cognitive Search into Semantic Kernel, developers can easily add memory to prompts and plugins, following the existing Semantic Memory architecture. This simplifies the process of managing memory in your applications and enhances overall efficiency.

By leveraging the power of Azure Cognitive Search with Semantic Kernel, you can now enjoy advanced search capabilities, seamless integration with other Azure services, and simplified memory management, taking your search experience to the next level.

C# sample

Note: Remember to install the required NuGet package (Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch) which is available here.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch;
using Microsoft.SemanticKernel.Memory;

var kernel = new KernelBuilder()
    .WithAzureTextEmbeddingGenerationService(
        "text-embedding-ada-002",
        AZURE_OPENAI_ENDPOINT,
        AZURE_OPENAI_API_KEY)
    .WithMemoryStorage(new AzureCognitiveSearchMemoryStore(
        AZURE_SEARCH_ENDPOINT,
        AZURE_SEARCH_ADMIN_KEY))
    .Build();

var gitHubFiles = new Dictionary<string, string>
{
    ["https://github.com/microsoft/semantic-kernel/blob/main/README.md"]
        = "README: Installation, getting started, and how to contribute",
    ["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb"]
        = "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function",
    ["https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb"]
        = "Jupyter notebook describing how to get started with the Semantic Kernel",
    ["https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT"]
        = "Sample demonstrating how to create a chat skill interfacing with ChatGPT",
    ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs"]
        = "C# class that defines a volatile embedding store",
    ["https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/KernelHttpServer/README.md"]
        = "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4",
    ["https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md"]
        = "README: README associated with a sample chat summary react-based webapp",
};

foreach (var entry in gitHubFiles)
{
    await kernel.Memory.SaveReferenceAsync(
        collection: "GitHubFiles",
        externalSourceName: "GitHub",
        externalId: entry.Key,
        description: entry.Value,
        text: entry.Value);
}

IAsyncEnumerable<MemoryQueryResult> memories = kernel.Memory.SearchAsync("GitHubFiles", "How do I get started?", limit: 1);
var memory = await memories.FirstOrDefaultAsync(_ => true);
Console.WriteLine("URL:     : " + memory.Metadata.Id);
Console.WriteLine("Title    : " + memory.Metadata.Description);
Console.WriteLine("Relevance: " + memory.Relevance);

memories = kernel.Memory.SearchAsync("GitHubFiles", "Can I build a chat with SK?", limit: 1);
memory = await memories.FirstOrDefaultAsync(_ => true);
Console.WriteLine("URL:     : " + memory.Metadata.Id);
Console.WriteLine("Title    : " + memory.Metadata.Description);
Console.WriteLine("Relevance: " + memory.Relevance);

Python

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, AzureTextEmbedding
from semantic_kernel.connectors.memory.azure_cognitive_search import AzureCognitiveSearchMemoryStore

kernel = Kernel()

kernel.add_text_embedding_generation_service("ada", 
    AzureTextEmbedding("text-embedding-ada-002", AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY))

kernel.register_memory_store(memory_store=AzureCognitiveSearchMemoryStore(1536))

await kernel.memory.save_information_async("aboutMe", id="info1", text="My name is Andrea")
await kernel.memory.save_information_async("aboutMe", id="info2", text="I currently work as a tour guide")
await kernel.memory.save_information_async("aboutMe", id="info3", text="I've been living in Seattle since 2005")
await kernel.memory.save_information_async("aboutMe", id="info4", text="I visited France and Italy five times since 2015")
await kernel.memory.save_information_async("aboutMe", id="info5", text="My family is from New York")

questions = [
    "what's my name",
    "where do I live?",
    "where's my family from?",
    "where have I traveled?",
    "what do I do for work",
]

for question in questions:
    print(f"Question: {question}")
    result = await kernel.memory.search_async("aboutMe", question)
    print(f"Answer: {result[0].text}\n")

Next Steps:

Image skpatternsmallbw

1 comment

Comments are closed. Login to edit/delete your existing comments

  • Alex ChaoMicrosoft employee 2

    See the walkthrough I did with Devis Lucato, the principal architect of the Semantic Kernel, on how you can bring Azure Cognitive Search to your own AI applications! https://youtu.be/4bvnDf0F6yk

Feedback usabilla icon