Docs: Text to sql example - minor naming / dedupe (#376)

* Fix up minor naming oddities

Dedupe insert row logic

* Improve readability
This commit is contained in:
Graham 2025-01-28 09:43:06 +00:00 committed by GitHub
parent 33b38e6cb7
commit 242a6e59c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 10 deletions

View File

@ -56,7 +56,12 @@ from sqlalchemy import (
engine = create_engine("sqlite:///:memory:") engine = create_engine("sqlite:///:memory:")
metadata_obj = MetaData() metadata_obj = MetaData()
# create city SQL table def insert_rows_into_table(rows, table, engine=engine):
for row in rows:
stmt = insert(table).values(**row)
with engine.begin() as connection:
connection.execute(stmt)
table_name = "receipts" table_name = "receipts"
receipts = Table( receipts = Table(
table_name, table_name,
@ -74,10 +79,7 @@ rows = [
{"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43}, {"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43},
{"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00}, {"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00},
] ]
for row in rows: insert_rows_into_table(rows, receipts)
stmt = insert(receipts).values(**row)
with engine.begin() as connection:
cursor = connection.execute(stmt)
``` ```
### Build our agent ### Build our agent
@ -155,7 +157,7 @@ So lets make a second table recording the names of waiters for each receipt_i
```py ```py
table_name = "waiters" table_name = "waiters"
receipts = Table( waiters = Table(
table_name, table_name,
metadata_obj, metadata_obj,
Column("receipt_id", Integer, primary_key=True), Column("receipt_id", Integer, primary_key=True),
@ -169,10 +171,7 @@ rows = [
{"receipt_id": 3, "waiter_name": "Michael Watts"}, {"receipt_id": 3, "waiter_name": "Michael Watts"},
{"receipt_id": 4, "waiter_name": "Margaret James"}, {"receipt_id": 4, "waiter_name": "Margaret James"},
] ]
for row in rows: insert_rows_into_table(rows, waiters)
stmt = insert(receipts).values(**row)
with engine.begin() as connection:
cursor = connection.execute(stmt)
``` ```
Since we changed the table, we update the `SQLExecutorTool` with this tables description to let the LLM properly leverage information from this table. Since we changed the table, we update the `SQLExecutorTool` with this tables description to let the LLM properly leverage information from this table.