File size: 2,764 Bytes
6b62834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""ReAct prompt builder — delegates to centralized prompts module.

v2: Compact tool descriptions, Chinese-first with English fallback.
"""

from agentic_rag.config.prompts import Prompts

# Legacy alias — still used by router.py and react_engine.py
SYSTEM_PROMPT = Prompts.react_system()


def build_react_prompt(
    tools_description: str,
    memory_context: str = "",
) -> str:
    """Build the ReAct system prompt with tool descriptions and conversation history.

    Uses the Chinese-first prompt by default. Call ``build_react_prompt_en()``
    for the English variant.
    """
    return SYSTEM_PROMPT.format(
        tools_description=tools_description,
        memory_context=memory_context or "(无历史)",
    )


def build_react_prompt_en(
    tools_description: str,
    memory_context: str = "",
) -> str:
    """Build the English variant of the ReAct system prompt."""
    return Prompts.react_system_en().format(
        tools_description=tools_description,
        memory_context=memory_context or "(no history)",
    )


def build_tools_description(tools) -> str:
    """Build a compact text description of available tools.

    Format (one line per tool + compact param list):
        rag_search: 搜索知识库
          query (required): 搜索关键词
          top_k: 返回数量 [default: 5]
    """
    lines = []
    for tool in tools:
        # Handle both ToolDefinition/Pydantic model and plain dict
        if hasattr(tool, 'name'):
            name = tool.name
            desc = tool.description
            params = tool.parameters
        else:
            name = tool.get("name", "unknown")
            desc = tool.get("description", "")
            params = tool.get("parameters", {})

        params_props = params.get("properties", {}) if isinstance(params, dict) else {}
        if isinstance(params_props, dict) and params_props:
            required_params = params.get("required", []) if isinstance(params, dict) else []
            param_strs = []
            for pname, pinfo in params_props.items():
                req_mark = " (必填)" if pname in required_params else ""
                pdesc = pinfo.get("description", "") if isinstance(pinfo, dict) else str(pinfo)
                if isinstance(pinfo, dict) and "default" in pinfo:
                    pdesc += f" [默认: {pinfo['default']}]"
                if isinstance(pinfo, dict) and "enum" in pinfo:
                    pdesc += f" (可选: {', '.join(str(v) for v in pinfo['enum'])})"
                param_strs.append(f"  {pname}: {pdesc}{req_mark}")
            params_block = "\n".join(param_strs)
        else:
            params_block = "  (无参数)"

        lines.append(f"{name}: {desc}\n{params_block}")

    return "\n".join(lines)