Skip to content

Commit

Permalink
SearchScenarioTool prompts (no tool description yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
KarmaKamikaze committed Mar 6, 2025
1 parent aee812a commit 756d4ed
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:

steps:
- uses: actions/checkout@v4
- name: Setup .NET 7.0
- name: Setup .NET 9.0
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0
dotnet-version: 9.0
- name: Install dependencies
run: dotnet restore
working-directory: ChatRPG/
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
pull_request:
branches: [master, dev]

permissions: {}

jobs:
build:
name: Lint Code Base
Expand All @@ -18,14 +20,14 @@ jobs:

steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
# Full git history is needed to get a proper
# list of changed files within `super-linter`
fetch-depth: 0

- name: Lint Code Base
uses: super-linter/super-linter@v5
uses: super-linter/super-linter@v7.3.0
env:
VALIDATE_ALL_CODEBASE: false
VALIDATE_CSS: false
Expand Down
2 changes: 1 addition & 1 deletion ChatRPG/API/ReActLlmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private List<AgentTool> CreateTools(Campaign campaign)
"most once.");
tools.Add(battleTool);

var searchScenarioTool = new SearchScenarioTool(_configuration, campaign, "TODO: Narrator-specific instruction", "searchscenariotool", "");
var searchScenarioTool = new SearchScenarioTool(_configuration, campaign, "searchscenariotool", "");
tools.Add(searchScenarioTool);

return tools;
Expand Down
10 changes: 6 additions & 4 deletions ChatRPG/API/Tools/SearchScenarioTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace ChatRPG.API.Tools;
public class SearchScenarioTool(
IConfiguration configuration,
Campaign campaign,
string instruction,
string name,
string? description = null) : AgentTool(name, description)
{
private readonly bool _shouldIncludePreviousMessages = configuration.GetValue<bool>("ShouldSummarize");

public override async Task<string> ToolTask(string input, CancellationToken token = new CancellationToken())
{
var provider = new OpenAiProvider(configuration.GetSection("ApiKeys").GetValue<string>("OpenAI")!);
Expand All @@ -31,11 +32,12 @@ public class SearchScenarioTool(
var vectorCollection = await vectorDatabase.GetCollectionAsync("collection-" + campaign.Id, token);

var prompt = new StringBuilder();
prompt.Append(configuration.GetSection("SystemPrompts").GetValue<string>("SearchScenario")!
.Replace("{instruction}", instruction));
var summary = ToolUtilities.ConstructSummary(campaign, _shouldIncludePreviousMessages);
prompt.Append(configuration.GetSection("SystemPrompts").GetValue<string>("SearchScenario"))!.Replace(
"{summary}", summary);

var chain = Set(input, "input")
| RetrieveSimilarDocuments(vectorCollection, embeddingModel, amount: 20)
| RetrieveSimilarDocuments(vectorCollection, embeddingModel, inputKey: "input", amount: 20)
| CombineDocuments(outputKey: "context")
| Template(prompt.ToString())
| LLM(llm.UseConsoleForDebug()); // TODO: Remove debug mode
Expand Down
32 changes: 20 additions & 12 deletions ChatRPG/API/Tools/ToolUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,7 @@ public class ToolUtilities(IConfiguration configuration)
query.Append(configuration.GetSection("SystemPrompts").GetValue<string>("FindCharacter")!
.Replace("{instruction}", instruction));

query.Append($"\n\nThe story up until now: {campaign.GameSummary}");

if (_shouldIncludePreviousMessages)
{
var content = campaign.Messages.TakeLast(IncludedPreviousMessages).Select(m => m.Content);
query.Append(
"\n\nUse these previous messages as context. They only serve to give a hint of the current scenario:");
foreach (var message in content)
{
query.Append($"\n {message}");
}
}
query.Append(ConstructSummary(campaign, _shouldIncludePreviousMessages));

query.Append("\n\nHere is the list of all characters present in the story:\n\n{\"characters\": [");

Expand Down Expand Up @@ -102,4 +91,23 @@ public static string RemoveMarkdown(string text)

return text;
}

public static string ConstructSummary(Campaign campaign, bool shouldIncludePreviousMessages)
{
var result = $"\n\nThe story up until now: {campaign.GameSummary}";

if (shouldIncludePreviousMessages)
{
var content = campaign.Messages.TakeLast(IncludedPreviousMessages).Select(m => m.Content);
result += "\n\nUse these previous messages as context. They only serve to give a hint of the current scenario:";
foreach (var message in content)
{
result += $"\n {message}";
}
}

return result;
}


}
6 changes: 2 additions & 4 deletions ChatRPG/Services/ScenarioDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public ScenarioDocumentService(IConfiguration configuration)
ArgumentException.ThrowIfNullOrEmpty(configuration.GetSection("ConnectionStrings")
.GetValue<string>("DefaultConnection"));
ArgumentException.ThrowIfNullOrEmpty(configuration.GetSection("ApiKeys").GetValue<string>("OpenAI"));
ArgumentException.ThrowIfNullOrEmpty(configuration.GetSection("SystemPrompts").GetValue<string>("StartingScenario"));
ArgumentException.ThrowIfNullOrEmpty(configuration.GetSection("SystemPrompts")
.GetValue<string>("StartingScenario"));
_connectionString = configuration.GetSection("ConnectionStrings")
.GetValue<string>("DefaultConnection")!;
_openAiKey = configuration.GetSection("ApiKeys").GetValue<string>("OpenAI")!;
Expand All @@ -48,7 +49,6 @@ public async Task StoreScenarioEmbedding(int campaignId, byte[] scenarioDocument

public async Task<string> GenerateStartingScenario(int campaignId)
{

var provider = new OpenAiProvider(_openAiKey);
var embeddingModel = new TextEmbeddingV3SmallModel(provider);
var llm = new Gpt4OmniModel(provider)
Expand All @@ -72,7 +72,5 @@ public async Task<string> GenerateStartingScenario(int campaignId)
var response = await chain.RunAsync("text");

return response ?? "System: I'm sorry, I couldn't find any relevant scenarios.";

}

}
Loading

0 comments on commit 756d4ed

Please sign in to comment.