based on the databases' schema as well as on the databases' content (like describing a specific table)It can recover from errors by running a generated query, catching the traceback and regenerating it correctlyTo initialize the agent, we use create_sql_agent function. This agent contains the SQLDatabaseToolkit which contains tools to: Create and execute queriesCheck query syntaxRetrieve table descriptions... and morefrom langchain.agents import create_sql_agentfrom langchain.agents.agent_toolkits import SQLDatabaseToolkit# from langchain.agents import AgentExecutorfrom langchain.agents.agent_types import AgentTypedb = SQLDatabase.from_uri("sqlite:///Chinook.db")llm = OpenAI(temperature=0, verbose=True)agent_executor = create_sql_agent(    llm=OpenAI(temperature=0),    toolkit=SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0)),    verbose=True,    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,)Agent task example #1 - Running queries‚Äãagent_executor.run(    "List the total sales per country. Which country's customers spent the most?")            > Entering new AgentExecutor chain...    Action: sql_db_list_tables    Action Input:     Observation: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track    Thought: I should query the schema of the Invoice and Customer tables.    Action: sql_db_schema    Action Input: Invoice, Customer    Observation:     CREATE TABLE "Customer" (        "CustomerId" INTEGER NOT NULL,         "FirstName" NVARCHAR(40) NOT NULL,         "LastName" NVARCHAR(20) NOT NULL,         "Company" NVARCHAR(80),         "Address" NVARCHAR(70),         "City" NVARCHAR(40),         "State" NVARCHAR(40),         "Country" NVARCHAR(40),         "PostalCode" NVARCHAR(10),         "Phone" NVARCHAR(24),         "Fax" NVARCHAR(24),         "Email" NVARCHAR(60) NOT NULL,         "SupportRepId" INTEGER,         PRIMARY KEY ("CustomerId"),         FOREIGN KEY("SupportRepId") REFERENCES "Employee"
Open In Colab
Open In Colab ->: based on the databases' schema as well as on the databases' content (like describing a specific table)It can recover from errors by running a generated query, catching the traceback and regenerating it correctlyTo initialize the agent, we use create_sql_agent function. This agent contains the SQLDatabaseToolkit which contains tools to: Create and execute queriesCheck query syntaxRetrieve table descriptions... and morefrom langchain.agents import create_sql_agentfrom langchain.agents.agent_toolkits import SQLDatabaseToolkit# from langchain.agents import AgentExecutorfrom langchain.agents.agent_types import AgentTypedb = SQLDatabase.from_uri("sqlite:///Chinook.db")llm = OpenAI(temperature=0, verbose=True)agent_executor = create_sql_agent(    llm=OpenAI(temperature=0),    toolkit=SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0)),    verbose=True,    agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,)Agent task example #1 - Running queries‚Äãagent_executor.run(    "List the total sales per country. Which country's customers spent the most?")            > Entering new AgentExecutor chain...    Action: sql_db_list_tables    Action Input:     Observation: Album, Artist, Customer, Employee, Genre, Invoice, InvoiceLine, MediaType, Playlist, PlaylistTrack, Track    Thought: I should query the schema of the Invoice and Customer tables.    Action: sql_db_schema    Action Input: Invoice, Customer    Observation:     CREATE TABLE "Customer" (        "CustomerId" INTEGER NOT NULL,         "FirstName" NVARCHAR(40) NOT NULL,         "LastName" NVARCHAR(20) NOT NULL,         "Company" NVARCHAR(80),         "Address" NVARCHAR(70),         "City" NVARCHAR(40),         "State" NVARCHAR(40),         "Country" NVARCHAR(40),         "PostalCode" NVARCHAR(10),         "Phone" NVARCHAR(24),         "Fax" NVARCHAR(24),         "Email" NVARCHAR(60) NOT NULL,         "SupportRepId" INTEGER,         PRIMARY KEY ("CustomerId"),         FOREIGN KEY("SupportRepId") REFERENCES "Employee"