DanilaKopitayko commited on
Commit
dc1985a
Β·
1 Parent(s): 1d71965

Agent output improved

Browse files

Agent output is now in markdown

Files changed (1) hide show
  1. examples/data_agent_demo.ipynb +37 -21
examples/data_agent_demo.ipynb CHANGED
@@ -150,9 +150,9 @@
150
  },
151
  "outputs": [],
152
  "source": [
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
  },
@@ -372,7 +372,7 @@
372
  "\n",
373
  "\n",
374
  "# --- Agent Logic ---\n",
375
- "def ask_agent(files, question):\n",
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,7 +399,7 @@
399
  " output = result[\"output\"] #retrieves the final answer\n",
400
  "\n",
401
  "\n",
402
- " return output, trace\n",
403
  "\n",
404
  " except Exception as e:\n",
405
  " return f\"❌ Agent error: {e}\", \"\"\n"
@@ -481,8 +481,10 @@
481
  " return HumanMessage[p1:]\n",
482
  " except Exception as e:\n",
483
  " print(f'Extraction error: {e}')\n",
 
 
484
  "#Function that receives dataframes, puts them in a database and uses an AI agent to create quieries based on the user's question\n",
485
- "def sql_pipeline(tables,question):\n",
486
  " print(\"=\"*10+\"\\nSQL_PIPELINE\\n\"+\"=\"*10)\n",
487
  " db = create_db(tables) #uploads the files added by the user and puts them in a database\n",
488
  "\n",
@@ -500,7 +502,7 @@
500
  " output += step[\"messages\"][-1].content\n",
501
  " #query = extract_code(output)\n",
502
  " final_answer = extract_code(output)\n",
503
- " return output, final_answer\n",
504
  " except Exception as e:\n",
505
  " return f\"SQL agent error: {e}\""
506
  ],
@@ -559,10 +561,10 @@
559
  "\n",
560
  "#llm is the agent that creates the etl pipeline\n",
561
  "#dataframe is a string with the name of the dataframe push through the etl process\n",
562
- "def etl_pipeline(llm,dataframe):\n",
563
  " tools = [preview_data, suggest_transformation, generate_python_code]\n",
564
  "\n",
565
- " agent = initialize_agent(tools, llm, agent='zero-shot-react-description',verbose=True)\n",
566
  "\n",
567
  " input_prompt = f\"\"\"\n",
568
  " Preview the table {dataframe} and \\\n",
@@ -581,7 +583,7 @@
581
  " print(\"Generated Python Code:\\n\")\n",
582
  " print(response)\n",
583
  " response2 = response.strip('`').replace('python', '')\n",
584
- " return response2\n"
585
  ],
