Harrison/agent exec kwargs (#3917)

Co-authored-by: Zach Schillaci <40636930+zachschillaci27@users.noreply.github.com>
This commit is contained in:
Harrison Chase 2023-05-01 20:28:43 -07:00 committed by GitHub
parent 05170b6764
commit c5cc09d4e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 76 additions and 28 deletions

View File

@ -1,5 +1,5 @@
"""Json agent.""" """Json agent."""
from typing import Any, List, Optional from typing import Any, Dict, List, Optional
from langchain.agents.agent import AgentExecutor from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX from langchain.agents.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX
@ -20,7 +20,8 @@ def create_json_agent(
format_instructions: str = FORMAT_INSTRUCTIONS, format_instructions: str = FORMAT_INSTRUCTIONS,
input_variables: Optional[List[str]] = None, input_variables: Optional[List[str]] = None,
verbose: bool = False, verbose: bool = False,
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a json agent from an LLM and tools.""" """Construct a json agent from an LLM and tools."""
tools = toolkit.get_tools() tools = toolkit.get_tools()
@ -39,5 +40,9 @@ def create_json_agent(
tool_names = [tool.name for tool in tools] tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs) agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
return AgentExecutor.from_agent_and_tools( return AgentExecutor.from_agent_and_tools(
agent=agent, tools=toolkit.get_tools(), verbose=verbose agent=agent,
tools=tools,
callback_manager=callback_manager,
verbose=verbose,
**(agent_executor_kwargs or {}),
) )

View File

@ -1,5 +1,5 @@
"""OpenAPI spec agent.""" """OpenAPI spec agent."""
from typing import Any, List, Optional from typing import Any, Dict, List, Optional
from langchain.agents.agent import AgentExecutor from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits.openapi.prompt import ( from langchain.agents.agent_toolkits.openapi.prompt import (
@ -27,7 +27,8 @@ def create_openapi_agent(
early_stopping_method: str = "force", early_stopping_method: str = "force",
verbose: bool = False, verbose: bool = False,
return_intermediate_steps: bool = False, return_intermediate_steps: bool = False,
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a json agent from an LLM and tools.""" """Construct a json agent from an LLM and tools."""
tools = toolkit.get_tools() tools = toolkit.get_tools()
@ -47,10 +48,12 @@ def create_openapi_agent(
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs) agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
return AgentExecutor.from_agent_and_tools( return AgentExecutor.from_agent_and_tools(
agent=agent, agent=agent,
tools=toolkit.get_tools(), tools=tools,
callback_manager=callback_manager,
verbose=verbose, verbose=verbose,
return_intermediate_steps=return_intermediate_steps, return_intermediate_steps=return_intermediate_steps,
max_iterations=max_iterations, max_iterations=max_iterations,
max_execution_time=max_execution_time, max_execution_time=max_execution_time,
early_stopping_method=early_stopping_method, early_stopping_method=early_stopping_method,
**(agent_executor_kwargs or {}),
) )

View File

@ -2,7 +2,7 @@
import json import json
import re import re
from functools import partial from functools import partial
from typing import Callable, List, Optional from typing import Any, Callable, Dict, List, Optional
import yaml import yaml
from pydantic import Field from pydantic import Field
@ -29,6 +29,7 @@ from langchain.agents.agent_toolkits.openapi.spec import ReducedOpenAPISpec
from langchain.agents.mrkl.base import ZeroShotAgent from langchain.agents.mrkl.base import ZeroShotAgent
from langchain.agents.tools import Tool from langchain.agents.tools import Tool
from langchain.base_language import BaseLanguageModel from langchain.base_language import BaseLanguageModel
from langchain.callbacks.base import BaseCallbackManager
from langchain.chains.llm import LLMChain from langchain.chains.llm import LLMChain
from langchain.llms.openai import OpenAI from langchain.llms.openai import OpenAI
from langchain.memory import ReadOnlySharedMemory from langchain.memory import ReadOnlySharedMemory
@ -261,7 +262,10 @@ def create_openapi_agent(
requests_wrapper: RequestsWrapper, requests_wrapper: RequestsWrapper,
llm: BaseLanguageModel, llm: BaseLanguageModel,
shared_memory: Optional[ReadOnlySharedMemory] = None, shared_memory: Optional[ReadOnlySharedMemory] = None,
callback_manager: Optional[BaseCallbackManager] = None,
verbose: bool = True, verbose: bool = True,
agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Instantiate API planner and controller for a given spec. """Instantiate API planner and controller for a given spec.
@ -288,5 +292,12 @@ def create_openapi_agent(
agent = ZeroShotAgent( agent = ZeroShotAgent(
llm_chain=LLMChain(llm=llm, prompt=prompt, memory=shared_memory), llm_chain=LLMChain(llm=llm, prompt=prompt, memory=shared_memory),
allowed_tools=[tool.name for tool in tools], allowed_tools=[tool.name for tool in tools],
**kwargs,
)
return AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
callback_manager=callback_manager,
verbose=verbose,
**(agent_executor_kwargs or {}),
) )
return AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=verbose)

View File

@ -1,5 +1,5 @@
"""Agent for working with pandas objects.""" """Agent for working with pandas objects."""
from typing import Any, List, Optional from typing import Any, Dict, List, Optional
from langchain.agents.agent import AgentExecutor from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits.pandas.prompt import PREFIX, SUFFIX from langchain.agents.agent_toolkits.pandas.prompt import PREFIX, SUFFIX
@ -22,7 +22,8 @@ def create_pandas_dataframe_agent(
max_iterations: Optional[int] = 15, max_iterations: Optional[int] = 15,
max_execution_time: Optional[float] = None, max_execution_time: Optional[float] = None,
early_stopping_method: str = "force", early_stopping_method: str = "force",
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a pandas agent from an LLM and dataframe.""" """Construct a pandas agent from an LLM and dataframe."""
import pandas as pd import pandas as pd
@ -51,10 +52,11 @@ def create_pandas_dataframe_agent(
return AgentExecutor.from_agent_and_tools( return AgentExecutor.from_agent_and_tools(
agent=agent, agent=agent,
tools=tools, tools=tools,
callback_manager=callback_manager,
verbose=verbose, verbose=verbose,
return_intermediate_steps=return_intermediate_steps, return_intermediate_steps=return_intermediate_steps,
max_iterations=max_iterations, max_iterations=max_iterations,
max_execution_time=max_execution_time, max_execution_time=max_execution_time,
early_stopping_method=early_stopping_method, early_stopping_method=early_stopping_method,
callback_manager=callback_manager, **(agent_executor_kwargs or {}),
) )

View File

@ -27,7 +27,7 @@ def create_pbi_agent(
input_variables: Optional[List[str]] = None, input_variables: Optional[List[str]] = None,
top_k: int = 10, top_k: int = 10,
verbose: bool = False, verbose: bool = False,
agent_kwargs: Optional[Dict[str, Any]] = None, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any], **kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a pbi agent from an LLM and tools.""" """Construct a pbi agent from an LLM and tools."""
@ -51,12 +51,12 @@ def create_pbi_agent(
verbose=verbose, verbose=verbose,
), ),
allowed_tools=[tool.name for tool in tools], allowed_tools=[tool.name for tool in tools],
**(agent_kwargs or {}), **kwargs,
) )
return AgentExecutor.from_agent_and_tools( return AgentExecutor.from_agent_and_tools(
agent=agent, agent=agent,
tools=tools, tools=tools,
callback_manager=callback_manager, callback_manager=callback_manager,
verbose=verbose, verbose=verbose,
**kwargs, **(agent_executor_kwargs or {}),
) )

View File

@ -2,6 +2,7 @@
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from langchain.agents import AgentExecutor from langchain.agents import AgentExecutor
from langchain.agents.agent import AgentOutputParser
from langchain.agents.agent_toolkits.powerbi.prompt import ( from langchain.agents.agent_toolkits.powerbi.prompt import (
POWERBI_CHAT_PREFIX, POWERBI_CHAT_PREFIX,
POWERBI_CHAT_SUFFIX, POWERBI_CHAT_SUFFIX,
@ -20,6 +21,7 @@ def create_pbi_chat_agent(
toolkit: Optional[PowerBIToolkit], toolkit: Optional[PowerBIToolkit],
powerbi: Optional[PowerBIDataset] = None, powerbi: Optional[PowerBIDataset] = None,
callback_manager: Optional[BaseCallbackManager] = None, callback_manager: Optional[BaseCallbackManager] = None,
output_parser: Optional[AgentOutputParser] = None,
prefix: str = POWERBI_CHAT_PREFIX, prefix: str = POWERBI_CHAT_PREFIX,
suffix: str = POWERBI_CHAT_SUFFIX, suffix: str = POWERBI_CHAT_SUFFIX,
examples: Optional[str] = None, examples: Optional[str] = None,
@ -27,7 +29,7 @@ def create_pbi_chat_agent(
memory: Optional[BaseChatMemory] = None, memory: Optional[BaseChatMemory] = None,
top_k: int = 10, top_k: int = 10,
verbose: bool = False, verbose: bool = False,
agent_kwargs: Optional[Dict[str, Any]] = None, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any], **kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a pbi agent from an Chat LLM and tools. """Construct a pbi agent from an Chat LLM and tools.
@ -43,11 +45,12 @@ def create_pbi_chat_agent(
llm=llm, llm=llm,
tools=tools, tools=tools,
system_message=prefix.format(top_k=top_k), system_message=prefix.format(top_k=top_k),
user_message=suffix, human_message=suffix,
input_variables=input_variables, input_variables=input_variables,
callback_manager=callback_manager, callback_manager=callback_manager,
output_parser=output_parser,
verbose=verbose, verbose=verbose,
**(agent_kwargs or {}), **kwargs,
) )
return AgentExecutor.from_agent_and_tools( return AgentExecutor.from_agent_and_tools(
agent=agent, agent=agent,
@ -56,5 +59,5 @@ def create_pbi_chat_agent(
memory=memory memory=memory
or ConversationBufferMemory(memory_key="chat_history", return_messages=True), or ConversationBufferMemory(memory_key="chat_history", return_messages=True),
verbose=verbose, verbose=verbose,
**kwargs, **(agent_executor_kwargs or {}),
) )

View File

@ -1,6 +1,6 @@
"""Python agent.""" """Python agent."""
from typing import Any, Optional from typing import Any, Dict, Optional
from langchain.agents.agent import AgentExecutor from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits.python.prompt import PREFIX from langchain.agents.agent_toolkits.python.prompt import PREFIX
@ -17,7 +17,8 @@ def create_python_agent(
callback_manager: Optional[BaseCallbackManager] = None, callback_manager: Optional[BaseCallbackManager] = None,
verbose: bool = False, verbose: bool = False,
prefix: str = PREFIX, prefix: str = PREFIX,
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a python agent from an LLM and tool.""" """Construct a python agent from an LLM and tool."""
tools = [tool] tools = [tool]
@ -29,4 +30,10 @@ def create_python_agent(
) )
tool_names = [tool.name for tool in tools] tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs) agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
return AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=verbose) return AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
callback_manager=callback_manager,
verbose=verbose,
**(agent_executor_kwargs or {}),
)

View File

@ -1,5 +1,5 @@
"""SQL agent.""" """SQL agent."""
from typing import Any, List, Optional from typing import Any, Dict, List, Optional
from langchain.agents.agent import AgentExecutor from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits.sql.prompt import SQL_PREFIX, SQL_SUFFIX from langchain.agents.agent_toolkits.sql.prompt import SQL_PREFIX, SQL_SUFFIX
@ -24,7 +24,8 @@ def create_sql_agent(
max_execution_time: Optional[float] = None, max_execution_time: Optional[float] = None,
early_stopping_method: str = "force", early_stopping_method: str = "force",
verbose: bool = False, verbose: bool = False,
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a sql agent from an LLM and tools.""" """Construct a sql agent from an LLM and tools."""
tools = toolkit.get_tools() tools = toolkit.get_tools()
@ -46,8 +47,10 @@ def create_sql_agent(
return AgentExecutor.from_agent_and_tools( return AgentExecutor.from_agent_and_tools(
agent=agent, agent=agent,
tools=tools, tools=tools,
callback_manager=callback_manager,
verbose=verbose, verbose=verbose,
max_iterations=max_iterations, max_iterations=max_iterations,
max_execution_time=max_execution_time, max_execution_time=max_execution_time,
early_stopping_method=early_stopping_method, early_stopping_method=early_stopping_method,
**(agent_executor_kwargs or {}),
) )

View File

@ -1,5 +1,5 @@
"""VectorStore agent.""" """VectorStore agent."""
from typing import Any, Optional from typing import Any, Dict, Optional
from langchain.agents.agent import AgentExecutor from langchain.agents.agent import AgentExecutor
from langchain.agents.agent_toolkits.vectorstore.prompt import PREFIX, ROUTER_PREFIX from langchain.agents.agent_toolkits.vectorstore.prompt import PREFIX, ROUTER_PREFIX
@ -19,7 +19,8 @@ def create_vectorstore_agent(
callback_manager: Optional[BaseCallbackManager] = None, callback_manager: Optional[BaseCallbackManager] = None,
prefix: str = PREFIX, prefix: str = PREFIX,
verbose: bool = False, verbose: bool = False,
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a vectorstore agent from an LLM and tools.""" """Construct a vectorstore agent from an LLM and tools."""
tools = toolkit.get_tools() tools = toolkit.get_tools()
@ -31,7 +32,13 @@ def create_vectorstore_agent(
) )
tool_names = [tool.name for tool in tools] tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs) agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
return AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=verbose) return AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
callback_manager=callback_manager,
verbose=verbose,
**(agent_executor_kwargs or {}),
)
def create_vectorstore_router_agent( def create_vectorstore_router_agent(
@ -40,7 +47,8 @@ def create_vectorstore_router_agent(
callback_manager: Optional[BaseCallbackManager] = None, callback_manager: Optional[BaseCallbackManager] = None,
prefix: str = ROUTER_PREFIX, prefix: str = ROUTER_PREFIX,
verbose: bool = False, verbose: bool = False,
**kwargs: Any, agent_executor_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Dict[str, Any],
) -> AgentExecutor: ) -> AgentExecutor:
"""Construct a vectorstore router agent from an LLM and tools.""" """Construct a vectorstore router agent from an LLM and tools."""
tools = toolkit.get_tools() tools = toolkit.get_tools()
@ -52,4 +60,10 @@ def create_vectorstore_router_agent(
) )
tool_names = [tool.name for tool in tools] tool_names = [tool.name for tool in tools]
agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs) agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names, **kwargs)
return AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=verbose) return AgentExecutor.from_agent_and_tools(
agent=agent,
tools=tools,
callback_manager=callback_manager,
verbose=verbose,
**(agent_executor_kwargs or {}),
)