- 1.75.0 (latest)
- 1.74.0
- 1.73.0
- 1.72.0
- 1.71.1
- 1.70.0
- 1.69.0
- 1.68.0
- 1.67.1
- 1.66.0
- 1.65.0
- 1.63.0
- 1.62.0
- 1.60.0
- 1.59.0
- 1.58.0
- 1.57.0
- 1.56.0
- 1.55.0
- 1.54.1
- 1.53.0
- 1.52.0
- 1.51.0
- 1.50.0
- 1.49.0
- 1.48.0
- 1.47.0
- 1.46.0
- 1.45.0
- 1.44.0
- 1.43.0
- 1.39.0
- 1.38.1
- 1.37.0
- 1.36.4
- 1.35.0
- 1.34.0
- 1.33.1
- 1.32.0
- 1.31.1
- 1.30.1
- 1.29.0
- 1.28.1
- 1.27.1
- 1.26.1
- 1.25.0
- 1.24.1
- 1.23.0
- 1.22.1
- 1.21.0
- 1.20.0
- 1.19.1
- 1.18.3
- 1.17.1
- 1.16.1
- 1.15.1
- 1.14.0
- 1.13.1
- 1.12.1
- 1.11.0
- 1.10.0
- 1.9.0
- 1.8.1
- 1.7.1
- 1.6.2
- 1.5.0
- 1.4.3
- 1.3.0
- 1.2.0
- 1.1.1
- 1.0.1
- 0.9.0
- 0.8.0
- 0.7.1
- 0.6.0
- 0.5.1
- 0.4.0
- 0.3.1
LangchainAgent(
model: str,
*,
prompt: typing.Optional[RunnableSerializable] = None,
tools: typing.Optional[
typing.Sequence[typing.Union[typing.Callable, BaseTool]]
] = None,
output_parser: typing.Optional[RunnableSerializable] = None,
chat_history: typing.Optional[GetSessionHistoryCallable] = None,
model_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
model_tool_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
agent_executor_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
runnable_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
model_builder: typing.Optional[typing.Callable] = None,
runnable_builder: typing.Optional[typing.Callable] = None
)
A Langchain Agent.
Reference:
Methods
LangchainAgent
LangchainAgent(
model: str,
*,
prompt: typing.Optional[RunnableSerializable] = None,
tools: typing.Optional[
typing.Sequence[typing.Union[typing.Callable, BaseTool]]
] = None,
output_parser: typing.Optional[RunnableSerializable] = None,
chat_history: typing.Optional[GetSessionHistoryCallable] = None,
model_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
model_tool_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
agent_executor_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
runnable_kwargs: typing.Optional[typing.Mapping[str, typing.Any]] = None,
model_builder: typing.Optional[typing.Callable] = None,
runnable_builder: typing.Optional[typing.Callable] = None
)
Initializes the LangchainAgent.
Under-the-hood, assuming .set_up() is called, this will correspond to
model = model_builder(model_name=model, model_kwargs=model_kwargs)
runnable = runnable_builder(
prompt=prompt,
model=model,
tools=tools,
output_parser=output_parser,
chat_history=chat_history,
agent_executor_kwargs=agent_executor_kwargs,
runnable_kwargs=runnable_kwargs,
)
When everything is based on their default values, this corresponds to
# model_builder
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(model_name=model, **model_kwargs)
# runnable_builder
from langchain import agents
from langchain_core.runnables.history import RunnableWithMessageHistory
llm_with_tools = llm.bind_tools(tools=tools, **model_tool_kwargs)
agent_executor = agents.AgentExecutor(
agent=prompt | llm_with_tools | output_parser,
tools=tools,
**agent_executor_kwargs,
)
runnable = RunnableWithMessageHistory(
runnable=agent_executor,
get_session_history=chat_history,
**runnable_kwargs,
)
Parameters | |
---|---|
Name | Description |
model |
str
Optional. The name of the model (e.g. "gemini-1.0-pro"). |
prompt |
langchain_core.runnables.RunnableSerializable
Optional. The prompt template for the model. Defaults to a ChatPromptTemplate. |
tools |
Sequence[langchain_core.tools.BaseTool, Callable]
Optional. The tools for the agent to be able to use. All input callables (e.g. function or class method) will be converted to a langchain.tools.base.StructuredTool. Defaults to None. |
output_parser |
langchain_core.runnables.RunnableSerializable
Optional. The output parser for the model. Defaults to an output parser that works with Gemini function-calling. |
chat_history |
langchain_core.runnables.history.GetSessionHistoryCallable
Optional. Callable that returns a new BaseChatMessageHistory. Defaults to None, i.e. chat_history is not preserved. |
model_kwargs |
Mapping[str, Any]
Optional. Additional keyword arguments for the constructor of chat_models.ChatVertexAI. An example would be { # temperature (float): Sampling temperature, it controls the # degree of randomness in token selection. "temperature": 0.28, # max_output_tokens (int): Token limit determines the # maximum amount of text output from one prompt. "max_output_tokens": 1000, # top_p (float): Tokens are selected from most probable to # least, until the sum of their probabilities equals the # top_p value. "top_p": 0.95, # top_k (int): How the model selects tokens for output, the # next token is selected from among the top_k most probable # tokens. "top_k": 40, } |
model_tool_kwargs |
Mapping[str, Any]
Optional. Additional keyword arguments when binding tools to the model using |
agent_executor_kwargs |
Mapping[str, Any]
Optional. Additional keyword arguments for the constructor of langchain.agents.AgentExecutor. An example would be { # Whether to return the agent's trajectory of intermediate # steps at the end in addition to the final output. "return_intermediate_steps": False, # The maximum number of steps to take before ending the # execution loop. "max_iterations": 15, # The method to use for early stopping if the agent never # returns |
runnable_kwargs |
Mapping[str, Any]
Optional. Additional keyword arguments for the constructor of langchain.runnables.history.RunnableWithMessageHistory if chat_history is specified. If chat_history is None, this will be ignored. |
model_builder |
Callable
Optional. Callable that returns a new language model. Defaults to a a callable that returns ChatVertexAI based on |
runnable_builder |
Callable
Optional. Callable that returns a new runnable. This can be used for customizing the orchestration logic of the Agent based on the model returned by |
Exceptions | |
---|---|
Type | Description |
TypeError |
If there is an invalid tool (e.g. function with an input |
tha |
did not specify its type).: |
clone
clone() -> vertexai.preview.reasoning_engines.templates.langchain.LangchainAgent
Returns a clone of the LangchainAgent.
query
query(
*,
input: typing.Union[str, typing.Mapping[str, typing.Any]],
config: typing.Optional[RunnableConfig] = None,
**kwargs: typing.Any
) -> typing.Dict[str, typing.Any]
Queries the Agent with the given input and config.
Parameters | |
---|---|
Name | Description |
input |
Union[str, Mapping[str, Any]]
Required. The input to be passed to the Agent. |
config |
langchain_core.runnables.RunnableConfig
Optional. The config (if any) to be used for invoking the Agent. |
set_up
set_up()
Sets up the agent for execution of queries at runtime.
It initializes the model, binds the model with tools, and connects it with the prompt template and output parser.
This method should not be called for an object that being passed to the ReasoningEngine service for deployment, as it initializes clients that can not be serialized.