The agent builds off of SQLDatabaseChain and is designed to answer more general questions about a database, as well as recover from errors.Note that, as this agent is in active development, all answers might not be correct. Additionally, it is not guaranteed that the agent won't perform DML statements on your database given certain questions. Be careful running it on sensitive data!This uses the example Chinook database. To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository.Initialization‚Äãfrom langchain.agents import create_sql_agentfrom langchain.agents.agent_toolkits import SQLDatabaseToolkitfrom langchain.sql_database import SQLDatabasefrom langchain.llms.openai import OpenAIfrom langchain.agents import AgentExecutorfrom langchain.agents.agent_types import AgentTypefrom langchain.chat_models import ChatOpenAIdb = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))Using ZERO_SHOT_REACT_DESCRIPTION‚ÄãThis shows how to initialize the agent using the ZERO_SHOT_REACT_DESCRIPTION agent type.agent_executor = create_sql_agent(    llm=OpenAI(temperature=0),    toolkit=toolkit,    verbose=True,    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,)Using OpenAI Functions‚ÄãThis shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. Note that this is an alternative to the above.# agent_executor = create_sql_agent(#     llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"),#     toolkit=toolkit,#     verbose=True,#     agent_type=AgentType.OPENAI_FUNCTIONS# )Disclaimer ‚ö†Ô∏è‚ÄãThe query chain may generate insert/update/delete queries. When this is not expected, use a custom prompt or create a SQL users without write permissions.The final user might overload your SQL database by asking a simple question such as "run the biggest query possible". The generated query
This notebook showcases an agent designed to interact with a SQL databases.
This notebook showcases an agent designed to interact with a SQL databases. ->: The agent builds off of SQLDatabaseChain and is designed to answer more general questions about a database, as well as recover from errors.Note that, as this agent is in active development, all answers might not be correct. Additionally, it is not guaranteed that the agent won't perform DML statements on your database given certain questions. Be careful running it on sensitive data!This uses the example Chinook database. To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file in a notebooks folder at the root of this repository.Initialization‚Äãfrom langchain.agents import create_sql_agentfrom langchain.agents.agent_toolkits import SQLDatabaseToolkitfrom langchain.sql_database import SQLDatabasefrom langchain.llms.openai import OpenAIfrom langchain.agents import AgentExecutorfrom langchain.agents.agent_types import AgentTypefrom langchain.chat_models import ChatOpenAIdb = SQLDatabase.from_uri("sqlite:///../../../../../notebooks/Chinook.db")toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))Using ZERO_SHOT_REACT_DESCRIPTION‚ÄãThis shows how to initialize the agent using the ZERO_SHOT_REACT_DESCRIPTION agent type.agent_executor = create_sql_agent(    llm=OpenAI(temperature=0),    toolkit=toolkit,    verbose=True,    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,)Using OpenAI Functions‚ÄãThis shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. Note that this is an alternative to the above.# agent_executor = create_sql_agent(#     llm=ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"),#     toolkit=toolkit,#     verbose=True,#     agent_type=AgentType.OPENAI_FUNCTIONS# )Disclaimer ‚ö†Ô∏è‚ÄãThe query chain may generate insert/update/delete queries. When this is not expected, use a custom prompt or create a SQL users without write permissions.The final user might overload your SQL database by asking a simple question such as "run the biggest query possible". The generated query