Skip to content
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

Implement Response Streaming #726

Merged
merged 35 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f082f5d
added ask_stream endpoint
carloMobilesoft Aug 1, 2024
274b3f8
added streaming client
carlodek Aug 1, 2024
3edabdb
added qdrant
carlodek Aug 7, 2024
02d1e9e
Merge branch 'main' into stream_response
carlodek Aug 7, 2024
488b8d5
docker for us
carlodek Aug 9, 2024
5c54916
added gitignore for AzureOpenAI
carlodek Oct 28, 2024
6816b36
Merge branch 'main' into feature/docker
carlodek Oct 29, 2024
01f1fdc
custom notFound response implemented correctly
carlodek Oct 29, 2024
dcb54ba
added response if no memory is found
carlodek Nov 4, 2024
5b926ce
content moderation
carlodek Nov 6, 2024
f07dbe1
Merge branch 'main' of github.com:microsoft/kernel-memory into featur…
carlodek Nov 11, 2024
202cecf
removed circular import
carlodek Nov 11, 2024
947a4fe
Merge branch 'main' into feature/docker
carlodek Nov 28, 2024
7158bb8
changed docker file
carlodek Nov 28, 2024
257e101
Merge branch 'feature/docker' into stream_response
carlodek Nov 28, 2024
f9d9eaa
code cleaning
carlodek Nov 28, 2024
1afbb85
cleaned code for build automation
carlodek Nov 28, 2024
b5978f7
Update .dockerignore
dluc Nov 29, 2024
e727099
Update .gitignore
dluc Nov 29, 2024
4523be1
Update Directory.Packages.props
dluc Nov 29, 2024
008b50e
Update Dockerfile
dluc Nov 29, 2024
3d7118e
Update Dockerfile
dluc Nov 29, 2024
f9eeb51
Update Dockerfile
dluc Nov 29, 2024
9079d28
Update service/Core/Core.csproj
dluc Nov 29, 2024
11ce13c
Update service/Core/Search/SearchClient.cs
dluc Nov 29, 2024
4ce9571
Update service/Core/Search/SearchClient.cs
dluc Nov 29, 2024
2656f2e
Update service/Service/Program.cs
dluc Nov 29, 2024
dfc1ebc
Update service/Service/Program.cs
dluc Nov 29, 2024
a434136
Update service/Service/Program.cs
dluc Nov 29, 2024
b2485d8
Update service/Core/Search/SearchClient.cs
dluc Nov 29, 2024
f16e9b3
Update service/Service.AspNetCore/WebAPIEndpoints.cs
dluc Nov 29, 2024
44a12bf
Update SearchClient.cs
carlodek Nov 29, 2024
987599a
Update Service.csproj
carlodek Nov 29, 2024
92c0d33
Undo changes
dluc Nov 29, 2024
a4a4bc7
Refactoring
dluc Dec 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions clients/dotnet/WebClient/MemoryWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.Internals;

Expand Down Expand Up @@ -371,6 +372,60 @@ public async Task<MemoryAnswer> AskAsync(
return JsonSerializer.Deserialize<MemoryAnswer>(json, s_caseInsensitiveJsonOptions) ?? new MemoryAnswer();
}

/// <inheritdoc />
public async IAsyncEnumerable<MemoryAnswer> AskAsyncChunk(
string question,
string? index = null,
MemoryFilter? filter = null,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (filter != null)
{
if (filters == null) { filters = new List<MemoryFilter>(); }

filters.Add(filter);
}

MemoryQuery request = new()
{
Index = index,
Question = question,
Filters = (filters is { Count: > 0 }) ? filters.ToList() : new(),
MinRelevance = minRelevance,
ContextArguments = (context?.Arguments ?? new Dictionary<string, object?>()).ToDictionary(),
};
using StringContent content = new(JsonSerializer.Serialize(request), Encoding.UTF8, "application/json");

var url = Constants.HttpAskChunkEndpoint.CleanUrlPath();
HttpResponseMessage response = await this._client.PostAsync(url, content, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
var eosToken = context.GetCustomEosTokenOrDefault("#DONE#");
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string? line;
while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null)
{
if (line.StartsWith("data:", StringComparison.Ordinal))
{
var jsonData = line.Substring(6);
var chunk = JsonSerializer.Deserialize<MemoryAnswer>(jsonData);
if (chunk != null)
{
yield return chunk;
if (chunk.Result == eosToken)
{
break;
}
}
}
}
}
}

