-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
33478ca
commit aee812a
Showing
12 changed files
with
313 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Text; | ||
using ChatRPG.Data.Models; | ||
using LangChain.Chains.StackableChains.Agents.Tools; | ||
using LangChain.Databases.Postgres; | ||
using LangChain.Providers; | ||
using LangChain.Providers.OpenAI; | ||
using LangChain.Providers.OpenAI.Predefined; | ||
using static LangChain.Chains.Chain; | ||
|
||
namespace ChatRPG.API.Tools; | ||
|
||
public class SearchScenarioTool( | ||
IConfiguration configuration, | ||
Campaign campaign, | ||
string instruction, | ||
string name, | ||
string? description = null) : AgentTool(name, description) | ||
{ | ||
public override async Task<string> ToolTask(string input, CancellationToken token = new CancellationToken()) | ||
{ | ||
var provider = new OpenAiProvider(configuration.GetSection("ApiKeys").GetValue<string>("OpenAI")!); | ||
var embeddingModel = new TextEmbeddingV3SmallModel(provider); | ||
var llm = new Gpt4OmniModel(provider) | ||
{ | ||
Settings = new OpenAiChatSettings() { UseStreaming = false, Temperature = 0.1 } | ||
}; | ||
|
||
var vectorDatabase = | ||
new PostgresVectorDatabase(configuration.GetSection("ConnectionStrings") | ||
.GetValue<string>("DefaultConnection")!, configuration.GetValue<string>("VectorDatabaseTable")!); | ||
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 chain = Set(input, "input") | ||
| RetrieveSimilarDocuments(vectorCollection, embeddingModel, amount: 20) | ||
| CombineDocuments(outputKey: "context") | ||
| Template(prompt.ToString()) | ||
| LLM(llm.UseConsoleForDebug()); // TODO: Remove debug mode | ||
|
||
var response = await chain.RunAsync("text", cancellationToken: token); | ||
|
||
return response ?? "System: I'm sorry, I couldn't find any relevant scenarios."; | ||
} | ||
} |
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
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
Oops, something went wrong.