Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this quickstart, you create a .NET console app to perform semantic search on a vector store to find relevant results for the user's query. You learn how to generate embeddings for user prompts and use those embeddings to query the vector data store. Vector search functionality is also a key component for Retrieval Augmented Generation (RAG) scenarios. The app uses the Microsoft.Extensions.AI and Microsoft.Extensions.VectorData.Abstractions libraries so you can write code using AI abstractions rather than a specific SDK. AI abstractions help create loosely coupled code that allows you to change the underlying AI model with minimal app changes.
You can create your own app using the steps in the sections ahead, or you can clone the GitHub repository that contains the completed sample apps for all of the quickstarts. If you plan to use Azure OpenAI, the sample repo is also structured as an Azure Developer CLI template that can provision an Azure OpenAI resource for you.
git clone https://github.com/dotnet/ai-samples.git
Vector stores or vector databases are essential for tasks like semantic search, Retrieval Augmented Generation (RAG), and other scenarios that require grounding generative AI responses. While relational databases and document databases are optimized for structured and semi-structured data, vector databases are built to efficiently store, index, and manage data represented as embedding vectors. As a result, the indexing and search algorithms used by vector databases are optimized to efficiently retrieve data that can be used downstream in your applications.
Microsoft.Extensions.VectorData.Abstractions is a .NET library developed in collaboration with Semantic Kernel and the broader .NET ecosystem to provide a unified layer of abstractions for interacting with vector stores.
The abstractions in Microsoft.Extensions.VectorData.Abstractions
provide library authors and developers with the following functionality:
Note
The Microsoft.Extensions.VectorData.Abstractions library is currently in preview.
Complete the following steps to create a .NET console app that can accomplish the following:
In an empty directory on your computer, use the dotnet new
command to create a new console app:
dotnet new console -o VectorDataAI
Change directory into the app folder:
cd VectorDataAI
Install the required packages:
dotnet add package Azure.Identity
dotnet add package Azure.AI.OpenAI
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
dotnet add package Microsoft.Extensions.VectorData.Abstractions --prerelease
dotnet add package Microsoft.SemanticKernel.Connectors.InMemory --prerelease
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
The following list describes what each package is used for in the VectorDataAI
app:
Azure.Identity
provides Microsoft Entra ID
token authentication support across the Azure SDK using classes such as DefaultAzureCredential
.Azure.AI.OpenAI
is the official package for using OpenAI's .NET library with the Azure OpenAI Service.Microsoft.SemanticKernel.Connectors.InMemory
provides an in-memory vector store class to hold queryable vector data records.Microsoft.Extensions.VectorData.Abstractions
enables Create-Read-Update-Delete (CRUD) and search operations on vector stores.Microsoft.Extensions.Configuration.UserSecrets
is a user secrets configuration provider implementation for Microsoft.Extensions.Configuration
.dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
dotnet add package Microsoft.Extensions.VectorData.Abstractions --prerelease
dotnet add package Microsoft.SemanticKernel.Connectors.InMemory --prerelease
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
The following list describes what each package is used for in the VectorDataAI
app:
Microsoft.Extensions.AI.OpenAI
provides AI abstractions for OpenAI-compatible models or endpoints. This library also includes the official OpenAI
library for the OpenAI service API as a dependency.Microsoft.SemanticKernel.Connectors.InMemory
provides an in-memory vector store class to hold queryable vector data records.Microsoft.Extensions.VectorData.Abstractions
enables Create-Read-Update-Delete (CRUD) and search operations on vector stores.Microsoft.Extensions.Configuration.UserSecrets
is a user secrets configuration provider implementation for Microsoft.Extensions.Configuration
.Open the app in Visual Studio Code (or your editor of choice).
code .
The sample GitHub repository is structured as an Azure Developer CLI (azd
) template, which azd
can use to provision the Azure OpenAI service and model for you.
From a terminal or command prompt, navigate to the src\quickstarts\azure-openai
directory of the sample repo.
Run the azd up
command to provision the Azure OpenAI resources. It might take several minutes to create the Azure OpenAI service and deploy the model.
azd up
azd
also configures the required user secrets for the sample app, such as the Azure OpenAI endpoint and model name.
Navigate to the root of your .NET project from a terminal or command prompt.
Run the following commands to configure your OpenAI API key as a secret for the sample app:
dotnet user-secrets init
dotnet user-secrets set OpenAIKey <your-openai-key>
dotnet user-secrets set ModelName <your-openai-model-name>
Note
For the ModelName
value, you need to specify an OpenAI text embedding model such as text-embedding-3-small
or text-embedding-3-large
to generate embeddings for vector search in the sections that follow.
Add a new class named CloudService to your project with the following properties:
using Microsoft.Extensions.VectorData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VectorDataAI
{
internal class CloudService
{
[VectorStoreRecordKey]
public int Key { get; set; }
[VectorStoreRecordData]
public string Name { get; set; }
[VectorStoreRecordData]
public string Description { get; set; }
[VectorStoreRecordVector(384, DistanceFunction.CosineSimilarity)]
public ReadOnlyMemory<float> Vector { get; set; }
}
}
In the preceding code:
Microsoft.Extensions.VectorData
influence how each property is handled when used in a vector store.In the Program.cs file, add the following code to create a data set that describes a collection of cloud services:
var cloudServices = new List<CloudService>()
{
new CloudService
{
Key=0,
Name="Azure App Service",
Description="Host .NET, Java, Node.js, and Python web applications and APIs in a fully managed Azure service. You only need to deploy your code to Azure. Azure takes care of all the infrastructure management like high availability, load balancing, and autoscaling."
},
new CloudService
{
Key=1,
Name="Azure Service Bus",
Description="A fully managed enterprise message broker supporting both point to point and publish-subscribe integrations. It's ideal for building decoupled applications, queue-based load leveling, or facilitating communication between microservices."
},
new CloudService
{
Key=2,
Name="Azure Blob Storage",
Description="Azure Blob Storage allows your applications to store and retrieve files in the cloud. Azure Storage is highly scalable to store massive amounts of data and data is stored redundantly to ensure high availability."
},
new CloudService
{
Key=3,
Name="Microsoft Entra ID",
Description="Manage user identities and control access to your apps, data, and resources.."
},
new CloudService
{
Key=4,
Name="Azure Key Vault",
Description="Store and access application secrets like connection strings and API keys in an encrypted vault with restricted access to make sure your secrets and your application aren't compromised."
},
new CloudService
{
Key=5,
Name="Azure AI Search",
Description="Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization."
}
Create and configure an IEmbeddingGenerator
implementation to send requests to an embedding AI model:
// Load the configuration values
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
string model = config["AZURE_OPENAI_GPT_NAME"];
// Create the embedding generator
IEmbeddingGenerator<string, Embedding<float>> generator =
new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential())
Note
DefaultAzureCredential searches for authentication credentials from your local tooling. If you aren't using the azd
template to provision the Azure OpenAI resource, you'll need to assign the Azure AI Developer
role to the account you used to sign in to Visual Studio or the Azure CLI. For more information, see Authenticate to Azure AI services with .NET.
// Load the configuration values
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string model = config["ModelName"];
string key = config["OpenAIKey"];
// Create the embedding generator
IEmbeddingGenerator<string, Embedding<float>> generator =
new OpenAIClient(new ApiKeyCredential(key))
.AsEmbeddingGenerator(modelId: model);
Create and populate a vector store with the cloud service data. Use the IEmbeddingGenerator
implementation to create and assign an embedding vector for each record in the cloud service data:
// Create and populate the vector store
var vectorStore = new InMemoryVectorStore();
var cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices");
await cloudServicesStore.CreateCollectionIfNotExistsAsync();
foreach (var service in cloudServices)
{
service.Vector = await generator.GenerateEmbeddingVectorAsync(service.Description);
await cloudServicesStore.UpsertAsync(service);
}
The embeddings are numerical representations of the semantic meaning for each data record, which makes them compatible with vector search features.
Create an embedding for a search query and use it to perform a vector search on the vector store:
// Convert a search query to a vector and search the vector store
var query = "Which Azure service should I use to store my Word documents?";
var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query);
var results = await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions()
{
Top = 1,
VectorPropertyName = "Vector"
});
await foreach (var result in results.Results)
{
Console.WriteLine($"Name: {result.Record.Name}");
Console.WriteLine($"Description: {result.Record.Description}");
Console.WriteLine($"Vector match score: {result.Score}");
Console.WriteLine();
}
Use the dotnet run
command to run the app:
dotnet run
The app prints out the top result of the vector search, which is the cloud service that is most relevant to the original query. You can modify the query to try different search scenarios.
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Perform vector search and retrieval in Azure AI Search - Training
Perform vector search and retrieval in Azure AI Search.
Certification
Microsoft Certified: Azure AI Engineer Associate - Certifications
Design and implement an Azure AI solution using Azure AI services, Azure AI Search, and Azure Open AI.
Documentation
Quickstart - Generate images using AI with .NET - .NET
Create a simple app using to generate images using .NET and the OpenAI or Azure OpenAI models.
Quickstart - Summarize text using an AI chat app with .NET - .NET
Create a simple chat app using Microsoft.Extensions.AI to summarize a text.
Quickstart - Connect to and chat with a local AI using .NET - .NET
Set up a local AI model and chat with it using a .NET console app and the Microsoft.Extensions.AI libraries