#region private

private static (string contentType, long contentLength, DateTimeOffset lastModified) GetFileDetails(HttpResponseMessage response)
Expand Down
5 changes: 5 additions & 0 deletions service/Abstractions/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public static class TextGeneration
public static class Rag

{
// Used to override EosToken config for streaming
public const string EosToken = "custom_rag_eos_token_str";

// Used to override No Answer config
public const string EmptyAnswer = "custom_rag_empty_answer_str";

Expand Down Expand Up @@ -128,6 +131,8 @@ public static class Summary
public const string ReservedPayloadVectorGeneratorField = "vector_generator";

// Endpoints

public const string HttpAskChunkEndpoint = "/ask_stream";
public const string HttpAskEndpoint = "/ask";
public const string HttpSearchEndpoint = "/search";
public const string HttpDownloadEndpoint = "/download";
Expand Down
10 changes: 10 additions & 0 deletions service/Abstractions/Context/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public static string GetCustomEmptyAnswerTextOrDefault(this IContext? context, s
return defaultValue;
}

public static string GetCustomEosTokenOrDefault(this IContext? context, string defaultValue)
{
if (context.TryGetArg<string>(Constants.CustomContext.Rag.EosToken, out var customValue))
{
return customValue;
}

return defaultValue;
}

public static string GetCustomRagFactTemplateOrDefault(this IContext? context, string defaultValue)
{
if (context.TryGetArg<string>(Constants.CustomContext.Rag.FactTemplate, out var customValue))
Expand Down
9 changes: 9 additions & 0 deletions service/Abstractions/IKernelMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,13 @@ public Task<MemoryAnswer> AskAsync(
double minRelevance = 0,
IContext? context = null,
CancellationToken cancellationToken = default);

public IAsyncEnumerable<MemoryAnswer> AskAsyncChunk(
string question,
string? index = null,
MemoryFilter? filter = null,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
CancellationToken cancellationToken = default);
}
8 changes: 8 additions & 0 deletions service/Abstractions/Search/ISearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Task<MemoryAnswer> AskAsync(
IContext? context = null,
CancellationToken cancellationToken = default);

IAsyncEnumerable<MemoryAnswer> AskAsyncChunk(
string index,
string question,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
CancellationToken cancellationToken = default);

/// <summary>
/// List the available memory indexes (aka collections).
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions service/Core/MemoryServerless.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,30 @@ public Task<MemoryAnswer> AskAsync(
context: context,
cancellationToken: cancellationToken);
}

public IAsyncEnumerable<MemoryAnswer> AskAsyncChunk(
string question,
string? index = null,
MemoryFilter? filter = null,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
CancellationToken cancellationToken = default)
{
if (filter != null)
{
if (filters == null) { filters = new List<MemoryFilter>(); }

filters.Add(filter);
}

index = IndexName.CleanName(index, this._defaultIndexName);
return this._searchClient.AskAsyncChunk(
index: index,
question: question,
filters: filters,
minRelevance: minRelevance,
context: context,
cancellationToken: cancellationToken);
}
}
29 changes: 29 additions & 0 deletions service/Core/MemoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,33 @@ public Task<MemoryAnswer> AskAsync(
context: context,
cancellationToken: cancellationToken);
}

public IAsyncEnumerable<MemoryAnswer> AskAsyncChunk(
string question,
string? index = null,
MemoryFilter? filter = null,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
CancellationToken cancellationToken = default)
{
if (filter != null)
{
if (filters == null)
{
filters = new List<MemoryFilter>();
}

filters.Add(filter);
}

index = IndexName.CleanName(index, this._defaultIndexName);
return this._searchClient.AskAsyncChunk(
index: index,
question: question,
filters: filters,
minRelevance: minRelevance,
context: context,
cancellationToken: cancellationToken);
}
}
24 changes: 15 additions & 9 deletions service/Core/Search/AnswerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,41 @@ internal async Task<MemoryAnswer> GenerateAnswerAsync(
this._log.LogTrace("Answer generated in {0} msecs", watch.ElapsedMilliseconds);
}

