langchain_community.agent_toolkits.sql.base
.create_sql_agent¶
- langchain_community.agent_toolkits.sql.base.create_sql_agent(llm: BaseLanguageModel, toolkit: Optional[SQLDatabaseToolkit] = None, agent_type: Optional[Union[AgentType, Literal['openai-tools', 'tool-calling']]] = None, callback_manager: Optional[BaseCallbackManager] = None, prefix: Optional[str] = None, suffix: Optional[str] = None, format_instructions: Optional[str] = None, input_variables: Optional[List[str]] = None, top_k: int = 10, max_iterations: Optional[int] = 15, max_execution_time: Optional[float] = None, early_stopping_method: str = 'force', verbose: bool = False, agent_executor_kwargs: Optional[Dict[str, Any]] = None, extra_tools: Sequence[BaseTool] = (), *, db: Optional[SQLDatabase] = None, prompt: Optional[BasePromptTemplate] = None, **kwargs: Any) AgentExecutor [source]¶
Construct a SQL agent from an LLM and toolkit or database.
- Parameters
llm (BaseLanguageModel) â Language model to use for the agent. If agent_type is âtool-callingâ then llm is expected to support tool calling.
toolkit (Optional[SQLDatabaseToolkit]) â SQLDatabaseToolkit for the agent to use. Must provide exactly one of âtoolkitâ or âdbâ. Specify âtoolkitâ if you want to use a different model for the agent and the toolkit.
agent_type (Optional[Union[AgentType, Literal['openai-tools', 'tool-calling']]]) â One of âtool-callingâ, âopenai-toolsâ, âopenai-functionsâ, or âzero-shot-react-descriptionâ. Defaults to âzero-shot-react-descriptionâ. âtool-callingâ is recommended over the legacy âopenai-toolsâ and âopenai-functionsâ types.
callback_manager (Optional[BaseCallbackManager]) â DEPRECATED. Pass âcallbacksâ key into âagent_executor_kwargsâ instead to pass constructor callbacks to AgentExecutor.
prefix (Optional[str]) â Prompt prefix string. Must contain variables âtop_kâ and âdialectâ.
suffix (Optional[str]) â Prompt suffix string. Default depends on agent type.
format_instructions (Optional[str]) â Formatting instructions to pass to ZeroShotAgent.create_prompt() when âagent_typeâ is âzero-shot-react-descriptionâ. Otherwise ignored.
input_variables (Optional[List[str]]) â DEPRECATED.
top_k (int) â Number of rows to query for by default.
max_iterations (Optional[int]) â Passed to AgentExecutor init.
max_execution_time (Optional[float]) â Passed to AgentExecutor init.
early_stopping_method (str) â Passed to AgentExecutor init.
verbose (bool) â AgentExecutor verbosity.
agent_executor_kwargs (Optional[Dict[str, Any]]) â Arbitrary additional AgentExecutor args.
extra_tools (Sequence[BaseTool]) â Additional tools to give to agent on top of the ones that come with SQLDatabaseToolkit.
db (Optional[SQLDatabase]) â SQLDatabase from which to create a SQLDatabaseToolkit. Toolkit is created using âdbâ and âllmâ. Must provide exactly one of âdbâ or âtoolkitâ.
prompt (Optional[BasePromptTemplate]) â Complete agent prompt. prompt and {prefix, suffix, format_instructions, input_variables} are mutually exclusive.
**kwargs (Any) â Arbitrary additional Agent args.
- Returns
An AgentExecutor with the specified agent_type agent.
- Return type
Example
from langchain_openai import ChatOpenAI from langchain_community.agent_toolkits import create_sql_agent from langchain_community.utilities import SQLDatabase
db = SQLDatabase.from_uri(âsqlite:///Chinook.dbâ) llm = ChatOpenAI(model=âgpt-3.5-turboâ, temperature=0) agent_executor = create_sql_agent(llm, db=db, agent_type=âtool-callingâ, verbose=True)