create_json_chat_agent() — langchain Function Reference
Architecture documentation for the create_json_chat_agent() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 3764ab03_8e7e_5fe2_bddb_a569bac3b63b["create_json_chat_agent()"] ac47f022_c133_ac28_49a2_247def19d750["base.py"] 3764ab03_8e7e_5fe2_bddb_a569bac3b63b -->|defined in| ac47f022_c133_ac28_49a2_247def19d750 style 3764ab03_8e7e_5fe2_bddb_a569bac3b63b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/agents/json_chat/base.py lines 14–195
def create_json_chat_agent(
llm: BaseLanguageModel,
tools: Sequence[BaseTool],
prompt: ChatPromptTemplate,
stop_sequence: bool | list[str] = True, # noqa: FBT001,FBT002
tools_renderer: ToolsRenderer = render_text_description,
template_tool_response: str = TEMPLATE_TOOL_RESPONSE,
) -> Runnable:
r"""Create an agent that uses JSON to format its logic, build for Chat Models.
Args:
llm: LLM to use as the agent.
tools: Tools this agent has access to.
prompt: The prompt to use. See Prompt section below for more.
stop_sequence: bool or list of str.
If `True`, adds a stop token of "Observation:" to avoid hallucinates.
If `False`, does not add a stop token.
If a list of str, uses the provided list as the stop tokens.
You may to set this to False if the LLM you are using does not support stop
sequences.
tools_renderer: This controls how the tools are converted into a string and
then passed into the LLM.
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
variables as the prompt passed in does. It returns as output either an
AgentAction or AgentFinish.
Raises:
ValueError: If the prompt is missing required variables.
ValueError: If the template_tool_response is missing
the required variable 'observation'.
Example:
```python
from langchain_classic import hub
from langchain_openai import ChatOpenAI
from langchain_classic.agents import AgentExecutor, create_json_chat_agent
prompt = hub.pull("hwchase17/react-chat-json")
model = ChatOpenAI()
tools = ...
agent = create_json_chat_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools)
agent_executor.invoke({"input": "hi"})
# Using with chat history
from langchain_core.messages import AIMessage, HumanMessage
agent_executor.invoke(
{
"input": "what's my name?",
"chat_history": [
HumanMessage(content="hi! my name is bob"),
AIMessage(content="Hello Bob! How can I assist you today?"),
],
}
)
```
Prompt:
The prompt must have input keys:
* `tools`: contains descriptions and arguments for each tool.
* `tool_names`: contains all tool names.
* `agent_scratchpad`: must be a MessagesPlaceholder. Contains previous
agent actions and tool outputs as messages.
Here's an example:
```python
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
system = '''Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering
Domain
Subdomains
Source
Frequently Asked Questions
What does create_json_chat_agent() do?
create_json_chat_agent() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/json_chat/base.py.
Where is create_json_chat_agent() defined?
create_json_chat_agent() is defined in libs/langchain/langchain_classic/agents/json_chat/base.py at line 14.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free