July 19th, 2023

Announcing Semantic Kernel integration with Azure AI Search (formerly Azure Cognitive Search)

Nilesh Acharya
Principal Product Manager

Image skpatternlarge

We’re excited to announce integration of Azure AI 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 AI 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 AI 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 AI 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 AI 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


using Microsoft.SemanticKernel.Connectors.AzureAISearch;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;

var gitHubFiles = new Dictionary
{
    ["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/dotnet/notebooks/02-running-prompts-from-file.ipynb"]
        = "Jupyter notebook describing how to pass prompts from a file to a semantic plugin or function",
    ["https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks//00-getting-started.ipynb"]
        = "Jupyter notebook describing how to get started with the Semantic Kernel",
    ["https://github.com/microsoft/semantic-kernel/tree/main/samples/plugins/ChatPlugin/ChatGPT"]
        = "Sample demonstrating how to create a chat plugin 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"
};

var memory = new MemoryBuilder()
                    .WithAzureOpenAITextEmbeddingGeneration("text-embedding-ada-002", AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY)
                    .WithMemoryStore(new AzureAISearchMemoryStore(AZURE_SEARCH_ENDPOINT, AZURE_SEARCH_API_KEY))
                    .Build();

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

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

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

Python

from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureTextCompletion, AzureTextEmbedding
from semantic_kernel.connectors.memory.azure_ai_search import AzureAISearchMemoryStore

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=AzureAISearchMemoryStore(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

Author

Nilesh Acharya
Principal Product Manager

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Alex ChaoMicrosoft employee · Edited

    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