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']]] = 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 – Language model to use for the agent.

  • toolkit – 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 – One of “openai-tools”, “openai-functions”, or “zero-shot-react-description”. Defaults to “zero-shot-react-description”. “openai-tools” is recommended over “openai-functions”.

  • callback_manager – DEPRECATED. Pass “callbacks” key into ‘agent_executor_kwargs’ instead to pass constructor callbacks to AgentExecutor.

  • prefix – Prompt prefix string. Must contain variables “top_k” and “dialect”.

  • suffix – Prompt suffix string. Default depends on agent type.

  • format_instructions – Formatting instructions to pass to ZeroShotAgent.create_prompt() when ‘agent_type’ is “zero-shot-react-description”. Otherwise ignored.

  • input_variables – DEPRECATED. Input variables to explicitly specify as part of ZeroShotAgent.create_prompt() when ‘agent_type’ is “zero-shot-react-description”. Otherwise ignored.

  • top_k – Number of rows to query for by default.

  • max_iterations – Passed to AgentExecutor init.

  • max_execution_time – Passed to AgentExecutor init.

  • early_stopping_method – Passed to AgentExecutor init.

  • verbose – AgentExecutor verbosity.

  • agent_executor_kwargs – Arbitrary additional AgentExecutor args.

  • extra_tools – Additional tools to give to agent on top of the ones that come with SQLDatabaseToolkit.

  • db – SQLDatabase from which to create a SQLDatabaseToolkit. Toolkit is created using ‘db’ and ‘llm’. Must provide exactly one of ‘db’ or ‘toolkit’.

  • prompt – Complete agent prompt. prompt and {prefix, suffix, format_instructions, input_variables} are mutually exclusive.

  • **kwargs – DEPRECATED. Not used, kept for backwards compatibility.

Returns

An AgentExecutor with the specified agent_type agent.

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=”openai-tools”, verbose=True)

Examples using create_sql_agent¶