Skip to content

Commit 8e4ab0f

Browse files
committed
Create custom ReActAgentChain
1 parent 90707d1 commit 8e4ab0f

File tree

3 files changed

+171
-5
lines changed

3 files changed

+171
-5
lines changed

ChatRPG/API/Memory/ChatRPGSummarizer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ namespace ChatRPG.API.Memory;
77
public static class ChatRPGSummarizer
88
{
99
public const string SummaryPrompt = @"
10-
Progressively summarize the lines of conversation provided, adding onto the previous summary returning a new summary.
10+
Progressively summarize the interaction between the player and the GM. The player describes their actions in response to the game world, and the GM narrates the outcome, revealing the next part of the adventure. Return a new summary based on each exchange.
1111
1212
EXAMPLE
1313
Current summary:
14-
The human asks what the AI thinks of artificial intelligence.The AI thinks artificial intelligence is a force for good.
14+
The player enters the forest and cautiously looks around. The GM describes towering trees and a narrow path leading deeper into the woods. The player decides to follow the path, staying alert.
1515
1616
New lines of conversation:
17-
Human: Why do you think artificial intelligence is a force for good?
18-
AI: Because artificial intelligence will help humans reach their full potential.
17+
Player: I move carefully down the path, keeping an eye out for any hidden dangers.
18+
GM: As you continue, the air grows colder, and you hear rustling in the bushes ahead. Suddenly, a shadowy figure leaps out in front of you.
1919
2020
New summary:
21-
The human asks what the AI thinks of artificial intelligence. The AI thinks artificial intelligence is a force for good because it will help humans reach their full potential.
21+
The player enters the forest and follows a narrow path, staying alert. The GM introduces a shadowy figure that appears ahead after rustling is heard in the bushes.
2222
END OF EXAMPLE
2323
2424
Current summary:

ChatRPG/API/ReActAgentChain.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
using System.Globalization;
2+
using LangChain.Abstractions.Schema;
3+
using LangChain.Chains.HelperChains;
4+
using LangChain.Chains.StackableChains.Agents.Tools;
5+
using LangChain.Chains.StackableChains.ReAct;
6+
using LangChain.Memory;
7+
using LangChain.Providers;
8+
using LangChain.Schema;
9+
using static LangChain.Chains.Chain;
10+
11+
namespace ChatRPG.API;
12+
13+
public sealed class ReActAgentChain : BaseStackableChain
14+
{
15+
private StackChain? _chain;
16+
private bool _useCache;
17+
private Dictionary<string, AgentTool> _tools = new();
18+
private readonly IChatModel _model;
19+
private readonly string _reActPrompt;
20+
private readonly int _maxActions;
21+
private readonly BaseChatMemory _conversationSummaryMemory;
22+
private string _userInput = string.Empty;
23+
private const string ReActAnswer = "answer";
24+
private readonly bool _useStreaming;
25+
26+
public string DefaultPrompt = @"Assistant is a large language model trained by OpenAI.
27+
28+
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
29+
30+
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
31+
32+
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
33+
34+
TOOLS:
35+
------
36+
37+
Assistant has access to the following tools:
38+
39+
{tools}
40+
41+
To use a tool, please use the following format:
42+
43+
```
44+
Thought: Do I need to use a tool? Yes
45+
Action: the action to take, should be one of [{tool_names}]
46+
Action Input: the input to the action
47+
Observation: the result of the action
48+
```
49+
50+
When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format:
51+
52+
```
53+
Thought: Do I need to use a tool? No
54+
Final Answer: [your response here]
55+
```
56+
57+
Always add [END] after final answer
58+
59+
Begin!
60+
61+
Previous conversation history:
62+
{history}
63+
64+
New input: {input}";
65+
66+
public ReActAgentChain(
67+
IChatModel model,
68+
BaseChatMemory memory,
69+
string? reActPrompt = null,
70+
string inputKey = "input",
71+
string outputKey = "text",
72+
int maxActions = 10,
73+
bool useStreaming = true)
74+
{
75+
_model = model;
76+
_reActPrompt = reActPrompt ?? DefaultPrompt;
77+
_maxActions = maxActions;
78+
79+
InputKeys = [inputKey];
80+
OutputKeys = [outputKey];
81+
82+
_useStreaming = useStreaming;
83+
_conversationSummaryMemory = memory;
84+
}
85+
86+
private void InitializeChain()
87+
{
88+
var toolNames = string.Join(",", _tools.Select(x => x.Key));
89+
var tools = string.Join("\n", _tools.Select(x => $"{x.Value.Name}, {x.Value.Description}"));
90+
91+
var chain =
92+
Set(() => _userInput, "input")
93+
| Set(tools, "tools")
94+
| Set(toolNames, "tool_names")
95+
| LoadMemory(_conversationSummaryMemory, outputKey: "history")
96+
| Template(_reActPrompt)
97+
| LLM(_model, settings: new ChatSettings
98+
{
99+
StopSequences = ["Observation", "[END]"],
100+
UseStreaming = _useStreaming
101+
}).UseCache(_useCache)
102+
| UpdateMemory(_conversationSummaryMemory, requestKey: "input", responseKey: "text")
103+
| ReActParser(inputKey: "text", outputKey: ReActAnswer);
104+
105+
_chain = chain;
106+
}
107+
108+
public ReActAgentChain UseCache(bool enabled = true)
109+
{
110+
_useCache = enabled;
111+
return this;
112+
}
113+
114+
public ReActAgentChain UseTool(AgentTool tool)
115+
{
116+
tool = tool ?? throw new ArgumentNullException(nameof(tool));
117+
118+
_tools.Add(tool.Name, tool);
119+
return this;
120+
}
121+
122+
protected override async Task<IChainValues> InternalCallAsync(IChainValues values,
123+
CancellationToken cancellationToken = new())
124+
{
125+
values = values ?? throw new ArgumentNullException(nameof(values));
126+
127+
var input = (string)values.Value[InputKeys[0]];
128+
var valuesChain = new ChainValues();
129+
130+
_userInput = input;
131+
132+
if (_chain == null)
133+
{
134+
InitializeChain();
135+
}
136+
137+
for (int i = 0; i < _maxActions; i++)
138+
{
139+
var res = await _chain!.CallAsync(valuesChain, cancellationToken: cancellationToken).ConfigureAwait(false);
140+
switch (res.Value[ReActAnswer])
141+
{
142+
case AgentAction:
143+
{
144+
var action = (AgentAction)res.Value[ReActAnswer];
145+
var tool = _tools[action.Action.ToLower(CultureInfo.InvariantCulture)];
146+
var toolRes = await tool.ToolTask(action.ActionInput, cancellationToken).ConfigureAwait(false);
147+
await _conversationSummaryMemory.ChatHistory.AddMessage(new Message("Observation: " + toolRes, MessageRole.System))
148+
.ConfigureAwait(false);
149+
await _conversationSummaryMemory.ChatHistory.AddMessage(new Message("Thought:", MessageRole.System))
150+
.ConfigureAwait(false);
151+
break;
152+
}
153+
case AgentFinish:
154+
{
155+
var finish = (AgentFinish)res.Value[ReActAnswer];
156+
values.Value[OutputKeys[0]] = finish.Output;
157+
return values;
158+
}
159+
}
160+
}
161+
162+
return values;
163+
}
164+
}

ChatRPG/API/ReActLlmClient.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public ReActLlmClient(IConfiguration configuration)
2929
//agent.UseTool();
3030
_llm.Settings = new OpenAiChatSettings { UseStreaming = true };
3131

32+
var test = new ReActAgentChain(_llm, _memory);
33+
3234
}
3335

3436
public async Task<string> GetChatCompletion(IList<OpenAiGptMessage> inputs, string systemPrompt)

0 commit comments

Comments
 (0)