SQLDatabaseToolkit#
- class langchain_community.agent_toolkits.sql.toolkit.SQLDatabaseToolkit[source]#
Bases:
BaseToolkit
SQLDatabaseToolkit for interacting with SQL databases.
- Setup:
Install
langchain-community
.pip install -U langchain-community
- Key init args:
- db: SQLDatabase
The SQL database.
- llm: BaseLanguageModel
The language model (for use with QuerySQLCheckerTool)
- Instantiate:
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit from langchain_community.utilities.sql_database import SQLDatabase from langchain_openai import ChatOpenAI db = SQLDatabase.from_uri("sqlite:///Chinook.db") llm = ChatOpenAI(temperature=0) toolkit = SQLDatabaseToolkit(db=db, llm=llm)
- Tools:
toolkit.get_tools()
- Use within an agent:
from langchain import hub from langgraph.prebuilt import create_react_agent # Pull prompt (or define your own) prompt_template = hub.pull("langchain-ai/sql-agent-system-prompt") system_message = prompt_template.format(dialect="SQLite", top_k=5) # Create agent agent_executor = create_react_agent( llm, toolkit.get_tools(), state_modifier=system_message ) # Query agent example_query = "Which country's customers spent the most?" events = agent_executor.stream( {"messages": [("user", example_query)]}, stream_mode="values", ) for event in events: event["messages"][-1].pretty_print()
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- param db: SQLDatabase [Required]#
- param llm: BaseLanguageModel [Required]#
- property dialect: str#
Return string representation of SQL dialect to use.
Examples using SQLDatabaseToolkit