Spaces:
Sleeping
Sleeping
e-candeloro commited on
Commit ·
6b2d886
1
Parent(s): c753d39
updated readme and deleted notebooks
Browse files- README.md +12 -12
- image_processing.py +0 -28
- media/demo_agent_ui.png +0 -0
- notebooks/agent_test.ipynb +0 -1623
README.md
CHANGED
|
@@ -14,16 +14,20 @@ hf_oauth_expiration_minutes: 480
|
|
| 14 |
|
| 15 |
## 🚀 HF Agent GAIA 🕵🏻♂️
|
| 16 |
|
| 17 |
-
This repository contains an AI agent developed for the [Hugging Face Agents Course](https://huggingface.co/agents-course). The primary goal of this project is to create a robust agent capable of tackling tasks from the [GAIA Benchmark](https://huggingface.co/gaia-benchmark).
|
| 18 |
-
|
| 19 |
-
The agent is built using Python and leverages the power of [LangGraph](https://langchain-ai.github.io/langgraph/) for creating a stateful, multi-actor agent. It interacts with various tools to gather information, perform actions, and ultimately solve complex problems.
|
| 20 |
-
|
| 21 |
[](https://www.python.org/)
|
| 22 |
[](https://langchain-ai.github.io/langgraph/)
|
| 23 |
[](https://huggingface.co/spaces/ecandeloro/hf_agent_gaia_30)
|
| 24 |
[](https://github.com/astral-sh/ruff)
|
| 25 |
[](https://github.com/astral-sh/uv)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
### 🛠️ Setup with `uv`
|
| 28 |
|
| 29 |
This project uses `uv` for Python package management. `uv` is a fast Python package installer and resolver, written in Rust.
|
|
@@ -50,8 +54,8 @@ To use this agent, you will need API keys for the following services:
|
|
| 50 |
Once you have your keys, create a `.env` file in the root of the project and add your keys like this:
|
| 51 |
|
| 52 |
```env
|
| 53 |
-
GROQ_API_KEY=
|
| 54 |
-
TAVILY_API_KEY=
|
| 55 |
```
|
| 56 |
|
| 57 |
Replace `gsk_YOUR_GROQ_API_KEY` and `tvly-YOUR_TAVILY_API_KEY` with your actual API keys. The agent will load these keys automatically.
|
|
@@ -63,14 +67,12 @@ The core of the agent is built with LangGraph. It follows a ReAct (Reason + Act)
|
|
| 63 |
1. **LLM**: The agent uses a Large Language Model (LLM) hosted on Groq (e.g., `qwen/qwen3-32b` or `llama3-8b-8192`) for its reasoning capabilities.
|
| 64 |
2. **Prompting**: A base prompt (`base_prompt.txt`) guides the LLM's behavior, instructing it on how to use the available tools and respond to user queries.
|
| 65 |
3. **Tools**: The agent has access to a suite of tools to interact with the external world. These tools allow it to:
|
| 66 |
-
* Perform mathematical calculations (e.g., `calculator`, `multiply`,
|
| 67 |
* Search the web and specific platforms (e.g., `web_search` via Tavily, `wiki_search`, `arxiv_search`).
|
| 68 |
* Read and write files (e.g., `read_file`, `write_file`, `list_files`).
|
| 69 |
* Download files from URLs (`download_file`).
|
| 70 |
* Fetch and parse web page content (`get_url`, `get_url_text`).
|
| 71 |
* Process images (captioning with `image_captioner`, OCR with `ocr`).
|
| 72 |
-
* Execute Python code (`python_interpreter`).
|
| 73 |
-
* Ask for human clarification if stuck (`ask_human_for_help`).
|
| 74 |
4. **Graph**: The LangGraph framework orchestrates the flow of information between the LLM and the tools. The `agent.py` file defines the graph structure, including:
|
| 75 |
* An `AgentState` to hold the current state of the conversation and any input files.
|
| 76 |
* An `assistant` node that calls the LLM.
|
|
@@ -87,6 +89,4 @@ The `create_react_agent` function from `langgraph.prebuilt` is used to quickly s
|
|
| 87 |
* `base_prompt.txt`: The system prompt for the LLM.
|
| 88 |
* `pyproject.toml`: Project metadata and dependencies for `uv`.
|
| 89 |
* `requirements.txt`: List of Python dependencies (can be generated from `pyproject.toml`).
|
| 90 |
-
* `tools.py`: Defines all the tools available to the agent.
|
| 91 |
-
* `notebooks/`: Contains Jupyter notebooks for testing and experimentation.
|
| 92 |
-
* `agent_test.ipynb`: A notebook for testing the agent.
|
|
|
|
| 14 |
|
| 15 |
## 🚀 HF Agent GAIA 🕵🏻♂️
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
[](https://www.python.org/)
|
| 18 |
[](https://langchain-ai.github.io/langgraph/)
|
| 19 |
[](https://huggingface.co/spaces/ecandeloro/hf_agent_gaia_30)
|
| 20 |
[](https://github.com/astral-sh/ruff)
|
| 21 |
[](https://github.com/astral-sh/uv)
|
| 22 |
|
| 23 |
+
This repository contains an AI agent developed for the [Hugging Face Agents Course](https://huggingface.co/agents-course). The primary goal of this project is to create a robust agent capable of tackling tasks from the [GAIA Benchmark](https://huggingface.co/gaia-benchmark).
|
| 24 |
+
|
| 25 |
+
The agent is built using Python and leverages the power of [LangGraph](https://langchain-ai.github.io/langgraph/) for creating a stateful, multi-actor agent. It interacts with various tools to gather information, perform actions, and ultimately solve complex problems.
|
| 26 |
+
|
| 27 |
+
### 📸 Demo
|
| 28 |
+
<p align="center">
|
| 29 |
+
<img src="media/demo_agent_ui.png" alt="demo_ui_screen" style="max-width: 600px; width: 100%;">
|
| 30 |
+
</p>
|
| 31 |
### 🛠️ Setup with `uv`
|
| 32 |
|
| 33 |
This project uses `uv` for Python package management. `uv` is a fast Python package installer and resolver, written in Rust.
|
|
|
|
| 54 |
Once you have your keys, create a `.env` file in the root of the project and add your keys like this:
|
| 55 |
|
| 56 |
```env
|
| 57 |
+
GROQ_API_KEY="gsk_YOUR_GROQ_API_KEY"
|
| 58 |
+
TAVILY_API_KEY="tvly-YOUR_TAVILY_API_KEY"
|
| 59 |
```
|
| 60 |
|
| 61 |
Replace `gsk_YOUR_GROQ_API_KEY` and `tvly-YOUR_TAVILY_API_KEY` with your actual API keys. The agent will load these keys automatically.
|
|
|
|
| 67 |
1. **LLM**: The agent uses a Large Language Model (LLM) hosted on Groq (e.g., `qwen/qwen3-32b` or `llama3-8b-8192`) for its reasoning capabilities.
|
| 68 |
2. **Prompting**: A base prompt (`base_prompt.txt`) guides the LLM's behavior, instructing it on how to use the available tools and respond to user queries.
|
| 69 |
3. **Tools**: The agent has access to a suite of tools to interact with the external world. These tools allow it to:
|
| 70 |
+
* Perform mathematical calculations (e.g., `calculator`, `multiply`, ...).
|
| 71 |
* Search the web and specific platforms (e.g., `web_search` via Tavily, `wiki_search`, `arxiv_search`).
|
| 72 |
* Read and write files (e.g., `read_file`, `write_file`, `list_files`).
|
| 73 |
* Download files from URLs (`download_file`).
|
| 74 |
* Fetch and parse web page content (`get_url`, `get_url_text`).
|
| 75 |
* Process images (captioning with `image_captioner`, OCR with `ocr`).
|
|
|
|
|
|
|
| 76 |
4. **Graph**: The LangGraph framework orchestrates the flow of information between the LLM and the tools. The `agent.py` file defines the graph structure, including:
|
| 77 |
* An `AgentState` to hold the current state of the conversation and any input files.
|
| 78 |
* An `assistant` node that calls the LLM.
|
|
|
|
| 89 |
* `base_prompt.txt`: The system prompt for the LLM.
|
| 90 |
* `pyproject.toml`: Project metadata and dependencies for `uv`.
|
| 91 |
* `requirements.txt`: List of Python dependencies (can be generated from `pyproject.toml`).
|
| 92 |
+
* `tools.py`: Defines all the tools available to the agent.
|
|
|
|
|
|
image_processing.py
DELETED
|
@@ -1,28 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import io
|
| 3 |
-
import base64
|
| 4 |
-
import uuid
|
| 5 |
-
from PIL import Image
|
| 6 |
-
|
| 7 |
-
# Helper functions for image processing
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def encode_image(image_path: str) -> str:
|
| 11 |
-
"""Convert an image file to base64 string."""
|
| 12 |
-
with open(image_path, "rb") as image_file:
|
| 13 |
-
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def decode_image(base64_string: str) -> Image.Image:
|
| 17 |
-
"""Convert a base64 string to a PIL Image."""
|
| 18 |
-
image_data = base64.b64decode(base64_string)
|
| 19 |
-
return Image.open(io.BytesIO(image_data))
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def save_image(image: Image.Image, directory: str = "image_outputs") -> str:
|
| 23 |
-
"""Save a PIL Image to disk and return the path."""
|
| 24 |
-
os.makedirs(directory, exist_ok=True)
|
| 25 |
-
image_id = str(uuid.uuid4())
|
| 26 |
-
image_path = os.path.join(directory, f"{image_id}.png")
|
| 27 |
-
image.save(image_path)
|
| 28 |
-
return image_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
media/demo_agent_ui.png
ADDED
|
notebooks/agent_test.ipynb
DELETED
|
@@ -1,1623 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "markdown",
|
| 5 |
-
"id": "9f22091a",
|
| 6 |
-
"metadata": {},
|
| 7 |
-
"source": [
|
| 8 |
-
"# LLM Agent Test Notebook"
|
| 9 |
-
]
|
| 10 |
-
},
|
| 11 |
-
{
|
| 12 |
-
"cell_type": "markdown",
|
| 13 |
-
"id": "56cb0f77",
|
| 14 |
-
"metadata": {},
|
| 15 |
-
"source": [
|
| 16 |
-
"## 1. Test simple LLM with HF Inference via Langraph"
|
| 17 |
-
]
|
| 18 |
-
},
|
| 19 |
-
{
|
| 20 |
-
"cell_type": "code",
|
| 21 |
-
"execution_count": 1,
|
| 22 |
-
"id": "5030718d",
|
| 23 |
-
"metadata": {},
|
| 24 |
-
"outputs": [],
|
| 25 |
-
"source": [
|
| 26 |
-
"import os\n",
|
| 27 |
-
"from typing import Annotated, List, Optional, TypedDict\n",
|
| 28 |
-
"\n",
|
| 29 |
-
"from dotenv import load_dotenv\n",
|
| 30 |
-
"from IPython.display import Image, display\n",
|
| 31 |
-
"from langchain.tools.retriever import create_retriever_tool\n",
|
| 32 |
-
"from langchain_community.document_loaders import ArxivLoader, WikipediaLoader\n",
|
| 33 |
-
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
|
| 34 |
-
"\n",
|
| 35 |
-
"# from langchain_community.vectorstores import SupabaseVectorStore\n",
|
| 36 |
-
"from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage\n",
|
| 37 |
-
"from langchain_core.tools import tool\n",
|
| 38 |
-
"from langchain_core.documents import Document\n",
|
| 39 |
-
"\n",
|
| 40 |
-
"# from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 41 |
-
"# from langchain_groq import ChatGroq\n",
|
| 42 |
-
"from langchain_huggingface import (\n",
|
| 43 |
-
" ChatHuggingFace,\n",
|
| 44 |
-
" HuggingFaceEmbeddings,\n",
|
| 45 |
-
" HuggingFaceEndpoint,\n",
|
| 46 |
-
")\n",
|
| 47 |
-
"from langchain_ollama.chat_models import ChatOllama\n",
|
| 48 |
-
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 49 |
-
"from langgraph.graph import START, MessagesState, StateGraph\n",
|
| 50 |
-
"from langgraph.graph.message import add_messages\n",
|
| 51 |
-
"from langgraph.prebuilt import ToolNode, tools_condition\n",
|
| 52 |
-
"\n",
|
| 53 |
-
"# from supabase.client import Client, create_client\n",
|
| 54 |
-
"from duckduckgo_search import DDGS\n",
|
| 55 |
-
"import trafilatura # pip install trafilatura\n",
|
| 56 |
-
"from langchain_core.tools import tool\n",
|
| 57 |
-
"from urllib.parse import urljoin, urlparse\n",
|
| 58 |
-
"import requests\n",
|
| 59 |
-
"from bs4 import BeautifulSoup"
|
| 60 |
-
]
|
| 61 |
-
},
|
| 62 |
-
{
|
| 63 |
-
"cell_type": "markdown",
|
| 64 |
-
"id": "796395fe",
|
| 65 |
-
"metadata": {},
|
| 66 |
-
"source": [
|
| 67 |
-
"### Defining the LLM model"
|
| 68 |
-
]
|
| 69 |
-
},
|
| 70 |
-
{
|
| 71 |
-
"cell_type": "code",
|
| 72 |
-
"execution_count": 2,
|
| 73 |
-
"id": "03ae4a90",
|
| 74 |
-
"metadata": {},
|
| 75 |
-
"outputs": [],
|
| 76 |
-
"source": [
|
| 77 |
-
"load_dotenv()\n",
|
| 78 |
-
"\n",
|
| 79 |
-
"HF_LLM_API_TOKEN = os.getenv(\"HF_LLM_API_TOKEN\")\n",
|
| 80 |
-
"\n",
|
| 81 |
-
"# llm = ChatHuggingFace(llm=HuggingFaceEndpoint(\n",
|
| 82 |
-
"# repo_id=\"deepseek-ai/DeepSeek-V3-0324\",\n",
|
| 83 |
-
"# huggingfacehub_api_token=HF_LLM_API_TOKEN\n",
|
| 84 |
-
"# ), verbose=True)\n",
|
| 85 |
-
"\n",
|
| 86 |
-
"# LLM_MODEL = \"llama3.2:1b\" # Specify the LLM model to use\n",
|
| 87 |
-
"\n",
|
| 88 |
-
"# llm = ChatOllama(model=LLM_MODEL, format=\"json\", temperature=0)\n",
|
| 89 |
-
"\n",
|
| 90 |
-
"llm = ChatGoogleGenerativeAI(model=\"gemini-2.5-flash-preview-05-20\")"
|
| 91 |
-
]
|
| 92 |
-
},
|
| 93 |
-
{
|
| 94 |
-
"cell_type": "markdown",
|
| 95 |
-
"id": "7aa2955c",
|
| 96 |
-
"metadata": {},
|
| 97 |
-
"source": [
|
| 98 |
-
"### Defining the Agent State"
|
| 99 |
-
]
|
| 100 |
-
},
|
| 101 |
-
{
|
| 102 |
-
"cell_type": "code",
|
| 103 |
-
"execution_count": 3,
|
| 104 |
-
"id": "8094ba6f",
|
| 105 |
-
"metadata": {},
|
| 106 |
-
"outputs": [],
|
| 107 |
-
"source": [
|
| 108 |
-
"class AgentState(TypedDict):\n",
|
| 109 |
-
" \"\"\"State for the agent.\"\"\"\n",
|
| 110 |
-
"\n",
|
| 111 |
-
" # TODO: Add any additional state variables we need\n",
|
| 112 |
-
" messages: Annotated[list[AnyMessage], add_messages]"
|
| 113 |
-
]
|
| 114 |
-
},
|
| 115 |
-
{
|
| 116 |
-
"cell_type": "markdown",
|
| 117 |
-
"id": "385e58df",
|
| 118 |
-
"metadata": {},
|
| 119 |
-
"source": [
|
| 120 |
-
"### Defining basics Agent Tools"
|
| 121 |
-
]
|
| 122 |
-
},
|
| 123 |
-
{
|
| 124 |
-
"cell_type": "code",
|
| 125 |
-
"execution_count": 4,
|
| 126 |
-
"id": "54d3b881",
|
| 127 |
-
"metadata": {},
|
| 128 |
-
"outputs": [],
|
| 129 |
-
"source": [
|
| 130 |
-
"# basic calculator tools\n",
|
| 131 |
-
"from langchain_core.tools import tool\n",
|
| 132 |
-
"from typing import Sequence\n",
|
| 133 |
-
"import math\n",
|
| 134 |
-
"\n",
|
| 135 |
-
"SAFE_GLOBALS = {\"__builtins__\": {}, \"math\": math}\n",
|
| 136 |
-
"\n",
|
| 137 |
-
"\n",
|
| 138 |
-
"@tool\n",
|
| 139 |
-
"def calculator(expr: str) -> float:\n",
|
| 140 |
-
" \"\"\"\n",
|
| 141 |
-
" Calculate a basic arithmetic or math expression.\n",
|
| 142 |
-
"\n",
|
| 143 |
-
" Accepted syntax\n",
|
| 144 |
-
" ---------------\n",
|
| 145 |
-
" • Literals: integers or floats (e.g. ``2``, ``3.14``)\n",
|
| 146 |
-
" • Operators: ``+``, ``-``, ``*``, ``/``, ``**``\n",
|
| 147 |
-
" • Unary minus (``-5``)\n",
|
| 148 |
-
" • Functions/consts from ``math`` (e.g. ``sin(0.5)``, ``pi``)\n",
|
| 149 |
-
" • Parentheses for grouping\n",
|
| 150 |
-
"\n",
|
| 151 |
-
" Parameters\n",
|
| 152 |
-
" ----------\n",
|
| 153 |
-
" expr : str\n",
|
| 154 |
-
" The expression to evaluate.\n",
|
| 155 |
-
"\n",
|
| 156 |
-
" Returns\n",
|
| 157 |
-
" -------\n",
|
| 158 |
-
" float\n",
|
| 159 |
-
" Result of the computation.\n",
|
| 160 |
-
"\n",
|
| 161 |
-
" Raises\n",
|
| 162 |
-
" ------\n",
|
| 163 |
-
" ValueError\n",
|
| 164 |
-
" If the expression contains unsupported syntax or names.\n",
|
| 165 |
-
" \"\"\"\n",
|
| 166 |
-
" try:\n",
|
| 167 |
-
" if \".\" in expr or \"__\" in expr:\n",
|
| 168 |
-
" raise ValueError(\"Attribute access not allowed\")\n",
|
| 169 |
-
" return eval(expr, SAFE_GLOBALS)\n",
|
| 170 |
-
" except (ValueError, SyntaxError, TypeError) as exc:\n",
|
| 171 |
-
" raise ValueError(f\"Invalid expression '{expr}': {exc}\") from exc\n",
|
| 172 |
-
"\n",
|
| 173 |
-
"\n",
|
| 174 |
-
"# ──────────────────────── generic search utils ───────────────────────\n",
|
| 175 |
-
"\n",
|
| 176 |
-
"_SEPARATOR = \"\\n\\n---\\n\\n\"\n",
|
| 177 |
-
"\n",
|
| 178 |
-
"\n",
|
| 179 |
-
"def _format_docs(docs: Sequence, max_chars: int = 5000) -> str:\n",
|
| 180 |
-
" \"\"\"Uniformly format loader docs for the LLM / calling agent.\"\"\"\n",
|
| 181 |
-
" if not docs:\n",
|
| 182 |
-
" return \"No results found.\"\n",
|
| 183 |
-
" chunks = []\n",
|
| 184 |
-
" for doc in docs:\n",
|
| 185 |
-
" meta = doc.metadata\n",
|
| 186 |
-
" snippet = doc.page_content[:max_chars].strip()\n",
|
| 187 |
-
" chunks.append(\n",
|
| 188 |
-
" f'<Document source=\"{meta.get(\"source\")}\" page=\"{meta.get(\"page\", \"\")}\">\\n'\n",
|
| 189 |
-
" f\"{snippet}\\n</Document>\"\n",
|
| 190 |
-
" )\n",
|
| 191 |
-
" return _SEPARATOR.join(chunks)\n",
|
| 192 |
-
"\n",
|
| 193 |
-
"\n",
|
| 194 |
-
"# ───────────────────────── wiki_search ──────────────────────────\n",
|
| 195 |
-
"\n",
|
| 196 |
-
"\n",
|
| 197 |
-
"@tool\n",
|
| 198 |
-
"def wiki_search(query: str) -> str:\n",
|
| 199 |
-
" \"\"\"Return up to 2 Wikipedia pages about *query*.\"\"\"\n",
|
| 200 |
-
" docs = WikipediaLoader(query=query, load_max_docs=2).load()\n",
|
| 201 |
-
" return _format_docs(docs)\n",
|
| 202 |
-
"\n",
|
| 203 |
-
"\n",
|
| 204 |
-
"# ───────────────────────── web_search ──────────────────────────\n",
|
| 205 |
-
"\n",
|
| 206 |
-
"\n",
|
| 207 |
-
"@tool\n",
|
| 208 |
-
"def web_search(query: str, max_results: int = 3) -> str:\n",
|
| 209 |
-
" \"\"\"\n",
|
| 210 |
-
" Return up to `max_results` DuckDuckGo search results for *query*.\n",
|
| 211 |
-
"\n",
|
| 212 |
-
" The output is formatted by `_format_docs`, so it matches the schema your\n",
|
| 213 |
-
" other tools already use.\n",
|
| 214 |
-
" \"\"\"\n",
|
| 215 |
-
" docs = []\n",
|
| 216 |
-
" with DDGS() as ddgs:\n",
|
| 217 |
-
" for hit in ddgs.text(query, max_results=max_results):\n",
|
| 218 |
-
" docs.append(\n",
|
| 219 |
-
" Document(\n",
|
| 220 |
-
" page_content=hit.get(\"body\") or hit.get(\"snippet\") or \"\",\n",
|
| 221 |
-
" metadata={\"source\": hit.get(\"href\") or hit.get(\"url\"), \"page\": \"\"},\n",
|
| 222 |
-
" )\n",
|
| 223 |
-
" )\n",
|
| 224 |
-
"\n",
|
| 225 |
-
" return _format_docs(docs)\n",
|
| 226 |
-
"\n",
|
| 227 |
-
"\n",
|
| 228 |
-
"# ───────────────────────── arxiv_search ──────────────────────────\n",
|
| 229 |
-
"\n",
|
| 230 |
-
"\n",
|
| 231 |
-
"@tool\n",
|
| 232 |
-
"def arxiv_search(query: str) -> str:\n",
|
| 233 |
-
" \"\"\"Return up to 3 recent ArXiv papers about *query*.\"\"\"\n",
|
| 234 |
-
" docs = ArxivLoader(query=query, load_max_docs=3).load()\n",
|
| 235 |
-
" return _format_docs(docs)\n",
|
| 236 |
-
"\n",
|
| 237 |
-
"\n",
|
| 238 |
-
"# ---------- 1. Search → list of links -----------------------\n",
|
| 239 |
-
"\n",
|
| 240 |
-
"\n",
|
| 241 |
-
"@tool\n",
|
| 242 |
-
"def list_webpage_links(url: str, same_domain_only: bool = False) -> list[str]:\n",
|
| 243 |
-
" \"\"\"\n",
|
| 244 |
-
" Return all unique <a href=\"...\"> links found in the HTML at `url`.\n",
|
| 245 |
-
"\n",
|
| 246 |
-
" Parameters\n",
|
| 247 |
-
" ----------\n",
|
| 248 |
-
" url : str\n",
|
| 249 |
-
" Page to scrape.\n",
|
| 250 |
-
" same_domain_only : bool, optional\n",
|
| 251 |
-
" If True, keep only links on the same domain as `url`. Default = False.\n",
|
| 252 |
-
"\n",
|
| 253 |
-
" Returns\n",
|
| 254 |
-
" -------\n",
|
| 255 |
-
" list[str]\n",
|
| 256 |
-
" Absolute URLs, deduplicated and sorted.\n",
|
| 257 |
-
" \"\"\"\n",
|
| 258 |
-
" try:\n",
|
| 259 |
-
" html = requests.get(url, timeout=10).text\n",
|
| 260 |
-
" except Exception as exc:\n",
|
| 261 |
-
" return [f\"ERROR: fetch failed – {exc}\"]\n",
|
| 262 |
-
"\n",
|
| 263 |
-
" base = \"{uri.scheme}://{uri.netloc}\".format(uri=urlparse(url))\n",
|
| 264 |
-
" soup = BeautifulSoup(html, \"html.parser\")\n",
|
| 265 |
-
"\n",
|
| 266 |
-
" links: set[str] = set()\n",
|
| 267 |
-
" for tag in soup.find_all(\"a\", href=True):\n",
|
| 268 |
-
" href: str = tag[\"href\"].strip()\n",
|
| 269 |
-
" # Convert relative → absolute\n",
|
| 270 |
-
" full = urljoin(base, href)\n",
|
| 271 |
-
" if same_domain_only and urlparse(full).netloc != urlparse(url).netloc:\n",
|
| 272 |
-
" continue\n",
|
| 273 |
-
" links.add(full)\n",
|
| 274 |
-
"\n",
|
| 275 |
-
" return sorted(links)\n",
|
| 276 |
-
"\n",
|
| 277 |
-
"\n",
|
| 278 |
-
"# ---------- 2. Browse → cleaned article text ----------------\n",
|
| 279 |
-
"@tool\n",
|
| 280 |
-
"def browse_webpage_link(url: str) -> str:\n",
|
| 281 |
-
" \"\"\"\n",
|
| 282 |
-
" Download `url` and return the main readable text (no html, ads, nav bars).\n",
|
| 283 |
-
" Relies on trafilatura’s article extractor.\n",
|
| 284 |
-
" \"\"\"\n",
|
| 285 |
-
" raw = trafilatura.fetch_url(url)\n",
|
| 286 |
-
" if raw is None:\n",
|
| 287 |
-
" return \"🛑 Could not fetch the page.\"\n",
|
| 288 |
-
"\n",
|
| 289 |
-
" text = trafilatura.extract(\n",
|
| 290 |
-
" raw,\n",
|
| 291 |
-
" include_comments=False,\n",
|
| 292 |
-
" include_tables=False,\n",
|
| 293 |
-
" include_links=False,\n",
|
| 294 |
-
" )\n",
|
| 295 |
-
" return text or \"🛑 Page fetched but no readable text found.\"\n",
|
| 296 |
-
"\n",
|
| 297 |
-
"\n",
|
| 298 |
-
"@tool\n",
|
| 299 |
-
"def search_links_for_match(\n",
|
| 300 |
-
" url: str,\n",
|
| 301 |
-
" keyword: str,\n",
|
| 302 |
-
" max_links: int = 100,\n",
|
| 303 |
-
" same_domain_only: bool = True,\n",
|
| 304 |
-
" case_sensitive: bool = False,\n",
|
| 305 |
-
") -> list[str]:\n",
|
| 306 |
-
" \"\"\"\n",
|
| 307 |
-
" Search the content of up to `max_links` found on a webpage, and return URLs that contain the given keyword.\n",
|
| 308 |
-
"\n",
|
| 309 |
-
" Parameters:\n",
|
| 310 |
-
" ----------\n",
|
| 311 |
-
" url : str\n",
|
| 312 |
-
" The starting webpage to extract links from.\n",
|
| 313 |
-
" keyword : str\n",
|
| 314 |
-
" The keyword or phrase to match inside linked pages.\n",
|
| 315 |
-
" max_links : int, optional\n",
|
| 316 |
-
" Number of links to follow (default: 10).\n",
|
| 317 |
-
" same_domain_only : bool, optional\n",
|
| 318 |
-
" Only consider links from the same domain (default: True).\n",
|
| 319 |
-
" case_sensitive : bool, optional\n",
|
| 320 |
-
" Whether the keyword match should be case-sensitive.\n",
|
| 321 |
-
"\n",
|
| 322 |
-
" Returns:\n",
|
| 323 |
-
" -------\n",
|
| 324 |
-
" list[str]\n",
|
| 325 |
-
" List of URLs whose content contains the keyword.\n",
|
| 326 |
-
" \"\"\"\n",
|
| 327 |
-
"\n",
|
| 328 |
-
" # Use the tool's .func() to access base function\n",
|
| 329 |
-
" all_links = list_webpage_links.func(url=url, same_domain_only=same_domain_only)\n",
|
| 330 |
-
" matched_links = []\n",
|
| 331 |
-
"\n",
|
| 332 |
-
" # Normalize keyword\n",
|
| 333 |
-
" kw = keyword if case_sensitive else keyword.lower()\n",
|
| 334 |
-
"\n",
|
| 335 |
-
" for link in all_links[:max_links]:\n",
|
| 336 |
-
" try:\n",
|
| 337 |
-
" text = browse_webpage_link.func(link)\n",
|
| 338 |
-
" if not case_sensitive:\n",
|
| 339 |
-
" text = text.lower()\n",
|
| 340 |
-
" if kw in text:\n",
|
| 341 |
-
" matched_links.append(link)\n",
|
| 342 |
-
" except Exception:\n",
|
| 343 |
-
" continue\n",
|
| 344 |
-
"\n",
|
| 345 |
-
" return matched_links or [\"No matches found.\"]"
|
| 346 |
-
]
|
| 347 |
-
},
|
| 348 |
-
{
|
| 349 |
-
"cell_type": "code",
|
| 350 |
-
"execution_count": 5,
|
| 351 |
-
"id": "68fe50cb",
|
| 352 |
-
"metadata": {},
|
| 353 |
-
"outputs": [],
|
| 354 |
-
"source": [
|
| 355 |
-
"# # test web search\n",
|
| 356 |
-
"# web_search = web_search.invoke(\n",
|
| 357 |
-
"# {\"query\": \"What is the capital of France?\"}\n",
|
| 358 |
-
"# )\n",
|
| 359 |
-
"# print(web_search)"
|
| 360 |
-
]
|
| 361 |
-
},
|
| 362 |
-
{
|
| 363 |
-
"cell_type": "code",
|
| 364 |
-
"execution_count": 6,
|
| 365 |
-
"id": "a12f053b",
|
| 366 |
-
"metadata": {},
|
| 367 |
-
"outputs": [],
|
| 368 |
-
"source": [
|
| 369 |
-
"# links = list_webpage_links.invoke(\n",
|
| 370 |
-
"# {\"url\": \"https://en.wikipedia.org/wiki/Artificial_intelligence\",\n",
|
| 371 |
-
"# \"same_domain_only\": True}\n",
|
| 372 |
-
"# )\n",
|
| 373 |
-
"# print(len(links), \"links\")\n",
|
| 374 |
-
"# print(links[:100])"
|
| 375 |
-
]
|
| 376 |
-
},
|
| 377 |
-
{
|
| 378 |
-
"cell_type": "code",
|
| 379 |
-
"execution_count": 7,
|
| 380 |
-
"id": "71f249eb",
|
| 381 |
-
"metadata": {},
|
| 382 |
-
"outputs": [],
|
| 383 |
-
"source": [
|
| 384 |
-
"# browse_link = browse_webpage_link.invoke(\n",
|
| 385 |
-
"# {\"url\": \"https://en.wikipedia.org/wiki/Artificial_intelligence\"}\n",
|
| 386 |
-
"# )\n",
|
| 387 |
-
"# print(\"Browse link result:\", browse_link[:200], \"...\")"
|
| 388 |
-
]
|
| 389 |
-
},
|
| 390 |
-
{
|
| 391 |
-
"cell_type": "code",
|
| 392 |
-
"execution_count": 8,
|
| 393 |
-
"id": "499121a2",
|
| 394 |
-
"metadata": {},
|
| 395 |
-
"outputs": [],
|
| 396 |
-
"source": [
|
| 397 |
-
"# matched_links = search_links_for_match.invoke({\n",
|
| 398 |
-
"# \"url\": \"https://en.wikipedia.org/wiki/Artificial_intelligence\",\n",
|
| 399 |
-
"# \"keyword\": \"machine learning\", # ← space not underscore\n",
|
| 400 |
-
"# \"max_links\": 30, # scan first 30 links\n",
|
| 401 |
-
"# \"same_domain_only\": True,\n",
|
| 402 |
-
"# \"case_sensitive\": False\n",
|
| 403 |
-
"# })\n",
|
| 404 |
-
"# print(\"Matched links:\", matched_links)"
|
| 405 |
-
]
|
| 406 |
-
},
|
| 407 |
-
{
|
| 408 |
-
"cell_type": "code",
|
| 409 |
-
"execution_count": 9,
|
| 410 |
-
"id": "33c4751e",
|
| 411 |
-
"metadata": {},
|
| 412 |
-
"outputs": [],
|
| 413 |
-
"source": [
|
| 414 |
-
"tools = [\n",
|
| 415 |
-
" calculator,\n",
|
| 416 |
-
" web_search,\n",
|
| 417 |
-
" wiki_search,\n",
|
| 418 |
-
" arxiv_search,\n",
|
| 419 |
-
" list_webpage_links,\n",
|
| 420 |
-
" browse_webpage_link,\n",
|
| 421 |
-
" search_links_for_match,\n",
|
| 422 |
-
"]\n",
|
| 423 |
-
"# Bind tools to LLM\n",
|
| 424 |
-
"llm_with_tools = llm.bind_tools(tools)"
|
| 425 |
-
]
|
| 426 |
-
},
|
| 427 |
-
{
|
| 428 |
-
"cell_type": "code",
|
| 429 |
-
"execution_count": 10,
|
| 430 |
-
"id": "550ae2b7",
|
| 431 |
-
"metadata": {},
|
| 432 |
-
"outputs": [
|
| 433 |
-
{
|
| 434 |
-
"name": "stdout",
|
| 435 |
-
"output_type": "stream",
|
| 436 |
-
"text": [
|
| 437 |
-
"['calculator', 'web_search', 'wiki_search', 'arxiv_search', 'list_webpage_links', 'browse_webpage_link', 'search_links_for_match']\n"
|
| 438 |
-
]
|
| 439 |
-
}
|
| 440 |
-
],
|
| 441 |
-
"source": [
|
| 442 |
-
"print([tool.name for tool in tools])"
|
| 443 |
-
]
|
| 444 |
-
},
|
| 445 |
-
{
|
| 446 |
-
"cell_type": "code",
|
| 447 |
-
"execution_count": null,
|
| 448 |
-
"id": "f2a491fd",
|
| 449 |
-
"metadata": {},
|
| 450 |
-
"outputs": [],
|
| 451 |
-
"source": [
|
| 452 |
-
"SYSTEM_PROMPT = f\"\"\"\n",
|
| 453 |
-
"You are a helpful assistant tasked with answering questions using a set of tools.\n",
|
| 454 |
-
"You have access to the following tools:\n",
|
| 455 |
-
"{\", \".join([tool.name for tool in tools])}\n",
|
| 456 |
-
"You can use these tools to search for information, perform calculations, and retrieve data from various sources.\n",
|
| 457 |
-
"If the tool is not available, you can try to find the information online. You can also use your own knowledge to answer the question. \n",
|
| 458 |
-
"You need to provide a step-by-step explanation of how you arrived at the answer.\n",
|
| 459 |
-
"\n",
|
| 460 |
-
"==========================\n",
|
| 461 |
-
"Here is a few examples from humans, showing you how to answer the question step by step.\n",
|
| 462 |
-
"\n",
|
| 463 |
-
"Question 1: In terms of geographical distance between capital cities, which 2 countries are the furthest from each other within the ASEAN bloc according to wikipedia? Answer using a comma separated list, ordering the countries by alphabetical order.\n",
|
| 464 |
-
"Steps:\n",
|
| 465 |
-
"1. Search the web for \"ASEAN bloc\".\n",
|
| 466 |
-
"2. Click the Wikipedia result for the ASEAN Free Trade Area.\n",
|
| 467 |
-
"3. Scroll down to find the list of member states.\n",
|
| 468 |
-
"4. Click into the Wikipedia pages for each member state, and note its capital.\n",
|
| 469 |
-
"5. Search the web for the distance between the first two capitals. The results give travel distance, not geographic distance, which might affect the answer.\n",
|
| 470 |
-
"6. Thinking it might be faster to judge the distance by looking at a map, search the web for \"ASEAN bloc\" and click into the images tab.\n",
|
| 471 |
-
"7. View a map of the member countries. Since they're clustered together in an arrangement that's not very linear, it's difficult to judge distances by eye.\n",
|
| 472 |
-
"8. Return to the Wikipedia page for each country. Click the GPS coordinates for each capital to get the coordinates in decimal notation.\n",
|
| 473 |
-
"9. Place all these coordinates into a spreadsheet.\n",
|
| 474 |
-
"10. Write formulas to calculate the distance between each capital.\n",
|
| 475 |
-
"11. Write formula to get the largest distance value in the spreadsheet.\n",
|
| 476 |
-
"12. Note which two capitals that value corresponds to: Jakarta and Naypyidaw.\n",
|
| 477 |
-
"13. Return to the Wikipedia pages to see which countries those respective capitals belong to: Indonesia, Myanmar.\n",
|
| 478 |
-
"Tools:\n",
|
| 479 |
-
"1. Search engine\n",
|
| 480 |
-
"2. Web browser\n",
|
| 481 |
-
"3. Microsoft Excel / Google Sheets\n",
|
| 482 |
-
"Final Answer: Indonesia, Myanmar\n",
|
| 483 |
-
"\n",
|
| 484 |
-
"Your Actions, to follow the human example, should be similar to the following:\n",
|
| 485 |
-
"1. Use the wiki_search tool to search for the ASEAN Free Trade Area.\n",
|
| 486 |
-
"2. Retrieve the list of member states from the Wikipedia page, and note their capitals if they are available. If not, use the web_search tool to find the capitals.\n",
|
| 487 |
-
"3. Once you have the capitals lists, use the web_search tool to find the GPS coordinates of each capital city.\n",
|
| 488 |
-
"3. Calculate the geographical distance between each pair of capitals. You can search for a formula to calculate the distance between two GPS coordinates, then use the calculator tool to perform the calculations.\n",
|
| 489 |
-
"4. Identify the pair of capitals with the maximum distance.\n",
|
| 490 |
-
"5. Provide the final answer in a comma-separated list, ordering the countries by alphabetical order.\n",
|
| 491 |
-
"Final Answer: Indonesia, Myanmar\n",
|
| 492 |
-
"==========================\n",
|
| 493 |
-
"IMPORTANT: if you are not able to answer the question, even with the help of the tools, you MUST say \"I don't know\" instead of making up an answer!!!\n",
|
| 494 |
-
"Report your thoughts, and finish your answer with the following template: [ANSWER]. The [ANSWER] should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.\n",
|
| 495 |
-
"\n",
|
| 496 |
-
"Now, please answer the following question step by step.\n",
|
| 497 |
-
"\"\"\""
|
| 498 |
-
]
|
| 499 |
-
},
|
| 500 |
-
{
|
| 501 |
-
"cell_type": "code",
|
| 502 |
-
"execution_count": 12,
|
| 503 |
-
"id": "78553b0b",
|
| 504 |
-
"metadata": {},
|
| 505 |
-
"outputs": [],
|
| 506 |
-
"source": [
|
| 507 |
-
"def assistant(state: AgentState):\n",
|
| 508 |
-
" # System message\n",
|
| 509 |
-
" sys_msg = SystemMessage(content=SYSTEM_PROMPT)\n",
|
| 510 |
-
"\n",
|
| 511 |
-
" return {\n",
|
| 512 |
-
" \"messages\": [llm_with_tools.invoke([sys_msg] + state[\"messages\"])],\n",
|
| 513 |
-
" }"
|
| 514 |
-
]
|
| 515 |
-
},
|
| 516 |
-
{
|
| 517 |
-
"cell_type": "code",
|
| 518 |
-
"execution_count": 13,
|
| 519 |
-
"id": "6461b263",
|
| 520 |
-
"metadata": {},
|
| 521 |
-
"outputs": [
|
| 522 |
-
{
|
| 523 |
-
"data": {
|
| 524 |
-
"image/png": "iVBORw0KGgoAAAANSUhEUgAAANgAAAD5CAIAAADKsmwpAAAAAXNSR0IArs4c6QAAIABJREFUeJzt3XdcU1f/B/BzswcJkIRpQEAFZCgoSktdFSviqGLdWtfP3UWrtbXWqt3DPlqt1WK1VrSOinvUotYFooKCAiogStkQRhKy1++P+FAeDBE0N/eEe94v/8B7wz1f8OO5565zMZP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G/RGgx7GIDJYGIVK4fI5HD5V1IXh5Ox4vXin97zXOQSeDIEn6leQ54WuqDoSrjPNoSc9EHgy2xq8oSA6EjaXIinXEF3FM9JpjWUFCmeR5f0nCqIj8ejK0mkcdVKe+iqNlVs8URAdiU8gB8PArfMOOVnZ+d8rXnq1zUnz4XpfM9Ielw7V6nSmbr34Qm8HmFVfIdNLazV/76t6faUvt+3zFSiIDin3qjQvXaZWGjS4zQxjE25dmI01Wv9w7ktjRNZfZ4mC6MBMJqBVQx1Ek9HE4rbrwhUKIgIFdLCCQAEFEYECCiICBRREBAooiAgUUBARKPw/UQ7qSwMCYJAAAAAASUVORK5CYII=",
|
| 525 |
-
"text/plain": [
|
| 526 |
-
"<IPython.core.display.Image object>"
|
| 527 |
-
]
|
| 528 |
-
},
|
| 529 |
-
"metadata": {},
|
| 530 |
-
"output_type": "display_data"
|
| 531 |
-
}
|
| 532 |
-
],
|
| 533 |
-
"source": [
|
| 534 |
-
"# The graph\n",
|
| 535 |
-
"builder = StateGraph(AgentState)\n",
|
| 536 |
-
"\n",
|
| 537 |
-
"# Define nodes: these do the work\n",
|
| 538 |
-
"builder.add_node(\"assistant\", assistant)\n",
|
| 539 |
-
"builder.add_node(\"tools\", ToolNode(tools))\n",
|
| 540 |
-
"\n",
|
| 541 |
-
"# Define edges: these determine how the control flow moves\n",
|
| 542 |
-
"builder.add_edge(START, \"assistant\")\n",
|
| 543 |
-
"builder.add_conditional_edges(\n",
|
| 544 |
-
" \"assistant\",\n",
|
| 545 |
-
" # If the latest message requires a tool, route to tools\n",
|
| 546 |
-
" # Otherwise, provide a direct response\n",
|
| 547 |
-
" tools_condition,\n",
|
| 548 |
-
")\n",
|
| 549 |
-
"builder.add_edge(\"tools\", \"assistant\")\n",
|
| 550 |
-
"react_graph = builder.compile()\n",
|
| 551 |
-
"\n",
|
| 552 |
-
"# Show the butler's thought process\n",
|
| 553 |
-
"display(Image(react_graph.get_graph(xray=True).draw_mermaid_png()))"
|
| 554 |
-
]
|
| 555 |
-
},
|
| 556 |
-
{
|
| 557 |
-
"cell_type": "code",
|
| 558 |
-
"execution_count": 14,
|
| 559 |
-
"id": "6179a86a",
|
| 560 |
-
"metadata": {},
|
| 561 |
-
"outputs": [],
|
| 562 |
-
"source": [
|
| 563 |
-
"# messages = [HumanMessage(content=\"Divide 6790 by 5\")]\n",
|
| 564 |
-
"# messages = react_graph.invoke({\"messages\": messages})\n",
|
| 565 |
-
"\n",
|
| 566 |
-
"# # Show the messages\n",
|
| 567 |
-
"# for m in messages['messages']:\n",
|
| 568 |
-
"# m.pretty_print()"
|
| 569 |
-
]
|
| 570 |
-
},
|
| 571 |
-
{
|
| 572 |
-
"cell_type": "code",
|
| 573 |
-
"execution_count": 15,
|
| 574 |
-
"id": "d66c9de3",
|
| 575 |
-
"metadata": {},
|
| 576 |
-
"outputs": [
|
| 577 |
-
{
|
| 578 |
-
"name": "stdout",
|
| 579 |
-
"output_type": "stream",
|
| 580 |
-
"text": [
|
| 581 |
-
"================================\u001b[1m Human Message \u001b[0m=================================\n",
|
| 582 |
-
"\n",
|
| 583 |
-
"What writer is quoted by Merriam-Webster for the Word of the Day from June 27, 2022?\n",
|
| 584 |
-
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
| 585 |
-
"Tool Calls:\n",
|
| 586 |
-
" web_search (97ba28af-fcb3-444a-b964-866d03df628a)\n",
|
| 587 |
-
" Call ID: 97ba28af-fcb3-444a-b964-866d03df628a\n",
|
| 588 |
-
" Args:\n",
|
| 589 |
-
" query: Merriam-Webster Word of the Day June 27 2022\n",
|
| 590 |
-
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
| 591 |
-
"Name: web_search\n",
|
| 592 |
-
"\n",
|
| 593 |
-
"<Document source=\"https://www.merriam-webster.com/word-of-the-day/candidate-2022-06-27\" page=\"\">\n",
|
| 594 |
-
"June 27, 2022 | extreme patriotism or nationalism Jingoism originated during the Russo-Turkish War of 1877-1878, when many British citizens were hostile toward Russia and felt Britain should intervene\n",
|
| 595 |
-
"</Document>\n",
|
| 596 |
-
"\n",
|
| 597 |
-
"---\n",
|
| 598 |
-
"\n",
|
| 599 |
-
"<Document source=\"https://www.merriam-webster.com/word-of-the-day/calendar\" page=\"\">\n",
|
| 600 |
-
"Learn a new word every day! Follow Merriam-Webster for the most trusted Word of the Day, trending info, word games, and more. ... June 10, 2025 . minutia play . a small or minor detail. June 09, 2025 . eloquent ... May 27 interminable; May 28 fiasco; May 29 nascent; May 30 gust; May 31 opportune; April 2025. Apr 01 cynosure;\n",
|
| 601 |
-
"</Document>\n",
|
| 602 |
-
"\n",
|
| 603 |
-
"---\n",
|
| 604 |
-
"\n",
|
| 605 |
-
"<Document source=\"https://en.wiktionary.org/wiki/Wiktionary:Word_of_the_day/2022/June_27\" page=\"\">\n",
|
| 606 |
-
"Pages for logged out editors learn more. Contributions; Talk; Wiktionary: Word of the day/2022/June 27\n",
|
| 607 |
-
"</Document>\n",
|
| 608 |
-
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
| 609 |
-
"\n",
|
| 610 |
-
"The Word of the Day for June 27, 2022, according to Merriam-Webster, was \"jingoism.\" The provided search results do not explicitly state which writer is quoted. I will browse the link to find the specific quote and its author.\n",
|
| 611 |
-
"Tool Calls:\n",
|
| 612 |
-
" browse_webpage_link (c6de65fe-ef36-43bd-991f-76277be7e671)\n",
|
| 613 |
-
" Call ID: c6de65fe-ef36-43bd-991f-76277be7e671\n",
|
| 614 |
-
" Args:\n",
|
| 615 |
-
" url: https://www.merriam-webster.com/word-of-the-day/candidate-2022-06-27\n",
|
| 616 |
-
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
| 617 |
-
"Name: browse_webpage_link\n",
|
| 618 |
-
"\n",
|
| 619 |
-
"Word of the Day\n",
|
| 620 |
-
": June 27, 2022jingoism\n",
|
| 621 |
-
"playWhat It Means\n",
|
| 622 |
-
"Jingoism is excessive patriotism or nationalism, especially when marked by a belligerent foreign policy.\n",
|
| 623 |
-
"// When the war began many people were caught up in a wave of jingoism.\n",
|
| 624 |
-
"jingoism in Context\n",
|
| 625 |
-
"\"War is bad for culture. Not least of all because it turns our cultural institutions into bastions of jingoism.\" — Annie Levin, The New York Observer, 7 Mar. 2022\n",
|
| 626 |
-
"Did You Know?\n",
|
| 627 |
-
"Jingoism originated during the Russo-Turkish War of 1877-1878, when many British citizens were hostile toward Russia and felt Britain should intervene in the conflict. Supporters of the cause expressed their sentiments in a music-hall ditty with this refrain:\n",
|
| 628 |
-
"We don't want to fight, yet by jingo if we do,\n",
|
| 629 |
-
"We've got the ships, we've got the men,\n",
|
| 630 |
-
"We've got the money, too!\n",
|
| 631 |
-
"Someone holding the attitude implied in the song became known as a jingo or jingoist, and the attitude itself was dubbed jingoism. The jingo in the tune is probably a euphemism for Jesus.\n",
|
| 632 |
-
"Quiz\n",
|
| 633 |
-
"Unscramble the letters to find a word that means \"a supporter of a war or warlike policy\": KWAH\n",
|
| 634 |
-
"VIEW THE ANSWERPodcast\n",
|
| 635 |
-
"More Words of the Day\n",
|
| 636 |
-
"-\n",
|
| 637 |
-
"Jun 13\n",
|
| 638 |
-
"rambunctious\n",
|
| 639 |
-
"-\n",
|
| 640 |
-
"Jun 12\n",
|
| 641 |
-
"impute\n",
|
| 642 |
-
"-\n",
|
| 643 |
-
"Jun 11\n",
|
| 644 |
-
"debilitating\n",
|
| 645 |
-
"-\n",
|
| 646 |
-
"Jun 10\n",
|
| 647 |
-
"minutia\n",
|
| 648 |
-
"-\n",
|
| 649 |
-
"Jun 09\n",
|
| 650 |
-
"eloquent\n",
|
| 651 |
-
"-\n",
|
| 652 |
-
"Jun 08\n",
|
| 653 |
-
"cataract\n",
|
| 654 |
-
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
| 655 |
-
"\n",
|
| 656 |
-
"The writer quoted by Merriam-Webster for the Word of the Day from June 27, 2022, is Annie Levin.\n",
|
| 657 |
-
"[ANSWER] Annie Levin\n"
|
| 658 |
-
]
|
| 659 |
-
}
|
| 660 |
-
],
|
| 661 |
-
"source": [
|
| 662 |
-
"messages = [\n",
|
| 663 |
-
" HumanMessage(\n",
|
| 664 |
-
" content=\"What writer is quoted by Merriam-Webster for the Word of the Day from June 27, 2022?\"\n",
|
| 665 |
-
" )\n",
|
| 666 |
-
"]\n",
|
| 667 |
-
"messages = react_graph.invoke({\"messages\": messages}, config={\"debug\": True})\n",
|
| 668 |
-
"\n",
|
| 669 |
-
"# Show the messages\n",
|
| 670 |
-
"for m in messages[\"messages\"]:\n",
|
| 671 |
-
" m.pretty_print()"
|
| 672 |
-
]
|
| 673 |
-
},
|
| 674 |
-
{
|
| 675 |
-
"cell_type": "code",
|
| 676 |
-
"execution_count": null,
|
| 677 |
-
"id": "de2b6256",
|
| 678 |
-
"metadata": {},
|
| 679 |
-
"outputs": [],
|
| 680 |
-
"source": []
|
| 681 |
-
},
|
| 682 |
-
{
|
| 683 |
-
"cell_type": "markdown",
|
| 684 |
-
"id": "c88bfa7a",
|
| 685 |
-
"metadata": {},
|
| 686 |
-
"source": [
|
| 687 |
-
"## 2. Structured Output LLM Agent"
|
| 688 |
-
]
|
| 689 |
-
},
|
| 690 |
-
{
|
| 691 |
-
"cell_type": "code",
|
| 692 |
-
"execution_count": 8,
|
| 693 |
-
"id": "6acbffab",
|
| 694 |
-
"metadata": {},
|
| 695 |
-
"outputs": [],
|
| 696 |
-
"source": [
|
| 697 |
-
"from __future__ import annotations\n",
|
| 698 |
-
"\n",
|
| 699 |
-
"import os\n",
|
| 700 |
-
"from typing import List\n",
|
| 701 |
-
"\n",
|
| 702 |
-
"from dotenv import load_dotenv\n",
|
| 703 |
-
"from pydantic import BaseModel, Field\n",
|
| 704 |
-
"\n",
|
| 705 |
-
"from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage\n",
|
| 706 |
-
"from langchain_core.output_parsers import JsonOutputParser\n",
|
| 707 |
-
"from langchain_core.prompts import PromptTemplate\n",
|
| 708 |
-
"from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint\n",
|
| 709 |
-
"from langgraph.graph import START, StateGraph\n",
|
| 710 |
-
"from langgraph.graph.message import add_messages\n",
|
| 711 |
-
"from langgraph.prebuilt import ToolNode, tools_condition\n",
|
| 712 |
-
"from langchain.output_parsers import OutputFixingParser # optional but handy\n",
|
| 713 |
-
"\n",
|
| 714 |
-
"# ─────────────────────── 1. LLM backend ─────────────────────────── #\n",
|
| 715 |
-
"load_dotenv()\n",
|
| 716 |
-
"llm = ChatHuggingFace(\n",
|
| 717 |
-
" llm=HuggingFaceEndpoint(\n",
|
| 718 |
-
" repo_id=\"deepseek-ai/DeepSeek-V3-0324\",\n",
|
| 719 |
-
" huggingfacehub_api_token=os.getenv(\"HF_LLM_API_TOKEN\"),\n",
|
| 720 |
-
" ),\n",
|
| 721 |
-
" verbose=False,\n",
|
| 722 |
-
")\n",
|
| 723 |
-
"\n",
|
| 724 |
-
"# ─────────────────────── 2. Pydantic schema ─────────────────────── #\n",
|
| 725 |
-
"\n",
|
| 726 |
-
"\n",
|
| 727 |
-
"class Answer(BaseModel):\n",
|
| 728 |
-
" \"\"\"Structured reply returned to the caller.\"\"\"\n",
|
| 729 |
-
"\n",
|
| 730 |
-
" final_answer: str = Field(..., description=\"The concise answer for the user\")\n",
|
| 731 |
-
" reasoning: List[str] = Field(..., description=\"Chronological chain-of-thought\")\n",
|
| 732 |
-
"\n",
|
| 733 |
-
"\n",
|
| 734 |
-
"# ───────────── 3. Parser (+ auto-repair wrapper, optional) ────────── #\n",
|
| 735 |
-
"# :contentReference[oaicite:0]{index=0}\n",
|
| 736 |
-
"base_parser = JsonOutputParser(pydantic_object=Answer)\n",
|
| 737 |
-
"parser = OutputFixingParser.from_llm(llm=llm, parser=base_parser, max_retries=2)\n",
|
| 738 |
-
"# :contentReference[oaicite:1]{index=1}\n",
|
| 739 |
-
"fmt_instructions = parser.get_format_instructions()\n",
|
| 740 |
-
"\n",
|
| 741 |
-
"# ─────────────────────── 4. Prompt template ──────────────────────── #\n",
|
| 742 |
-
"\n",
|
| 743 |
-
"SYSTEM_PROMPT = f\"\"\"\n",
|
| 744 |
-
"You are a helpful assistant that answers questions using a set of tools.\n",
|
| 745 |
-
"\n",
|
| 746 |
-
"• Tools available: {\", \".join(t.name for t in tools)}\n",
|
| 747 |
-
"• Use a tool whenever the information or computation is not already in your head.\n",
|
| 748 |
-
"• After thinking, respond with EITHER:\n",
|
| 749 |
-
" • a single JSON *tool-call* (if you still need external info), OR\n",
|
| 750 |
-
" • the **final JSON answer** described below.\n",
|
| 751 |
-
" • Do NOT wrap the JSON in triple back-ticks.\n",
|
| 752 |
-
"• If you cannot answer even with tools, set \"final_answer\" to \"I don't know\".\n",
|
| 753 |
-
"\n",
|
| 754 |
-
"Worked example\n",
|
| 755 |
-
"--------------\n",
|
| 756 |
-
"Q: Which ASEAN capitals are farthest apart?\n",
|
| 757 |
-
"→ Tool calls (look up capitals, compute distances) \n",
|
| 758 |
-
"→ Final JSON:\n",
|
| 759 |
-
"{{\n",
|
| 760 |
-
" \"final_answer\": \"Indonesia, Myanmar\",\n",
|
| 761 |
-
" \"reasoning\": [\n",
|
| 762 |
-
" \"Gather capitals from Wikipedia\",\n",
|
| 763 |
-
" \"Compute pairwise distances\",\n",
|
| 764 |
-
" \"Select maximum distance pair\"\n",
|
| 765 |
-
" ]\n",
|
| 766 |
-
"}}\n",
|
| 767 |
-
"\"\"\"\n",
|
| 768 |
-
"\n",
|
| 769 |
-
"\n",
|
| 770 |
-
"PROMPT_TMPL = \"\"\"{system_prompt}\n",
|
| 771 |
-
"\n",
|
| 772 |
-
"{format_instructions}\n",
|
| 773 |
-
"\n",
|
| 774 |
-
"Conversation so far:\n",
|
| 775 |
-
"{history}\n",
|
| 776 |
-
"\n",
|
| 777 |
-
"User: {question}\n",
|
| 778 |
-
"\n",
|
| 779 |
-
"Assistant:\"\"\"\n",
|
| 780 |
-
"\n",
|
| 781 |
-
"\n",
|
| 782 |
-
"prompt = PromptTemplate(\n",
|
| 783 |
-
" template=PROMPT_TMPL,\n",
|
| 784 |
-
" input_variables=[\"history\", \"question\"],\n",
|
| 785 |
-
" partial_variables={\n",
|
| 786 |
-
" \"system_prompt\": SYSTEM_PROMPT,\n",
|
| 787 |
-
" \"format_instructions\": fmt_instructions,\n",
|
| 788 |
-
" },\n",
|
| 789 |
-
")\n",
|
| 790 |
-
"\n",
|
| 791 |
-
"# ─────────────────────── 5. Agent state ──────────────────────────── #\n",
|
| 792 |
-
"\n",
|
| 793 |
-
"\n",
|
| 794 |
-
"# ---------------- State -----------------\n",
|
| 795 |
-
"class AgentState(BaseModel):\n",
|
| 796 |
-
" messages: List[AnyMessage] = Field(\n",
|
| 797 |
-
" default_factory=list, json_schema_extra={\"x_add\": add_messages}\n",
|
| 798 |
-
" )\n",
|
| 799 |
-
" reasoning: List[str] = Field(default_factory=list) # NEW\n",
|
| 800 |
-
" final_answer: str | None = None # NEW\n",
|
| 801 |
-
" # NEW\n",
|
| 802 |
-
"\n",
|
| 803 |
-
"\n",
|
| 804 |
-
"# ─────────────────────── 6. Assistant node ───────────────────────── #\n",
|
| 805 |
-
"\n",
|
| 806 |
-
"\n",
|
| 807 |
-
"def assistant_node(state: AgentState):\n",
|
| 808 |
-
" history = \"\\n\".join(\n",
|
| 809 |
-
" m.content for m in state.messages if isinstance(m, HumanMessage)\n",
|
| 810 |
-
" )\n",
|
| 811 |
-
" question = state.messages[-1].content\n",
|
| 812 |
-
" full_prompt = prompt.format(\n",
|
| 813 |
-
" system=SYSTEM_PROMPT, history=history, question=question\n",
|
| 814 |
-
" )\n",
|
| 815 |
-
"\n",
|
| 816 |
-
" raw = llm.invoke(full_prompt)\n",
|
| 817 |
-
" parsed = parser.parse(raw.content)\n",
|
| 818 |
-
"\n",
|
| 819 |
-
" # If you aren’t sure which type you get, normalise:\n",
|
| 820 |
-
" if isinstance(parsed, dict):\n",
|
| 821 |
-
" structured = Answer.model_validate(parsed)\n",
|
| 822 |
-
" else: # it’s already an Answer\n",
|
| 823 |
-
" structured = parsed\n",
|
| 824 |
-
"\n",
|
| 825 |
-
" return {\n",
|
| 826 |
-
" \"messages\": [raw],\n",
|
| 827 |
-
" \"final_answer\": structured.final_answer,\n",
|
| 828 |
-
" \"reasoning\": structured.reasoning,\n",
|
| 829 |
-
" }\n",
|
| 830 |
-
"\n",
|
| 831 |
-
"\n",
|
| 832 |
-
"# ─────────────────────── 7. Summariser node ─────────────────────── #\n",
|
| 833 |
-
"\n",
|
| 834 |
-
"\n",
|
| 835 |
-
"def summariser(state: AgentState) -> Answer:\n",
|
| 836 |
-
" return Answer(final_answer=state.final_answer, reasoning=state.reasoning)\n",
|
| 837 |
-
"\n",
|
| 838 |
-
"\n",
|
| 839 |
-
"# ─────────────────────── 8. Build LangGraph ─────────────────────── #\n",
|
| 840 |
-
"g = StateGraph(AgentState)\n",
|
| 841 |
-
"g.add_node(\"assistant\", assistant_node)\n",
|
| 842 |
-
"g.add_node(\"tools\", ToolNode(tools)) # <-- your tools list goes here\n",
|
| 843 |
-
"g.add_node(\"summarise\", summariser)\n",
|
| 844 |
-
"\n",
|
| 845 |
-
"g.add_edge(START, \"assistant\")\n",
|
| 846 |
-
"g.add_conditional_edges(\"assistant\", tools_condition, (\"tools\", \"summarise\"))\n",
|
| 847 |
-
"g.add_edge(\"tools\", \"assistant\")\n",
|
| 848 |
-
"g.set_finish_point(\"summarise\")\n",
|
| 849 |
-
"\n",
|
| 850 |
-
"agent = g.compile()"
|
| 851 |
-
]
|
| 852 |
-
},
|
| 853 |
-
{
|
| 854 |
-
"cell_type": "code",
|
| 855 |
-
"execution_count": 13,
|
| 856 |
-
"id": "7cfe85e6",
|
| 857 |
-
"metadata": {},
|
| 858 |
-
"outputs": [],
|
| 859 |
-
"source": [
|
| 860 |
-
"import base64\n",
|
| 861 |
-
"import cmath\n",
|
| 862 |
-
"import json\n",
|
| 863 |
-
"import math\n",
|
| 864 |
-
"import os\n",
|
| 865 |
-
"import re\n",
|
| 866 |
-
"import tempfile\n",
|
| 867 |
-
"import uuid\n",
|
| 868 |
-
"from pathlib import Path\n",
|
| 869 |
-
"from typing import Any, Dict, List, Optional, Sequence\n",
|
| 870 |
-
"from urllib.parse import urljoin, urlparse\n",
|
| 871 |
-
"\n",
|
| 872 |
-
"import numpy as np\n",
|
| 873 |
-
"import pandas as pd\n",
|
| 874 |
-
"import pytesseract\n",
|
| 875 |
-
"import requests\n",
|
| 876 |
-
"import trafilatura\n",
|
| 877 |
-
"from bs4 import BeautifulSoup\n",
|
| 878 |
-
"from dotenv import load_dotenv\n",
|
| 879 |
-
"from duckduckgo_search import DDGS\n",
|
| 880 |
-
"from groq import Groq\n",
|
| 881 |
-
"from langchain.agents import tool\n",
|
| 882 |
-
"from langchain_community.document_loaders import ArxivLoader, WikipediaLoader\n",
|
| 883 |
-
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
|
| 884 |
-
"from langchain_community.utilities import GoogleSerperAPIWrapper\n",
|
| 885 |
-
"from langchain_core.documents import Document\n",
|
| 886 |
-
"from langchain_core.messages.ai import AIMessage\n",
|
| 887 |
-
"from langchain_core.tools import tool\n",
|
| 888 |
-
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 889 |
-
"from langchain_groq import ChatGroq\n",
|
| 890 |
-
"from langchain_huggingface import (\n",
|
| 891 |
-
" ChatHuggingFace,\n",
|
| 892 |
-
" HuggingFaceEmbeddings,\n",
|
| 893 |
-
" HuggingFaceEndpoint,\n",
|
| 894 |
-
")\n",
|
| 895 |
-
"from langgraph.graph import START, MessagesState, StateGraph\n",
|
| 896 |
-
"from langgraph.prebuilt import ToolNode, tools_condition\n",
|
| 897 |
-
"from markitdown import MarkItDown\n",
|
| 898 |
-
"from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont\n",
|
| 899 |
-
"\n",
|
| 900 |
-
"load_dotenv()\n",
|
| 901 |
-
"### =============== MATHEMATICAL TOOLS =============== ###\n",
|
| 902 |
-
"\n",
|
| 903 |
-
"SAFE_GLOBALS = {\"__builtins__\": {}, \"math\": math}\n",
|
| 904 |
-
"GOOGLE_API_KEY = os.getenv(\"GOOGLE_API_KEY\")\n",
|
| 905 |
-
"# one Groq client reused for all calls\n",
|
| 906 |
-
"_GROQ_CLIENT = Groq(api_key=os.getenv(\"GROQ_API_KEY\"))\n",
|
| 907 |
-
"\n",
|
| 908 |
-
"TEMP_DIR = os.getenv(\"TEMP_DIR\", \"./tmp\") # Default temp directory\n",
|
| 909 |
-
"QUESTIONS_FILES_DIR = os.path.join(TEMP_DIR, \"questions_files\")\n",
|
| 910 |
-
"os.makedirs(QUESTIONS_FILES_DIR, exist_ok=True)\n",
|
| 911 |
-
"\n",
|
| 912 |
-
"\n",
|
| 913 |
-
"@tool\n",
|
| 914 |
-
"def calculator(expr: str) -> float:\n",
|
| 915 |
-
" \"\"\"\n",
|
| 916 |
-
" Calculate a basic arithmetic or math expression.\n",
|
| 917 |
-
"\n",
|
| 918 |
-
" Accepted syntax\n",
|
| 919 |
-
" ---------------\n",
|
| 920 |
-
" • Literals: integers or floats (e.g. ``2``, ``3.14``)\n",
|
| 921 |
-
" • Operators: ``+``, ``-``, ``*``, ``/``, ``**``\n",
|
| 922 |
-
" • Unary minus (``-5``)\n",
|
| 923 |
-
" • Functions/consts from ``math`` (e.g. ``sin(0.5)``, ``pi``)\n",
|
| 924 |
-
" • Parentheses for grouping\n",
|
| 925 |
-
"\n",
|
| 926 |
-
" Parameters\n",
|
| 927 |
-
" ----------\n",
|
| 928 |
-
" expr : str\n",
|
| 929 |
-
" The expression to evaluate.\n",
|
| 930 |
-
"\n",
|
| 931 |
-
" Returns\n",
|
| 932 |
-
" -------\n",
|
| 933 |
-
" float\n",
|
| 934 |
-
" Result of the computation.\n",
|
| 935 |
-
"\n",
|
| 936 |
-
" Raises\n",
|
| 937 |
-
" ------\n",
|
| 938 |
-
" ValueError\n",
|
| 939 |
-
" If the expression contains unsupported syntax or names.\n",
|
| 940 |
-
" \"\"\"\n",
|
| 941 |
-
" try:\n",
|
| 942 |
-
" if \".\" in expr or \"__\" in expr:\n",
|
| 943 |
-
" raise ValueError(\"Attribute access not allowed\")\n",
|
| 944 |
-
" return eval(expr, SAFE_GLOBALS)\n",
|
| 945 |
-
" except (ValueError, SyntaxError, TypeError) as exc:\n",
|
| 946 |
-
" raise ValueError(f\"Invalid expression '{expr}': {exc}\") from exc\n",
|
| 947 |
-
"\n",
|
| 948 |
-
"\n",
|
| 949 |
-
"@tool\n",
|
| 950 |
-
"def multiply(a: float, b: float) -> float:\n",
|
| 951 |
-
" \"\"\"\n",
|
| 952 |
-
" Multiplies two numbers.\n",
|
| 953 |
-
" Args:\n",
|
| 954 |
-
" a (float): the first number\n",
|
| 955 |
-
" b (float): the second number\n",
|
| 956 |
-
" \"\"\"\n",
|
| 957 |
-
" return a * b\n",
|
| 958 |
-
"\n",
|
| 959 |
-
"\n",
|
| 960 |
-
"@tool\n",
|
| 961 |
-
"def add(a: float, b: float) -> float:\n",
|
| 962 |
-
" \"\"\"\n",
|
| 963 |
-
" Adds two numbers.\n",
|
| 964 |
-
" Args:\n",
|
| 965 |
-
" a (float): the first number\n",
|
| 966 |
-
" b (float): the second number\n",
|
| 967 |
-
" \"\"\"\n",
|
| 968 |
-
" return a + b\n",
|
| 969 |
-
"\n",
|
| 970 |
-
"\n",
|
| 971 |
-
"@tool\n",
|
| 972 |
-
"def subtract(a: float, b: float) -> int:\n",
|
| 973 |
-
" \"\"\"\n",
|
| 974 |
-
" Subtracts two numbers.\n",
|
| 975 |
-
" Args:\n",
|
| 976 |
-
" a (float): the first number\n",
|
| 977 |
-
" b (float): the second number\n",
|
| 978 |
-
" \"\"\"\n",
|
| 979 |
-
" return a - b\n",
|
| 980 |
-
"\n",
|
| 981 |
-
"\n",
|
| 982 |
-
"@tool\n",
|
| 983 |
-
"def divide(a: float, b: float) -> float:\n",
|
| 984 |
-
" \"\"\"\n",
|
| 985 |
-
" Divides two numbers.\n",
|
| 986 |
-
" Args:\n",
|
| 987 |
-
" a (float): the first float number\n",
|
| 988 |
-
" b (float): the second float number\n",
|
| 989 |
-
" \"\"\"\n",
|
| 990 |
-
" if b == 0:\n",
|
| 991 |
-
" raise ValueError(\"Cannot divided by zero.\")\n",
|
| 992 |
-
" return a / b\n",
|
| 993 |
-
"\n",
|
| 994 |
-
"\n",
|
| 995 |
-
"@tool\n",
|
| 996 |
-
"def modulus(a: int, b: int) -> int:\n",
|
| 997 |
-
" \"\"\"\n",
|
| 998 |
-
" Get the modulus of two numbers.\n",
|
| 999 |
-
" Args:\n",
|
| 1000 |
-
" a (int): the first number\n",
|
| 1001 |
-
" b (int): the second number\n",
|
| 1002 |
-
" \"\"\"\n",
|
| 1003 |
-
" return a % b\n",
|
| 1004 |
-
"\n",
|
| 1005 |
-
"\n",
|
| 1006 |
-
"@tool\n",
|
| 1007 |
-
"def power(a: float, b: float) -> float:\n",
|
| 1008 |
-
" \"\"\"\n",
|
| 1009 |
-
" Get the power of two numbers.\n",
|
| 1010 |
-
" Args:\n",
|
| 1011 |
-
" a (float): the first number\n",
|
| 1012 |
-
" b (float): the second number\n",
|
| 1013 |
-
" \"\"\"\n",
|
| 1014 |
-
" return a**b\n",
|
| 1015 |
-
"\n",
|
| 1016 |
-
"\n",
|
| 1017 |
-
"@tool\n",
|
| 1018 |
-
"def square_root(a: float) -> float | complex:\n",
|
| 1019 |
-
" \"\"\"\n",
|
| 1020 |
-
" Get the square root of a number.\n",
|
| 1021 |
-
" Args:\n",
|
| 1022 |
-
" a (float): the number to get the square root of\n",
|
| 1023 |
-
" \"\"\"\n",
|
| 1024 |
-
" if a >= 0:\n",
|
| 1025 |
-
" return a**0.5\n",
|
| 1026 |
-
" return cmath.sqrt(a)\n",
|
| 1027 |
-
"\n",
|
| 1028 |
-
"\n",
|
| 1029 |
-
"# ──────────────────────── generic search utils ───────────────────────\n",
|
| 1030 |
-
"_SEPARATOR = \"\\n\\n---\\n\\n\"\n",
|
| 1031 |
-
"\n",
|
| 1032 |
-
"\n",
|
| 1033 |
-
"def _format_docs(docs: Sequence, max_chars: int = 5000) -> str:\n",
|
| 1034 |
-
" \"\"\"Uniformly format loader docs for the LLM / calling agent.\"\"\"\n",
|
| 1035 |
-
" if not docs:\n",
|
| 1036 |
-
" return \"No results found.\"\n",
|
| 1037 |
-
" chunks = []\n",
|
| 1038 |
-
" for doc in docs:\n",
|
| 1039 |
-
" meta = doc.metadata\n",
|
| 1040 |
-
" snippet = doc.page_content[:max_chars].strip()\n",
|
| 1041 |
-
" chunks.append(\n",
|
| 1042 |
-
" f'<Document source=\"{meta.get(\"source\")}\" page=\"{meta.get(\"page\", \"\")}\">\\n'\n",
|
| 1043 |
-
" f\"{snippet}\\n</Document>\"\n",
|
| 1044 |
-
" )\n",
|
| 1045 |
-
" return _SEPARATOR.join(chunks)\n",
|
| 1046 |
-
"\n",
|
| 1047 |
-
"\n",
|
| 1048 |
-
"# ───────────────────────── wiki_search ──────────────────────────\n",
|
| 1049 |
-
"\n",
|
| 1050 |
-
"\n",
|
| 1051 |
-
"@tool\n",
|
| 1052 |
-
"def wiki_search(query: str) -> str:\n",
|
| 1053 |
-
" \"\"\"Return up to 2 Wikipedia pages about *query*.\"\"\"\n",
|
| 1054 |
-
" docs = WikipediaLoader(query=query, load_max_docs=2).load()\n",
|
| 1055 |
-
" return _format_docs(docs)\n",
|
| 1056 |
-
"\n",
|
| 1057 |
-
"\n",
|
| 1058 |
-
"# ───────────────────────── web_search ──────────────────────────\n",
|
| 1059 |
-
"\n",
|
| 1060 |
-
"\n",
|
| 1061 |
-
"# ``Document`` and ``_format_docs`` are provided by the host application.\n",
|
| 1062 |
-
"\n",
|
| 1063 |
-
"\n",
|
| 1064 |
-
"######################################################################\n",
|
| 1065 |
-
"# Public API #\n",
|
| 1066 |
-
"######################################################################\n",
|
| 1067 |
-
"@tool\n",
|
| 1068 |
-
"def web_search(query: str, max_results: int = 3) -> str:\n",
|
| 1069 |
-
" # docstring\n",
|
| 1070 |
-
" \"\"\"\n",
|
| 1071 |
-
" Return up to `max_results` Google search results for *query*.\n",
|
| 1072 |
-
" The output is formatted by `_format_docs`, so it matches the schema your\n",
|
| 1073 |
-
" other tools already use.\n",
|
| 1074 |
-
" \"\"\"\n",
|
| 1075 |
-
"\n",
|
| 1076 |
-
" docs: List[Document] = []\n",
|
| 1077 |
-
"\n",
|
| 1078 |
-
" try:\n",
|
| 1079 |
-
" wrapper = GoogleSerperAPIWrapper(k=max_results)\n",
|
| 1080 |
-
" result_json = wrapper.results(query)\n",
|
| 1081 |
-
"\n",
|
| 1082 |
-
" # Primary path — structured organic hits\n",
|
| 1083 |
-
" for hit in result_json.get(\"organic\", [])[:max_results]:\n",
|
| 1084 |
-
" docs.append(\n",
|
| 1085 |
-
" Document(\n",
|
| 1086 |
-
" page_content=hit.get(\"snippet\", \"\"),\n",
|
| 1087 |
-
" metadata={\"source\": hit.get(\"link\"), \"page\": \"\"},\n",
|
| 1088 |
-
" )\n",
|
| 1089 |
-
" )\n",
|
| 1090 |
-
"\n",
|
| 1091 |
-
" # Fallback — single‑string answer if no organic results\n",
|
| 1092 |
-
" if not docs:\n",
|
| 1093 |
-
" answer = wrapper.run(query)\n",
|
| 1094 |
-
" docs.append(\n",
|
| 1095 |
-
" Document(\n",
|
| 1096 |
-
" page_content=answer,\n",
|
| 1097 |
-
" metadata={\"source\": \"serper\", \"page\": \"\"},\n",
|
| 1098 |
-
" )\n",
|
| 1099 |
-
" )\n",
|
| 1100 |
-
" except Exception:\n",
|
| 1101 |
-
" # Total failure → return empty formatted structure\n",
|
| 1102 |
-
" pass\n",
|
| 1103 |
-
"\n",
|
| 1104 |
-
" return _format_docs(docs[:max_results])\n",
|
| 1105 |
-
"\n",
|
| 1106 |
-
"\n",
|
| 1107 |
-
"# @tool\n",
|
| 1108 |
-
"# def web_search(query: str, max_results: int = 3) -> str:\n",
|
| 1109 |
-
"# \"\"\"\n",
|
| 1110 |
-
"# Return up to `max_results` DuckDuckGo search results for *query*.\n",
|
| 1111 |
-
"\n",
|
| 1112 |
-
"# The output is formatted by `_format_docs`, so it matches the schema your\n",
|
| 1113 |
-
"# other tools already use.\n",
|
| 1114 |
-
"# \"\"\"\n",
|
| 1115 |
-
"# docs = []\n",
|
| 1116 |
-
"# with DDGS() as ddgs:\n",
|
| 1117 |
-
"# for hit in ddgs.text(query, max_results=max_results):\n",
|
| 1118 |
-
"# docs.append(\n",
|
| 1119 |
-
"# Document(\n",
|
| 1120 |
-
"# page_content=hit.get(\"body\") or hit.get(\"snippet\") or \"\",\n",
|
| 1121 |
-
"# metadata={\"source\": hit.get(\n",
|
| 1122 |
-
"# \"href\") or hit.get(\"url\"), \"page\": \"\"},\n",
|
| 1123 |
-
"# )\n",
|
| 1124 |
-
"# )\n",
|
| 1125 |
-
"\n",
|
| 1126 |
-
"# return _format_docs(docs)\n",
|
| 1127 |
-
"\n",
|
| 1128 |
-
"# ───────────────────────── arxiv_search ─────────���────────────────\n",
|
| 1129 |
-
"\n",
|
| 1130 |
-
"\n",
|
| 1131 |
-
"@tool\n",
|
| 1132 |
-
"def arxiv_search(query: str) -> str:\n",
|
| 1133 |
-
" \"\"\"Return up to 3 recent ArXiv papers about *query*.\"\"\"\n",
|
| 1134 |
-
" docs = ArxivLoader(query=query, load_max_docs=3).load()\n",
|
| 1135 |
-
" return _format_docs(docs)\n",
|
| 1136 |
-
"\n",
|
| 1137 |
-
"\n",
|
| 1138 |
-
"# ---------- 1. Search → list of links -----------------------\n",
|
| 1139 |
-
"\n",
|
| 1140 |
-
"\n",
|
| 1141 |
-
"@tool\n",
|
| 1142 |
-
"def list_webpage_links(url: str, same_domain_only: bool = False) -> list[str]:\n",
|
| 1143 |
-
" \"\"\"\n",
|
| 1144 |
-
" Return all unique <a href=\"...\"> links found in the HTML at `url`.\n",
|
| 1145 |
-
"\n",
|
| 1146 |
-
" Parameters\n",
|
| 1147 |
-
" ----------\n",
|
| 1148 |
-
" url : str\n",
|
| 1149 |
-
" Page to scrape.\n",
|
| 1150 |
-
" same_domain_only : bool, optional\n",
|
| 1151 |
-
" If True, keep only links on the same domain as `url`. Default = False.\n",
|
| 1152 |
-
"\n",
|
| 1153 |
-
" Returns\n",
|
| 1154 |
-
" -------\n",
|
| 1155 |
-
" list[str]\n",
|
| 1156 |
-
" Absolute URLs, deduplicated and sorted.\n",
|
| 1157 |
-
" \"\"\"\n",
|
| 1158 |
-
" try:\n",
|
| 1159 |
-
" html = requests.get(url, timeout=10).text\n",
|
| 1160 |
-
" except Exception as exc:\n",
|
| 1161 |
-
" return [f\"ERROR: fetch failed – {exc}\"]\n",
|
| 1162 |
-
"\n",
|
| 1163 |
-
" base = \"{uri.scheme}://{uri.netloc}\".format(uri=urlparse(url))\n",
|
| 1164 |
-
" soup = BeautifulSoup(html, \"html.parser\")\n",
|
| 1165 |
-
"\n",
|
| 1166 |
-
" links: set[str] = set()\n",
|
| 1167 |
-
" for tag in soup.find_all(\"a\", href=True):\n",
|
| 1168 |
-
" href: str = tag[\"href\"].strip()\n",
|
| 1169 |
-
" # Convert relative → absolute\n",
|
| 1170 |
-
" full = urljoin(base, href)\n",
|
| 1171 |
-
" if same_domain_only and urlparse(full).netloc != urlparse(url).netloc:\n",
|
| 1172 |
-
" continue\n",
|
| 1173 |
-
" links.add(full)\n",
|
| 1174 |
-
"\n",
|
| 1175 |
-
" return sorted(links)\n",
|
| 1176 |
-
"\n",
|
| 1177 |
-
"\n",
|
| 1178 |
-
"# ---------- 2. Browse → cleaned article text ----------------\n",
|
| 1179 |
-
"@tool\n",
|
| 1180 |
-
"def extract_webpage_text(url: str) -> str:\n",
|
| 1181 |
-
" \"\"\"\n",
|
| 1182 |
-
" Download `url` and return the main readable text (no html, ads, nav bars).\n",
|
| 1183 |
-
" Relies on trafilatura’s article extractor.\n",
|
| 1184 |
-
" \"\"\"\n",
|
| 1185 |
-
" raw = trafilatura.fetch_url(url)\n",
|
| 1186 |
-
" if raw is None:\n",
|
| 1187 |
-
" return \"🛑 Could not fetch the page.\"\n",
|
| 1188 |
-
"\n",
|
| 1189 |
-
" text = trafilatura.extract(\n",
|
| 1190 |
-
" raw,\n",
|
| 1191 |
-
" include_comments=False,\n",
|
| 1192 |
-
" include_tables=False,\n",
|
| 1193 |
-
" include_links=False,\n",
|
| 1194 |
-
" )\n",
|
| 1195 |
-
" return text or \"🛑 Page fetched but no readable text found.\"\n",
|
| 1196 |
-
"\n",
|
| 1197 |
-
"\n",
|
| 1198 |
-
"@tool\n",
|
| 1199 |
-
"def search_links_for_match(\n",
|
| 1200 |
-
" url: str,\n",
|
| 1201 |
-
" keyword: str,\n",
|
| 1202 |
-
" max_links: int = 100,\n",
|
| 1203 |
-
" same_domain_only: bool = True,\n",
|
| 1204 |
-
" case_sensitive: bool = False,\n",
|
| 1205 |
-
") -> list[str]:\n",
|
| 1206 |
-
" \"\"\"\n",
|
| 1207 |
-
" Search the content of up to `max_links` found on a webpage, and return URLs that contain the given keyword.\n",
|
| 1208 |
-
"\n",
|
| 1209 |
-
" Parameters:\n",
|
| 1210 |
-
" ----------\n",
|
| 1211 |
-
" url : str\n",
|
| 1212 |
-
" The starting webpage to extract links from.\n",
|
| 1213 |
-
" keyword : str\n",
|
| 1214 |
-
" The keyword or phrase to match inside linked pages.\n",
|
| 1215 |
-
" max_links : int, optional\n",
|
| 1216 |
-
" Number of links to follow (default: 10).\n",
|
| 1217 |
-
" same_domain_only : bool, optional\n",
|
| 1218 |
-
" Only consider links from the same domain (default: True).\n",
|
| 1219 |
-
" case_sensitive : bool, optional\n",
|
| 1220 |
-
" Whether the keyword match should be case-sensitive.\n",
|
| 1221 |
-
"\n",
|
| 1222 |
-
" Returns:\n",
|
| 1223 |
-
" -------\n",
|
| 1224 |
-
" list[str]\n",
|
| 1225 |
-
" List of URLs whose content contains the keyword.\n",
|
| 1226 |
-
" \"\"\"\n",
|
| 1227 |
-
"\n",
|
| 1228 |
-
" # Use the tool's .func() to access base function\n",
|
| 1229 |
-
" all_links = list_webpage_links.func(url=url, same_domain_only=same_domain_only)\n",
|
| 1230 |
-
" matched_links = []\n",
|
| 1231 |
-
"\n",
|
| 1232 |
-
" # Normalize keyword\n",
|
| 1233 |
-
" kw = keyword if case_sensitive else keyword.lower()\n",
|
| 1234 |
-
"\n",
|
| 1235 |
-
" for link in all_links[:max_links]:\n",
|
| 1236 |
-
" try:\n",
|
| 1237 |
-
" text = browse_webpage_link.func(link)\n",
|
| 1238 |
-
" if not case_sensitive:\n",
|
| 1239 |
-
" text = text.lower()\n",
|
| 1240 |
-
" if kw in text:\n",
|
| 1241 |
-
" matched_links.append(link)\n",
|
| 1242 |
-
" except Exception:\n",
|
| 1243 |
-
" continue\n",
|
| 1244 |
-
"\n",
|
| 1245 |
-
" return matched_links or [\"No matches found.\"]\n",
|
| 1246 |
-
"\n",
|
| 1247 |
-
"\n",
|
| 1248 |
-
"### =============== DOCUMENT PROCESSING TOOLS =============== ###\n",
|
| 1249 |
-
"\n",
|
| 1250 |
-
"# ─────────────────────────────────────────────────────────────────────────────\n",
|
| 1251 |
-
"# MarkItDown initialisation\n",
|
| 1252 |
-
"# • Works out-of-the-box for PDFs, Word, PowerPoint, Excel, images, etc.\n",
|
| 1253 |
-
"# • If DOCINTEL_ENDPOINT is set, heavy lifting (scanned PDFs, OCR tables…)\n",
|
| 1254 |
-
"# is delegated to Azure Document Intelligence.\n",
|
| 1255 |
-
"# ─────────────────────────────────────────────────────────────────────────────\n",
|
| 1256 |
-
"_DOCINTEL_ENDPOINT = os.getenv(\"DOCINTEL_ENDPOINT\") # set in env if needed\n",
|
| 1257 |
-
"_MD = MarkItDown(enable_plugins=False, docintel_endpoint=_DOCINTEL_ENDPOINT or None)\n",
|
| 1258 |
-
"\n",
|
| 1259 |
-
"\n",
|
| 1260 |
-
"@tool(\"read_document\", return_direct=True)\n",
|
| 1261 |
-
"def read_document(file_path: str, max_pages: Optional[int] = 10) -> str:\n",
|
| 1262 |
-
" \"\"\"\n",
|
| 1263 |
-
" Extract plain text from **any** local document supported by MarkItDown\n",
|
| 1264 |
-
" (PDF, DOCX, PPTX, XLSX, images, HTML,.py, etc.).\n",
|
| 1265 |
-
"\n",
|
| 1266 |
-
" Parameters\n",
|
| 1267 |
-
" ----------\n",
|
| 1268 |
-
" file_path : str\n",
|
| 1269 |
-
" Path to the document on disk.\n",
|
| 1270 |
-
" max_pages : int, optional\n",
|
| 1271 |
-
" Truncate output after this many pages/slides (only applies to\n",
|
| 1272 |
-
" paginated formats). If omitted, return the full text.\n",
|
| 1273 |
-
"\n",
|
| 1274 |
-
" Returns\n",
|
| 1275 |
-
" -------\n",
|
| 1276 |
-
" str\n",
|
| 1277 |
-
" The extracted text, or an error string that starts with\n",
|
| 1278 |
-
" “[read_document error] …”.\n",
|
| 1279 |
-
" \"\"\"\n",
|
| 1280 |
-
" path = Path(file_path).expanduser()\n",
|
| 1281 |
-
" if not path.exists():\n",
|
| 1282 |
-
" return f\"[read_document error] file not found: {file_path}\"\n",
|
| 1283 |
-
"\n",
|
| 1284 |
-
" try:\n",
|
| 1285 |
-
" result = _MD.convert(str(path))\n",
|
| 1286 |
-
" text = result.text_content or \"\"\n",
|
| 1287 |
-
"\n",
|
| 1288 |
-
" # For paginated formats MarkItDown uses form-feed (\\f) between pages\n",
|
| 1289 |
-
" if max_pages and max_pages > 0:\n",
|
| 1290 |
-
" pages = text.split(\"\\f\")\n",
|
| 1291 |
-
" text = \"\\f\".join(pages[:max_pages])\n",
|
| 1292 |
-
"\n",
|
| 1293 |
-
" cleaned = text.strip()\n",
|
| 1294 |
-
" return cleaned if cleaned else \"[read_document] no text found\"\n",
|
| 1295 |
-
"\n",
|
| 1296 |
-
" except Exception as err:\n",
|
| 1297 |
-
" return f\"[read_document error] {err}\"\n",
|
| 1298 |
-
"\n",
|
| 1299 |
-
"\n",
|
| 1300 |
-
"@tool\n",
|
| 1301 |
-
"def save_and_read_file(content: str, filename: Optional[str] = None) -> str:\n",
|
| 1302 |
-
" \"\"\"\n",
|
| 1303 |
-
" Save content to a file and return the path.\n",
|
| 1304 |
-
" Args:\n",
|
| 1305 |
-
" content (str): the content to save to the file\n",
|
| 1306 |
-
" filename (str, optional): the name of the file. If not provided, a random name file will be created.\n",
|
| 1307 |
-
" \"\"\"\n",
|
| 1308 |
-
" temp_dir = Path(QUESTIONS_FILES_DIR)\n",
|
| 1309 |
-
" if filename is None:\n",
|
| 1310 |
-
" temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir)\n",
|
| 1311 |
-
" filepath = temp_file.name\n",
|
| 1312 |
-
" else:\n",
|
| 1313 |
-
" filepath = os.path.join(temp_dir, filename)\n",
|
| 1314 |
-
"\n",
|
| 1315 |
-
" with open(filepath, \"w\") as f:\n",
|
| 1316 |
-
" f.write(content)\n",
|
| 1317 |
-
"\n",
|
| 1318 |
-
" return f\"File saved to {filepath}. You can read this file to process its contents.\"\n",
|
| 1319 |
-
"\n",
|
| 1320 |
-
"\n",
|
| 1321 |
-
"@tool\n",
|
| 1322 |
-
"def download_file_from_url(url: str, filename: Optional[str] = None) -> str:\n",
|
| 1323 |
-
" \"\"\"\n",
|
| 1324 |
-
" Download a file from a URL and save it to a temporary location.\n",
|
| 1325 |
-
" Args:\n",
|
| 1326 |
-
" url (str): the URL of the file to download.\n",
|
| 1327 |
-
" filename (str, optional): the name of the file. If not provided, a random name file will be created.\n",
|
| 1328 |
-
" \"\"\"\n",
|
| 1329 |
-
" try:\n",
|
| 1330 |
-
" # Parse URL to get filename if not provided\n",
|
| 1331 |
-
" if not filename:\n",
|
| 1332 |
-
" path = urlparse(url).path\n",
|
| 1333 |
-
" filename = os.path.basename(path)\n",
|
| 1334 |
-
" if not filename:\n",
|
| 1335 |
-
" filename = f\"downloaded_{uuid.uuid4().hex[:8]}\"\n",
|
| 1336 |
-
"\n",
|
| 1337 |
-
" # Create temporary file\n",
|
| 1338 |
-
" temp_dir = Path(QUESTIONS_FILES_DIR)\n",
|
| 1339 |
-
" filepath = os.path.join(temp_dir, filename)\n",
|
| 1340 |
-
"\n",
|
| 1341 |
-
" # Download the file\n",
|
| 1342 |
-
" response = requests.get(url, stream=True)\n",
|
| 1343 |
-
" response.raise_for_status()\n",
|
| 1344 |
-
"\n",
|
| 1345 |
-
" # Save the file\n",
|
| 1346 |
-
" with open(filepath, \"wb\") as f:\n",
|
| 1347 |
-
" for chunk in response.iter_content(chunk_size=8192):\n",
|
| 1348 |
-
" f.write(chunk)\n",
|
| 1349 |
-
"\n",
|
| 1350 |
-
" return f\"File downloaded to {filepath}. You can read this file to process its contents.\"\n",
|
| 1351 |
-
" except Exception as e:\n",
|
| 1352 |
-
" return f\"Error downloading file: {str(e)}\"\n",
|
| 1353 |
-
"\n",
|
| 1354 |
-
"\n",
|
| 1355 |
-
"# @tool\n",
|
| 1356 |
-
"# def extract_text_from_image(image_path: str) -> str:\n",
|
| 1357 |
-
"# \"\"\"\n",
|
| 1358 |
-
"# Extract text from an image using OCR library pytesseract (if available).\n",
|
| 1359 |
-
"# Args:\n",
|
| 1360 |
-
"# image_path (str): the path to the image file.\n",
|
| 1361 |
-
"# \"\"\"\n",
|
| 1362 |
-
"# try:\n",
|
| 1363 |
-
"# # Open the image\n",
|
| 1364 |
-
"# image = Image.open(image_path)\n",
|
| 1365 |
-
"\n",
|
| 1366 |
-
"# # Extract text from the image\n",
|
| 1367 |
-
"# text = pytesseract.image_to_string(image)\n",
|
| 1368 |
-
"\n",
|
| 1369 |
-
"# return f\"Extracted text from image:\\n\\n{text}\"\n",
|
| 1370 |
-
"# except Exception as e:\n",
|
| 1371 |
-
"# return f\"Error extracting text from image: {str(e)}\"\n",
|
| 1372 |
-
"\n",
|
| 1373 |
-
"\n",
|
| 1374 |
-
"@tool\n",
|
| 1375 |
-
"def analyze_csv_file(file_path: str, query: str) -> str:\n",
|
| 1376 |
-
" \"\"\"\n",
|
| 1377 |
-
" Analyze a CSV file using pandas and answer a question about it.\n",
|
| 1378 |
-
" Args:\n",
|
| 1379 |
-
" file_path (str): the path to the CSV file.\n",
|
| 1380 |
-
" query (str): Question about the data\n",
|
| 1381 |
-
" \"\"\"\n",
|
| 1382 |
-
" try:\n",
|
| 1383 |
-
" # Read the CSV file\n",
|
| 1384 |
-
" df = pd.read_csv(file_path)\n",
|
| 1385 |
-
"\n",
|
| 1386 |
-
" # Run various analyses based on the query\n",
|
| 1387 |
-
" result = f\"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\\n\"\n",
|
| 1388 |
-
" result += f\"Columns: {', '.join(df.columns)}\\n\\n\"\n",
|
| 1389 |
-
"\n",
|
| 1390 |
-
" # Add summary statistics\n",
|
| 1391 |
-
" result += \"Summary statistics:\\n\"\n",
|
| 1392 |
-
" result += str(df.describe())\n",
|
| 1393 |
-
"\n",
|
| 1394 |
-
" return result\n",
|
| 1395 |
-
"\n",
|
| 1396 |
-
" except Exception as e:\n",
|
| 1397 |
-
" return f\"Error analyzing CSV file: {str(e)}\"\n",
|
| 1398 |
-
"\n",
|
| 1399 |
-
"\n",
|
| 1400 |
-
"@tool\n",
|
| 1401 |
-
"def analyze_excel_file(file_path: str, query: str) -> str:\n",
|
| 1402 |
-
" \"\"\"\n",
|
| 1403 |
-
" Analyze an Excel file using pandas and answer a question about it.\n",
|
| 1404 |
-
" Args:\n",
|
| 1405 |
-
" file_path (str): the path to the Excel file.\n",
|
| 1406 |
-
" query (str): Question about the data\n",
|
| 1407 |
-
" \"\"\"\n",
|
| 1408 |
-
" try:\n",
|
| 1409 |
-
" # Read the Excel file\n",
|
| 1410 |
-
" df = pd.read_excel(file_path)\n",
|
| 1411 |
-
"\n",
|
| 1412 |
-
" # Run various analyses based on the query\n",
|
| 1413 |
-
" result = (\n",
|
| 1414 |
-
" f\"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\\n\"\n",
|
| 1415 |
-
" )\n",
|
| 1416 |
-
" result += f\"Columns: {', '.join(df.columns)}\\n\\n\"\n",
|
| 1417 |
-
"\n",
|
| 1418 |
-
" # Add summary statistics\n",
|
| 1419 |
-
" result += \"Summary statistics:\\n\"\n",
|
| 1420 |
-
" result += str(df.describe())\n",
|
| 1421 |
-
"\n",
|
| 1422 |
-
" return result\n",
|
| 1423 |
-
"\n",
|
| 1424 |
-
" except Exception as e:\n",
|
| 1425 |
-
" return f\"Error analyzing Excel file: {str(e)}\"\n",
|
| 1426 |
-
"\n",
|
| 1427 |
-
"\n",
|
| 1428 |
-
"# ─────────── vision tool ────────────────────────────\n",
|
| 1429 |
-
"_VISION_PROMPT = \"\"\"\n",
|
| 1430 |
-
"You are a GAIA-benchmark vision assistant. Return **exactly three sections**:\n",
|
| 1431 |
-
"\n",
|
| 1432 |
-
"1. Description – ≤40-word caption of the whole scene.\n",
|
| 1433 |
-
"2. Objects – JSON array of {\"name\": str, \"bbox\": [x0,y0,x1,y1]} for each visible item.\n",
|
| 1434 |
-
"3. Extracted text – verbatim text in the image or “[none]”.\n",
|
| 1435 |
-
"\n",
|
| 1436 |
-
"No extra commentary.\n",
|
| 1437 |
-
"\"\"\".strip()\n",
|
| 1438 |
-
"\n",
|
| 1439 |
-
"\n",
|
| 1440 |
-
"def _b64(path: Path) -> str:\n",
|
| 1441 |
-
" with path.open(\"rb\") as f:\n",
|
| 1442 |
-
" return base64.b64encode(f.read()).decode()\n",
|
| 1443 |
-
"\n",
|
| 1444 |
-
"\n",
|
| 1445 |
-
"@tool(\"describe_image\", return_direct=True)\n",
|
| 1446 |
-
"def describe_image(local_path: str) -> str:\n",
|
| 1447 |
-
" \"\"\"\n",
|
| 1448 |
-
" Caption a **local** image + list objects + OCR text using Groq’s\n",
|
| 1449 |
-
" meta-llama/llama-4-scout-17b-16e-instruct vision model.\n",
|
| 1450 |
-
"\n",
|
| 1451 |
-
" Steps for the agent:\n",
|
| 1452 |
-
" • If you only have a URL, first call `download_file_from_url`\n",
|
| 1453 |
-
" (that tool returns the tmp path). Then call this tool.\n",
|
| 1454 |
-
"\n",
|
| 1455 |
-
" Returns\n",
|
| 1456 |
-
" -------\n",
|
| 1457 |
-
" str\n",
|
| 1458 |
-
" Three-section GAIA-formatted answer, or an error string.\n",
|
| 1459 |
-
" \"\"\"\n",
|
| 1460 |
-
" p = Path(local_path).expanduser()\n",
|
| 1461 |
-
" if not p.exists():\n",
|
| 1462 |
-
" return f\"[describe_image] file not found: {local_path}\"\n",
|
| 1463 |
-
"\n",
|
| 1464 |
-
" try:\n",
|
| 1465 |
-
" data_uri = f\"data:image/{p.suffix.lstrip('.').lower()};base64,{_b64(p)}\"\n",
|
| 1466 |
-
" resp = _GROQ_CLIENT.chat.completions.create(\n",
|
| 1467 |
-
" model=\"meta-llama/llama-4-scout-17b-16e-instruct\",\n",
|
| 1468 |
-
" messages=[\n",
|
| 1469 |
-
" {\n",
|
| 1470 |
-
" \"role\": \"user\",\n",
|
| 1471 |
-
" \"content\": [\n",
|
| 1472 |
-
" {\"type\": \"text\", \"text\": _VISION_PROMPT},\n",
|
| 1473 |
-
" {\"type\": \"image_url\", \"image_url\": {\"url\": data_uri}},\n",
|
| 1474 |
-
" ],\n",
|
| 1475 |
-
" }\n",
|
| 1476 |
-
" ],\n",
|
| 1477 |
-
" )\n",
|
| 1478 |
-
" return resp.choices[0].message.content.strip()\n",
|
| 1479 |
-
"\n",
|
| 1480 |
-
" except Exception as err:\n",
|
| 1481 |
-
" return f\"[describe_image error] {err}\"\n",
|
| 1482 |
-
"\n",
|
| 1483 |
-
"\n",
|
| 1484 |
-
"# ──────────────────────── audio tool ──────────────────────────────\n",
|
| 1485 |
-
"\n",
|
| 1486 |
-
"\n",
|
| 1487 |
-
"@tool(\"transcribe_audio\", return_direct=True)\n",
|
| 1488 |
-
"def transcribe_audio(audio_path: str) -> str:\n",
|
| 1489 |
-
" \"\"\"\n",
|
| 1490 |
-
" Transcribe spoken content from a local audio file using Groq Whisper-large-v3.\n",
|
| 1491 |
-
"\n",
|
| 1492 |
-
" Parameters\n",
|
| 1493 |
-
" ----------\n",
|
| 1494 |
-
" audio_path : str\n",
|
| 1495 |
-
" Path to a .wav/.mp3/.m4a/.flac file on disk.\n",
|
| 1496 |
-
"\n",
|
| 1497 |
-
" Returns\n",
|
| 1498 |
-
" -------\n",
|
| 1499 |
-
" str\n",
|
| 1500 |
-
" The plain-text transcription, or an error string if something fails.\n",
|
| 1501 |
-
" \"\"\"\n",
|
| 1502 |
-
" p = Path(audio_path).expanduser()\n",
|
| 1503 |
-
" if not p.exists():\n",
|
| 1504 |
-
" return f\"[transcribe_audio] file not found: {audio_path}\"\n",
|
| 1505 |
-
"\n",
|
| 1506 |
-
" try:\n",
|
| 1507 |
-
" with p.open(\"rb\") as fh:\n",
|
| 1508 |
-
" resp = _GROQ_CLIENT.audio.transcriptions.create(\n",
|
| 1509 |
-
" file=(p.name, fh.read()),\n",
|
| 1510 |
-
" model=\"whisper-large-v3\",\n",
|
| 1511 |
-
" response_format=\"text\", # “text” → plain string in .text\n",
|
| 1512 |
-
" )\n",
|
| 1513 |
-
" txt = resp.text.strip() if hasattr(resp, \"text\") else str(resp).strip()\n",
|
| 1514 |
-
" return txt or \"[empty transcription]\"\n",
|
| 1515 |
-
"\n",
|
| 1516 |
-
" except Exception as err:\n",
|
| 1517 |
-
" return f\"[transcribe_audio error] {err}\"\n",
|
| 1518 |
-
"\n",
|
| 1519 |
-
"\n",
|
| 1520 |
-
"tools = [\n",
|
| 1521 |
-
" calculator,\n",
|
| 1522 |
-
" wiki_search,\n",
|
| 1523 |
-
" web_search,\n",
|
| 1524 |
-
" arxiv_search,\n",
|
| 1525 |
-
" list_webpage_links,\n",
|
| 1526 |
-
" extract_webpage_text,\n",
|
| 1527 |
-
" search_links_for_match,\n",
|
| 1528 |
-
" save_and_read_file,\n",
|
| 1529 |
-
" download_file_from_url,\n",
|
| 1530 |
-
" # extract_text_from_image,\n",
|
| 1531 |
-
" analyze_csv_file,\n",
|
| 1532 |
-
" analyze_excel_file,\n",
|
| 1533 |
-
" read_document,\n",
|
| 1534 |
-
" # analyze_image,\n",
|
| 1535 |
-
" # transform_image,\n",
|
| 1536 |
-
" # draw_on_image,\n",
|
| 1537 |
-
" # generate_simple_image,\n",
|
| 1538 |
-
" # combine_images,\n",
|
| 1539 |
-
" multiply,\n",
|
| 1540 |
-
" add,\n",
|
| 1541 |
-
" subtract,\n",
|
| 1542 |
-
" divide,\n",
|
| 1543 |
-
" modulus,\n",
|
| 1544 |
-
" power,\n",
|
| 1545 |
-
" square_root,\n",
|
| 1546 |
-
" describe_image,\n",
|
| 1547 |
-
" transcribe_audio,\n",
|
| 1548 |
-
"]\n",
|
| 1549 |
-
"\n",
|
| 1550 |
-
"\n",
|
| 1551 |
-
"def get_tools() -> list:\n",
|
| 1552 |
-
" \"\"\"\n",
|
| 1553 |
-
" Return the list of tools available for the agent.\n",
|
| 1554 |
-
" This can be used to dynamically load tools in the agent.\n",
|
| 1555 |
-
" \"\"\"\n",
|
| 1556 |
-
" return tools"
|
| 1557 |
-
]
|
| 1558 |
-
},
|
| 1559 |
-
{
|
| 1560 |
-
"cell_type": "code",
|
| 1561 |
-
"execution_count": null,
|
| 1562 |
-
"id": "f8c939d8",
|
| 1563 |
-
"metadata": {},
|
| 1564 |
-
"outputs": [
|
| 1565 |
-
{
|
| 1566 |
-
"data": {
|
| 1567 |
-
"text/plain": [
|
| 1568 |
-
"'**Description**: A cat lounges on a white ledge.\\n\\n**Objects**: \\n[\\n {\"name\": \"cat\", \"bbox\": [0.061, 0.184,0.969,0.874]}\\n]\\n\\n**Extracted text**: [none]'"
|
| 1569 |
-
]
|
| 1570 |
-
},
|
| 1571 |
-
"execution_count": 7,
|
| 1572 |
-
"metadata": {},
|
| 1573 |
-
"output_type": "execute_result"
|
| 1574 |
-
}
|
| 1575 |
-
],
|
| 1576 |
-
"source": [
|
| 1577 |
-
"describe_image.invoke({\"local_path\": \"./tmp/questions_files/Cat_August_2010-4.jpg\"})"
|
| 1578 |
-
]
|
| 1579 |
-
},
|
| 1580 |
-
{
|
| 1581 |
-
"cell_type": "code",
|
| 1582 |
-
"execution_count": 14,
|
| 1583 |
-
"id": "48f169bb",
|
| 1584 |
-
"metadata": {},
|
| 1585 |
-
"outputs": [
|
| 1586 |
-
{
|
| 1587 |
-
"data": {
|
| 1588 |
-
"text/plain": [
|
| 1589 |
-
"'<Document source=\"https://en.wikipedia.org/wiki/Paris\" page=\"\">\\nParis is the capital and largest city of France. With an estimated population of 2,048,472 residents in January 2025 in an area of more than 105 km2 (41 sq ...\\n</Document>\\n\\n---\\n\\n<Document source=\"https://www.coe.int/en/web/interculturalcities/paris\" page=\"\">\\nParis is the capital and most populous city of France. Situated on the Seine River, in the north of the country, it is in the centre of the Île-de-France ...\\n</Document>\\n\\n---\\n\\n<Document source=\"https://home.adelphi.edu/~ca19535/page%204.html\" page=\"\">\\nParis is the capital of France, the largest country of Europe with 550 000 km2 (65 millions inhabitants). Paris has 2.234 million inhabitants end 2011.\\n</Document>'"
|
| 1590 |
-
]
|
| 1591 |
-
},
|
| 1592 |
-
"execution_count": 14,
|
| 1593 |
-
"metadata": {},
|
| 1594 |
-
"output_type": "execute_result"
|
| 1595 |
-
}
|
| 1596 |
-
],
|
| 1597 |
-
"source": [
|
| 1598 |
-
"web_search.invoke({\"query\": \"What is the capital of France?\"})"
|
| 1599 |
-
]
|
| 1600 |
-
}
|
| 1601 |
-
],
|
| 1602 |
-
"metadata": {
|
| 1603 |
-
"kernelspec": {
|
| 1604 |
-
"display_name": ".venv",
|
| 1605 |
-
"language": "python",
|
| 1606 |
-
"name": "python3"
|
| 1607 |
-
},
|
| 1608 |
-
"language_info": {
|
| 1609 |
-
"codemirror_mode": {
|
| 1610 |
-
"name": "ipython",
|
| 1611 |
-
"version": 3
|
| 1612 |
-
},
|
| 1613 |
-
"file_extension": ".py",
|
| 1614 |
-
"mimetype": "text/x-python",
|
| 1615 |
-
"name": "python",
|
| 1616 |
-
"nbconvert_exporter": "python",
|
| 1617 |
-
"pygments_lexer": "ipython3",
|
| 1618 |
-
"version": "3.11.8"
|
| 1619 |
-
}
|
| 1620 |
-
},
|
| 1621 |
-
"nbformat": 4,
|
| 1622 |
-
"nbformat_minor": 5
|
| 1623 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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