Browse Source

perf:优化提示模板

main
gjl 1 year ago
parent
commit
4e90e0ba5f
3 changed files with 108 additions and 29 deletions
  1. +70
    -3
      src/mindpilot/app/agent/agents_registry.py
  2. +0
    -1
      src/mindpilot/app/chat/chat.py
  3. +38
    -25
      src/mindpilot/app/configs/prompt_config.py

+ 70
- 3
src/mindpilot/app/agent/agents_registry.py View File

@@ -10,7 +10,7 @@ from langchain_core.messages import SystemMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import BaseTool

from langchain_core.prompts import SystemMessagePromptTemplate, MessagesPlaceholder, HumanMessagePromptTemplate, PromptTemplate
def agents_registry(
llm: BaseLanguageModel,
tools: Sequence[BaseTool] = [],
@@ -21,8 +21,75 @@ def agents_registry(
if prompt is not None:
prompt = ChatPromptTemplate.from_messages([SystemMessage(content=prompt)])
else:
prompt = hub.pull("hwchase17/structured-chat-agent") # default prompt
# print(prompt)
prompt = ChatPromptTemplate(
input_variables=['agent_scratchpad', 'input', 'tool_names', 'tools'],
optional_variables=['chat_history'],
input_types={'chat_history': typing.List[typing.Union[
langchain_core.messages.ai.AIMessage, langchain_core.messages.human.HumanMessage, langchain_core.messages.chat.ChatMessage, langchain_core.messages.system.SystemMessage, langchain_core.messages.function.FunctionMessage, langchain_core.messages.tool.ToolMessage]]},
partial_variables={'chat_history': []},
metadata={'lc_hub_owner': 'hwchase17', 'lc_hub_repo': 'structured-chat-agent',
'lc_hub_commit_hash': 'ea510f70a5872eb0f41a4e3b7bb004d5711dc127adee08329c664c6c8be5f13c'},
messages=[
SystemMessagePromptTemplate(
prompt=PromptTemplate(
input_variables=['tool_names', 'tools'],
template='''
Respond to the human as helpfully and accurately as possible. You have access to the following tools:

{tools}

Use a JSON blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).

Valid "action" values: "Final Answer" or {tool_names}

Provide only ONE action per $JSON_BLOB, as shown:

```
{{
"action": $TOOL_NAME,
"action_input": $INPUT
}}
```

Please strictly follow format below:

Question: input question to answer
Thought: consider previous and subsequent steps
Action:
```
$JSON_BLOB
```
Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
```
{{
"action": "Final Answer",
"action_input": "Final response to human"
}}
```

Begin! Reminder to ALWAYS respond with a valid JSON blob of a single action. Use tools if necessary. Try to reply in Chinese as much as possible.Don't forget the Question, Thought, and Observation sections.Please provide as much output content as possible for the Final Answer.
''',
)
),
MessagesPlaceholder(variable_name='chat_history', optional=True),
HumanMessagePromptTemplate(
prompt=PromptTemplate(
input_variables=['agent_scratchpad', 'input'],
template='''
{input}

{agent_scratchpad}
(reminder to respond in a JSON blob no matter what)
'''
)
)
]
)
# prompt = hub.pull("hwchase17/structured-chat-agent") # default prompt

agent = create_structured_chat_agent(llm=llm, tools=tools, prompt=prompt)

agent_executor = AgentExecutor(


+ 0
- 1
src/mindpilot/app/chat/chat.py View File

@@ -102,7 +102,6 @@ async def chat(
callbacks=callbacks, configs=chat_model_config, stream=stream
)
all_tools = get_tool().values()
print(all_tools)
tool_configs = tool_config or TOOL_CONFIG
tools = [tool for tool in all_tools if tool.name in tool_configs]
tools = [t.copy(update={"callbacks": callbacks}) for t in tools]


+ 38
- 25
src/mindpilot/app/configs/prompt_config.py View File

@@ -24,31 +24,44 @@ PROMPT_TEMPLATES = {
"rag_default": "{{question}}",
},
"action_model": {
"GPT-4": "Answer the following questions as best you can. You have access to the following tools:\n"
"The way you use the tools is by specifying a json blob.\n"
"Specifically, this json should have a `action` key (with the name of the tool to use) and a `action_input` key (with the input to the tool going here).\n"
'The only values that should be in the "action" field are: {tool_names}\n'
"The $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n"
"```\n\n"
"{{{{\n"
' "action": $TOOL_NAME,\n'
' "action_input": $INPUT\n'
"}}}}\n"
"```\n\n"
"ALWAYS use the following format:\n"
"Question: the input question you must answer\n"
"Thought: you should always think about what to do\n"
"Action:\n"
"```\n\n"
"$JSON_BLOB"
"```\n\n"
"Observation: the result of the action\n"
"... (this Thought/Action/Observation can repeat N times)\n"
"Thought: I now know the final answer\n"
"Final Answer: the final answer to the original input question\n"
"Begin! Reminder to always use the exact characters `Final Answer` when responding.\n"
"Question:{input}\n"
"Thought:{agent_scratchpad}\n",
"GPT-4": '''Respond to the human as helpfully and accurately as possible. You have access to the following tools:

{tools}

Use a JSON blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).

Valid "action" values: "Final Answer" or {tool_names}

Provide only ONE action per $JSON_BLOB, as shown:

```
{{
"action": $TOOL_NAME,
"action_input": $INPUT
}}
```

Please strictly follow format below:

Question: input question to answer
Thought: consider previous and subsequent steps
Action:
```
$JSON_BLOB
```
Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
```
{{
"action": "Final Answer",
"action_input": "Final response to human"
}}
```

Begin! Reminder to ALWAYS respond with a valid JSON blob of a single action. Use tools if necessary. Try to reply in Chinese as much as possible.Don't forget the Question, Thought, and Observation sections.Please provide as much output content as possible for the Final Answer.
''',

"ChatGLM3": """You can answer using the tools.Respond to the human as helpfully and accurately as possible.\n
You have access to the following tools:\n


Loading…
Cancel
Save