mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 15:43:54 +00:00
langchain[patch]: add template_tool_response arg to create_json_chat (#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 <Davide.Menini@swisscom.com>
This commit is contained in:
parent
688ca48019
commit
824dbc49ee
@ -17,6 +17,7 @@ def create_json_chat_agent(
|
|||||||
prompt: ChatPromptTemplate,
|
prompt: ChatPromptTemplate,
|
||||||
stop_sequence: Union[bool, List[str]] = True,
|
stop_sequence: Union[bool, List[str]] = True,
|
||||||
tools_renderer: ToolsRenderer = render_text_description,
|
tools_renderer: ToolsRenderer = render_text_description,
|
||||||
|
template_tool_response: str = TEMPLATE_TOOL_RESPONSE,
|
||||||
) -> Runnable:
|
) -> Runnable:
|
||||||
"""Create an agent that uses JSON to format its logic, build for Chat Models.
|
"""Create an agent that uses JSON to format its logic, build for Chat Models.
|
||||||
|
|
||||||
@ -33,6 +34,8 @@ def create_json_chat_agent(
|
|||||||
does not support stop sequences.
|
does not support stop sequences.
|
||||||
tools_renderer: This controls how the tools are converted into a string and
|
tools_renderer: This controls how the tools are converted into a string and
|
||||||
then passed into the LLM. Default is `render_text_description`.
|
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:
|
Returns:
|
||||||
A Runnable sequence representing an agent. It takes as input all the same input
|
A Runnable sequence representing an agent. It takes as input all the same input
|
||||||
@ -157,6 +160,11 @@ def create_json_chat_agent(
|
|||||||
if missing_vars:
|
if missing_vars:
|
||||||
raise ValueError(f"Prompt missing required variables: {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(
|
prompt = prompt.partial(
|
||||||
tools=tools_renderer(list(tools)),
|
tools=tools_renderer(list(tools)),
|
||||||
tool_names=", ".join([t.name for t in tools]),
|
tool_names=", ".join([t.name for t in tools]),
|
||||||
@ -170,7 +178,7 @@ def create_json_chat_agent(
|
|||||||
agent = (
|
agent = (
|
||||||
RunnablePassthrough.assign(
|
RunnablePassthrough.assign(
|
||||||
agent_scratchpad=lambda x: format_log_to_messages(
|
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
|
| prompt
|
||||||
|
Loading…
Reference in New Issue
Block a user