Skip to content

Commit 62b0121

Browse files
committed
More cleanups
1 parent ca72287 commit 62b0121

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

topic/chatbot/table-augmented-generation/aws/cratedb_tag_inline_agent.ipynb

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,10 @@
8080
"outputs": [],
8181
"source": [
8282
"# Install dependencies\n",
83-
"!pip install pandas matplotlib sqlalchemy-cratedb python-dotenv ipython-sql\n",
84-
"\n",
85-
"# CrateDB Client\n",
86-
"!pip install crate cratedb-mcp==0.0.0\n",
87-
"\n",
8883
"# tqdm for nicer loading bars\n",
89-
"!pip install tqdm\n",
90-
"\n",
91-
"# AWS Inline Agent library\n",
92-
"!pip install /Users/niklasschmidtmer/Documents/GitHub/amazon-bedrock-agent-samples/src/InlineAgent/dist/inlineagent-0+untagged.276.g0d4f7be.dirty-py3-none-any.whl"
84+
"!pip install -U pandas matplotlib ipython-sql tqdm \\\n",
85+
" crate cratedb-mcp==0.0.0 sqlalchemy-cratedb \\\n",
86+
" inlineagent-0+untagged.276.g0d4f7be.dirty-py3-none-any.whl"
9387
]
9488
},
9589
{
@@ -137,16 +131,7 @@
137131
"execution_count": null,
138132
"id": "2585e16e",
139133
"metadata": {},
140-
"outputs": [
141-
{
142-
"name": "stdout",
143-
"output_type": "stream",
144-
"text": [
145-
"✅ Successfully connected to CrateDB!\n",
146-
"Sample query result from sys.summits: Mont Blanc\n"
147-
]
148-
}
149-
],
134+
"outputs": [],
150135
"source": [
151136
"import os\n",
152137
"import sqlalchemy as sa\n",
@@ -193,15 +178,7 @@
193178
"execution_count": null,
194179
"id": "856582bb",
195180
"metadata": {},
196-
"outputs": [
197-
{
198-
"name": "stdout",
199-
"output_type": "stream",
200-
"text": [
201-
"✅ Table 'motor_readings' created (if not already existing).\n"
202-
]
203-
}
204-
],
181+
"outputs": [],
205182
"source": [
206183
"from sqlalchemy import text\n",
207184
"\n",
@@ -420,14 +397,17 @@
420397
"def fetch_table_schema(table_name):\n",
421398
" \"Fetch the column names and data types for a given table from CrateDB's system catalog.\"\n",
422399
"\n",
423-
" query = \"\"\"\n",
424-
" SELECT column_name, data_type\n",
425-
" FROM information_schema.columns\n",
426-
" WHERE table_name = ?\n",
427-
" ORDER BY ordinal_position\n",
428-
" \"\"\"\n",
400+
" query = text(\n",
401+
" \"\"\"\n",
402+
" SELECT column_name, data_type\n",
403+
" FROM information_schema.columns\n",
404+
" WHERE table_name = :tbl\n",
405+
" ORDER BY ordinal_position\n",
406+
" \"\"\"\n",
407+
" )\n",
429408
" try:\n",
430-
" df = pd.read_sql(query, con=engine, params=(table_name,))\n",
409+
" stmt = query.bindparams(tbl=table_name)\n",
410+
" df = pd.read_sql(stmt, con=engine)\n",
431411
"\n",
432412
" schema_text = f\"Table: {table_name}\\nColumns:\\n\"\n",
433413
" for _, row in df.iterrows():\n",
@@ -534,10 +514,15 @@
534514
" # Prompt the LLM\n",
535515
" response = prompt_llm(table_schema, question)\n",
536516
"\n",
517+
" # Uncomment the line below to see the complete LLM response\n",
518+
" # print(\"Debug: The LLM output was: \\n\", response)\n",
519+
"\n",
537520
" # Extract the actual SQL query from the response\n",
538-
" sql = re.search(\"(?<=`sql)([^`]*)(?=`)\", response).group(1).strip()\n",
521+
" match = re.search(\"(?<=`sql)([^`]*)(?=`)\", response)\n",
522+
" if match:\n",
523+
" return match.group(1).strip()\n",
539524
"\n",
540-
" return sql"
525+
" raise ValueError(\"Failed to extract SQL from LLM reply\", response)"
541526
]
542527
},
543528
{
@@ -712,7 +697,7 @@
712697
},
713698
{
714699
"cell_type": "code",
715-
"execution_count": 3,
700+
"execution_count": null,
716701
"id": "cbbb38f2",
717702
"metadata": {},
718703
"outputs": [],
@@ -912,7 +897,7 @@
912897
"df_manuals = pd.DataFrame(manuals)\n",
913898
"\n",
914899
"# Store in CrateDB\n",
915-
"df_manuals.to_sql(\"machine_manuals\", con=engine, if_exists=\"replace\", index=False)\n",
900+
"df_manuals.to_sql(\"machine_manuals\", con=engine, if_exists=\"append\", index=False)\n",
916901
"print(f\"✅ Stored manuals for {len(df_manuals)} machines in 'machine_manuals'.\")\n",
917902
"_ = connection.execute(sa.text(\"REFRESH TABLE machine_manuals;\"))"
918903
]

0 commit comments

Comments
 (0)