|
| 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 | +} |
0 commit comments