Skip to content

Commit 60a258f

Browse files
Merge pull request #46589 from dotnet/main
Merge main into live
2 parents 43b1e2a + e42a868 commit 60a258f

File tree

22 files changed

+208
-142
lines changed

22 files changed

+208
-142
lines changed

docs/ai/quickstarts/build-vector-search-app.md

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,44 @@ zone_pivot_groups: openai-library
1010

1111
# Build a .NET AI vector search app
1212

13-
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 <xref:Microsoft.Extensions.AI> and [Microsoft.Extensions.VectorData.Abstractions](https://www.nuget.org/packages/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.
13+
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.
1414

15-
:::zone target="docs" pivot="openai"
15+
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.
1616

17-
[!INCLUDE [openai-prereqs](includes/prerequisites-openai.md)]
17+
## About the libraries
1818

19-
:::zone-end
19+
The app uses the <xref:Microsoft.Extensions.AI> and <xref:Microsoft.Extensions.VectorData> 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.
2020

21-
:::zone target="docs" pivot="azure-openai"
21+
[📦 Microsoft.Extensions.VectorData.Abstractions](https://www.nuget.org/packages/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:
2222

23-
[!INCLUDE [azure-openai-prereqs](includes/prerequisites-azure-openai.md)]
23+
- Perform create-read-update-delete (CRUD) operations on vector stores.
24+
- Use vector and text search on vector stores.
2425

25-
:::zone-end
26+
> [!NOTE]
27+
> The [Microsoft.Extensions.VectorData.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.VectorData.Abstractions/) library is currently in preview.
2628
27-
## Interact with your data using vector stores
29+
<!--Prerequisites section-->
2830

29-
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.
31+
:::zone target="docs" pivot="openai"
3032

31-
### Explore Microsoft.Extensions.VectorData.Abstractions
33+
[!INCLUDE [openai-prereqs](includes/prerequisites-openai.md)]
3234

33-
[Microsoft.Extensions.VectorData.Abstractions](https://www.nuget.org/packages/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.
35+
:::zone-end
3436

35-
The abstractions in `Microsoft.Extensions.VectorData.Abstractions` provide library authors and developers with the following functionality:
37+
:::zone target="docs" pivot="azure-openai"
3638

37-
- Perform Create-Read-Update-Delete (CRUD) operations on vector stores
38-
- Use vector and text search on vector stores
39+
[!INCLUDE [azure-openai-prereqs](includes/prerequisites-azure-openai.md)]
3940

40-
> [!NOTE]
41-
> The [Microsoft.Extensions.VectorData.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.VectorData.Abstractions/) library is currently in preview.
41+
:::zone-end
4242

4343
## Create the app
4444

4545
Complete the following steps to create a .NET console app that can:
4646

47-
- Create and populate a vector store by generating embeddings for a data set
48-
- Generate an embedding for the user prompt
49-
- Query the vector store using the user prompt embedding
50-
- Display the relevant results from the vector search
47+
- Create and populate a vector store by generating embeddings for a data set.
48+
- Generate an embedding for the user prompt.
49+
- Query the vector store using the user prompt embedding.
50+
- Display the relevant results from the vector search.
5151

5252
1. In an empty directory on your computer, use the `dotnet new` command to create a new console app:
5353

@@ -80,8 +80,8 @@ Complete the following steps to create a .NET console app that can:
8080
8181
- [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) provides [`Microsoft Entra ID`](/entra/fundamentals/whatis) token authentication support across the Azure SDK using classes such as `DefaultAzureCredential`.
8282
- [`Azure.AI.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI) is the official package for using OpenAI's .NET library with the Azure OpenAI Service.
83-
- [`Microsoft.SemanticKernel.Connectors.InMemory`](https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.InMemory) provides an in-memory vector store class to hold queryable vector data records.
8483
- [`Microsoft.Extensions.VectorData.Abstractions`](https://www.nuget.org/packages/Microsoft.Extensions.AI) enables Create-Read-Update-Delete (CRUD) and search operations on vector stores.
84+
- [`Microsoft.SemanticKernel.Connectors.InMemory`](https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.InMemory) provides an in-memory vector store class to hold queryable vector data records.
8585
- [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration) provides an implementation of key-value pair&mdash;based configuration.
8686
- [`Microsoft.Extensions.Configuration.UserSecrets`](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.UserSecrets) is a user secrets configuration provider implementation for `Microsoft.Extensions.Configuration`.
8787
@@ -101,8 +101,8 @@ Complete the following steps to create a .NET console app that can:
101101
The following list describes each package in the `VectorDataAI` app:
102102
103103
- [`Microsoft.Extensions.AI.OpenAI`](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI) provides AI abstractions for OpenAI-compatible models or endpoints. This library also includes the official [`OpenAI`](https://www.nuget.org/packages/OpenAI) library for the OpenAI service API as a dependency.
104-
- [`Microsoft.SemanticKernel.Connectors.InMemory`](https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.InMemory) provides an in-memory vector store class to hold queryable vector data records.
105104
- [`Microsoft.Extensions.VectorData.Abstractions`](https://www.nuget.org/packages/Microsoft.Extensions.AI) enables Create-Read-Update-Delete (CRUD) and search operations on vector stores.
105+
- [`Microsoft.SemanticKernel.Connectors.InMemory`](https://www.nuget.org/packages/Microsoft.SemanticKernel.Connectors.InMemory) provides an in-memory vector store class to hold queryable vector data records.
106106
- [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration) provides an implementation of key-value pair&mdash;based configuration.
107107
- [`Microsoft.Extensions.Configuration.UserSecrets`](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.UserSecrets) is a user secrets configuration provider implementation for `Microsoft.Extensions.Configuration`.
108108
@@ -134,21 +134,18 @@ Complete the following steps to create a .NET console app that can:
134134
dotnet user-secrets set ModelName <your-OpenAI-model-name>
135135
```
136136
137-
> [!NOTE]
138-
> 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.
139-
140137
:::zone-end
141138
139+
> [!NOTE]
140+
> For the model name, you need to specify a 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. For more information about embedding models, see [Embeddings](/azure/ai-services/openai/concepts/models#embeddings).
141+
142142
## Add the app code
143143
144144
1. Add a new class named `CloudService` to your project with the following properties:
145145
146146
:::code language="csharp" source="snippets/chat-with-data/azure-openai/CloudService.cs" :::
147147
148-
In the preceding code:
149-
150-
- The C# attributes provided by `Microsoft.Extensions.VectorData` influence how each property is handled when used in a vector store.
151-
- The `Vector` property stores a generated embedding that represents the semantic meaning of the `Name` and `Description` for vector searches.
148+
The <xref:Microsoft.Extensions.VectorData> attributes, such as <xref:Microsoft.Extensions.VectorData.VectorStoreKeyAttribute>, influence how each property is handled when used in a vector store. The `Vector` property stores a generated embedding that represents the semantic meaning of the `Description` value for vector searches.
152149
153150
1. In the `Program.cs` file, add the following code to create a data set that describes a collection of cloud services:
154151
@@ -158,10 +155,10 @@ Complete the following steps to create a .NET console app that can:
158155
159156
:::zone target="docs" pivot="azure-openai"
160157
161-
:::code language="csharp" source="snippets/chat-with-data/azure-openai/program.cs" id="EmbeddingGen":::
158+
:::code language="csharp" source="snippets/chat-with-data/azure-openai/program.cs" id="EmbeddingGenerator":::
162159
163160
> [!NOTE]
164-
> <xref:Azure.Identity.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](../azure-ai-services-authentication.md).
161+
> <xref:Azure.Identity.DefaultAzureCredential> searches for authentication credentials from your local tooling. 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](../azure-ai-services-authentication.md).
165162
166163
:::zone-end
167164
@@ -193,7 +190,7 @@ Complete the following steps to create a .NET console app that can:
193190
194191
## Clean up resources
195192
196-
If you no longer need them, delete the Azure OpenAI resource and GPT-4 model deployment.
193+
If you no longer need them, delete the Azure OpenAI resource and model deployment.
197194
198195
1. In the [Azure Portal](https://aka.ms/azureportal), navigate to the Azure OpenAI resource.
199196
1. Select the Azure OpenAI resource, and then select **Delete**.

docs/ai/quickstarts/includes/prerequisites-openai.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ ms.topic: include
77
## Prerequisites
88

99
- .NET 8.0 SDK or higher - [Install the .NET 8.0 SDK](https://dotnet.microsoft.com/download/dotnet/8.0).
10-
- An [API key from OpenAI](https://platform.openai.com/docs/quickstart/account-setup) so you can run this sample.
10+
- An [API key from OpenAI](https://platform.openai.com/docs/libraries#create-and-export-an-api-key) so you can run this sample.

docs/ai/quickstarts/snippets/chat-with-data/azure-openai/CloudService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ internal class CloudService
1313
[VectorStoreData]
1414
public string Description { get; set; }
1515

16-
[VectorStoreVector(Dimensions: 384, DistanceFunction = DistanceFunction.CosineSimilarity)]
16+
[VectorStoreVector(
17+
Dimensions: 384,
18+
DistanceFunction = DistanceFunction.CosineSimilarity)]
1719
public ReadOnlyMemory<float> Vector { get; set; }
1820
}

docs/ai/quickstarts/snippets/chat-with-data/azure-openai/Program.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Configuration;
55
using Microsoft.Extensions.VectorData;
66
using Microsoft.SemanticKernel.Connectors.InMemory;
7+
using System.Linq;
78
using VectorDataAI;
89

910
// <SnippetDataSet>
@@ -42,21 +43,21 @@
4243
];
4344
// </SnippetDataSet>
4445

45-
// <SnippetEmbeddingGen>
46-
// Load the configuration values
46+
// <SnippetEmbeddingGenerator>
47+
// Load the configuration values.
4748
IConfigurationRoot config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
4849
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
4950
string model = config["AZURE_OPENAI_GPT_NAME"];
5051

51-
// Create the embedding generator
52+
// Create the embedding generator.
5253
IEmbeddingGenerator<string, Embedding<float>> generator =
5354
new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
5455
.GetEmbeddingClient(deploymentName: model)
5556
.AsIEmbeddingGenerator();
56-
// </SnippetEmbeddingGen>
57+
// </SnippetEmbeddingGenerator>
5758

5859
// <SnippetVectorStore>
59-
// Create and populate the vector store
60+
// Create and populate the vector store.
6061
var vectorStore = new InMemoryVectorStore();
6162
VectorStoreCollection<int, CloudService> cloudServicesStore =
6263
vectorStore.GetCollection<int, CloudService>("cloudServices");
@@ -70,14 +71,15 @@
7071
// </SnippetVectorStore>
7172

7273
// <SnippetSearch>
73-
// Convert a search query to a vector and search the vector store
74+
// Convert a search query to a vector
75+
// and search the vector store.
7476
string query = "Which Azure service should I use to store my Word documents?";
7577
ReadOnlyMemory<float> queryEmbedding = await generator.GenerateVectorAsync(query);
7678

77-
List<VectorSearchResult<CloudService>> results =
78-
await cloudServicesStore.SearchAsync(queryEmbedding, top: 1).ToListAsync();
79+
IAsyncEnumerable<VectorSearchResult<CloudService>> results =
80+
cloudServicesStore.SearchAsync(queryEmbedding, top: 1);
7981

80-
foreach (VectorSearchResult<CloudService> result in results)
82+
await foreach (VectorSearchResult<CloudService> result in results)
8183
{
8284
Console.WriteLine($"Name: {result.Record.Name}");
8385
Console.WriteLine($"Description: {result.Record.Description}");

docs/ai/quickstarts/snippets/chat-with-data/azure-openai/VectorDataAI.csproj

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<UserSecretsId>5981f38c-e59c-46cc-80bb-463f8c3f1691</UserSecretsId>
8+
<UserSecretsId>8565c67c-c4b8-4824-a3e7-7f6e352adb33</UserSecretsId>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Azure.Identity" Version="1.14.0" />
1312
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
13+
<PackageReference Include="Azure.Identity" Version="1.14.0" />
1414
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.5.0-preview.1.25265.7" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.5" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.5" />
1517
<PackageReference Include="Microsoft.Extensions.VectorData.Abstractions" Version="9.5.0" />
16-
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.53.1-preview" />
17-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.0-preview.4.25258.110" />
18-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="10.0.0-preview.4.25258.110" />
19-
<PackageReference Include="System.Linq.AsyncEnumerable" Version="10.0.0-preview.4.25258.110" />
18+
<PackageReference Include="Microsoft.SemanticKernel.Connectors.InMemory" Version="1.55.0-preview" />
2019
</ItemGroup>
2120

2221
</Project>
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
using Microsoft.Extensions.VectorData;
22

3-
namespace VectorDataAI
3+
namespace VectorDataAI;
4+
5+
internal class CloudService
46
{
5-
internal class CloudService
6-
{
7-
[VectorStoreRecordKey]
8-
public int Key { get; set; }
7+
[VectorStoreKey]
8+
public int Key { get; set; }
99

10-
[VectorStoreRecordData]
11-
public string Name { get; set; }
10+
[VectorStoreData]
11+
public string Name { get; set; }
1212

13-
[VectorStoreRecordData]
14-
public string Description { get; set; }
13+
[VectorStoreData]
14+
public string Description { get; set; }
1515

16-
[VectorStoreRecordVector(Dimensions: 384, DistanceFunction = DistanceFunction.CosineSimilarity)]
17-
public ReadOnlyMemory<float> Vector { get; set; }
18-
}
16+
[VectorStoreVector(
17+
Dimensions: 384,
18+
DistanceFunction = DistanceFunction.CosineSimilarity)]
19+
public ReadOnlyMemory<float> Vector { get; set; }
1920
}

0 commit comments

Comments
 (0)