-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
103 changed files
with
268 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions
45
samples/007-using-azure-cognitive-search/007-using-azure-cognitive-search.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Search.Documents" Version="11.5.0-beta.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\dotnet\CoreLib\CoreLib.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="4.6.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.3"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.6.40"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Roslynator.CodeAnalysis.Analyzers" Version="4.4.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Roslynator.Formatting.Analyzers" Version="4.4.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
// ReSharper disable InconsistentNaming | ||
|
||
/* launchSettings.json | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"DeleteVectors": { | ||
"commandName": "Project", | ||
"environmentVariables": { | ||
"SEARCH_ENDPOINT": "...", | ||
"SEARCH_KEY": "..." | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
using Azure; | ||
using Azure.Search.Documents; | ||
using Azure.Search.Documents.Indexes; | ||
using Azure.Search.Documents.Indexes.Models; | ||
using Azure.Search.Documents.Models; | ||
using Microsoft.SemanticKernel.AI.Embeddings; | ||
using Microsoft.SemanticMemory.Client.Models; | ||
|
||
public class Program | ||
{ | ||
// Azure Search Index name | ||
private const string IndexName = "test01"; | ||
|
||
// A Memory ID example. This value is later serialized. | ||
private const string ExternalRecordId = "usr=user2//ppl=f05//prt=7b9bad8968804121bb9b1264104608ac"; | ||
|
||
// Size of the vectors | ||
private const int EmbeddingSize = 3; | ||
|
||
private static SearchIndexClient adminClient = null!; | ||
private static readonly string Endpoint = Environment.GetEnvironmentVariable("SEARCH_ENDPOINT")!; | ||
private static readonly string APIKey = Environment.GetEnvironmentVariable("SEARCH_KEY")!; | ||
|
||
public static async Task Main(string[] args) | ||
{ | ||
// Azure Cognitive Search service client | ||
adminClient = new SearchIndexClient(new Uri(Endpoint), new AzureKeyCredential(APIKey), | ||
new SearchClientOptions { Diagnostics = { IsTelemetryEnabled = true, ApplicationId = "SemanticMemory" } }); | ||
|
||
// Create an index (if doesn't exist) | ||
await CreateIndexAsync(IndexName); | ||
|
||
// Insert a record | ||
var recordId = await InsertRecordAsync(IndexName, | ||
ExternalRecordId, | ||
new Dictionary<string, object> { { "text", "a b c" } }, | ||
new TagCollection(), | ||
new Embedding<float>(new[] { 0f, 0.5f, 1 })); | ||
|
||
// Delete the record | ||
await DeleteRecordAsync(IndexName, | ||
recordId, | ||
ExternalRecordId, | ||
new Dictionary<string, object> { { "text", "a b c" } }, | ||
new TagCollection(), | ||
new Embedding<float>(new[] { 0f, 0.5f, 1 })); | ||
} | ||
|
||
// =============================================================================================== | ||
private static async Task CreateIndexAsync(string name) | ||
{ | ||
Console.WriteLine("\n== CREATE INDEX ==\n"); | ||
|
||
const string VectorSearchConfigName = "SemanticMemoryDefaultCosine"; | ||
|
||
var indexSchema = new SearchIndex(name) | ||
{ | ||
Fields = new List<SearchField>(), | ||
VectorSearch = new VectorSearch | ||
{ | ||
AlgorithmConfigurations = | ||
{ | ||
new HnswVectorSearchAlgorithmConfiguration(VectorSearchConfigName) | ||
{ | ||
Parameters = new HnswParameters { Metric = VectorSearchAlgorithmMetric.Cosine } | ||
} | ||
} | ||
} | ||
}; | ||
|
||
indexSchema.Fields.Add(new SearchField("id", SearchFieldDataType.String) | ||
{ | ||
IsKey = true, | ||
IsFilterable = true, | ||
IsFacetable = false, | ||
IsSortable = false, | ||
IsSearchable = true, | ||
}); | ||
|
||
indexSchema.Fields.Add(new SimpleField("tags", SearchFieldDataType.Collection(SearchFieldDataType.String)) | ||
{ | ||
IsKey = false, | ||
IsFilterable = true, | ||
IsFacetable = false, | ||
IsSortable = false, | ||
}); | ||
|
||
indexSchema.Fields.Add(new SearchField("metadata", SearchFieldDataType.String) | ||
{ | ||
IsKey = false, | ||
IsFilterable = true, | ||
IsFacetable = false, | ||
IsSortable = false, | ||
IsSearchable = true, | ||
}); | ||
|
||
indexSchema.Fields.Add(new SearchField("embedding", SearchFieldDataType.Collection(SearchFieldDataType.Single)) | ||
{ | ||
IsKey = false, | ||
IsFilterable = false, | ||
IsSearchable = true, | ||
IsFacetable = false, | ||
IsSortable = false, | ||
VectorSearchDimensions = EmbeddingSize, | ||
VectorSearchConfiguration = VectorSearchConfigName, | ||
}); | ||
|
||
try | ||
{ | ||
await adminClient.CreateIndexAsync(indexSchema); | ||
} | ||
catch (RequestFailedException e) when (e.Message.Contains("already exists")) | ||
{ | ||
Console.WriteLine("Index already exists"); | ||
} | ||
} | ||
|
||
// =============================================================================================== | ||
private static async Task<string> InsertRecordAsync(string indexName, | ||
string externalId, Dictionary<string, object> metadata, TagCollection tags, Embedding<float> embedding) | ||
{ | ||
Console.WriteLine("\n== INSERT ==\n"); | ||
var client = adminClient.GetSearchClient(indexName); | ||
|
||
var record = new Microsoft.SemanticMemory.Core.MemoryStorage.MemoryRecord | ||
{ | ||
Id = externalId, | ||
Vector = embedding, | ||
Owner = "userAB", | ||
Tags = tags, | ||
Metadata = metadata | ||
}; | ||
|
||
Microsoft.SemanticMemory.Core.MemoryStorage.AzureCognitiveSearch.AzureCognitiveSearchMemoryRecord localRecord = Microsoft.SemanticMemory.Core.MemoryStorage.AzureCognitiveSearch.AzureCognitiveSearchMemoryRecord.FromMemoryRecord(record); | ||
|
||
Console.WriteLine($"CREATING {localRecord.Id}\n"); | ||
|
||
var response = await client.IndexDocumentsAsync( | ||
IndexDocumentsBatch.Upload(new[] { localRecord }), | ||
new IndexDocumentsOptions { ThrowOnAnyError = true }); | ||
|
||
Console.WriteLine("Status: " + response.Value.Results.FirstOrDefault()?.Status); | ||
Console.WriteLine("Key: " + response.Value.Results.FirstOrDefault()?.Key); | ||
Console.WriteLine("Succeeded: " + (response.Value.Results.FirstOrDefault()?.Succeeded ?? false ? "true" : "false")); | ||
Console.WriteLine("ErrorMessage: " + response.Value.Results.FirstOrDefault()?.ErrorMessage); | ||
Console.WriteLine("Status: " + response.GetRawResponse().Status); | ||
Console.WriteLine("Content: " + response.GetRawResponse().Content); | ||
|
||
return response.Value.Results.FirstOrDefault()?.Key ?? string.Empty; | ||
} | ||
|
||
// =============================================================================================== | ||
private static async Task DeleteRecordAsync(string indexName, | ||
string recordId, string externalId, Dictionary<string, object> metadata, TagCollection tags, Embedding<float> embedding) | ||
{ | ||
Console.WriteLine("\n== DELETE ==\n"); | ||
|
||
var client = adminClient.GetSearchClient(indexName); | ||
|
||
Console.WriteLine($"DELETING {recordId}\n"); | ||
|
||
Response<IndexDocumentsResult>? response = await client.DeleteDocumentsAsync("id", new List<string> { recordId }); | ||
|
||
Console.WriteLine("Status: " + response.Value.Results.FirstOrDefault()?.Status); | ||
Console.WriteLine("Key: " + response.Value.Results.FirstOrDefault()?.Key); | ||
Console.WriteLine("Succeeded: " + (response.Value.Results.FirstOrDefault()?.Succeeded ?? false ? "true" : "false")); | ||
Console.WriteLine("ErrorMessage: " + response.Value.Results.FirstOrDefault()?.ErrorMessage); | ||
Console.WriteLine("Status: " + response.GetRawResponse().Status); | ||
Console.WriteLine("Content: " + response.GetRawResponse().Content); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.