Skip to content

Commit

Permalink
langchain[patch]: add template_tool_response arg to create_json_chat (#…
Browse files Browse the repository at this point in the history
…19696)

In this small PR I added the `template_tool_response` arg to the
`create_json_chat` function, so that users can customize this prompt in
case of need.
Thanks for your reviews!

---------

Co-authored-by: taamedag <[email protected]>
  • Loading branch information
dmenini and taamedag authored Mar 28, 2024
1 parent 688ca48 commit 824dbc4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libs/langchain/langchain/agents/json_chat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def create_json_chat_agent(
prompt: ChatPromptTemplate,
stop_sequence: Union[bool, List[str]] = True,
tools_renderer: ToolsRenderer = render_text_description,
template_tool_response: str = TEMPLATE_TOOL_RESPONSE,
) -> Runnable:
"""Create an agent that uses JSON to format its logic, build for Chat Models.
Expand All @@ -33,6 +34,8 @@ def create_json_chat_agent(
does not support stop sequences.
tools_renderer: This controls how the tools are converted into a string and
then passed into the LLM. Default is `render_text_description`.
template_tool_response: Template prompt that uses the tool response (observation)
to make the LLM generate the next action to take.
Returns:
A Runnable sequence representing an agent. It takes as input all the same input
Expand Down Expand Up @@ -157,6 +160,11 @@ def create_json_chat_agent(
if missing_vars:
raise ValueError(f"Prompt missing required variables: {missing_vars}")

if "{observation}" not in template_tool_response:
raise ValueError(
"Template tool response missing required variable 'observation'"
)

prompt = prompt.partial(
tools=tools_renderer(list(tools)),
tool_names=", ".join([t.name for t in tools]),
Expand All @@ -170,7 +178,7 @@ def create_json_chat_agent(
agent = (
RunnablePassthrough.assign(
agent_scratchpad=lambda x: format_log_to_messages(
x["intermediate_steps"], template_tool_response=TEMPLATE_TOOL_RESPONSE
x["intermediate_steps"], template_tool_response=template_tool_response
)
)
| prompt
Expand Down

0 comments on commit 824dbc4

Please sign in to comment.