Spaces:
Runtime error
Runtime error
Commit ·
0787976
1
Parent(s): 8266e7b
Agents outputs history
Browse filesCreated a class for keeping the history of the agents outputs
- examples/data_agent_demo.ipynb +208 -132
examples/data_agent_demo.ipynb
CHANGED
|
@@ -2,6 +2,10 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"source": [
|
| 6 |
"# Introduction\n",
|
| 7 |
"\n",
|
|
@@ -20,14 +24,16 @@
|
|
| 20 |
"- gradio\n",
|
| 21 |
"\n",
|
| 22 |
" Before starting your work install all the required tools:"
|
| 23 |
-
]
|
| 24 |
-
"metadata": {
|
| 25 |
-
"id": "w8J6iWjvjZJd"
|
| 26 |
-
},
|
| 27 |
-
"id": "w8J6iWjvjZJd"
|
| 28 |
},
|
| 29 |
{
|
| 30 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"source": [
|
| 32 |
"# Clean out any mixed installs first\n",
|
| 33 |
"%pip uninstall -y langchain langchain-core langchain-community langchain-openai langchain-anthropic langchain-google-vertexai langchain-experimental langgraph langchain-scrapegraph\n",
|
|
@@ -46,16 +52,16 @@
|
|
| 46 |
" \"gradio>=4.0\" \\\n",
|
| 47 |
" \"langchain-experimental\"\\\n",
|
| 48 |
" \"langchain-scrapegraph\""
|
| 49 |
-
]
|
| 50 |
-
"metadata": {
|
| 51 |
-
"id": "L0KlJIwcdCed"
|
| 52 |
-
},
|
| 53 |
-
"execution_count": null,
|
| 54 |
-
"outputs": [],
|
| 55 |
-
"id": "L0KlJIwcdCed"
|
| 56 |
},
|
| 57 |
{
|
| 58 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
"source": [
|
| 60 |
"import sys, importlib.util, importlib.metadata as md\n",
|
| 61 |
"\n",
|
|
@@ -77,16 +83,16 @@
|
|
| 77 |
"print(\"langchain-scrapegraph:\", v(\"langchain-scrapegraph\"))\n",
|
| 78 |
"\n",
|
| 79 |
"print(\"langgraph importable?\", importlib.util.find_spec(\"langgraph\") is not None)"
|
| 80 |
-
]
|
| 81 |
-
"metadata": {
|
| 82 |
-
"id": "AWWI1FJ6ChHN"
|
| 83 |
-
},
|
| 84 |
-
"id": "AWWI1FJ6ChHN",
|
| 85 |
-
"execution_count": null,
|
| 86 |
-
"outputs": []
|
| 87 |
},
|
| 88 |
{
|
| 89 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
"source": [
|
| 91 |
"import os\n",
|
| 92 |
"import io\n",
|
|
@@ -119,16 +125,14 @@
|
|
| 119 |
"\n",
|
| 120 |
"\n",
|
| 121 |
"print(\"✅ Imports OK\")"
|
| 122 |
-
]
|
| 123 |
-
"metadata": {
|
| 124 |
-
"id": "g9izxdgGCl-V"
|
| 125 |
-
},
|
| 126 |
-
"id": "g9izxdgGCl-V",
|
| 127 |
-
"execution_count": null,
|
| 128 |
-
"outputs": []
|
| 129 |
},
|
| 130 |
{
|
| 131 |
"cell_type": "markdown",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
"source": [
|
| 133 |
"To access AzureOpenAI models you'll need to create an Azure account, create a deployment of an Azure OpenAI model, get the name and endpoint for your deployment, get an Azure OpenAI API key and install the langchain-openai integration package.\n",
|
| 134 |
"\n",
|
|
@@ -136,15 +140,12 @@
|
|
| 136 |
"To access SmartScraperTool you will need a ScrapeGraphAI (SGAI) account and get an API key to launch the agent.\n",
|
| 137 |
"\n",
|
| 138 |
"Replace the placeholders with the actual values."
|
| 139 |
-
]
|
| 140 |
-
"metadata": {
|
| 141 |
-
"id": "F5YuW2LyosRX"
|
| 142 |
-
},
|
| 143 |
-
"id": "F5YuW2LyosRX"
|
| 144 |
},
|
| 145 |
{
|
| 146 |
"cell_type": "code",
|
| 147 |
"execution_count": null,
|
|
|
|
| 148 |
"metadata": {
|
| 149 |
"id": "5ldGnv_jPlP8"
|
| 150 |
},
|
|
@@ -153,22 +154,22 @@
|
|
| 153 |
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"INSERT THE AZURE OPENAI ENDPOINT\"\n",
|
| 154 |
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"INSERT YOUR AZURE OPENAI API KEY\"\n",
|
| 155 |
"os.environ[\"SGAI_API_KEY\"] = \"INSERT YOUR SGAI API KEY\""
|
| 156 |
-
]
|
| 157 |
-
"id": "5ldGnv_jPlP8"
|
| 158 |
},
|
| 159 |
{
|
| 160 |
"cell_type": "markdown",
|
| 161 |
-
"
|
| 162 |
-
"To set up the Azure OpenAI model choose the name for ```AZURE_DEPLOYMENT_NAME``` and insert ```AZURE_API_VERSION``` (the latest supported version can be found here: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference)."
|
| 163 |
-
],
|
| 164 |
"metadata": {
|
| 165 |
"id": "TWDsP2d0qP5q"
|
| 166 |
},
|
| 167 |
-
"
|
|
|
|
|
|
|
| 168 |
},
|
| 169 |
{
|
| 170 |
"cell_type": "code",
|
| 171 |
"execution_count": null,
|
|
|
|
| 172 |
"metadata": {
|
| 173 |
"id": "f5e1b596-4568-4078-ae14-b20d25cba62b"
|
| 174 |
},
|
|
@@ -187,11 +188,14 @@
|
|
| 187 |
" streaming=True,\n",
|
| 188 |
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
| 189 |
")\n"
|
| 190 |
-
]
|
| 191 |
-
"id": "f5e1b596-4568-4078-ae14-b20d25cba62b"
|
| 192 |
},
|
| 193 |
{
|
| 194 |
"cell_type": "markdown",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
"source": [
|
| 196 |
"The following block contains prompts that define the agents behaviour.\n",
|
| 197 |
"\n",
|
|
@@ -204,14 +208,16 @@
|
|
| 204 |
"```SCRAPING_PROMPT_PREFIX``` is responsible for the web scraping agent logic. It specifies how the agent should behave and defines its chain of thought when asked to find data online.\n",
|
| 205 |
"\n",
|
| 206 |
"```SCRAPING_PROMPT_SUFFIX``` is responsible for the output of the web scraping agent. It can be changed to set up the format of the output."
|
| 207 |
-
]
|
| 208 |
-
"metadata": {
|
| 209 |
-
"id": "9cMbxsJuyMPb"
|
| 210 |
-
},
|
| 211 |
-
"id": "9cMbxsJuyMPb"
|
| 212 |
},
|
| 213 |
{
|
| 214 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
"source": [
|
| 216 |
"# Prompt prefix to set the tone for the agent.\n",
|
| 217 |
"#By specifying the prompt prefix you may make the results of the agent more specific and consistent.\n",
|
|
@@ -337,29 +343,24 @@
|
|
| 337 |
"})\n",
|
| 338 |
"\n",
|
| 339 |
"'''"
|
| 340 |
-
]
|
| 341 |
-
"metadata": {
|
| 342 |
-
"id": "B6dGxRmnyJHS"
|
| 343 |
-
},
|
| 344 |
-
"execution_count": null,
|
| 345 |
-
"outputs": [],
|
| 346 |
-
"id": "B6dGxRmnyJHS"
|
| 347 |
},
|
| 348 |
{
|
| 349 |
"cell_type": "markdown",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
"source": [
|
| 351 |
"The following block is responsible for the logic of the agent and the output that it produces.\n",
|
| 352 |
"\n",
|
| 353 |
"```ask_agent``` function concatenates the dataframes into one and starts an AI agent for working with the concatenated dataframes. It uses the prompts from the previous blocks for its logic."
|
| 354 |
-
]
|
| 355 |
-
"metadata": {
|
| 356 |
-
"id": "bWNgj913yo8h"
|
| 357 |
-
},
|
| 358 |
-
"id": "bWNgj913yo8h"
|
| 359 |
},
|
| 360 |
{
|
| 361 |
"cell_type": "code",
|
| 362 |
"execution_count": null,
|
|
|
|
| 363 |
"metadata": {
|
| 364 |
"id": "789b46d9-2189-4d3c-8f77-61b4675bf950"
|
| 365 |
},
|
|
@@ -372,7 +373,7 @@
|
|
| 372 |
"\n",
|
| 373 |
"\n",
|
| 374 |
"# --- Agent Logic ---\n",
|
| 375 |
-
"def ask_agent(files, question
|
| 376 |
" try:\n",
|
| 377 |
" dfs = [pd.read_csv(f.name) for f in files]\n",
|
| 378 |
" df = pd.concat(dfs, ignore_index=True) #concatenation of all of the files uploaded into one\n",
|
|
@@ -399,15 +400,18 @@
|
|
| 399 |
" output = result[\"output\"] #retrieves the final answer\n",
|
| 400 |
"\n",
|
| 401 |
"\n",
|
| 402 |
-
" return
|
| 403 |
"\n",
|
| 404 |
" except Exception as e:\n",
|
| 405 |
" return f\"❌ Agent error: {e}\", \"\"\n"
|
| 406 |
-
]
|
| 407 |
-
"id": "789b46d9-2189-4d3c-8f77-61b4675bf950"
|
| 408 |
},
|
| 409 |
{
|
| 410 |
"cell_type": "markdown",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
"source": [
|
| 412 |
"The block below deals with creating SQL code.\n",
|
| 413 |
"\n",
|
|
@@ -418,14 +422,16 @@
|
|
| 418 |
"```extract_code``` is used for extracting the SQL query from the agent's output.\n",
|
| 419 |
"\n",
|
| 420 |
"```sql_pipeline``` defines the pipeline, starting from creating a database with the uploaded dataframes, starting the agent for working with databases and creating the query according to the user's question."
|
| 421 |
-
]
|
| 422 |
-
"metadata": {
|
| 423 |
-
"id": "buR79jvcJSnf"
|
| 424 |
-
},
|
| 425 |
-
"id": "buR79jvcJSnf"
|
| 426 |
},
|
| 427 |
{
|
| 428 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
"source": [
|
| 430 |
"#function create_db receives a dictionary with table names as a key and tables as values\n",
|
| 431 |
"def create_db(files):\n",
|
|
@@ -447,16 +453,16 @@
|
|
| 447 |
" except Exception as e:\n",
|
| 448 |
" return f\"Database error: {e}\"\n",
|
| 449 |
" return db"
|
| 450 |
-
]
|
| 451 |
-
"metadata": {
|
| 452 |
-
"id": "uajELHHaJjUS"
|
| 453 |
-
},
|
| 454 |
-
"id": "uajELHHaJjUS",
|
| 455 |
-
"execution_count": null,
|
| 456 |
-
"outputs": []
|
| 457 |
},
|
| 458 |
{
|
| 459 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
"source": [
|
| 461 |
"#Initialization of a LLM model for SQL queries\n",
|
| 462 |
"def start_llm(database):\n",
|
|
@@ -484,7 +490,7 @@
|
|
| 484 |
"\n",
|
| 485 |
"\n",
|
| 486 |
"#Function that receives dataframes, puts them in a database and uses an AI agent to create quieries based on the user's question\n",
|
| 487 |
-
"def sql_pipeline(tables,question
|
| 488 |
" print(\"=\"*10+\"\\nSQL_PIPELINE\\n\"+\"=\"*10)\n",
|
| 489 |
" db = create_db(tables) #uploads the files added by the user and puts them in a database\n",
|
| 490 |
"\n",
|
|
@@ -502,29 +508,29 @@
|
|
| 502 |
" output += step[\"messages\"][-1].content\n",
|
| 503 |
" #query = extract_code(output)\n",
|
| 504 |
" final_answer = extract_code(output)\n",
|
| 505 |
-
" return
|
| 506 |
" except Exception as e:\n",
|
| 507 |
" return f\"SQL agent error: {e}\""
|
| 508 |
-
]
|
| 509 |
-
"metadata": {
|
| 510 |
-
"id": "35sreALjyVuA"
|
| 511 |
-
},
|
| 512 |
-
"id": "35sreALjyVuA",
|
| 513 |
-
"execution_count": null,
|
| 514 |
-
"outputs": []
|
| 515 |
},
|
| 516 |
{
|
| 517 |
"cell_type": "markdown",
|
| 518 |
-
"
|
| 519 |
-
"THe following block is responsible for creating a smart ETL pipeline"
|
| 520 |
-
],
|
| 521 |
"metadata": {
|
| 522 |
"id": "2Wm72R894eUf"
|
| 523 |
},
|
| 524 |
-
"
|
|
|
|
|
|
|
| 525 |
},
|
| 526 |
{
|
| 527 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
"source": [
|
| 529 |
"@tool\n",
|
| 530 |
"def preview_data(table: str) -> str:\n",
|
|
@@ -561,15 +567,13 @@
|
|
| 561 |
"\n",
|
| 562 |
"#llm is the agent that creates the etl pipeline\n",
|
| 563 |
"#dataframe is a string with the name of the dataframe push through the etl process\n",
|
| 564 |
-
"def etl_pipeline(dataframe
|
| 565 |
" print(\"=\"*10 + \"\\nETL_PIPELINE\\n\"+\"=\"*10)\n",
|
| 566 |
"\n",
|
| 567 |
" try:\n",
|
| 568 |
"\n",
|
| 569 |
" table_name = dataframe[0]\n",
|
| 570 |
"\n",
|
| 571 |
-
"\n",
|
| 572 |
-
"\n",
|
| 573 |
" tools = [preview_data, suggest_transformation, generate_python_code]\n",
|
| 574 |
"\n",
|
| 575 |
" agent = initialize_agent(tools, model, agent='zero-shot-react-description',verbose=True)\n",
|
|
@@ -591,31 +595,31 @@
|
|
| 591 |
" print(\"Generated Python Code:\\n\")\n",
|
| 592 |
" print(response)\n",
|
| 593 |
" response2 = response.strip('`').replace('python', '')\n",
|
| 594 |
-
" return
|
| 595 |
" except Exception as e:\n",
|
| 596 |
" return f'ETL pipeline error: {e}',f'ETL pipeline error: {e}'\n"
|
| 597 |
-
]
|
| 598 |
-
"metadata": {
|
| 599 |
-
"id": "n6sfgj8W4eAA"
|
| 600 |
-
},
|
| 601 |
-
"id": "n6sfgj8W4eAA",
|
| 602 |
-
"execution_count": null,
|
| 603 |
-
"outputs": []
|
| 604 |
},
|
| 605 |
{
|
| 606 |
"cell_type": "markdown",
|
| 607 |
-
"
|
| 608 |
-
"The following code is responsible for AI web scraping agent"
|
| 609 |
-
],
|
| 610 |
"metadata": {
|
| 611 |
"id": "eGX4RmL-ULBU"
|
| 612 |
},
|
| 613 |
-
"
|
|
|
|
|
|
|
| 614 |
},
|
| 615 |
{
|
| 616 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 617 |
"source": [
|
| 618 |
-
"def web_scraping(question
|
| 619 |
" try:\n",
|
| 620 |
" tools = [\n",
|
| 621 |
" SmartScraperTool(),\n",
|
|
@@ -634,19 +638,93 @@
|
|
| 634 |
" with contextlib.redirect_stdout(buffer): #the output is redirected to the buffer\n",
|
| 635 |
" response = agent.run(SCRAPING_PROMPT_PREFIX + question + SCRAPING_PROMPT_SUFFIX)\n",
|
| 636 |
" trace = buffer.getvalue() #the trace of the agent is saved in the trace variable\n",
|
| 637 |
-
" return
|
| 638 |
" except Exception as e:\n",
|
| 639 |
" return f'Web scraping error {e}',f'Web scraping error {e}',\"\""
|
| 640 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 641 |
"metadata": {
|
| 642 |
-
"id": "
|
| 643 |
},
|
| 644 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
"execution_count": null,
|
| 646 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
},
|
| 648 |
{
|
| 649 |
"cell_type": "markdown",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
"source": [
|
| 651 |
"The next section creates a web interface using Gradio, providing a user-friendly way to analyze data and create SQL queries.\n",
|
| 652 |
"\n",
|
|
@@ -682,14 +760,16 @@
|
|
| 682 |
"`demo.launch(share=True,debug=False)` - Public access enabled, debugging disabled\n",
|
| 683 |
"\n",
|
| 684 |
"For debugging use `debug=True` in order to see the messages in the console.\n"
|
| 685 |
-
]
|
| 686 |
-
"metadata": {
|
| 687 |
-
"id": "-oxO_Rkq0ZMv"
|
| 688 |
-
},
|
| 689 |
-
"id": "-oxO_Rkq0ZMv"
|
| 690 |
},
|
| 691 |
{
|
| 692 |
"cell_type": "code",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
"source": [
|
| 694 |
"# --- Gradio UI ---\n",
|
| 695 |
"with gr.Blocks(\n",
|
|
@@ -736,6 +816,8 @@
|
|
| 736 |
" \"\"\"\n",
|
| 737 |
") as demo:\n",
|
| 738 |
"\n",
|
|
|
|
|
|
|
| 739 |
" gr.Markdown(\"<h2 id='title'>📊 NexDatawork Data Agent</h2>\")\n",
|
| 740 |
"\n",
|
| 741 |
" with gr.Column():\n",
|
|
@@ -765,36 +847,33 @@
|
|
| 765 |
"\n",
|
| 766 |
" scraping_button = gr.Button('Find the answer online')\n",
|
| 767 |
"\n",
|
| 768 |
-
" history = gr.State(value=\"\")\n",
|
| 769 |
"\n",
|
| 770 |
-
" sql_button.click(fn=
|
| 771 |
"\n",
|
| 772 |
-
" scraping_button.click(fn=
|
| 773 |
"\n",
|
| 774 |
-
" ask_button.click(fn=
|
| 775 |
-
" ask_button.click(fn=
|
| 776 |
"\n",
|
| 777 |
"demo.launch(share=True,debug=False)"
|
| 778 |
-
]
|
| 779 |
-
"metadata": {
|
| 780 |
-
"id": "OhM41SDzIQdI"
|
| 781 |
-
},
|
| 782 |
-
"id": "OhM41SDzIQdI",
|
| 783 |
-
"execution_count": null,
|
| 784 |
-
"outputs": []
|
| 785 |
},
|
| 786 |
{
|
| 787 |
"cell_type": "code",
|
| 788 |
-
"
|
|
|
|
| 789 |
"metadata": {
|
| 790 |
-
"id": "
|
| 791 |
},
|
| 792 |
-
"
|
| 793 |
-
"
|
| 794 |
-
"outputs": []
|
| 795 |
}
|
| 796 |
],
|
| 797 |
"metadata": {
|
|
|
|
|
|
|
|
|
|
| 798 |
"kernelspec": {
|
| 799 |
"display_name": "Python 3.10 (LangChain)",
|
| 800 |
"language": "python",
|
|
@@ -811,11 +890,8 @@
|
|
| 811 |
"nbconvert_exporter": "python",
|
| 812 |
"pygments_lexer": "ipython3",
|
| 813 |
"version": "3.10.16"
|
| 814 |
-
},
|
| 815 |
-
"colab": {
|
| 816 |
-
"provenance": []
|
| 817 |
}
|
| 818 |
},
|
| 819 |
"nbformat": 4,
|
| 820 |
"nbformat_minor": 5
|
| 821 |
-
}
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
| 5 |
+
"id": "w8J6iWjvjZJd",
|
| 6 |
+
"metadata": {
|
| 7 |
+
"id": "w8J6iWjvjZJd"
|
| 8 |
+
},
|
| 9 |
"source": [
|
| 10 |
"# Introduction\n",
|
| 11 |
"\n",
|
|
|
|
| 24 |
"- gradio\n",
|
| 25 |
"\n",
|
| 26 |
" Before starting your work install all the required tools:"
|
| 27 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
},
|
| 29 |
{
|
| 30 |
"cell_type": "code",
|
| 31 |
+
"execution_count": null,
|
| 32 |
+
"id": "L0KlJIwcdCed",
|
| 33 |
+
"metadata": {
|
| 34 |
+
"id": "L0KlJIwcdCed"
|
| 35 |
+
},
|
| 36 |
+
"outputs": [],
|
| 37 |
"source": [
|
| 38 |
"# Clean out any mixed installs first\n",
|
| 39 |
"%pip uninstall -y langchain langchain-core langchain-community langchain-openai langchain-anthropic langchain-google-vertexai langchain-experimental langgraph langchain-scrapegraph\n",
|
|
|
|
| 52 |
" \"gradio>=4.0\" \\\n",
|
| 53 |
" \"langchain-experimental\"\\\n",
|
| 54 |
" \"langchain-scrapegraph\""
|
| 55 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
},
|
| 57 |
{
|
| 58 |
"cell_type": "code",
|
| 59 |
+
"execution_count": null,
|
| 60 |
+
"id": "AWWI1FJ6ChHN",
|
| 61 |
+
"metadata": {
|
| 62 |
+
"id": "AWWI1FJ6ChHN"
|
| 63 |
+
},
|
| 64 |
+
"outputs": [],
|
| 65 |
"source": [
|
| 66 |
"import sys, importlib.util, importlib.metadata as md\n",
|
| 67 |
"\n",
|
|
|
|
| 83 |
"print(\"langchain-scrapegraph:\", v(\"langchain-scrapegraph\"))\n",
|
| 84 |
"\n",
|
| 85 |
"print(\"langgraph importable?\", importlib.util.find_spec(\"langgraph\") is not None)"
|
| 86 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
},
|
| 88 |
{
|
| 89 |
"cell_type": "code",
|
| 90 |
+
"execution_count": null,
|
| 91 |
+
"id": "g9izxdgGCl-V",
|
| 92 |
+
"metadata": {
|
| 93 |
+
"id": "g9izxdgGCl-V"
|
| 94 |
+
},
|
| 95 |
+
"outputs": [],
|
| 96 |
"source": [
|
| 97 |
"import os\n",
|
| 98 |
"import io\n",
|
|
|
|
| 125 |
"\n",
|
| 126 |
"\n",
|
| 127 |
"print(\"✅ Imports OK\")"
|
| 128 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
},
|
| 130 |
{
|
| 131 |
"cell_type": "markdown",
|
| 132 |
+
"id": "F5YuW2LyosRX",
|
| 133 |
+
"metadata": {
|
| 134 |
+
"id": "F5YuW2LyosRX"
|
| 135 |
+
},
|
| 136 |
"source": [
|
| 137 |
"To access AzureOpenAI models you'll need to create an Azure account, create a deployment of an Azure OpenAI model, get the name and endpoint for your deployment, get an Azure OpenAI API key and install the langchain-openai integration package.\n",
|
| 138 |
"\n",
|
|
|
|
| 140 |
"To access SmartScraperTool you will need a ScrapeGraphAI (SGAI) account and get an API key to launch the agent.\n",
|
| 141 |
"\n",
|
| 142 |
"Replace the placeholders with the actual values."
|
| 143 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
},
|
| 145 |
{
|
| 146 |
"cell_type": "code",
|
| 147 |
"execution_count": null,
|
| 148 |
+
"id": "5ldGnv_jPlP8",
|
| 149 |
"metadata": {
|
| 150 |
"id": "5ldGnv_jPlP8"
|
| 151 |
},
|
|
|
|
| 154 |
"os.environ[\"AZURE_OPENAI_ENDPOINT\"] = \"INSERT THE AZURE OPENAI ENDPOINT\"\n",
|
| 155 |
"os.environ[\"AZURE_OPENAI_API_KEY\"] = \"INSERT YOUR AZURE OPENAI API KEY\"\n",
|
| 156 |
"os.environ[\"SGAI_API_KEY\"] = \"INSERT YOUR SGAI API KEY\""
|
| 157 |
+
]
|
|
|
|
| 158 |
},
|
| 159 |
{
|
| 160 |
"cell_type": "markdown",
|
| 161 |
+
"id": "TWDsP2d0qP5q",
|
|
|
|
|
|
|
| 162 |
"metadata": {
|
| 163 |
"id": "TWDsP2d0qP5q"
|
| 164 |
},
|
| 165 |
+
"source": [
|
| 166 |
+
"To set up the Azure OpenAI model choose the name for ```AZURE_DEPLOYMENT_NAME``` and insert ```AZURE_API_VERSION``` (the latest supported version can be found here: https://learn.microsoft.com/en-us/azure/ai-services/openai/reference)."
|
| 167 |
+
]
|
| 168 |
},
|
| 169 |
{
|
| 170 |
"cell_type": "code",
|
| 171 |
"execution_count": null,
|
| 172 |
+
"id": "f5e1b596-4568-4078-ae14-b20d25cba62b",
|
| 173 |
"metadata": {
|
| 174 |
"id": "f5e1b596-4568-4078-ae14-b20d25cba62b"
|
| 175 |
},
|
|
|
|
| 188 |
" streaming=True,\n",
|
| 189 |
" callbacks=[StreamingStdOutCallbackHandler()],\n",
|
| 190 |
")\n"
|
| 191 |
+
]
|
|
|
|
| 192 |
},
|
| 193 |
{
|
| 194 |
"cell_type": "markdown",
|
| 195 |
+
"id": "9cMbxsJuyMPb",
|
| 196 |
+
"metadata": {
|
| 197 |
+
"id": "9cMbxsJuyMPb"
|
| 198 |
+
},
|
| 199 |
"source": [
|
| 200 |
"The following block contains prompts that define the agents behaviour.\n",
|
| 201 |
"\n",
|
|
|
|
| 208 |
"```SCRAPING_PROMPT_PREFIX``` is responsible for the web scraping agent logic. It specifies how the agent should behave and defines its chain of thought when asked to find data online.\n",
|
| 209 |
"\n",
|
| 210 |
"```SCRAPING_PROMPT_SUFFIX``` is responsible for the output of the web scraping agent. It can be changed to set up the format of the output."
|
| 211 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
},
|
| 213 |
{
|
| 214 |
"cell_type": "code",
|
| 215 |
+
"execution_count": null,
|
| 216 |
+
"id": "B6dGxRmnyJHS",
|
| 217 |
+
"metadata": {
|
| 218 |
+
"id": "B6dGxRmnyJHS"
|
| 219 |
+
},
|
| 220 |
+
"outputs": [],
|
| 221 |
"source": [
|
| 222 |
"# Prompt prefix to set the tone for the agent.\n",
|
| 223 |
"#By specifying the prompt prefix you may make the results of the agent more specific and consistent.\n",
|
|
|
|
| 343 |
"})\n",
|
| 344 |
"\n",
|
| 345 |
"'''"
|
| 346 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
},
|
| 348 |
{
|
| 349 |
"cell_type": "markdown",
|
| 350 |
+
"id": "bWNgj913yo8h",
|
| 351 |
+
"metadata": {
|
| 352 |
+
"id": "bWNgj913yo8h"
|
| 353 |
+
},
|
| 354 |
"source": [
|
| 355 |
"The following block is responsible for the logic of the agent and the output that it produces.\n",
|
| 356 |
"\n",
|
| 357 |
"```ask_agent``` function concatenates the dataframes into one and starts an AI agent for working with the concatenated dataframes. It uses the prompts from the previous blocks for its logic."
|
| 358 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
},
|
| 360 |
{
|
| 361 |
"cell_type": "code",
|
| 362 |
"execution_count": null,
|
| 363 |
+
"id": "789b46d9-2189-4d3c-8f77-61b4675bf950",
|
| 364 |
"metadata": {
|
| 365 |
"id": "789b46d9-2189-4d3c-8f77-61b4675bf950"
|
| 366 |
},
|
|
|
|
| 373 |
"\n",
|
| 374 |
"\n",
|
| 375 |
"# --- Agent Logic ---\n",
|
| 376 |
+
"def ask_agent(files, question):\n",
|
| 377 |
" try:\n",
|
| 378 |
" dfs = [pd.read_csv(f.name) for f in files]\n",
|
| 379 |
" df = pd.concat(dfs, ignore_index=True) #concatenation of all of the files uploaded into one\n",
|
|
|
|
| 400 |
" output = result[\"output\"] #retrieves the final answer\n",
|
| 401 |
"\n",
|
| 402 |
"\n",
|
| 403 |
+
" return output\n",
|
| 404 |
"\n",
|
| 405 |
" except Exception as e:\n",
|
| 406 |
" return f\"❌ Agent error: {e}\", \"\"\n"
|
| 407 |
+
]
|
|
|
|
| 408 |
},
|
| 409 |
{
|
| 410 |
"cell_type": "markdown",
|
| 411 |
+
"id": "buR79jvcJSnf",
|
| 412 |
+
"metadata": {
|
| 413 |
+
"id": "buR79jvcJSnf"
|
| 414 |
+
},
|
| 415 |
"source": [
|
| 416 |
"The block below deals with creating SQL code.\n",
|
| 417 |
"\n",
|
|
|
|
| 422 |
"```extract_code``` is used for extracting the SQL query from the agent's output.\n",
|
| 423 |
"\n",
|
| 424 |
"```sql_pipeline``` defines the pipeline, starting from creating a database with the uploaded dataframes, starting the agent for working with databases and creating the query according to the user's question."
|
| 425 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
},
|
| 427 |
{
|
| 428 |
"cell_type": "code",
|
| 429 |
+
"execution_count": null,
|
| 430 |
+
"id": "uajELHHaJjUS",
|
| 431 |
+
"metadata": {
|
| 432 |
+
"id": "uajELHHaJjUS"
|
| 433 |
+
},
|
| 434 |
+
"outputs": [],
|
| 435 |
"source": [
|
| 436 |
"#function create_db receives a dictionary with table names as a key and tables as values\n",
|
| 437 |
"def create_db(files):\n",
|
|
|
|
| 453 |
" except Exception as e:\n",
|
| 454 |
" return f\"Database error: {e}\"\n",
|
| 455 |
" return db"
|
| 456 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
},
|
| 458 |
{
|
| 459 |
"cell_type": "code",
|
| 460 |
+
"execution_count": null,
|
| 461 |
+
"id": "35sreALjyVuA",
|
| 462 |
+
"metadata": {
|
| 463 |
+
"id": "35sreALjyVuA"
|
| 464 |
+
},
|
| 465 |
+
"outputs": [],
|
| 466 |
"source": [
|
| 467 |
"#Initialization of a LLM model for SQL queries\n",
|
| 468 |
"def start_llm(database):\n",
|
|
|
|
| 490 |
"\n",
|
| 491 |
"\n",
|
| 492 |
"#Function that receives dataframes, puts them in a database and uses an AI agent to create quieries based on the user's question\n",
|
| 493 |
+
"def sql_pipeline(tables,question):\n",
|
| 494 |
" print(\"=\"*10+\"\\nSQL_PIPELINE\\n\"+\"=\"*10)\n",
|
| 495 |
" db = create_db(tables) #uploads the files added by the user and puts them in a database\n",
|
| 496 |
"\n",
|
|
|
|
| 508 |
" output += step[\"messages\"][-1].content\n",
|
| 509 |
" #query = extract_code(output)\n",
|
| 510 |
" final_answer = extract_code(output)\n",
|
| 511 |
+
" return final_answer\n",
|
| 512 |
" except Exception as e:\n",
|
| 513 |
" return f\"SQL agent error: {e}\""
|
| 514 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
},
|
| 516 |
{
|
| 517 |
"cell_type": "markdown",
|
| 518 |
+
"id": "2Wm72R894eUf",
|
|
|
|
|
|
|
| 519 |
"metadata": {
|
| 520 |
"id": "2Wm72R894eUf"
|
| 521 |
},
|
| 522 |
+
"source": [
|
| 523 |
+
"THe following block is responsible for creating a smart ETL pipeline"
|
| 524 |
+
]
|
| 525 |
},
|
| 526 |
{
|
| 527 |
"cell_type": "code",
|
| 528 |
+
"execution_count": null,
|
| 529 |
+
"id": "n6sfgj8W4eAA",
|
| 530 |
+
"metadata": {
|
| 531 |
+
"id": "n6sfgj8W4eAA"
|
| 532 |
+
},
|
| 533 |
+
"outputs": [],
|
| 534 |
"source": [
|
| 535 |
"@tool\n",
|
| 536 |
"def preview_data(table: str) -> str:\n",
|
|
|
|
| 567 |
"\n",
|
| 568 |
"#llm is the agent that creates the etl pipeline\n",
|
| 569 |
"#dataframe is a string with the name of the dataframe push through the etl process\n",
|
| 570 |
+
"def etl_pipeline(dataframe):\n",
|
| 571 |
" print(\"=\"*10 + \"\\nETL_PIPELINE\\n\"+\"=\"*10)\n",
|
| 572 |
"\n",
|
| 573 |
" try:\n",
|
| 574 |
"\n",
|
| 575 |
" table_name = dataframe[0]\n",
|
| 576 |
"\n",
|
|
|
|
|
|
|
| 577 |
" tools = [preview_data, suggest_transformation, generate_python_code]\n",
|
| 578 |
"\n",
|
| 579 |
" agent = initialize_agent(tools, model, agent='zero-shot-react-description',verbose=True)\n",
|
|
|
|
| 595 |
" print(\"Generated Python Code:\\n\")\n",
|
| 596 |
" print(response)\n",
|
| 597 |
" response2 = response.strip('`').replace('python', '')\n",
|
| 598 |
+
" return response2\n",
|
| 599 |
" except Exception as e:\n",
|
| 600 |
" return f'ETL pipeline error: {e}',f'ETL pipeline error: {e}'\n"
|
| 601 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
},
|
| 603 |
{
|
| 604 |
"cell_type": "markdown",
|
| 605 |
+
"id": "eGX4RmL-ULBU",
|
|
|
|
|
|
|
| 606 |
"metadata": {
|
| 607 |
"id": "eGX4RmL-ULBU"
|
| 608 |
},
|
| 609 |
+
"source": [
|
| 610 |
+
"The following code is responsible for AI web scraping agent"
|
| 611 |
+
]
|
| 612 |
},
|
| 613 |
{
|
| 614 |
"cell_type": "code",
|
| 615 |
+
"execution_count": null,
|
| 616 |
+
"id": "97vYgZVOUQnT",
|
| 617 |
+
"metadata": {
|
| 618 |
+
"id": "97vYgZVOUQnT"
|
| 619 |
+
},
|
| 620 |
+
"outputs": [],
|
| 621 |
"source": [
|
| 622 |
+
"def web_scraping(question):\n",
|
| 623 |
" try:\n",
|
| 624 |
" tools = [\n",
|
| 625 |
" SmartScraperTool(),\n",
|
|
|
|
| 638 |
" with contextlib.redirect_stdout(buffer): #the output is redirected to the buffer\n",
|
| 639 |
" response = agent.run(SCRAPING_PROMPT_PREFIX + question + SCRAPING_PROMPT_SUFFIX)\n",
|
| 640 |
" trace = buffer.getvalue() #the trace of the agent is saved in the trace variable\n",
|
| 641 |
+
" return response\n",
|
| 642 |
" except Exception as e:\n",
|
| 643 |
" return f'Web scraping error {e}',f'Web scraping error {e}',\"\""
|
| 644 |
+
]
|
| 645 |
+
},
|
| 646 |
+
{
|
| 647 |
+
"cell_type": "markdown",
|
| 648 |
+
"id": "Emh3GfQLSsP6",
|
| 649 |
"metadata": {
|
| 650 |
+
"id": "Emh3GfQLSsP6"
|
| 651 |
},
|
| 652 |
+
"source": [
|
| 653 |
+
"**AI_tools Class**\n",
|
| 654 |
+
"\n",
|
| 655 |
+
"A utility class that coordinates AI-powered data processing tasks and keeps the history of the agents answers.\n",
|
| 656 |
+
"\n",
|
| 657 |
+
"**Attributes**\n",
|
| 658 |
+
"- `analysis`: Stores general analysis results from AI agents\n",
|
| 659 |
+
"- `sql_etl`: Stores outputs from SQL and ETL pipeline operations\n",
|
| 660 |
+
"\n",
|
| 661 |
+
"**Methods**\n",
|
| 662 |
+
"- `SQL(tables, question)`: Generates SQL pipelines and stores results\n",
|
| 663 |
+
"- `ETL(dataframe)`: Generates ETL pipelines and stores results \n",
|
| 664 |
+
"- `agent_analysis(files, question)`: Performs AI analysis on files and stores results\n",
|
| 665 |
+
"\n",
|
| 666 |
+
"All methods append their outputs to the respective attributes, maintaining a history of operations."
|
| 667 |
+
]
|
| 668 |
+
},
|
| 669 |
+
{
|
| 670 |
+
"cell_type": "code",
|
| 671 |
"execution_count": null,
|
| 672 |
+
"id": "3_PPBrjqC0Os",
|
| 673 |
+
"metadata": {
|
| 674 |
+
"id": "3_PPBrjqC0Os"
|
| 675 |
+
},
|
| 676 |
+
"outputs": [],
|
| 677 |
+
"source": [
|
| 678 |
+
"class AI_tools:\n",
|
| 679 |
+
" def __init__(self):\n",
|
| 680 |
+
" #analysis attribute is for keeping the general analysis of the agent\n",
|
| 681 |
+
" self.analysis = \"\"\n",
|
| 682 |
+
" #sql_etl attribute is for keeping the outputs of the agents that are responsible for the etl and sql pipelines\n",
|
| 683 |
+
" self.sql_etl = \"\"\n",
|
| 684 |
+
" def SQL(self,tables,question):\n",
|
| 685 |
+
" 'SQL pipeline is generated and stored in the sql_etl attribute'\n",
|
| 686 |
+
" try:\n",
|
| 687 |
+
" final_answer = sql_pipeline(tables,question)\n",
|
| 688 |
+
" print(final_answer)\n",
|
| 689 |
+
" self.sql_etl += final_answer + \"\\n\"\n",
|
| 690 |
+
" return self.sql_etl\n",
|
| 691 |
+
" except Exception as e:\n",
|
| 692 |
+
" return f'Impossible to generate SQL query: {e}'\n",
|
| 693 |
+
" def ETL(self,dataframe):\n",
|
| 694 |
+
" 'ETL pipeline is generated and stored in the sql_etl attribute'\n",
|
| 695 |
+
" try:\n",
|
| 696 |
+
" final_answer = etl_pipeline(dataframe)\n",
|
| 697 |
+
" print(final_answer)\n",
|
| 698 |
+
" self.sql_etl += final_answer + \"\\n\"\n",
|
| 699 |
+
" return self.sql_etl\n",
|
| 700 |
+
" except Exception as e:\n",
|
| 701 |
+
" return f'Impossible to generate ETL pipeline: {e}'\n",
|
| 702 |
+
" def agent_analysis(self,files, question):\n",
|
| 703 |
+
" 'Analysis is generated and stored in the analysis attribute'\n",
|
| 704 |
+
" try:\n",
|
| 705 |
+
" final_answer = ask_agent(files, question)\n",
|
| 706 |
+
" print(final_answer)\n",
|
| 707 |
+
" self.analysis += final_answer + \"\\n\"\n",
|
| 708 |
+
" return self.analysis\n",
|
| 709 |
+
" except Exception as e:\n",
|
| 710 |
+
" return f'Impossible to generate analysis: {e}'\n",
|
| 711 |
+
" def web(self,question):\n",
|
| 712 |
+
" try:\n",
|
| 713 |
+
" final_answer = web_scraping(question)\n",
|
| 714 |
+
" print(final_answer)\n",
|
| 715 |
+
" self.analysis += final_answer + \"\\n\"\n",
|
| 716 |
+
"\n",
|
| 717 |
+
" return self.analysis\n",
|
| 718 |
+
" except Exception as e:\n",
|
| 719 |
+
" return f'Impossible to return output: {e}'"
|
| 720 |
+
]
|
| 721 |
},
|
| 722 |
{
|
| 723 |
"cell_type": "markdown",
|
| 724 |
+
"id": "-oxO_Rkq0ZMv",
|
| 725 |
+
"metadata": {
|
| 726 |
+
"id": "-oxO_Rkq0ZMv"
|
| 727 |
+
},
|
| 728 |
"source": [
|
| 729 |
"The next section creates a web interface using Gradio, providing a user-friendly way to analyze data and create SQL queries.\n",
|
| 730 |
"\n",
|
|
|
|
| 760 |
"`demo.launch(share=True,debug=False)` - Public access enabled, debugging disabled\n",
|
| 761 |
"\n",
|
| 762 |
"For debugging use `debug=True` in order to see the messages in the console.\n"
|
| 763 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 764 |
},
|
| 765 |
{
|
| 766 |
"cell_type": "code",
|
| 767 |
+
"execution_count": null,
|
| 768 |
+
"id": "OhM41SDzIQdI",
|
| 769 |
+
"metadata": {
|
| 770 |
+
"id": "OhM41SDzIQdI"
|
| 771 |
+
},
|
| 772 |
+
"outputs": [],
|
| 773 |
"source": [
|
| 774 |
"# --- Gradio UI ---\n",
|
| 775 |
"with gr.Blocks(\n",
|
|
|
|
| 816 |
" \"\"\"\n",
|
| 817 |
") as demo:\n",
|
| 818 |
"\n",
|
| 819 |
+
" agent = AI_tools()\n",
|
| 820 |
+
"\n",
|
| 821 |
" gr.Markdown(\"<h2 id='title'>📊 NexDatawork Data Agent</h2>\")\n",
|
| 822 |
"\n",
|
| 823 |
" with gr.Column():\n",
|
|
|
|
| 847 |
"\n",
|
| 848 |
" scraping_button = gr.Button('Find the answer online')\n",
|
| 849 |
"\n",
|
| 850 |
+
" #history = gr.State(value=\"\")\n",
|
| 851 |
"\n",
|
| 852 |
+
" sql_button.click(fn=agent.SQL,inputs=[file_input,question_input],outputs = [etl_display])\n",
|
| 853 |
"\n",
|
| 854 |
+
" scraping_button.click(fn=agent.web,inputs=[question_input],outputs = [trace_display])\n",
|
| 855 |
"\n",
|
| 856 |
+
" ask_button.click(fn=agent.agent_analysis,inputs=[file_input, question_input],outputs=[trace_display])\n",
|
| 857 |
+
" ask_button.click(fn=agent.ETL,inputs=[file_input],outputs=[etl_display])\n",
|
| 858 |
"\n",
|
| 859 |
"demo.launch(share=True,debug=False)"
|
| 860 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
},
|
| 862 |
{
|
| 863 |
"cell_type": "code",
|
| 864 |
+
"execution_count": null,
|
| 865 |
+
"id": "O1Dlfx9FWXlG",
|
| 866 |
"metadata": {
|
| 867 |
+
"id": "O1Dlfx9FWXlG"
|
| 868 |
},
|
| 869 |
+
"outputs": [],
|
| 870 |
+
"source": []
|
|
|
|
| 871 |
}
|
| 872 |
],
|
| 873 |
"metadata": {
|
| 874 |
+
"colab": {
|
| 875 |
+
"provenance": []
|
| 876 |
+
},
|
| 877 |
"kernelspec": {
|
| 878 |
"display_name": "Python 3.10 (LangChain)",
|
| 879 |
"language": "python",
|
|
|
|
| 890 |
"nbconvert_exporter": "python",
|
| 891 |
"pygments_lexer": "ipython3",
|
| 892 |
"version": "3.10.16"
|
|
|
|
|
|
|
|
|
|
| 893 |
}
|
| 894 |
},
|
| 895 |
"nbformat": 4,
|
| 896 |
"nbformat_minor": 5
|
| 897 |
+
}
|