// Validate the LLM output
return await this.ModeratedAnswerAsync(result.AskResult, cancellationToken).ConfigureAwait(false);
}

internal async Task<MemoryAnswer> ModeratedAnswerAsync(MemoryAnswer currentResult, CancellationToken cancellationToken)
{
if (this._contentModeration != null && this._config.UseContentModeration)
{
var isSafe = await this._contentModeration.IsSafeAsync(result.AskResult.Result, cancellationToken).ConfigureAwait(false);
var isSafe = await this._contentModeration.IsSafeAsync(currentResult.Result, cancellationToken).ConfigureAwait(false);
if (!isSafe)
{
this._log.LogWarning("Unsafe answer detected. Returning error message instead.");
this._log.LogSensitive("Unsafe answer: {0}", result.AskResult.Result);
result.AskResult.NoResultReason = "Content moderation failure";
result.AskResult.Result = this._config.ModeratedAnswer;
this._log.LogSensitive("Unsafe answer: {0}", currentResult.Result);
currentResult.NoResultReason = "Content moderation failure";
currentResult.Result = this._config.ModeratedAnswer;
currentResult.NoResult = true;
}
}

return result.AskResult;
return currentResult;
}

private IAsyncEnumerable<string> GenerateAnswerTokensAsync(string question, string facts, IContext? context, CancellationToken cancellationToken)
internal IAsyncEnumerable<string> GenerateAnswerTokensAsync(string question, string facts, IContext? context, CancellationToken cancellationToken)
{
string prompt = context.GetCustomRagPromptOrDefault(this._answerPrompt);
int maxTokens = context.GetCustomRagMaxTokensOrDefault(this._config.AnswerTokens);
double temperature = context.GetCustomRagTemperatureOrDefault(this._config.Temperature);
double nucleusSampling = context.GetCustomRagNucleusSamplingOrDefault(this._config.TopP);

string emptyAnswer = context.GetCustomEmptyAnswerTextOrDefault(this._config.EmptyAnswer);
prompt = prompt.Replace("{{$facts}}", facts.Trim(), StringComparison.OrdinalIgnoreCase);

question = question.Trim();
question = question.EndsWith('?') ? question : $"{question}?";
prompt = prompt.Replace("{{$input}}", question, StringComparison.OrdinalIgnoreCase);
prompt = prompt.Replace("{{$notFound}}", this._config.EmptyAnswer, StringComparison.OrdinalIgnoreCase);
prompt = prompt.Replace("{{$notFound}}", emptyAnswer, StringComparison.OrdinalIgnoreCase);
this._log.LogInformation("New prompt: {0}", prompt);

var options = new TextGenerationOptions
{
Expand Down
123 changes: 122 additions & 1 deletion service/Core/Search/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -174,6 +176,125 @@ public async Task<MemoryAnswer> AskAsync(
return await this._answerGenerator.GenerateAnswerAsync(question, result, context, cancellationToken).ConfigureAwait(false);
}

public async IAsyncEnumerable<MemoryAnswer> AskAsyncChunk(
string index,
string question,
ICollection<MemoryFilter>? filters = null,
double minRelevance = 0,
IContext? context = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
this._log.LogInformation("question: '{0}'", question);
string emptyAnswer = context.GetCustomEmptyAnswerTextOrDefault(this._config.EmptyAnswer);
string eosToken = context.GetCustomEosTokenOrDefault("#DONE#");
string answerPrompt = context.GetCustomRagPromptOrDefault(this._answerPrompt);
int limit = context.GetCustomRagMaxMatchesCountOrDefault(this._config.MaxMatchesCount);
var maxTokens = this._config.MaxAskPromptSize > 0
? this._config.MaxAskPromptSize
: this._textGenerator.MaxTokenTotal;
SearchClientResult result = SearchClientResult.AskResultInstance(
question: question,
emptyAnswer: emptyAnswer,
maxGroundingFacts: limit,
tokensAvailable: maxTokens
- this._textGenerator.CountTokens(answerPrompt)
- this._textGenerator.CountTokens(question)
- this._config.AnswerTokens
);
if (string.IsNullOrEmpty(question))
{
this._log.LogWarning("No question provided");
yield return result.AskResult;
result.AskResult.Result = eosToken;
this._log.LogInformation("Eos token: '{0}", result.AskResult.Result);
yield return result.AskResult;
yield break;
}

this._log.LogTrace("Fetching relevant memories");
IAsyncEnumerable<(MemoryRecord, double)> matches = this._memoryDb.GetSimilarListAsync(
index: index,
text: question,
filters: filters,
minRelevance: minRelevance,
limit: this._config.MaxMatchesCount,
withEmbeddings: false,
cancellationToken: cancellationToken);

string factTemplate = context.GetCustomRagFactTemplateOrDefault(this._config.FactTemplate);
if (!factTemplate.EndsWith('\n')) { factTemplate += "\n"; }

// Memories are sorted by relevance, starting from the most relevant
await foreach ((MemoryRecord memoryRecord, double recordRelevance) in matches.ConfigureAwait(false))
{
result.State = SearchState.Continue;
result = this.ProcessMemoryRecord(result, index, memoryRecord, recordRelevance, factTemplate);

if (result.State == SearchState.SkipRecord) { continue; }

if (result.State == SearchState.Stop) { break; }
}

if (result.FactsAvailableCount > 0 && result.FactsUsedCount == 0)
{
this._log.LogError("Unable to inject memories in the prompt, not enough tokens available");
result.AskResult.NoResultReason = "Unable to use memories";
yield return result.AskResult;
yield break;
}

if (result.FactsUsedCount == 0)
{
this._log.LogWarning("No memories available");
result.AskResult.NoResultReason = "No memories available";
yield return result.AskResult;
yield break;
}

this._log.LogTrace("{Count} records processed", result.RecordCount);
var charsGenerated = 0;
var wholeText = new StringBuilder();
await foreach (var x in this._answerGenerator.GenerateAnswerTokensAsync(question, result.Facts.ToString(), context, cancellationToken).ConfigureAwait(true))
carlodek marked this conversation as resolved.
Show resolved Hide resolved
{
var text = new StringBuilder();
text.Append(x);
wholeText.Append(x);
if (this._log.IsEnabled(LogLevel.Trace) && text.Length - charsGenerated >= 30)
{
charsGenerated = text.Length;
this._log.LogTrace("{0} chars generated", charsGenerated);
}

var newAnswer = new MemoryAnswer
{
Question = question,
NoResult = false,
Result = text.ToString()
};
this._log.LogInformation("Chunk: '{0}", newAnswer.Result);
yield return newAnswer;
}

var current = new MemoryAnswer
{
Question = question,
NoResult = false,
Result = wholeText.ToString(),
};
MemoryAnswer moderatedResult = await this._answerGenerator.ModeratedAnswerAsync(current, cancellationToken).ConfigureAwait(false);
result.AskResult.Result = moderatedResult.Result;
result.AskResult.NoResult = moderatedResult.NoResult;
result.AskResult.NoResultReason = moderatedResult.NoResultReason;
if (result.AskResult.NoResultReason == "Content moderation failure")
{
yield return result.AskResult;
}

result.AskResult.Result = eosToken;
this._log.LogInformation("Eos token: '{0}", result.AskResult.Result);
yield return result.AskResult;
}

/// <summary>
/// Process memory records for ASK and SEARCH calls
/// </summary>
Expand Down Expand Up @@ -202,7 +323,7 @@ private SearchClientResult ProcessMemoryRecord(
// Identify the file in case there are multiple files
string fileId = record.GetFileId(this._log);

// Note: this is not a URL and perhaps could be dropped. For now it acts as a unique identifier. See also SourceUrl.
// Note: this is not a URL and perhaps could be dropped. For now, it acts as a unique identifier. See also SourceUrl.
dluc marked this conversation as resolved.
Show resolved Hide resolved
string linkToFile = $"{index}/{documentId}/{fileId}";

// Note: this is "content.url" when importing web pages
Expand Down
Loading
Loading