586
  "metadata": {
587
  "id": "n6sfgj8W4eAA"
@@ -603,7 +605,7 @@
603
  {
604
  "cell_type": "code",
605
  "source": [
606
- "def web_scraping(question):\n",
607
  " try:\n",
608
  " tools = [\n",
609
  " SmartScraperTool(),\n",
@@ -622,7 +624,7 @@
622
  " with contextlib.redirect_stdout(buffer): #the output is redirected to the buffer\n",
623
  " response = agent.run(SCRAPING_PROMPT_PREFIX + question + SCRAPING_PROMPT_SUFFIX)\n",
624
  " trace = buffer.getvalue() #the trace of the agent is saved in the trace variable\n",
625
- " return trace, response\n",
626
  " except Exception as e:\n",
627
  " return f'Web scraping error {e}',f'Web scraping error {e}',\"\""
628
  ],
@@ -669,7 +671,7 @@
669
  "\n",
670
  "`demo.launch(share=True,debug=False)` - Public access enabled, debugging disabled\n",
671
  "\n",
672
- "For debugging use `debug=True` in odrder to see the messages in the console.\n"
673
  ],
674
  "metadata": {
675
  "id": "-oxO_Rkq0ZMv"
@@ -687,7 +689,6 @@
687
  " color: #1f2937 !important;\n",
688
  " font-family: 'Segoe UI', sans-serif;\n",
689
  " }\n",
690
- "\n",
691
  " #title {\n",
692
  " color: #1f2937 !important;\n",
693
  " font-size: 2rem;\n",
@@ -703,7 +704,11 @@
703
  " border-radius: 12px !important;\n",
704
  " color: #1f2937 !important;\n",
705
  " }\n",
706
- "\n",
 
 
 
 
707
  " textarea::placeholder, input::placeholder {\n",
708
  " color: rgba(31, 41, 55, 0.6) !important;\n",
709
  " }\n",
@@ -729,23 +734,34 @@
729
  "\n",
730
  " with gr.Row():\n",
731
  "\n",
732
- " trace_display = gr.Textbox(label=\"πŸ› οΈ Data Agent Reasoning - Your Explainable Agent\", lines=20)\n",
 
 
733
  "\n",
734
- " sql_display = gr.Textbox(label='SQL Code',lines=20)\n",
735
  "\n",
736
  " with gr.Row(equal_height=True):\n",
 
737
  " file_input = gr.File(label=\"πŸ“ Upload CSV(s)\", file_types=[\".csv\"], file_count=\"multiple\",height=120)\n",
 
738
  " question_input = gr.Textbox(label=\"πŸ’¬ Ask Your Agent\",placeholder=\"e.g., What is the trend for revenue over time?\",lines=2)\n",
 
739
  " with gr.Row():\n",
 
740
  " ask_button = gr.Button(\"πŸ’‘ Analyze\")\n",
 
741
  " with gr.Row():\n",
 
742
  " sql_button = gr.Button('Create Query')\n",
 
743
  " scraping_button = gr.Button('Find the answer online')\n",
744
  "\n",
 
 
 
 
 
745
  "\n",
746
- " sql_button.click(fn=sql_pipeline,inputs=[file_input,question_input],outputs = [sql_display,result_display])\n",
747
- " scraping_button.click(fn=web_scraping,inputs=[question_input],outputs = [trace_display,result_display])\n",
748
- " ask_button.click(fn=ask_agent,inputs=[file_input, question_input],outputs=[result_display, trace_display])\n",
749
  "\n",
750
  "demo.launch(share=True,debug=False)"
751
  ],
@@ -760,9 +776,9 @@
760
  "cell_type": "code",
761
  "source": [],
762
  "metadata": {
763
- "id": "_cjnKT-o8SS5"
764
  },
765
- "id": "_cjnKT-o8SS5",
766
  "execution_count": null,
767
  "outputs": []
768
  }
 
150
  },
151
  "outputs": [],
152
  "source": [
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
  },
 
372
  "\n",
373
  "\n",
374
  "# --- Agent Logic ---\n",
375
+ "def ask_agent(files, question, history):\n",
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
  " output = result[\"output\"] #retrieves the final answer\n",
400
  "\n",
401
  "\n",
402
+ " return history + output, output\n",
403
  "\n",
404
  " except Exception as e:\n",
405
  " return f\"❌ Agent error: {e}\", \"\"\n"
 
481
  " return HumanMessage[p1:]\n",
482
  " except Exception as e:\n",
483
  " print(f'Extraction error: {e}')\n",
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,history):\n",
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
  " output += step[\"messages\"][-1].content\n",
503
  " #query = extract_code(output)\n",
504
  " final_answer = extract_code(output)\n",
505
+ " return history + final_answer, final_answer\n",
506
  " except Exception as e:\n",
507
  " return f\"SQL agent error: {e}\""
508
  ],
 
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,history):\n",
565
  " tools = [preview_data, suggest_transformation, generate_python_code]\n",
566
  "\n",
567
+ " agent = initialize_agent(tools, model, agent='zero-shot-react-description',verbose=True)\n",
568
  "\n",
569
  " input_prompt = f\"\"\"\n",
570
  " Preview the table {dataframe} and \\\n",
 
583
  " print(\"Generated Python Code:\\n\")\n",
584
  " print(response)\n",
585
  " response2 = response.strip('`').replace('python', '')\n",
586
+ " return history + response2, response2\n"
587
  ],
