-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net: added readme #6390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
.Net: added readme #6390
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
302caff
added readme
yorek 2d5572f
Update dotnet/src/Connectors/Connectors.Memory.SqlServer/README.md
yorek 72f772f
Update dotnet/src/Connectors/Connectors.Memory.SqlServer/README.md
yorek a65a51c
Update dotnet/src/Connectors/Connectors.Memory.SqlServer/README.md
yorek b94562d
Update dotnet/src/Connectors/Connectors.Memory.SqlServer/README.md
yorek ec1d730
Update dotnet/src/Connectors/Connectors.Memory.SqlServer/README.md
markwallace-microsoft ed366eb
Merge branch 'main' into sql-server-readme
markwallace-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
dotnet/src/Connectors/Connectors.Memory.SqlServer/README.md
This file contains hidden or 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,118 @@ | ||
# Microsoft.SemanticKernel.Connectors.SqlServer | ||
|
||
This connector uses the SQL Server database engine to implement Semantic Memory. | ||
|
||
> [!IMPORTANT] | ||
> The features needed to use this connector are available in preview in Azure SQL only at the moment. Please take a look at the [Announcing EAP for Vector Support in Azure SQL Database](https://devblogs.microsoft.com/azure-sql/announcing-eap-native-vector-support-in-azure-sql-database/) for more information on how to enable the feature. | ||
|
||
## Quick start | ||
|
||
Create a new .NET console application: | ||
|
||
```bash | ||
dotnet new console --framework net8.0 -n MySemanticMemoryApp | ||
``` | ||
|
||
Add the Semantic Kernel packages needed to create a Chatbot: | ||
|
||
```bash | ||
dotnet add package Microsoft.SemanticKernel | ||
dotnet add package Microsoft.SemanticKernel.Connectors.OpenAI | ||
``` | ||
|
||
Add `Microsoft.SemanticKernel.Connectors.SqlServer` to give your Chatbot memories: | ||
|
||
```bash | ||
dotnet add package Microsoft.SemanticKernel.Connectors.SqlServer --prerelease | ||
``` | ||
|
||
Then you can use the following code to create a Chatbot with a memory that uses SQL Server: | ||
|
||
```csharp | ||
using System.Text; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.Connectors.OpenAI; | ||
using Microsoft.SemanticKernel.Connectors.SqlServer; | ||
using Microsoft.SemanticKernel.Memory; | ||
|
||
#pragma warning disable SKEXP0001, SKEXP0010, SKEXP0020 | ||
|
||
// Replace with your Azure OpenAI endpoint | ||
const string AzureOpenAIEndpoint = "https://.openai.azure.com/"; | ||
|
||
// Replace with your Azure OpenAI API key | ||
const string AzureOpenAIApiKey = ""; | ||
|
||
// Replace with your Azure OpenAI embedding deployment name | ||
const string EmbeddingModelDeploymentName = "embeddings"; | ||
|
||
// Replace with your Azure OpenAI chat completion deployment name | ||
const string ChatModelDeploymentName = "gpt-35"; | ||
|
||
// Complete with your Azure SQL connection string | ||
const string ConnectionString = "Data Source=.database.windows.net;Initial Catalog=;Authentication=Active Directory Default;Connection Timeout=30"; | ||
|
||
// Table where memories will be stored | ||
const string TableName = "ChatMemories"; | ||
|
||
|
||
var kernel = Kernel.CreateBuilder() | ||
.AddAzureOpenAIChatCompletion(ChatModelDeploymentName, AzureOpenAIEndpoint, AzureOpenAIApiKey) | ||
.Build(); | ||
|
||
var memory = new MemoryBuilder() | ||
.WithSqlServerMemoryStore(ConnectionString) | ||
.WithAzureOpenAITextEmbeddingGeneration(EmbeddingModelDeploymentName, AzureOpenAIEndpoint, AzureOpenAIApiKey) | ||
.Build(); | ||
|
||
await memory.SaveInformationAsync(TableName, "With the new connector Microsoft.SemanticKernel.Connectors.SqlServer it is possible to efficiently store and retrieve memories thanks to the newly added vector support", "semantic-kernel-mssql"); | ||
await memory.SaveInformationAsync(TableName, "At the moment Microsoft.SemanticKernel.Connectors.SqlServer can be used only with Azure SQL", "semantic-kernel-azuresql"); | ||
await memory.SaveInformationAsync(TableName, "Azure SQL support for vectors is in Early Adopter Preview.", "azuresql-vector-eap"); | ||
await memory.SaveInformationAsync(TableName, "Pizza is one of the favourite food in the world.", "pizza-favourite-food"); | ||
|
||
var ai = kernel.GetRequiredService<IChatCompletionService>(); | ||
var chat = new ChatHistory("You are an AI assistant that helps people find information."); | ||
var builder = new StringBuilder(); | ||
while (true) | ||
{ | ||
Console.Write("Question: "); | ||
var question = Console.ReadLine()!; | ||
|
||
Console.WriteLine("\nSearching information from the memory..."); | ||
builder.Clear(); | ||
await foreach (var result in memory.SearchAsync(TableName, question, limit: 3)) | ||
{ | ||
builder.AppendLine(result.Metadata.Text); | ||
} | ||
if (builder.Length != 0) { | ||
markwallace-microsoft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Console.WriteLine("\nFound information from the memory:"); | ||
Console.WriteLine(builder.ToString()); | ||
} | ||
|
||
Console.WriteLine("Answer: "); | ||
var contextToRemove = -1; | ||
if (builder.Length != 0) | ||
{ | ||
builder.Insert(0, "Here's some additional information: "); | ||
contextToRemove = chat.Count; | ||
chat.AddUserMessage(builder.ToString()); | ||
} | ||
|
||
chat.AddUserMessage(question); | ||
|
||
builder.Clear(); | ||
await foreach (var message in ai.GetStreamingChatMessageContentsAsync(chat)) | ||
{ | ||
Console.Write(message); | ||
builder.Append(message.Content); | ||
} | ||
Console.WriteLine(); | ||
chat.AddAssistantMessage(builder.ToString()); | ||
|
||
if (contextToRemove >= 0) | ||
chat.RemoveAt(contextToRemove); | ||
|
||
Console.WriteLine(); | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.