mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
|
"""Json agent."""
|
||
|
from __future__ import annotations
|
||
|
|
||
|
from typing import TYPE_CHECKING, Any, Dict, List, Optional
|
||
|
|
||
|
from langchain_core.callbacks import BaseCallbackManager
|
||
|
from langchain_core.language_models import BaseLanguageModel
|
||
|
|
||
|
from langchain_community.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX
|
||
|
from langchain_community.agent_toolkits.json.toolkit import JsonToolkit
|
||
|
|
||
|
if TYPE_CHECKING:
|
||
|
from langchain.agents.agent import AgentExecutor
|
||
|
|
||
|
|
||
|
def create_json_agent(
|
||
|
llm: BaseLanguageModel,
|
||
|
toolkit: JsonToolkit,
|
||
|
callback_manager: Optional[BaseCallbackManager] = None,
|
||
|
prefix: str = JSON_PREFIX,
|
||
|
suffix: str = JSON_SUFFIX,
|
||
|
format_instructions: Optional[str] = None,
|
||
|
input_variables: Optional[List[str]] = None,
|
||
|
verbose: bool = False,
|
||
|
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
|
||
|
**kwargs: Any,
|
||
|
) -> AgentExecutor:
|
||
|
"""Construct a json agent from an LLM and tools."""
|
||
|
from langchain.agents.agent import AgentExecutor
|
||
|
from langchain.agents.mrkl.base import ZeroShotAgent
|
||
|
from langchain.chains.llm import LLMChain
|
||
|
|
||
|
tools = toolkit.get_tools()
|
||
|
prompt_params = (
|
||
|
{"format_instructions": format_instructions}
|
||
|
if format_instructions is not None
|
||
|
else {}
|
||
|
)
|
||
|
prompt = ZeroShotAgent.create_prompt(
|
||
|
tools,
|
||
|
prefix=prefix,
|
||
|
suffix=suffix,
|
||
|
input_variables=input_variables,
|
||
|
**prompt_params,
|
||
|
)
|
||
|
llm_chain = LLMChain(
|
||
|
llm=llm,
|
||
|
prompt=prompt,
|
||
|
callback_manager=callback_manager,
|
||
|
)
|
||
|
tool_names = [tool.name for tool in tools]
|
||
|
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
|
||
|
return AgentExecutor.from_agent_and_tools(
|
||
|
agent=agent,
|
||
|
tools=tools,
|
||
|
callback_manager=callback_manager,
|
||
|
verbose=verbose,
|
||
|
**(agent_executor_kwargs or {}),
|
||
|
)
|