|
80 | 80 | "outputs": [], |
81 | 81 | "source": [ |
82 | 82 | "# 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", |
88 | 83 | "# 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" |
93 | 87 | ] |
94 | 88 | }, |
95 | 89 | { |
|
137 | 131 | "execution_count": null, |
138 | 132 | "id": "2585e16e", |
139 | 133 | "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": [], |
150 | 135 | "source": [ |
151 | 136 | "import os\n", |
152 | 137 | "import sqlalchemy as sa\n", |
|
193 | 178 | "execution_count": null, |
194 | 179 | "id": "856582bb", |
195 | 180 | "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": [], |
205 | 182 | "source": [ |
206 | 183 | "from sqlalchemy import text\n", |
207 | 184 | "\n", |
|
420 | 397 | "def fetch_table_schema(table_name):\n", |
421 | 398 | " \"Fetch the column names and data types for a given table from CrateDB's system catalog.\"\n", |
422 | 399 | "\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", |
429 | 408 | " 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", |
431 | 411 | "\n", |
432 | 412 | " schema_text = f\"Table: {table_name}\\nColumns:\\n\"\n", |
433 | 413 | " for _, row in df.iterrows():\n", |
|
534 | 514 | " # Prompt the LLM\n", |
535 | 515 | " response = prompt_llm(table_schema, question)\n", |
536 | 516 | "\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", |
537 | 520 | " # 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", |
539 | 524 | "\n", |
540 | | - " return sql" |
| 525 | + " raise ValueError(\"Failed to extract SQL from LLM reply\", response)" |
541 | 526 | ] |
542 | 527 | }, |
543 | 528 | { |
|
712 | 697 | }, |
713 | 698 | { |
714 | 699 | "cell_type": "code", |
715 | | - "execution_count": 3, |
| 700 | + "execution_count": null, |
716 | 701 | "id": "cbbb38f2", |
717 | 702 | "metadata": {}, |
718 | 703 | "outputs": [], |
|
912 | 897 | "df_manuals = pd.DataFrame(manuals)\n", |
913 | 898 | "\n", |
914 | 899 | "# 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", |
916 | 901 | "print(f\"✅ Stored manuals for {len(df_manuals)} machines in 'machine_manuals'.\")\n", |
917 | 902 | "_ = connection.execute(sa.text(\"REFRESH TABLE machine_manuals;\"))" |
918 | 903 | ] |
|
0 commit comments