Skip to main content

lc_agents/react/
prompt.rs

1// src/agents/react/prompt.rs
2//! ReAct prompt templates
3//!
4//! Provides the prompt templates used by the ReAct Agent.
5
6/// ReAct prompt prefix
7///
8/// Describes the available tools and the expected format
9pub const REACT_PREFIX: &str = r#"回答以下问题,你可以使用以下工具:
10
11{tools}
12
13使用以下格式:
14
15Question: 需要回答的问题
16Thought: 你应该思考要做什么
17Action: 要执行的动作,应该是 [{tool_names}] 之一
18Action Input: 动作的输入
19Observation: 动作的结果
20... (这个 Thought/Action/Action Input/Observation 可以重复 N 次)
21Thought: 我现在知道最终答案了
22Final Answer: 原始问题的最终答案
23
24开始!
25
26Question: {input}
27Thought:{agent_scratchpad}"#;
28
29/// Builds the ReAct prompt
30///
31/// # Parameters
32/// * `tools_description` - the tool descriptions string
33/// * `tool_names` - the tool name list
34/// * `input` - the user question
35/// * `scratchpad` - the agent's thought history
36///
37/// # Returns
38/// The complete prompt string
39pub fn build_react_prompt(
40    tools_description: &str,
41    tool_names: &[&str],
42    input: &str,
43    scratchpad: &str,
44) -> String {
45    REACT_PREFIX
46        .replace("{tools}", tools_description)
47        .replace("{tool_names}", &tool_names.join(", "))
48        .replace("{input}", input)
49        .replace("{agent_scratchpad}", scratchpad)
50}
51
52/// Formats `intermediate_steps` into a scratchpad
53///
54/// # Parameters
55/// * `steps` - the list of executed steps
56///
57/// # Returns
58/// The formatted thought-history string
59pub fn format_scratchpad(steps: &[crate::types::AgentStep]) -> String {
60    let mut scratchpad = String::new();
61
62    for step in steps {
63        scratchpad.push_str(&format!(
64            " {}\nAction: {}\nAction Input: {}\nObservation: {}\n",
65            step.action.log.lines().next().unwrap_or(""),
66            step.action.tool,
67            step.action.tool_input,
68            step.observation
69        ));
70    }
71
72    scratchpad
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn test_build_react_prompt() {
81        let prompt = build_react_prompt(
82            "calculator: 计算数学表达式",
83            &["calculator"],
84            "计算 2 + 2",
85            "",
86        );
87
88        assert!(prompt.contains("calculator: 计算数学表达式"));
89        assert!(prompt.contains("计算 2 + 2"));
90        assert!(prompt.contains("[calculator]"));
91    }
92
93    #[test]
94    fn test_format_scratchpad() {
95        use crate::{AgentAction, AgentStep, ToolInput};
96
97        let steps = vec![AgentStep::new(
98            AgentAction {
99                tool: "calculator".to_string(),
100                tool_input: ToolInput::String {
101                    value: "2 + 2".to_string(),
102                },
103                log: "我需要计算".to_string(),
104            },
105            "结果: 4".to_string(),
106        )];
107
108        let scratchpad = format_scratchpad(&steps);
109
110        assert!(scratchpad.contains("calculator"));
111        assert!(scratchpad.contains("结果: 4"));
112    }
113}