create_sql_agent#

langchain_cohere.sql_agent.agent.create_sql_agent(llm: BaseLanguageModel, toolkit: SQLDatabaseToolkit | None = None, callback_manager: BaseCallbackManager | None = None, prefix: str | None = None, suffix: str | None = None, top_k: int = 10, max_iterations: int | None = 15, max_execution_time: float | None = None, early_stopping_method: str = 'force', verbose: bool = False, agent_executor_kwargs: Dict[str, Any] | None = None, extra_tools: Sequence[BaseTool] = (), *, db: SQLDatabase | None = None, prompt: BasePromptTemplate | None = 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.

  • 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.

  • input_variables – 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. Must contain variables “top_k” and “dialect”. Can contain variables “table_info” or “table_names” if the prompt requires them.

  • **kwargs (Any) – Arbitrary additional Agent args.

Returns:

An AgentExecutor with the specified agent_type agent.

Return type:

AgentExecutor

Example


from langchain_cohere import ChatCohere, create_sql_agent from langchain_community.utilities import SQLDatabase

db = SQLDatabase.from_uri(“sqlite:///Chinook.db”) llm = ChatCohere(model=”command-r-plus”, temperature=0) agent_executor = create_sql_agent(llm, db=db, verbose=True) resp = agent_executor.run(“Show me the first 5 rows of the ‘Album’ table.”) print(resp.get(“output”))

Examples using create_sql_agent