From 242a6e59c5ac85117c5ce3a953c06e9d397f8687 Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 28 Jan 2025 09:43:06 +0000 Subject: [PATCH] Docs: Text to sql example - minor naming / dedupe (#376) * Fix up minor naming oddities Dedupe insert row logic * Improve readability --- docs/source/en/examples/text_to_sql.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/docs/source/en/examples/text_to_sql.md b/docs/source/en/examples/text_to_sql.md index 16cf821..600d8d9 100644 --- a/docs/source/en/examples/text_to_sql.md +++ b/docs/source/en/examples/text_to_sql.md @@ -56,7 +56,12 @@ from sqlalchemy import ( engine = create_engine("sqlite:///:memory:") 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" receipts = Table( table_name, @@ -74,10 +79,7 @@ rows = [ {"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}, ] -for row in rows: - stmt = insert(receipts).values(**row) - with engine.begin() as connection: - cursor = connection.execute(stmt) +insert_rows_into_table(rows, receipts) ``` ### Build our agent @@ -155,7 +157,7 @@ So let’s make a second table recording the names of waiters for each receipt_i ```py table_name = "waiters" -receipts = Table( +waiters = Table( table_name, metadata_obj, Column("receipt_id", Integer, primary_key=True), @@ -169,10 +171,7 @@ rows = [ {"receipt_id": 3, "waiter_name": "Michael Watts"}, {"receipt_id": 4, "waiter_name": "Margaret James"}, ] -for row in rows: - stmt = insert(receipts).values(**row) - with engine.begin() as connection: - cursor = connection.execute(stmt) +insert_rows_into_table(rows, waiters) ``` Since we changed the table, we update the `SQLExecutorTool` with this table’s description to let the LLM properly leverage information from this table.