forked from shroominic/codeinterpreter-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update tools and prompts for create_structured_chat_agent
- Loading branch information
Showing
5 changed files
with
150 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 96 additions & 63 deletions
159
src/codeinterpreterapi/agents/plan_and_execute/prompts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,121 @@ | ||
from textwrap import dedent | ||
|
||
from langchain_core.prompts import PromptTemplate | ||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder, PromptTemplate | ||
from langchain_experimental.tot.prompts import JSONListOutputParser | ||
|
||
HUMAN_MESSAGE_TEMPLATE = """Previous steps: {previous_steps} | ||
SYSTEM_MESSAGE_TEMPLATE = '''Respond to the human as helpfully and accurately as possible. You have access to the following tools: | ||
Current objective: {current_step} | ||
{tools} | ||
{agent_scratchpad}""" | ||
Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input). | ||
TASK_PREFIX = """{objective} | ||
Valid "action" values: "Final Answer" or {tool_names} | ||
""" | ||
Provide only ONE action per $JSON_BLOB, as shown: | ||
TOOLS_PREFIX = ( | ||
"""Respond to the human as helpfully and accurately as possible. You have access to the following tools:""" | ||
) | ||
FORMAT_INSTRUCTIONS = """Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input). | ||
``` | ||
{{ | ||
"action": $TOOL_NAME, | ||
"action_input": $INPUT | ||
}} | ||
``` | ||
Follow this format: | ||
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. | ||
Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation | ||
''' | ||
|
||
HUMAN_MESSAGE_TEMPLATE = '''{input} | ||
{agent_scratchpad} | ||
(reminder to respond in a JSON blob no matter what)''' | ||
|
||
SYSTEM_MESSAGE_TEMPLATE_JA = '''初期質問にできる限り丁寧かつ正確に答えてください。以下のツールが利用可能です: | ||
{tools} | ||
1つの $JSON_BLOB では常に単一のアクションで応答してください。 | ||
action は (TOOL_NAME) 、action_input は (INPUT) を使ってください。 | ||
Valid "action" values: "Final Answer" or {tool_names} | ||
Provide only ONE action per $JSON_BLOB, as shown: | ||
フォーマットは次のようになります。 | ||
``` | ||
{{{{ | ||
"action": $TOOL_NAME, | ||
"action_input": $INPUT | ||
}}}} | ||
``` | ||
``` | ||
{{ | ||
"action": $TOOL_NAME, | ||
"action_input": $INPUT | ||
}} | ||
``` | ||
Follow this format: | ||
その後のシーケンスは以下の形式に従ってください: | ||
Question: input question to answer | ||
Thought: consider previous and subsequent steps | ||
Question: 初期質問に正しく答えるための質問 | ||
Thought: 前後のステップを検討する | ||
Action: | ||
``` | ||
$JSON_BLOB | ||
``` | ||
Observation: action result | ||
... (repeat Thought/Action/Observation N times) | ||
Thought: I know what to respond | ||
Observation: アクションの結果 | ||
(Thought/Action/Observation をN回繰り返す) | ||
Thought: 何を答えるべきかわかった | ||
Action: | ||
``` | ||
{{{{ | ||
"action": "Final Answer", | ||
"action_input": "Final response to human" | ||
}}}} | ||
```""" | ||
SUFFIX = """Begin! Reminder to ALWAYS respond with a valid json blob of a single action. | ||
Use tools if necessary. Respond directly if appropriate. | ||
Format is like this. | ||
Action:```$JSON_BLOB``` | ||
Observation: | ||
Thought:""" | ||
|
||
TOOLS_PREFIX_JA = """できる限り丁寧かつ正確に人間の質問に答えてください。以下のツールが利用可能です:""" | ||
|
||
FORMAT_INSTRUCTIONS_JA = """ | ||
始めましょう!常に単一のアクションの有効なJSONブロブで応答することを忘れないでください。 | ||
必要に応じてツールを使用してください。適切な場合は直接回答してください。 | ||
フォーマットは次のようにしてください。 | ||
Action:```$JSON_BLOB``` | ||
Observation: | ||
Thought: | ||
""" | ||
SUFFIX_JA = """それでは始めましょう。常に単一のアクションの有効なJSON_BLOBで応答してください。 | ||
必要に応じてツールを使用してください。適切な場合は直接回答してください。 | ||
フォーマットは次のようにしてください。 | ||
Action:```$JSON_BLOB``` | ||
Observation: | ||
Thought:""" | ||
|
||
|
||
def get_tools_prefix_prompt(is_ja: bool) -> str: | ||
"""Get the prefix prompt for plan_and_execute.""" | ||
if is_ja: | ||
return TOOLS_PREFIX_JA | ||
else: | ||
return TOOLS_PREFIX | ||
{{ | ||
"action": "最終回答", | ||
"action_input": "初期質問への最終的な回答" | ||
}} | ||
``` | ||
それでは始めましょう。常に単一のアクションの有効なJSONブロブで応答することを忘れないでください。 | ||
必要に応じてツールを使用してください。 | ||
適切な場合は直接回答してください。 | ||
''' | ||
|
||
HUMAN_MESSAGE_TEMPLATE_JA = '''{input} | ||
{agent_scratchpad} | ||
リマインダ: 何があっても $JSON_BLOB だけで応答するようにしてください。 | ||
''' | ||
|
||
|
||
def get_plan_and_execute_prompt(is_ja: bool) -> str: | ||
"""Get the main prompt for plan_and_execute.""" | ||
def create_structured_chat_agent_prompt(is_ja: bool = True): | ||
if is_ja: | ||
return FORMAT_INSTRUCTIONS_JA | ||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", SYSTEM_MESSAGE_TEMPLATE_JA), | ||
MessagesPlaceholder("chat_history", optional=True), | ||
("human", HUMAN_MESSAGE_TEMPLATE_JA), | ||
] | ||
) | ||
else: | ||
return FORMAT_INSTRUCTIONS | ||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", SYSTEM_MESSAGE_TEMPLATE), | ||
MessagesPlaceholder("chat_history", optional=True), | ||
("human", HUMAN_MESSAGE_TEMPLATE), | ||
] | ||
) | ||
return prompt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters