mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-28 01:19:31 +00:00
fix sql (#1141)
This commit is contained in:
parent
85c1449a96
commit
45b5640fe5
@ -29,7 +29,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 1,
|
||||||
"id": "d0e27d88",
|
"id": "d0e27d88",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
@ -43,7 +43,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 2,
|
||||||
"id": "72ede462",
|
"id": "72ede462",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"pycharm": {
|
"pycharm": {
|
||||||
@ -92,7 +92,22 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
||||||
"How many employees are there? \n",
|
"How many employees are there? \n",
|
||||||
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee;\u001b[0m\n",
|
"SQLQuery:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"/Users/harrisonchase/workplace/langchain/langchain/sql_database.py:120: SAWarning: Dialect sqlite+pysqlite does *not* support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage.\n",
|
||||||
|
" sample_rows = connection.execute(command)\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee;\u001b[0m\n",
|
||||||
"SQLResult: \u001b[33;1m\u001b[1;3m[(8,)]\u001b[0m\n",
|
"SQLResult: \u001b[33;1m\u001b[1;3m[(8,)]\u001b[0m\n",
|
||||||
"Answer:\u001b[32;1m\u001b[1;3m There are 8 employees.\u001b[0m\n",
|
"Answer:\u001b[32;1m\u001b[1;3m There are 8 employees.\u001b[0m\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
@ -285,16 +300,16 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
||||||
"What are some example tracks by composer Johann Sebastian Bach? \n",
|
"What are some example tracks by composer Johann Sebastian Bach? \n",
|
||||||
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Name, Composer FROM Track WHERE Composer = 'Johann Sebastian Bach' LIMIT 3;\u001b[0m\n",
|
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT Name, Composer FROM Track WHERE Composer LIKE '%Johann Sebastian Bach%' LIMIT 3;\u001b[0m\n",
|
||||||
"SQLResult: \u001b[33;1m\u001b[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')]\u001b[0m\n",
|
"SQLResult: \u001b[33;1m\u001b[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')]\u001b[0m\n",
|
||||||
"Answer:\u001b[32;1m\u001b[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'.\u001b[0m\n",
|
"Answer:\u001b[32;1m\u001b[1;3m Some example 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'.\u001b[0m\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"' 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\\'.'"
|
"' Some example 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\\'.'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 11,
|
"execution_count": 11,
|
||||||
@ -317,7 +332,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 12,
|
||||||
"id": "9a22ee47",
|
"id": "9a22ee47",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -338,7 +353,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 14,
|
"execution_count": 13,
|
||||||
"id": "9de86267",
|
"id": "9de86267",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -346,43 +361,24 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"CREATE TABLE [Album]\n",
|
"\n",
|
||||||
"(\n",
|
"CREATE TABLE \"Track\" (\n",
|
||||||
" [AlbumId] INTEGER NOT NULL,\n",
|
"\t\"TrackId\" INTEGER NOT NULL, \n",
|
||||||
" [Title] NVARCHAR(160) NOT NULL,\n",
|
"\t\"Name\" NVARCHAR(200) NOT NULL, \n",
|
||||||
" [ArtistId] INTEGER NOT NULL,\n",
|
"\t\"AlbumId\" INTEGER, \n",
|
||||||
" CONSTRAINT [PK_Album] PRIMARY KEY ([AlbumId]),\n",
|
"\t\"MediaTypeId\" INTEGER NOT NULL, \n",
|
||||||
" FOREIGN KEY ([ArtistId]) REFERENCES [Artist] ([ArtistId]) \n",
|
"\t\"GenreId\" INTEGER, \n",
|
||||||
"\t\tON DELETE NO ACTION ON UPDATE NO ACTION\n",
|
"\t\"Composer\" NVARCHAR(220), \n",
|
||||||
|
"\t\"Milliseconds\" INTEGER NOT NULL, \n",
|
||||||
|
"\t\"Bytes\" INTEGER, \n",
|
||||||
|
"\t\"UnitPrice\" NUMERIC(10, 2) NOT NULL, \n",
|
||||||
|
"\tPRIMARY KEY (\"TrackId\"), \n",
|
||||||
|
"\tFOREIGN KEY(\"MediaTypeId\") REFERENCES \"MediaType\" (\"MediaTypeId\"), \n",
|
||||||
|
"\tFOREIGN KEY(\"GenreId\") REFERENCES \"Genre\" (\"GenreId\"), \n",
|
||||||
|
"\tFOREIGN KEY(\"AlbumId\") REFERENCES \"Album\" (\"AlbumId\")\n",
|
||||||
")\n",
|
")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"SELECT * FROM 'Album' LIMIT 2\n",
|
"SELECT * FROM 'Track' LIMIT 2;\n",
|
||||||
"AlbumId Title ArtistId\n",
|
|
||||||
"1 For Those About To Rock We Salute You 1\n",
|
|
||||||
"2 Balls to the Wall 2\n",
|
|
||||||
"\n",
|
|
||||||
"\n",
|
|
||||||
"CREATE TABLE [Track]\n",
|
|
||||||
"(\n",
|
|
||||||
" [TrackId] INTEGER NOT NULL,\n",
|
|
||||||
" [Name] NVARCHAR(200) NOT NULL,\n",
|
|
||||||
" [AlbumId] INTEGER,\n",
|
|
||||||
" [MediaTypeId] INTEGER NOT NULL,\n",
|
|
||||||
" [GenreId] INTEGER,\n",
|
|
||||||
" [Composer] NVARCHAR(220),\n",
|
|
||||||
" [Milliseconds] INTEGER NOT NULL,\n",
|
|
||||||
" [Bytes] INTEGER,\n",
|
|
||||||
" [UnitPrice] NUMERIC(10,2) NOT NULL,\n",
|
|
||||||
" CONSTRAINT [PK_Track] PRIMARY KEY ([TrackId]),\n",
|
|
||||||
" FOREIGN KEY ([AlbumId]) REFERENCES [Album] ([AlbumId]) \n",
|
|
||||||
"\t\tON DELETE NO ACTION ON UPDATE NO ACTION,\n",
|
|
||||||
" FOREIGN KEY ([GenreId]) REFERENCES [Genre] ([GenreId]) \n",
|
|
||||||
"\t\tON DELETE NO ACTION ON UPDATE NO ACTION,\n",
|
|
||||||
" FOREIGN KEY ([MediaTypeId]) REFERENCES [MediaType] ([MediaTypeId]) \n",
|
|
||||||
"\t\tON DELETE NO ACTION ON UPDATE NO ACTION\n",
|
|
||||||
")\n",
|
|
||||||
"\n",
|
|
||||||
"SELECT * FROM 'Track' LIMIT 2\n",
|
|
||||||
"TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice\n",
|
"TrackId Name AlbumId MediaTypeId GenreId Composer Milliseconds Bytes UnitPrice\n",
|
||||||
"1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.99\n",
|
"1 For Those About To Rock (We Salute You) 1 1 1 Angus Young, Malcolm Young, Brian Johnson 343719 11170334 0.99\n",
|
||||||
"2 Balls to the Wall 2 2 1 None 342562 5510424 0.99\n"
|
"2 Balls to the Wall 2 2 1 None 342562 5510424 0.99\n"
|
||||||
@ -395,7 +391,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": 14,
|
||||||
"id": "bcb7a489",
|
"id": "bcb7a489",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -405,7 +401,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 16,
|
"execution_count": 15,
|
||||||
"id": "81e05d82",
|
"id": "81e05d82",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -429,7 +425,7 @@
|
|||||||
"' 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\\'.'"
|
"' 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\\'.'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 16,
|
"execution_count": 15,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -457,17 +453,18 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 20,
|
||||||
"id": "e59a4740",
|
"id": "e59a4740",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.chains import SQLDatabaseSequentialChain"
|
"from langchain.chains import SQLDatabaseSequentialChain\n",
|
||||||
|
"db = SQLDatabase.from_uri(\"sqlite:///../../../../notebooks/Chinook.db\")"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 21,
|
||||||
"id": "58bb49b6",
|
"id": "58bb49b6",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
@ -477,7 +474,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 22,
|
||||||
"id": "95017b1a",
|
"id": "95017b1a",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -493,9 +490,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
"\u001b[1m> Entering new SQLDatabaseChain chain...\u001b[0m\n",
|
||||||
"How many employees are also customers? \n",
|
"How many employees are also customers? \n",
|
||||||
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Customer c INNER JOIN Employee e ON c.SupportRepId = e.EmployeeId;\u001b[0m\n",
|
"SQLQuery:\u001b[32;1m\u001b[1;3m SELECT COUNT(*) FROM Employee INNER JOIN Customer ON Employee.EmployeeId = Customer.SupportRepId;\u001b[0m\n",
|
||||||
"SQLResult: \u001b[33;1m\u001b[1;3m[(59,)]\u001b[0m\n",
|
"SQLResult: \u001b[33;1m\u001b[1;3m[(59,)]\u001b[0m\n",
|
||||||
"Answer:\u001b[32;1m\u001b[1;3m There are 59 employees who are also customers.\u001b[0m\n",
|
"Answer:\u001b[32;1m\u001b[1;3m 59 employees are also customers.\u001b[0m\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n",
|
"\u001b[1m> Finished chain.\u001b[0m\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\u001b[1m> Finished chain.\u001b[0m\n"
|
"\u001b[1m> Finished chain.\u001b[0m\n"
|
||||||
@ -504,10 +501,10 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"' There are 59 employees who are also customers.'"
|
"' 59 employees are also customers.'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 5,
|
"execution_count": 22,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -515,6 +512,14 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"chain.run(\"How many employees are also customers?\")"
|
"chain.run(\"How many employees are also customers?\")"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "5eb39db6",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -523,9 +528,9 @@
|
|||||||
"lastKernelId": null
|
"lastKernelId": null
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "langchain",
|
"display_name": "Python 3 (ipykernel)",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "langchain"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
@ -537,7 +542,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.8.16"
|
"version": "3.9.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
@ -114,15 +114,14 @@ class SQLDatabase:
|
|||||||
# save the columns in string format
|
# save the columns in string format
|
||||||
columns_str = " ".join([col.name for col in table.columns])
|
columns_str = " ".join([col.name for col in table.columns])
|
||||||
|
|
||||||
# get the sample rows
|
|
||||||
with self._engine.connect() as connection:
|
|
||||||
sample_rows = connection.execute(command)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# shorten values in the smaple rows
|
# get the sample rows
|
||||||
sample_rows = list(
|
with self._engine.connect() as connection:
|
||||||
map(lambda ls: [str(i)[:100] for i in ls], sample_rows)
|
sample_rows = connection.execute(command)
|
||||||
)
|
# shorten values in the sample rows
|
||||||
|
sample_rows = list(
|
||||||
|
map(lambda ls: [str(i)[:100] for i in ls], sample_rows)
|
||||||
|
)
|
||||||
|
|
||||||
# save the sample rows in string format
|
# save the sample rows in string format
|
||||||
sample_rows_str = "\n".join([" ".join(row) for row in sample_rows])
|
sample_rows_str = "\n".join([" ".join(row) for row in sample_rows])
|
||||||
|
Loading…
Reference in New Issue
Block a user