588
  "metadata": {
589
  "id": "n6sfgj8W4eAA"
 
605
  {
606
  "cell_type": "code",
607
  "source": [
608
+ "def web_scraping(question,history):\n",
609
  " try:\n",
610
  " tools = [\n",
611
  " SmartScraperTool(),\n",
 
624
  " with contextlib.redirect_stdout(buffer): #the output is redirected to the buffer\n",
625
  " response = agent.run(SCRAPING_PROMPT_PREFIX + question + SCRAPING_PROMPT_SUFFIX)\n",
626
  " trace = buffer.getvalue() #the trace of the agent is saved in the trace variable\n",
627
+ " return history + response, response\n",
628
  " except Exception as e:\n",
629
  " return f'Web scraping error {e}',f'Web scraping error {e}',\"\""
630
  ],
 
671
  "\n",
672
  "`demo.launch(share=True,debug=False)` - Public access enabled, debugging disabled\n",
673
  "\n",
674
+ "For debugging use `debug=True` in order to see the messages in the console.\n"
675
  ],
676
  "metadata": {
677
  "id": "-oxO_Rkq0ZMv"
 
689
  " color: #1f2937 !important;\n",
690
  " font-family: 'Segoe UI', sans-serif;\n",
691
  " }\n",
 
692
  " #title {\n",
693
  " color: #1f2937 !important;\n",
694
  " font-size: 2rem;\n",
 
704
  " border-radius: 12px !important;\n",
705
  " color: #1f2937 !important;\n",
706
  " }\n",
707
+ " .trace-markdown {\n",
708
+ " height: 400px !important;\n",
709
+ " overflow-y: scroll;\n",
710
+ " resize: none;\n",
711
+ " }\n",
712
  " textarea::placeholder, input::placeholder {\n",
713
  " color: rgba(31, 41, 55, 0.6) !important;\n",
714
  " }\n",
 
734
  "\n",
735
  " with gr.Row():\n",
736
  "\n",
737
+ " trace_display = gr.Markdown(label=\"πŸ› οΈ Data Agent Reasoning - Your Explainable Agent\", elem_classes=[\"trace-markdown\"])\n",
738
+ "\n",
739
+ " sql_display = gr.Markdown(label='SQL Process')\n",
740
  "\n",
 
741
  "\n",
742
  " with gr.Row(equal_height=True):\n",
743
+ "\n",
744
  " file_input = gr.File(label=\"πŸ“ Upload CSV(s)\", file_types=[\".csv\"], file_count=\"multiple\",height=120)\n",
745
+ "\n",
746
  " question_input = gr.Textbox(label=\"πŸ’¬ Ask Your Agent\",placeholder=\"e.g., What is the trend for revenue over time?\",lines=2)\n",
747
+ "\n",
748
  " with gr.Row():\n",
749
+ "\n",
750
  " ask_button = gr.Button(\"πŸ’‘ Analyze\")\n",
751
+ "\n",
752
  " with gr.Row():\n",
753
+ "\n",
754
  " sql_button = gr.Button('Create Query')\n",
755
+ "\n",
756
  " scraping_button = gr.Button('Find the answer online')\n",
757
  "\n",
758
+ " history = gr.State(value=\"\")\n",
759
+ "\n",
760
+ " sql_button.click(fn=sql_pipeline,inputs=[file_input,question_input,history],outputs = [trace_display,history])\n",
761
+ "\n",
762
+ " scraping_button.click(fn=web_scraping,inputs=[question_input,history],outputs = [trace_display,history])\n",
763
  "\n",
764
+ " ask_button.click(fn=ask_agent,inputs=[file_input, question_input,history],outputs=[trace_display,history])\n",
 
 
765
  "\n",
766
  "demo.launch(share=True,debug=False)"
767
  ],
 
776
  "cell_type": "code",
777
  "source": [],
778
  "metadata": {
779
+ "id": "4cty6sAp4HCz"
780
  },
781
+ "id": "4cty6sAp4HCz",
782
  "execution_count": null,
783
  "outputs": []
784
  }