Files
run-llama--llama_index/docs/examples/agent/agent_workflow_research_assistant.ipynb
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

253 lines
9.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Agent Workflow + Research Assistant using AgentQL\n",
"\n",
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/agent/agent_workflow_research_assistant.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
"\n",
"In this tutorial, we will use an `AgentWorkflow` to build a research assistant OpenAI agent using tools including AgentQL's browser tools, Playwright's tools, and the DuckDuckGoSearch tool. This agent performs a web search to find relevant resources for a research topic, interacts with them, and extracts key metadata (e.g., title, author, publication details, and abstract) from those resources."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initial Setup\n",
"\n",
"The main things we need to get started are:\n",
"\n",
"- <a href=\"https://platform.openai.com/api-keys\" target=\"_blank\">OpenAI's API key</a>\n",
"- <a href=\"https://dev.agentql.com/api-keys\" target=\"_blank\">AgentQL's API key</a>\n",
"\n",
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙 and Playwright."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index\n",
"%pip install llama-index-tools-agentql\n",
"%pip install llama-index-tools-playwright\n",
"%pip install llama-index-tools-duckduckgo\n",
"\n",
"!playwright install"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Store your `OPENAI_API_KEY` and `AGENTQL_API_KEY` keys in <a href=\"https://medium.com/@parthdasawant/how-to-use-secrets-in-google-colab-450c38e3ec75\" target=\"_blank\">Google Colab's secrets</a>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from google.colab import userdata\n",
"\n",
"os.environ[\"AGENTQL_API_KEY\"] = userdata.get(\"AGENTQL_API_KEY\")\n",
"os.environ[\"OPENAI_API_KEY\"] = userdata.get(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's start by enabling async for the notebook since an online environment like Google Colab only supports an asynchronous version of AgentQL."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an `async_browser` instance and select the <a href=\"https://docs.llamaindex.ai/en/latest/api_reference/tools/playwright/\" target=\"_blank\">Playwright tools</a> you want to use."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.tools.playwright.base import PlaywrightToolSpec\n",
"\n",
"async_browser = await PlaywrightToolSpec.create_async_playwright_browser(\n",
" headless=True\n",
")\n",
"\n",
"playwright_tool = PlaywrightToolSpec(async_browser=async_browser)\n",
"playwright_tool_list = playwright_tool.to_tool_list()\n",
"playwright_agent_tool_list = [\n",
" tool\n",
" for tool in playwright_tool_list\n",
" if tool.metadata.name in [\"click\", \"get_current_page\", \"navigate_to\"]\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import the <a href=\"https://docs.llamaindex.ai/en/latest/api_reference/tools/agentql/\" target=\"_blank\">AgentQL browser tools</a> and <a href=\"https://docs.llamaindex.ai/en/latest/api_reference/tools/duckduckgo/\" target=\"_blank\">DuckDuckGo full search tool</a>."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.tools.agentql import AgentQLBrowserToolSpec\n",
"from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec\n",
"\n",
"duckduckgo_search_tool = [\n",
" tool\n",
" for tool in DuckDuckGoSearchToolSpec().to_tool_list()\n",
" if tool.metadata.name == \"duckduckgo_full_search\"\n",
"]\n",
"\n",
"agentql_browser_tool = AgentQLBrowserToolSpec(async_browser=async_browser)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can now create an `AgentWorkFlow` that uses the tools that we have imported."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from llama_index.llms.openai import OpenAI\n",
"from llama_index.core.agent.workflow import AgentWorkflow\n",
"\n",
"llm = OpenAI(model=\"gpt-4o\")\n",
"\n",
"workflow = AgentWorkflow.from_tools_or_functions(\n",
" playwright_agent_tool_list\n",
" + agentql_browser_tool.to_tool_list()\n",
" + duckduckgo_search_tool,\n",
" llm=llm,\n",
" system_prompt=\"You are an expert that can do browser automation, data extraction and text summarization for finding and extracting data from research resources.\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`AgentWorkflow` also supports streaming, which works by using the handler that is returned from the workflow. To stream the LLM output, you can use the `AgentStream` events."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.11/dist-packages/agentql/_core/_utils.py:171: UserWarning: \u001b[31m🚨 The function get_data_by_prompt_experimental is experimental and may not work as expected 🚨\u001b[0m\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"I successfully extracted information from one resource. Here are the details:\n",
"\n",
"- **Title**: Role of Physical Activity on Mental Health and Well-Being: A Review\n",
"- **Authors**: Aditya Mahindru, Pradeep Patil, Varun Agrawal\n",
"- **Link**: [Role of Physical Activity on Mental Health and Well-Being: A Review](https://pmc.ncbi.nlm.nih.gov/articles/PMC9902068/)\n",
"- **Publication Date**: January 7, 2023\n",
"- **Journal Name**: Cureus\n",
"- **Volume Number**: 15\n",
"- **Issue Number**: 1\n",
"- **Abstract**: The article reviews the positive effects of physical activity on mental health, highlighting its benefits on self-concept, body image, and mood. It discusses the physiological and psychological mechanisms by which exercise improves mental health, including its impact on the hypothalamus-pituitary-adrenal axis, depression, anxiety, sleep, and psychiatric disorders. The review also notes the need for more research in the Indian context.\n",
"\n",
"I will now attempt to extract information from another resource.I successfully extracted information from a second resource. Here are the details:\n",
"\n",
"- **Title**: The Relationship Between Exercise Habits and Stress Among Individuals With Access to Internet-Connected Home Fitness Equipment: Single-Group Prospective Analysis\n",
"- **Authors**: Margaret Schneider, Amanda Woodworth, Milad Asgari Mehrabadi\n",
"- **Link**: [The Relationship Between Exercise Habits and Stress Among Individuals With Access to Internet-Connected Home Fitness Equipment](https://pmc.ncbi.nlm.nih.gov/articles/PMC9947760/)\n",
"- **Publication Date**: February 8, 2023\n",
"- **Journal Name**: JMIR Form Res\n",
"- **Volume Number**: 7\n",
"- **Issue Number**: e41877\n",
"- **Abstract**: This study examines the relationship between stress and exercise habits among habitual exercisers with internet-connected home fitness equipment during the COVID-19 lockdown. It found that stress did not negatively impact exercise participation among habitually active adults with such equipment. The study suggests that habitual exercise may buffer the impact of stress on regular moderate to vigorous activity, and highlights the potential role of home-based internet-connected exercise equipment in this buffering.\n",
"\n",
"Both resources provide valuable insights into the relationship between exercise and stress levels."
]
}
],
"source": [
"from llama_index.core.agent.workflow import (\n",
" AgentStream,\n",
")\n",
"\n",
"handler = workflow.run(\n",
" user_msg=\"\"\"\n",
" Use DuckDuckGoSearch to find URL resources on the web that are relevant to the research topic: What is the relationship between exercise and stress levels?\n",
" Go through each resource found. For each different resource, use Playwright to click on link to the resource, then use AgentQL to extract information, including the name of the resource, author name(s), link to the resource, publishing date, journal name, volume number, issue number, and the abstract.\n",
" Find more resources until there are two different resources that can be successfully extracted from.\n",
" \"\"\"\n",
")\n",
"\n",
"async for event in handler.stream_events():\n",
" if isinstance(event, AgentStream):\n",
" print(event.delta, end=\"\", flush=True)"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}