mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-06 09:55:21 +03:00
ec727bf166
Currently the chain is getting the column names and types on the one side and the example rows on the other. It is easier for the llm to read the table information if the column name and examples are shown together so that it can easily understand to which columns do the examples refer to. For an instantiation of this, please refer to the changes in the `sqlite.ipynb` notebook. Also changed `eval` for `ast.literal_eval` when interpreting the results from the sample row query since it is a better practice. --------- Co-authored-by: Francisco Ingham <> --------- Co-authored-by: Francisco Ingham <fpingham@gmail.com>
16 KiB
16 KiB
In [1]:
from langchain import OpenAI, SQLDatabase, SQLDatabaseChainIn [2]:
db = SQLDatabase.from_uri("sqlite:///../../../../notebooks/Chinook.db")
llm = OpenAI(temperature=0)In [3]:
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)In [4]:
db_chain.run("How many employees are there?")Out [4]:
[1m> Entering new SQLDatabaseChain chain...[0m How many employees are there? SQLQuery:[32;1m[1;3m SELECT COUNT(*) FROM Employee;[0m SQLResult: [33;1m[1;3m[(8,)][0m Answer:[32;1m[1;3m There are 8 employees.[0m [1m> Finished chain.[0m
' There are 8 employees.'
In [5]:
from langchain.prompts.prompt import PromptTemplate
_DEFAULT_TEMPLATE = """Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer.
Use the following format:
Question: "Question here"
SQLQuery: "SQL Query to run"
SQLResult: "Result of the SQLQuery"
Answer: "Final answer here"
Only use the following tables:
{table_info}
If someone asks for the table foobar, they really mean the employee table.
Question: {input}"""
PROMPT = PromptTemplate(
input_variables=["input", "table_info", "dialect"], template=_DEFAULT_TEMPLATE
)In [6]:
db_chain = SQLDatabaseChain(llm=llm, database=db, prompt=PROMPT, verbose=True)In [7]:
db_chain.run("How many employees are there in the foobar table?")Out [7]:
[1m> Entering new SQLDatabaseChain chain...[0m How many employees are there in the foobar table? SQLQuery:[32;1m[1;3m SELECT COUNT(*) FROM Employee;[0m SQLResult: [33;1m[1;3m[(8,)][0m Answer:[32;1m[1;3m There are 8 employees in the foobar table.[0m [1m> Finished chain.[0m
' There are 8 employees in the foobar table.'
In [8]:
db_chain = SQLDatabaseChain(llm=llm, database=db, prompt=PROMPT, verbose=True, return_intermediate_steps=True)In [9]:
result = db_chain("How many employees are there in the foobar table?")
result["intermediate_steps"]Out [9]:
[1m> Entering new SQLDatabaseChain chain...[0m How many employees are there in the foobar table? SQLQuery:[32;1m[1;3m SELECT COUNT(*) FROM Employee;[0m SQLResult: [33;1m[1;3m[(8,)][0m Answer:[32;1m[1;3m There are 8 employees in the foobar table.[0m [1m> Finished chain.[0m
[' SELECT COUNT(*) FROM Employee;', '[(8,)]']
In [10]:
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True, top_k=3)In [11]:
db_chain.run("What are some example tracks by composer Johann Sebastian Bach?")Out [11]:
[1m> Entering new SQLDatabaseChain chain...[0m
What are some example tracks by composer Johann Sebastian Bach?
SQLQuery:[32;1m[1;3m SELECT Name, Composer FROM Track WHERE Composer = 'Johann Sebastian Bach' LIMIT 3;[0m
SQLResult: [33;1m[1;3m[('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Johann Sebastian Bach'), ('Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria', 'Johann Sebastian Bach'), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', 'Johann Sebastian Bach')][0m
Answer:[32;1m[1;3m Examples of tracks by composer Johann Sebastian Bach are 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria', and 'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude'.[0m
[1m> Finished chain.[0m
' Examples of tracks by composer Johann Sebastian Bach are \'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\', \'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria\', and \'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\'.'
In [13]:
db = SQLDatabase.from_uri(
"sqlite:///../../../../notebooks/Chinook.db",
include_tables=['Track'], # we include only one table to save tokens in the prompt :)
sample_rows_in_table_info=2)In [14]:
print(db.table_info)
Table data will be described in the following format:
Table 'table name' has columns: {column1 name: (column1 type, [list of example values for column1]),
column2 name: (column2 type, [list of example values for column2], ...)
These are the tables you can use, together with their column information:
Table 'Track' has columns: {'TrackId': ['INTEGER', ['1', '2']], 'Name': ['NVARCHAR(200)', ['For Those About To Rock (We Salute You)', 'Balls to the Wall']], 'AlbumId': ['INTEGER', ['1', '2']], 'MediaTypeId': ['INTEGER', ['1', '2']], 'GenreId': ['INTEGER', ['1', '1']], 'Composer': ['NVARCHAR(220)', ['Angus Young, Malcolm Young, Brian Johnson', 'None']], 'Milliseconds': ['INTEGER', ['343719', '342562']], 'Bytes': ['INTEGER', ['11170334', '5510424']], 'UnitPrice': ['NUMERIC(10, 2)', ['0.99', '0.99']]}
In [15]:
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)In [16]:
db_chain.run("What are some example tracks by Bach?")Out [16]:
[1m> Entering new SQLDatabaseChain chain...[0m
What are some example tracks by Bach?
SQLQuery:[32;1m[1;3m SELECT Name FROM Track WHERE Composer LIKE '%Bach%' LIMIT 5;[0m
SQLResult: [33;1m[1;3m[('American Woman',), ('Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace',), ('Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria',), ('Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude',), ('Toccata and Fugue in D Minor, BWV 565: I. Toccata',)][0m
Answer:[32;1m[1;3m Some example tracks by Bach are 'American Woman', 'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace', 'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria', 'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude', and 'Toccata and Fugue in D Minor, BWV 565: I. Toccata'.[0m
[1m> Finished chain.[0m
' Some example tracks by Bach are \'American Woman\', \'Concerto for 2 Violins in D Minor, BWV 1043: I. Vivace\', \'Aria Mit 30 Veränderungen, BWV 988 "Goldberg Variations": Aria\', \'Suite for Solo Cello No. 1 in G Major, BWV 1007: I. Prélude\', and \'Toccata and Fugue in D Minor, BWV 565: I. Toccata\'.'
In [3]:
from langchain.chains import SQLDatabaseSequentialChainIn [4]:
chain = SQLDatabaseSequentialChain.from_llm(llm, db, verbose=True)In [5]:
chain.run("How many employees are also customers?")Out [5]:
[1m> Entering new SQLDatabaseSequentialChain chain...[0m Table names to use: [33;1m[1;3m['Customer', 'Employee'][0m [1m> Entering new SQLDatabaseChain chain...[0m How many employees are also customers? SQLQuery:[32;1m[1;3m SELECT COUNT(*) FROM Customer c INNER JOIN Employee e ON c.SupportRepId = e.EmployeeId;[0m SQLResult: [33;1m[1;3m[(59,)][0m Answer:[32;1m[1;3m There are 59 employees who are also customers.[0m [1m> Finished chain.[0m [1m> Finished chain.[0m
' There are 59 employees who are also customers.'