|
1 |
| -using Microsoft.Extensions.AI; |
2 |
| -using OpenAI; |
| 1 | +using System.ClientModel; |
| 2 | +using Microsoft.Extensions.AI; |
| 3 | +using Microsoft.Extensions.Configuration; |
3 | 4 | using Microsoft.Extensions.VectorData;
|
4 | 5 | using Microsoft.SemanticKernel.Connectors.InMemory;
|
| 6 | +using OpenAI; |
5 | 7 | using VectorDataAI;
|
6 |
| -using System.ClientModel; |
7 |
| -using Microsoft.Extensions.Configuration; |
8 | 8 |
|
9 | 9 | var cloudServices = new List<CloudService>()
|
10 | 10 | {
|
11 |
| - new CloudService |
12 |
| - { |
| 11 | + new() { |
13 | 12 | Key=0,
|
14 | 13 | Name="Azure App Service",
|
15 | 14 | 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."
|
16 | 15 | },
|
17 |
| - new CloudService |
18 |
| - { |
| 16 | + new() { |
19 | 17 | Key=1,
|
20 | 18 | Name="Azure Service Bus",
|
21 | 19 | 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."
|
22 | 20 | },
|
23 |
| - new CloudService |
24 |
| - { |
| 21 | + new() { |
25 | 22 | Key=2,
|
26 | 23 | Name="Azure Blob Storage",
|
27 | 24 | 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."
|
28 | 25 | },
|
29 |
| - new CloudService |
30 |
| - { |
| 26 | + new() { |
31 | 27 | Key=3,
|
32 | 28 | Name="Microsoft Entra ID",
|
33 | 29 | Description="Manage user identities and control access to your apps, data, and resources.."
|
34 | 30 | },
|
35 |
| - new CloudService |
36 |
| - { |
| 31 | + new() { |
37 | 32 | Key=4,
|
38 | 33 | Name="Azure Key Vault",
|
39 | 34 | 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."
|
40 | 35 | },
|
41 |
| - new CloudService |
42 |
| - { |
| 36 | + new() { |
43 | 37 | Key=5,
|
44 | 38 | Name="Azure AI Search",
|
45 | 39 | Description="Information retrieval at scale for traditional and conversational search applications, with security and options for AI enrichment and vectorization."
|
46 | 40 | }
|
47 | 41 | };
|
48 | 42 |
|
49 |
| -// Load the configuration values |
50 |
| -var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build(); |
| 43 | +// Load the configuration values. |
| 44 | +IConfigurationRoot config = new ConfigurationBuilder().AddUserSecrets<Program>().Build(); |
51 | 45 | string model = config["ModelName"];
|
52 | 46 | string key = config["OpenAIKey"];
|
53 | 47 |
|
54 |
| -// Create the embedding generator |
| 48 | +// Create the embedding generator. |
55 | 49 | IEmbeddingGenerator<string, Embedding<float>> generator =
|
56 | 50 | new OpenAIClient(new ApiKeyCredential(key))
|
57 | 51 | .AsEmbeddingGenerator(modelId: model);
|
58 | 52 |
|
59 |
| -// Create and populate the vector store |
| 53 | +// Create and populate the vector store. |
60 | 54 | var vectorStore = new InMemoryVectorStore();
|
61 |
| -var cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices"); |
| 55 | +IVectorStoreRecordCollection<int, CloudService> cloudServicesStore = vectorStore.GetCollection<int, CloudService>("cloudServices"); |
62 | 56 | await cloudServicesStore.CreateCollectionIfNotExistsAsync();
|
63 | 57 |
|
64 |
| -foreach (var service in cloudServices) |
| 58 | +foreach (CloudService service in cloudServices) |
65 | 59 | {
|
66 | 60 | service.Vector = await generator.GenerateEmbeddingVectorAsync(service.Description);
|
67 | 61 | await cloudServicesStore.UpsertAsync(service);
|
68 | 62 | }
|
69 | 63 |
|
70 |
| -// Convert a search query to a vector and search the vector store |
71 |
| -var query = "Which Azure service should I use to store my Word documents?"; |
72 |
| -var queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query); |
| 64 | +// Convert a search query to a vector and search the vector store. |
| 65 | +string query = "Which Azure service should I use to store my Word documents?"; |
| 66 | +ReadOnlyMemory<float> queryEmbedding = await generator.GenerateEmbeddingVectorAsync(query); |
73 | 67 |
|
74 |
| -var results = await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions() |
| 68 | +VectorSearchResults<CloudService> results = |
| 69 | + await cloudServicesStore.VectorizedSearchAsync(queryEmbedding, new VectorSearchOptions<CloudService>() |
75 | 70 | {
|
76 |
| - Top = 1, |
77 |
| - VectorPropertyName = "Vector" |
| 71 | + Top = 1 |
78 | 72 | });
|
79 | 73 |
|
80 |
| -await foreach (var result in results.Results) |
| 74 | +await foreach (VectorSearchResult<CloudService> result in results.Results) |
81 | 75 | {
|
82 | 76 | Console.WriteLine($"Name: {result.Record.Name}");
|
83 | 77 | Console.WriteLine($"Description: {result.Record.Description}");
|
|
0 commit comments