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

Add ability to control MaxMatchesCount for Ask via IContext #860

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions service/Abstractions/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public static class Rag
// Used to override the max tokens to generate when using the RAG prompt
public const string MaxTokens = "custom_rag_max_tokens_int";

// Used to override the max matches count used with the RAG prompt
public const string MaxMatchesCount = "custom_rag_max_matches_count_int";

// Used to override the temperature (default 0) used with the RAG prompt
public const string Temperature = "custom_rag_temperature_float";

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 @@ -130,6 +130,16 @@ public static int GetCustomRagMaxTokensOrDefault(this IContext? context, int def
return defaultValue;
}

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

return defaultValue;
}

public static double GetCustomRagTemperatureOrDefault(this IContext? context, double defaultValue)
{
if (context.TryGetArg<double>(Constants.CustomContext.Rag.Temperature, out var customValue))
Expand Down
4 changes: 3 additions & 1 deletion service/Core/Search/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ public async Task<MemoryAnswer> AskAsync(
string emptyAnswer = context.GetCustomEmptyAnswerTextOrDefault(this._config.EmptyAnswer);
string answerPrompt = context.GetCustomRagPromptOrDefault(this._answerPrompt);
string factTemplate = context.GetCustomRagFactTemplateOrDefault(this._config.FactTemplate);
int limit = context.GetCustomRagMaxMatchesCountOrDefault(this._config.MaxMatchesCount);

if (!factTemplate.EndsWith('\n')) { factTemplate += "\n"; }

var noAnswerFound = new MemoryAnswer
Expand Down Expand Up @@ -234,7 +236,7 @@ public async Task<MemoryAnswer> AskAsync(
text: question,
filters: filters,
minRelevance: minRelevance,
limit: this._config.MaxMatchesCount,
limit: limit,
withEmbeddings: false,
cancellationToken: cancellationToken);

Expand Down
Loading