Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- README.md +22 -13
- analysis.ipynb +0 -0
- requirements.txt +39 -0
- run.py +146 -0
- run_gaia.py +297 -0
- visual_vs_text_browser.ipynb +359 -0
README.md
CHANGED
|
@@ -1,13 +1,22 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Open Deep Research
|
| 2 |
+
|
| 3 |
+
Welcome to this open replication of [OpenAI's Deep Research](https://openai.com/index/introducing-deep-research/)!
|
| 4 |
+
|
| 5 |
+
Read more about this implementation's goal and methods [in our blog post](https://huggingface.co/blog/open-deep-research).
|
| 6 |
+
|
| 7 |
+
This agent achieves 55% pass@1 on GAIA validation set, vs 67% for Deep Research.
|
| 8 |
+
|
| 9 |
+
To install it, first run
|
| 10 |
+
```bash
|
| 11 |
+
pip install -r requirements.txt
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
And install smolagents dev version
|
| 15 |
+
```bash
|
| 16 |
+
pip install smolagents[dev]
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
Then you're good to go! Run the run.py script, as in:
|
| 20 |
+
```bash
|
| 21 |
+
python run.py --model-id "o1" "Your question here!"
|
| 22 |
+
```
|
analysis.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
anthropic>=0.37.1
|
| 2 |
+
beautifulsoup4>=4.12.3
|
| 3 |
+
datasets>=2.21.0
|
| 4 |
+
google_search_results>=2.4.2
|
| 5 |
+
huggingface_hub>=0.23.4
|
| 6 |
+
mammoth>=1.8.0
|
| 7 |
+
markdownify>=0.13.1
|
| 8 |
+
numexpr>=2.10.1
|
| 9 |
+
numpy>=2.1.2
|
| 10 |
+
openai>=1.52.2
|
| 11 |
+
openpyxl
|
| 12 |
+
pandas>=2.2.3
|
| 13 |
+
pathvalidate>=3.2.1
|
| 14 |
+
pdfminer>=20191125
|
| 15 |
+
pdfminer.six>=20240706
|
| 16 |
+
Pillow>=11.0.0
|
| 17 |
+
puremagic>=1.28
|
| 18 |
+
pypdf>=5.1.0
|
| 19 |
+
python-dotenv>=1.0.1
|
| 20 |
+
python_pptx>=1.0.2
|
| 21 |
+
Requests>=2.32.3
|
| 22 |
+
serpapi>=0.1.5
|
| 23 |
+
tqdm>=4.66.4
|
| 24 |
+
torch>=2.2.2
|
| 25 |
+
torchvision>=0.17.2
|
| 26 |
+
transformers>=4.46.0
|
| 27 |
+
youtube_transcript_api>=0.6.2
|
| 28 |
+
chess
|
| 29 |
+
sympy
|
| 30 |
+
pubchempy
|
| 31 |
+
Bio
|
| 32 |
+
scikit-learn
|
| 33 |
+
scipy
|
| 34 |
+
pydub
|
| 35 |
+
PyPDF2
|
| 36 |
+
python-pptx
|
| 37 |
+
torch
|
| 38 |
+
xlrd
|
| 39 |
+
SpeechRecognition
|
run.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import threading
|
| 4 |
+
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
+
from scripts.text_inspector_tool import TextInspectorTool
|
| 8 |
+
from scripts.text_web_browser import (
|
| 9 |
+
ArchiveSearchTool,
|
| 10 |
+
FinderTool,
|
| 11 |
+
FindNextTool,
|
| 12 |
+
PageDownTool,
|
| 13 |
+
PageUpTool,
|
| 14 |
+
SearchInformationTool,
|
| 15 |
+
SimpleTextBrowser,
|
| 16 |
+
VisitTool,
|
| 17 |
+
)
|
| 18 |
+
from scripts.visual_qa import visualizer
|
| 19 |
+
|
| 20 |
+
from smolagents import (
|
| 21 |
+
CodeAgent,
|
| 22 |
+
# HfApiModel,
|
| 23 |
+
LiteLLMModel,
|
| 24 |
+
ToolCallingAgent,
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
AUTHORIZED_IMPORTS = [
|
| 29 |
+
"requests",
|
| 30 |
+
"zipfile",
|
| 31 |
+
"os",
|
| 32 |
+
"pandas",
|
| 33 |
+
"numpy",
|
| 34 |
+
"sympy",
|
| 35 |
+
"json",
|
| 36 |
+
"bs4",
|
| 37 |
+
"pubchempy",
|
| 38 |
+
"xml",
|
| 39 |
+
"yahoo_finance",
|
| 40 |
+
"Bio",
|
| 41 |
+
"sklearn",
|
| 42 |
+
"scipy",
|
| 43 |
+
"pydub",
|
| 44 |
+
"io",
|
| 45 |
+
"PIL",
|
| 46 |
+
"chess",
|
| 47 |
+
"PyPDF2",
|
| 48 |
+
"pptx",
|
| 49 |
+
"torch",
|
| 50 |
+
"datetime",
|
| 51 |
+
"fractions",
|
| 52 |
+
"csv",
|
| 53 |
+
]
|
| 54 |
+
load_dotenv(override=True)
|
| 55 |
+
login(os.getenv("HF_TOKEN"))
|
| 56 |
+
|
| 57 |
+
append_answer_lock = threading.Lock()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def parse_args():
|
| 61 |
+
parser = argparse.ArgumentParser()
|
| 62 |
+
parser.add_argument(
|
| 63 |
+
"question", type=str, help="for example: 'How many studio albums did Mercedes Sosa release before 2007?'"
|
| 64 |
+
)
|
| 65 |
+
parser.add_argument("--model-id", type=str, default="o1")
|
| 66 |
+
return parser.parse_args()
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
|
| 70 |
+
|
| 71 |
+
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
| 72 |
+
|
| 73 |
+
BROWSER_CONFIG = {
|
| 74 |
+
"viewport_size": 1024 * 5,
|
| 75 |
+
"downloads_folder": "downloads_folder",
|
| 76 |
+
"request_kwargs": {
|
| 77 |
+
"headers": {"User-Agent": user_agent},
|
| 78 |
+
"timeout": 300,
|
| 79 |
+
},
|
| 80 |
+
"serpapi_key": os.getenv("SERPAPI_API_KEY"),
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def main():
|
| 87 |
+
args = parse_args()
|
| 88 |
+
text_limit = 100000
|
| 89 |
+
|
| 90 |
+
model = LiteLLMModel(
|
| 91 |
+
args.model_id,
|
| 92 |
+
custom_role_conversions=custom_role_conversions,
|
| 93 |
+
max_completion_tokens=8192,
|
| 94 |
+
reasoning_effort="high",
|
| 95 |
+
)
|
| 96 |
+
document_inspection_tool = TextInspectorTool(model, text_limit)
|
| 97 |
+
|
| 98 |
+
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
| 99 |
+
|
| 100 |
+
WEB_TOOLS = [
|
| 101 |
+
SearchInformationTool(browser),
|
| 102 |
+
VisitTool(browser),
|
| 103 |
+
PageUpTool(browser),
|
| 104 |
+
PageDownTool(browser),
|
| 105 |
+
FinderTool(browser),
|
| 106 |
+
FindNextTool(browser),
|
| 107 |
+
ArchiveSearchTool(browser),
|
| 108 |
+
TextInspectorTool(model, text_limit),
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
text_webbrowser_agent = ToolCallingAgent(
|
| 112 |
+
model=model,
|
| 113 |
+
tools=WEB_TOOLS,
|
| 114 |
+
max_steps=20,
|
| 115 |
+
verbosity_level=2,
|
| 116 |
+
planning_interval=4,
|
| 117 |
+
name="search_agent",
|
| 118 |
+
description="""A team member that will search the internet to answer your question.
|
| 119 |
+
Ask him for all your questions that require browsing the web.
|
| 120 |
+
Provide him as much context as possible, in particular if you need to search on a specific timeframe!
|
| 121 |
+
And don't hesitate to provide him with a complex search task, like finding a difference between two webpages.
|
| 122 |
+
Your request must be a real sentence, not a google search! Like "Find me this information (...)" rather than a few keywords.
|
| 123 |
+
""",
|
| 124 |
+
provide_run_summary=True,
|
| 125 |
+
)
|
| 126 |
+
text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to .txt online files.
|
| 127 |
+
If a non-html page is in another format, especially .pdf or a Youtube video, use tool 'inspect_file_as_text' to inspect it.
|
| 128 |
+
Additionally, if after some searching you find out that you need more information to answer the question, you can use `final_answer` with your request for clarification as argument to request for more information."""
|
| 129 |
+
|
| 130 |
+
manager_agent = CodeAgent(
|
| 131 |
+
model=model,
|
| 132 |
+
tools=[visualizer, document_inspection_tool],
|
| 133 |
+
max_steps=12,
|
| 134 |
+
verbosity_level=2,
|
| 135 |
+
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 136 |
+
planning_interval=4,
|
| 137 |
+
managed_agents=[text_webbrowser_agent],
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
answer = manager_agent.run(args.question)
|
| 141 |
+
|
| 142 |
+
print(f"Got this answer: {answer}")
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
main()
|
run_gaia.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import threading
|
| 5 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import List
|
| 9 |
+
|
| 10 |
+
import datasets
|
| 11 |
+
import pandas as pd
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
+
from huggingface_hub import login
|
| 14 |
+
from scripts.reformulator import prepare_response
|
| 15 |
+
from scripts.run_agents import (
|
| 16 |
+
get_single_file_description,
|
| 17 |
+
get_zip_description,
|
| 18 |
+
)
|
| 19 |
+
from scripts.text_inspector_tool import TextInspectorTool
|
| 20 |
+
from scripts.text_web_browser import (
|
| 21 |
+
ArchiveSearchTool,
|
| 22 |
+
FinderTool,
|
| 23 |
+
FindNextTool,
|
| 24 |
+
PageDownTool,
|
| 25 |
+
PageUpTool,
|
| 26 |
+
SearchInformationTool,
|
| 27 |
+
SimpleTextBrowser,
|
| 28 |
+
VisitTool,
|
| 29 |
+
)
|
| 30 |
+
from scripts.visual_qa import visualizer
|
| 31 |
+
from tqdm import tqdm
|
| 32 |
+
|
| 33 |
+
from smolagents import (
|
| 34 |
+
CodeAgent,
|
| 35 |
+
# HfApiModel,
|
| 36 |
+
LiteLLMModel,
|
| 37 |
+
Model,
|
| 38 |
+
ToolCallingAgent,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
AUTHORIZED_IMPORTS = [
|
| 43 |
+
"requests",
|
| 44 |
+
"zipfile",
|
| 45 |
+
"os",
|
| 46 |
+
"pandas",
|
| 47 |
+
"numpy",
|
| 48 |
+
"sympy",
|
| 49 |
+
"json",
|
| 50 |
+
"bs4",
|
| 51 |
+
"pubchempy",
|
| 52 |
+
"xml",
|
| 53 |
+
"yahoo_finance",
|
| 54 |
+
"Bio",
|
| 55 |
+
"sklearn",
|
| 56 |
+
"scipy",
|
| 57 |
+
"pydub",
|
| 58 |
+
"io",
|
| 59 |
+
"PIL",
|
| 60 |
+
"chess",
|
| 61 |
+
"PyPDF2",
|
| 62 |
+
"pptx",
|
| 63 |
+
"torch",
|
| 64 |
+
"datetime",
|
| 65 |
+
"fractions",
|
| 66 |
+
"csv",
|
| 67 |
+
]
|
| 68 |
+
load_dotenv(override=True)
|
| 69 |
+
login(os.getenv("HF_TOKEN"))
|
| 70 |
+
|
| 71 |
+
append_answer_lock = threading.Lock()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def parse_args():
|
| 75 |
+
parser = argparse.ArgumentParser()
|
| 76 |
+
parser.add_argument("--concurrency", type=int, default=8)
|
| 77 |
+
parser.add_argument("--model-id", type=str, default="o1")
|
| 78 |
+
parser.add_argument("--run-name", type=str, required=True)
|
| 79 |
+
return parser.parse_args()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
### IMPORTANT: EVALUATION SWITCHES
|
| 83 |
+
|
| 84 |
+
print("Make sure you deactivated Tailscale VPN, else some URLs will be blocked!")
|
| 85 |
+
|
| 86 |
+
USE_OPEN_MODELS = False
|
| 87 |
+
|
| 88 |
+
SET = "validation"
|
| 89 |
+
|
| 90 |
+
custom_role_conversions = {"tool-call": "assistant", "tool-response": "user"}
|
| 91 |
+
|
| 92 |
+
### LOAD EVALUATION DATASET
|
| 93 |
+
|
| 94 |
+
eval_ds = datasets.load_dataset("gaia-benchmark/GAIA", "2023_all")[SET]
|
| 95 |
+
eval_ds = eval_ds.rename_columns({"Question": "question", "Final answer": "true_answer", "Level": "task"})
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def preprocess_file_paths(row):
|
| 99 |
+
if len(row["file_name"]) > 0:
|
| 100 |
+
row["file_name"] = f"data/gaia/{SET}/" + row["file_name"]
|
| 101 |
+
return row
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
eval_ds = eval_ds.map(preprocess_file_paths)
|
| 105 |
+
eval_df = pd.DataFrame(eval_ds)
|
| 106 |
+
print("Loaded evaluation dataset:")
|
| 107 |
+
print(eval_df["task"].value_counts())
|
| 108 |
+
|
| 109 |
+
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
| 110 |
+
|
| 111 |
+
BROWSER_CONFIG = {
|
| 112 |
+
"viewport_size": 1024 * 5,
|
| 113 |
+
"downloads_folder": "downloads_folder",
|
| 114 |
+
"request_kwargs": {
|
| 115 |
+
"headers": {"User-Agent": user_agent},
|
| 116 |
+
"timeout": 300,
|
| 117 |
+
},
|
| 118 |
+
"serpapi_key": os.getenv("SERPAPI_API_KEY"),
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
os.makedirs(f"./{BROWSER_CONFIG['downloads_folder']}", exist_ok=True)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def create_agent_hierarchy(model: Model):
|
| 125 |
+
text_limit = 100000
|
| 126 |
+
ti_tool = TextInspectorTool(model, text_limit)
|
| 127 |
+
|
| 128 |
+
browser = SimpleTextBrowser(**BROWSER_CONFIG)
|
| 129 |
+
|
| 130 |
+
WEB_TOOLS = [
|
| 131 |
+
SearchInformationTool(browser),
|
| 132 |
+
VisitTool(browser),
|
| 133 |
+
PageUpTool(browser),
|
| 134 |
+
PageDownTool(browser),
|
| 135 |
+
FinderTool(browser),
|
| 136 |
+
FindNextTool(browser),
|
| 137 |
+
ArchiveSearchTool(browser),
|
| 138 |
+
TextInspectorTool(model, text_limit),
|
| 139 |
+
]
|
| 140 |
+
text_webbrowser_agent = ToolCallingAgent(
|
| 141 |
+
model=model,
|
| 142 |
+
tools=WEB_TOOLS,
|
| 143 |
+
max_steps=20,
|
| 144 |
+
verbosity_level=2,
|
| 145 |
+
planning_interval=4,
|
| 146 |
+
name="search_agent",
|
| 147 |
+
description="""A team member that will search the internet to answer your question.
|
| 148 |
+
Ask him for all your questions that require browsing the web.
|
| 149 |
+
Provide him as much context as possible, in particular if you need to search on a specific timeframe!
|
| 150 |
+
And don't hesitate to provide him with a complex search task, like finding a difference between two webpages.
|
| 151 |
+
Your request must be a real sentence, not a google search! Like "Find me this information (...)" rather than a few keywords.
|
| 152 |
+
""",
|
| 153 |
+
provide_run_summary=True,
|
| 154 |
+
)
|
| 155 |
+
text_webbrowser_agent.prompt_templates["managed_agent"]["task"] += """You can navigate to .txt online files.
|
| 156 |
+
If a non-html page is in another format, especially .pdf or a Youtube video, use tool 'inspect_file_as_text' to inspect it.
|
| 157 |
+
Additionally, if after some searching you find out that you need more information to answer the question, you can use `final_answer` with your request for clarification as argument to request for more information."""
|
| 158 |
+
|
| 159 |
+
manager_agent = CodeAgent(
|
| 160 |
+
model=model,
|
| 161 |
+
tools=[visualizer, ti_tool],
|
| 162 |
+
max_steps=12,
|
| 163 |
+
verbosity_level=2,
|
| 164 |
+
additional_authorized_imports=AUTHORIZED_IMPORTS,
|
| 165 |
+
planning_interval=4,
|
| 166 |
+
managed_agents=[text_webbrowser_agent],
|
| 167 |
+
)
|
| 168 |
+
return manager_agent
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def append_answer(entry: dict, jsonl_file: str) -> None:
|
| 172 |
+
jsonl_file = Path(jsonl_file)
|
| 173 |
+
jsonl_file.parent.mkdir(parents=True, exist_ok=True)
|
| 174 |
+
with append_answer_lock, open(jsonl_file, "a", encoding="utf-8") as fp:
|
| 175 |
+
fp.write(json.dumps(entry) + "\n")
|
| 176 |
+
assert os.path.exists(jsonl_file), "File not found!"
|
| 177 |
+
print("Answer exported to file:", jsonl_file.resolve())
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def answer_single_question(example, model_id, answers_file, visual_inspection_tool):
|
| 181 |
+
model = LiteLLMModel(
|
| 182 |
+
model_id,
|
| 183 |
+
custom_role_conversions=custom_role_conversions,
|
| 184 |
+
max_completion_tokens=8192,
|
| 185 |
+
reasoning_effort="high",
|
| 186 |
+
)
|
| 187 |
+
# model = HfApiModel("Qwen/Qwen2.5-72B-Instruct", provider="together")
|
| 188 |
+
# "https://lnxyuvj02bpe6mam.us-east-1.aws.endpoints.huggingface.cloud",
|
| 189 |
+
# custom_role_conversions=custom_role_conversions,
|
| 190 |
+
# # provider="sambanova",
|
| 191 |
+
# max_tokens=8096,
|
| 192 |
+
# )
|
| 193 |
+
document_inspection_tool = TextInspectorTool(model, 100000)
|
| 194 |
+
|
| 195 |
+
agent = create_agent_hierarchy(model)
|
| 196 |
+
|
| 197 |
+
augmented_question = """You have one question to answer. It is paramount that you provide a correct answer.
|
| 198 |
+
Give it all you can: I know for a fact that you have access to all the relevant tools to solve it and find the correct answer (the answer does exist). Failure or 'I cannot answer' or 'None found' will not be tolerated, success will be rewarded.
|
| 199 |
+
Run verification steps if that's needed, you must make sure you find the correct answer!
|
| 200 |
+
Here is the task:
|
| 201 |
+
""" + example["question"]
|
| 202 |
+
|
| 203 |
+
if example["file_name"]:
|
| 204 |
+
if ".zip" in example["file_name"]:
|
| 205 |
+
prompt_use_files = "\n\nTo solve the task above, you will have to use these attached files:\n"
|
| 206 |
+
prompt_use_files += get_zip_description(
|
| 207 |
+
example["file_name"], example["question"], visual_inspection_tool, document_inspection_tool
|
| 208 |
+
)
|
| 209 |
+
else:
|
| 210 |
+
prompt_use_files = "\n\nTo solve the task above, you will have to use this attached file:"
|
| 211 |
+
prompt_use_files += get_single_file_description(
|
| 212 |
+
example["file_name"], example["question"], visual_inspection_tool, document_inspection_tool
|
| 213 |
+
)
|
| 214 |
+
augmented_question += prompt_use_files
|
| 215 |
+
|
| 216 |
+
start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 217 |
+
try:
|
| 218 |
+
# Run agent 🚀
|
| 219 |
+
final_result = agent.run(augmented_question)
|
| 220 |
+
|
| 221 |
+
agent_memory = agent.write_memory_to_messages(summary_mode=True)
|
| 222 |
+
|
| 223 |
+
final_result = prepare_response(augmented_question, agent_memory, reformulation_model=model)
|
| 224 |
+
|
| 225 |
+
output = str(final_result)
|
| 226 |
+
for memory_step in agent.memory.steps:
|
| 227 |
+
memory_step.model_input_messages = None
|
| 228 |
+
intermediate_steps = [str(step) for step in agent.memory.steps]
|
| 229 |
+
|
| 230 |
+
# Check for parsing errors which indicate the LLM failed to follow the required format
|
| 231 |
+
parsing_error = True if any(["AgentParsingError" in step for step in intermediate_steps]) else False
|
| 232 |
+
|
| 233 |
+
# check if iteration limit exceeded
|
| 234 |
+
iteration_limit_exceeded = True if "Agent stopped due to iteration limit or time limit." in output else False
|
| 235 |
+
raised_exception = False
|
| 236 |
+
|
| 237 |
+
except Exception as e:
|
| 238 |
+
print("Error on ", augmented_question, e)
|
| 239 |
+
output = None
|
| 240 |
+
intermediate_steps = []
|
| 241 |
+
parsing_error = False
|
| 242 |
+
iteration_limit_exceeded = False
|
| 243 |
+
exception = e
|
| 244 |
+
raised_exception = True
|
| 245 |
+
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 246 |
+
annotated_example = {
|
| 247 |
+
"agent_name": model.model_id,
|
| 248 |
+
"question": example["question"],
|
| 249 |
+
"augmented_question": augmented_question,
|
| 250 |
+
"prediction": output,
|
| 251 |
+
"intermediate_steps": intermediate_steps,
|
| 252 |
+
"parsing_error": parsing_error,
|
| 253 |
+
"iteration_limit_exceeded": iteration_limit_exceeded,
|
| 254 |
+
"agent_error": str(exception) if raised_exception else None,
|
| 255 |
+
"start_time": start_time,
|
| 256 |
+
"end_time": end_time,
|
| 257 |
+
"task": example["task"],
|
| 258 |
+
"task_id": example["task_id"],
|
| 259 |
+
"true_answer": example["true_answer"],
|
| 260 |
+
}
|
| 261 |
+
append_answer(annotated_example, answers_file)
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def get_examples_to_answer(answers_file, eval_ds) -> List[dict]:
|
| 265 |
+
print(f"Loading answers from {answers_file}...")
|
| 266 |
+
try:
|
| 267 |
+
done_questions = pd.read_json(answers_file, lines=True)["question"].tolist()
|
| 268 |
+
print(f"Found {len(done_questions)} previous results!")
|
| 269 |
+
except Exception as e:
|
| 270 |
+
print("Error when loading records: ", e)
|
| 271 |
+
print("No usable records! ▶️ Starting new.")
|
| 272 |
+
done_questions = []
|
| 273 |
+
return [line for line in eval_ds.to_list() if line["question"] not in done_questions]
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def main():
|
| 277 |
+
args = parse_args()
|
| 278 |
+
print(f"Starting run with arguments: {args}")
|
| 279 |
+
|
| 280 |
+
answers_file = f"output/{SET}/{args.run_name}.jsonl"
|
| 281 |
+
tasks_to_run = get_examples_to_answer(answers_file, eval_ds)
|
| 282 |
+
|
| 283 |
+
with ThreadPoolExecutor(max_workers=args.concurrency) as exe:
|
| 284 |
+
futures = [
|
| 285 |
+
exe.submit(answer_single_question, example, args.model_id, answers_file, visualizer)
|
| 286 |
+
for example in tasks_to_run
|
| 287 |
+
]
|
| 288 |
+
for f in tqdm(as_completed(futures), total=len(tasks_to_run), desc="Processing tasks"):
|
| 289 |
+
f.result()
|
| 290 |
+
|
| 291 |
+
# for example in tasks_to_run:
|
| 292 |
+
# answer_single_question(example, args.model_id, answers_file, visualizer)
|
| 293 |
+
print("All tasks processed.")
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
if __name__ == "__main__":
|
| 297 |
+
main()
|
visual_vs_text_browser.ipynb
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# Compare a text-based vs a vision-based browser\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"Warning: this notebook is experimental, it probably won't work out of the box!"
|
| 10 |
+
]
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
"cell_type": "code",
|
| 14 |
+
"execution_count": null,
|
| 15 |
+
"metadata": {},
|
| 16 |
+
"outputs": [],
|
| 17 |
+
"source": [
|
| 18 |
+
"!pip install \"smolagents[litellm]\" -q"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "code",
|
| 23 |
+
"execution_count": null,
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"outputs": [],
|
| 26 |
+
"source": [
|
| 27 |
+
"import datasets\n",
|
| 28 |
+
"\n",
|
| 29 |
+
"\n",
|
| 30 |
+
"eval_ds = datasets.load_dataset(\"gaia-benchmark/GAIA\", \"2023_all\")[\"validation\"]"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "code",
|
| 35 |
+
"execution_count": 3,
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [],
|
| 38 |
+
"source": [
|
| 39 |
+
"to_keep = [\n",
|
| 40 |
+
" \"What's the last line of the rhyme under the flavor\",\n",
|
| 41 |
+
" 'Of the authors (First M. Last) that worked on the paper \"Pie Menus or Linear Menus',\n",
|
| 42 |
+
" \"In Series 9, Episode 11 of Doctor Who, the Doctor is trapped inside an ever-shifting maze. What is this location called in the official script for the episode? Give the setting exactly as it appears in the first scene heading.\",\n",
|
| 43 |
+
" \"Which contributor to the version of OpenCV where support was added for the Mask-RCNN model has the same name as a former Chinese head of government when the names are transliterated to the Latin alphabet?\",\n",
|
| 44 |
+
" \"The photograph in the Whitney Museum of American Art's collection with accession number 2022.128 shows a person holding a book. Which military unit did the author of this book join in 1813? Answer without using articles.\",\n",
|
| 45 |
+
" \"I went to Virtue restaurant & bar in Chicago for my birthday on March 22, 2021 and the main course I had was delicious! Unfortunately, when I went back about a month later on April 21, it was no longer on the dinner menu.\",\n",
|
| 46 |
+
" \"In Emily Midkiff's June 2014 article in a journal named for the one of Hreidmar's \",\n",
|
| 47 |
+
" \"Under DDC 633 on Bielefeld University Library's BASE, as of 2020\",\n",
|
| 48 |
+
" \"In the 2018 VSCode blog post on replit.com, what was the command they clicked on in the last video to remove extra lines?\",\n",
|
| 49 |
+
" \"The Metropolitan Museum of Art has a portrait in its collection with an accession number of 29.100.5. Of the consecrators and co-consecrators\",\n",
|
| 50 |
+
" \"In Nature journal's Scientific Reports conference proceedings from 2012, in the article that did not mention plasmons or plasmonics, what nano-compound is studied?\",\n",
|
| 51 |
+
" 'In the year 2022, and before December, what does \"R\" stand for in the three core policies of the type of content',\n",
|
| 52 |
+
" \"Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?\",\n",
|
| 53 |
+
"]\n",
|
| 54 |
+
"eval_ds = eval_ds.filter(lambda row: any([el in row[\"Question\"] for el in to_keep]))\n",
|
| 55 |
+
"eval_ds = eval_ds.rename_columns({\"Question\": \"question\", \"Final answer\": \"true_answer\", \"Level\": \"task\"})"
|
| 56 |
+
]
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"cell_type": "code",
|
| 60 |
+
"execution_count": null,
|
| 61 |
+
"metadata": {},
|
| 62 |
+
"outputs": [],
|
| 63 |
+
"source": [
|
| 64 |
+
"import os\n",
|
| 65 |
+
"\n",
|
| 66 |
+
"from dotenv import load_dotenv\n",
|
| 67 |
+
"from huggingface_hub import login\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"load_dotenv(override=True)\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"login(os.getenv(\"HF_TOKEN\"))"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "markdown",
|
| 77 |
+
"metadata": {},
|
| 78 |
+
"source": [
|
| 79 |
+
"### Text browser"
|
| 80 |
+
]
|
| 81 |
+
},
|
| 82 |
+
{
|
| 83 |
+
"cell_type": "code",
|
| 84 |
+
"execution_count": null,
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"outputs": [],
|
| 87 |
+
"source": [
|
| 88 |
+
"from scripts.run_agents import answer_questions\n",
|
| 89 |
+
"from scripts.text_inspector_tool import TextInspectorTool\n",
|
| 90 |
+
"from scripts.text_web_browser import (\n",
|
| 91 |
+
" ArchiveSearchTool,\n",
|
| 92 |
+
" FinderTool,\n",
|
| 93 |
+
" FindNextTool,\n",
|
| 94 |
+
" NavigationalSearchTool,\n",
|
| 95 |
+
" PageDownTool,\n",
|
| 96 |
+
" PageUpTool,\n",
|
| 97 |
+
" SearchInformationTool,\n",
|
| 98 |
+
" VisitTool,\n",
|
| 99 |
+
")\n",
|
| 100 |
+
"from scripts.visual_qa import VisualQAGPT4Tool\n",
|
| 101 |
+
"\n",
|
| 102 |
+
"from smolagents import CodeAgent, LiteLLMModel\n",
|
| 103 |
+
"\n",
|
| 104 |
+
"\n",
|
| 105 |
+
"proprietary_model = LiteLLMModel(\"gpt-4o\")"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
{
|
| 109 |
+
"cell_type": "code",
|
| 110 |
+
"execution_count": null,
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"outputs": [],
|
| 113 |
+
"source": [
|
| 114 |
+
"### BUILD AGENTS & TOOLS\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"WEB_TOOLS = [\n",
|
| 117 |
+
" SearchInformationTool(),\n",
|
| 118 |
+
" NavigationalSearchTool(),\n",
|
| 119 |
+
" VisitTool(),\n",
|
| 120 |
+
" PageUpTool(),\n",
|
| 121 |
+
" PageDownTool(),\n",
|
| 122 |
+
" FinderTool(),\n",
|
| 123 |
+
" FindNextTool(),\n",
|
| 124 |
+
" ArchiveSearchTool(),\n",
|
| 125 |
+
"]\n",
|
| 126 |
+
"\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"surfer_agent = CodeAgent(\n",
|
| 129 |
+
" model=proprietary_model,\n",
|
| 130 |
+
" tools=WEB_TOOLS,\n",
|
| 131 |
+
" max_steps=20,\n",
|
| 132 |
+
" verbosity_level=2,\n",
|
| 133 |
+
")\n",
|
| 134 |
+
"\n",
|
| 135 |
+
"results_text = answer_questions(\n",
|
| 136 |
+
" eval_ds,\n",
|
| 137 |
+
" surfer_agent,\n",
|
| 138 |
+
" \"code_gpt4o_27-01_text\",\n",
|
| 139 |
+
" reformulation_model=proprietary_model,\n",
|
| 140 |
+
" output_folder=\"output_browsers\",\n",
|
| 141 |
+
" visual_inspection_tool=VisualQAGPT4Tool(),\n",
|
| 142 |
+
" text_inspector_tool=TextInspectorTool(proprietary_model, 40000),\n",
|
| 143 |
+
")"
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"cell_type": "markdown",
|
| 148 |
+
"metadata": {},
|
| 149 |
+
"source": [
|
| 150 |
+
"### Vision browser"
|
| 151 |
+
]
|
| 152 |
+
},
|
| 153 |
+
{
|
| 154 |
+
"cell_type": "code",
|
| 155 |
+
"execution_count": null,
|
| 156 |
+
"metadata": {},
|
| 157 |
+
"outputs": [],
|
| 158 |
+
"source": [
|
| 159 |
+
"!pip install helium -q"
|
| 160 |
+
]
|
| 161 |
+
},
|
| 162 |
+
{
|
| 163 |
+
"cell_type": "code",
|
| 164 |
+
"execution_count": null,
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"outputs": [],
|
| 167 |
+
"source": [
|
| 168 |
+
"from scripts.visual_qa import VisualQAGPT4Tool\n",
|
| 169 |
+
"\n",
|
| 170 |
+
"from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel\n",
|
| 171 |
+
"from smolagents.vision_web_browser import (\n",
|
| 172 |
+
" close_popups,\n",
|
| 173 |
+
" go_back,\n",
|
| 174 |
+
" helium_instructions,\n",
|
| 175 |
+
" initialize_agent,\n",
|
| 176 |
+
" save_screenshot,\n",
|
| 177 |
+
" search_item_ctrl_f,\n",
|
| 178 |
+
")\n",
|
| 179 |
+
"\n",
|
| 180 |
+
"\n",
|
| 181 |
+
"proprietary_model = LiteLLMModel(\"gpt-4o\")\n",
|
| 182 |
+
"vision_browser_agent = initialize_agent(proprietary_model)\n",
|
| 183 |
+
"### BUILD AGENTS & TOOLS\n",
|
| 184 |
+
"\n",
|
| 185 |
+
"CodeAgent(\n",
|
| 186 |
+
" tools=[DuckDuckGoSearchTool(), go_back, close_popups, search_item_ctrl_f],\n",
|
| 187 |
+
" model=proprietary_model,\n",
|
| 188 |
+
" additional_authorized_imports=[\"helium\"],\n",
|
| 189 |
+
" step_callbacks=[save_screenshot],\n",
|
| 190 |
+
" max_steps=20,\n",
|
| 191 |
+
" verbosity_level=2,\n",
|
| 192 |
+
")\n",
|
| 193 |
+
"\n",
|
| 194 |
+
"results_vision = answer_questions(\n",
|
| 195 |
+
" eval_ds,\n",
|
| 196 |
+
" vision_browser_agent,\n",
|
| 197 |
+
" \"code_gpt4o_27-01_vision\",\n",
|
| 198 |
+
" reformulation_model=proprietary_model,\n",
|
| 199 |
+
" output_folder=\"output_browsers\",\n",
|
| 200 |
+
" visual_inspection_tool=VisualQAGPT4Tool(),\n",
|
| 201 |
+
" text_inspector_tool=TextInspectorTool(proprietary_model, 40000),\n",
|
| 202 |
+
" postprompt=helium_instructions\n",
|
| 203 |
+
" + \"Any web browser controls won't work on .pdf urls, rather use the tool 'inspect_file_as_text' to read them\",\n",
|
| 204 |
+
")"
|
| 205 |
+
]
|
| 206 |
+
},
|
| 207 |
+
{
|
| 208 |
+
"cell_type": "markdown",
|
| 209 |
+
"metadata": {},
|
| 210 |
+
"source": [
|
| 211 |
+
"### Browser-use browser"
|
| 212 |
+
]
|
| 213 |
+
},
|
| 214 |
+
{
|
| 215 |
+
"cell_type": "code",
|
| 216 |
+
"execution_count": null,
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"outputs": [],
|
| 219 |
+
"source": [
|
| 220 |
+
"!pip install browser-use lxml_html_clean -q\n",
|
| 221 |
+
"!playwright install"
|
| 222 |
+
]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"cell_type": "code",
|
| 226 |
+
"execution_count": null,
|
| 227 |
+
"metadata": {},
|
| 228 |
+
"outputs": [],
|
| 229 |
+
"source": [
|
| 230 |
+
"import asyncio\n",
|
| 231 |
+
"\n",
|
| 232 |
+
"import nest_asyncio\n",
|
| 233 |
+
"\n",
|
| 234 |
+
"\n",
|
| 235 |
+
"nest_asyncio.apply()\n",
|
| 236 |
+
"\n",
|
| 237 |
+
"from browser_use import Agent\n",
|
| 238 |
+
"from dotenv import load_dotenv\n",
|
| 239 |
+
"from langchain_openai import ChatOpenAI\n",
|
| 240 |
+
"\n",
|
| 241 |
+
"\n",
|
| 242 |
+
"load_dotenv()\n",
|
| 243 |
+
"\n",
|
| 244 |
+
"\n",
|
| 245 |
+
"class BrowserUseAgent:\n",
|
| 246 |
+
" logs = []\n",
|
| 247 |
+
"\n",
|
| 248 |
+
" def write_inner_memory_from_logs(self, summary_mode):\n",
|
| 249 |
+
" return self.results\n",
|
| 250 |
+
"\n",
|
| 251 |
+
" def run(self, task, **kwargs):\n",
|
| 252 |
+
" agent = Agent(\n",
|
| 253 |
+
" task=task,\n",
|
| 254 |
+
" llm=ChatOpenAI(model=\"gpt-4o\"),\n",
|
| 255 |
+
" )\n",
|
| 256 |
+
" self.results = asyncio.get_event_loop().run_until_complete(agent.run())\n",
|
| 257 |
+
" return self.results.history[-1].result[0].extracted_content\n",
|
| 258 |
+
"\n",
|
| 259 |
+
"\n",
|
| 260 |
+
"browser_use_agent = BrowserUseAgent()\n",
|
| 261 |
+
"\n",
|
| 262 |
+
"results_browseruse = answer_questions(\n",
|
| 263 |
+
" eval_ds,\n",
|
| 264 |
+
" browser_use_agent,\n",
|
| 265 |
+
" \"gpt-4o_27-01_browseruse\",\n",
|
| 266 |
+
" reformulation_model=proprietary_model,\n",
|
| 267 |
+
" output_folder=\"output_browsers\",\n",
|
| 268 |
+
" visual_inspection_tool=VisualQAGPT4Tool(),\n",
|
| 269 |
+
" text_inspector_tool=TextInspectorTool(proprietary_model, 40000),\n",
|
| 270 |
+
" postprompt=\"\",\n",
|
| 271 |
+
" run_simple=True,\n",
|
| 272 |
+
")"
|
| 273 |
+
]
|
| 274 |
+
},
|
| 275 |
+
{
|
| 276 |
+
"cell_type": "markdown",
|
| 277 |
+
"metadata": {},
|
| 278 |
+
"source": [
|
| 279 |
+
"### Get results"
|
| 280 |
+
]
|
| 281 |
+
},
|
| 282 |
+
{
|
| 283 |
+
"cell_type": "code",
|
| 284 |
+
"execution_count": null,
|
| 285 |
+
"metadata": {},
|
| 286 |
+
"outputs": [],
|
| 287 |
+
"source": [
|
| 288 |
+
"import pandas as pd\n",
|
| 289 |
+
"from scripts.gaia_scorer import question_scorer\n",
|
| 290 |
+
"\n",
|
| 291 |
+
"\n",
|
| 292 |
+
"results_vision, results_text, results_browseruse = (\n",
|
| 293 |
+
" pd.DataFrame(results_vision),\n",
|
| 294 |
+
" pd.DataFrame(results_text),\n",
|
| 295 |
+
" pd.DataFrame(results_browseruse),\n",
|
| 296 |
+
")\n",
|
| 297 |
+
"\n",
|
| 298 |
+
"results_vision[\"is_correct\"] = results_vision.apply(\n",
|
| 299 |
+
" lambda x: question_scorer(x[\"prediction\"], x[\"true_answer\"]), axis=1\n",
|
| 300 |
+
")\n",
|
| 301 |
+
"results_text[\"is_correct\"] = results_text.apply(lambda x: question_scorer(x[\"prediction\"], x[\"true_answer\"]), axis=1)\n",
|
| 302 |
+
"results_browseruse[\"is_correct\"] = results_browseruse.apply(\n",
|
| 303 |
+
" lambda x: question_scorer(x[\"prediction\"], x[\"true_answer\"]), axis=1\n",
|
| 304 |
+
")"
|
| 305 |
+
]
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"cell_type": "code",
|
| 309 |
+
"execution_count": null,
|
| 310 |
+
"metadata": {},
|
| 311 |
+
"outputs": [],
|
| 312 |
+
"source": [
|
| 313 |
+
"results = pd.concat([results_vision, results_text, results_browseruse])\n",
|
| 314 |
+
"results.groupby(\"agent_name\")[\"is_correct\"].mean()"
|
| 315 |
+
]
|
| 316 |
+
},
|
| 317 |
+
{
|
| 318 |
+
"cell_type": "code",
|
| 319 |
+
"execution_count": null,
|
| 320 |
+
"metadata": {},
|
| 321 |
+
"outputs": [],
|
| 322 |
+
"source": [
|
| 323 |
+
"correct_vision_results = results_vision.loc[results_vision[\"is_correct\"]]\n",
|
| 324 |
+
"correct_vision_results"
|
| 325 |
+
]
|
| 326 |
+
},
|
| 327 |
+
{
|
| 328 |
+
"cell_type": "code",
|
| 329 |
+
"execution_count": null,
|
| 330 |
+
"metadata": {},
|
| 331 |
+
"outputs": [],
|
| 332 |
+
"source": [
|
| 333 |
+
"false_text_results = results_text.loc[~results_text[\"is_correct\"]]\n",
|
| 334 |
+
"false_text_results"
|
| 335 |
+
]
|
| 336 |
+
}
|
| 337 |
+
],
|
| 338 |
+
"metadata": {
|
| 339 |
+
"kernelspec": {
|
| 340 |
+
"display_name": "gaia",
|
| 341 |
+
"language": "python",
|
| 342 |
+
"name": "python3"
|
| 343 |
+
},
|
| 344 |
+
"language_info": {
|
| 345 |
+
"codemirror_mode": {
|
| 346 |
+
"name": "ipython",
|
| 347 |
+
"version": 3
|
| 348 |
+
},
|
| 349 |
+
"file_extension": ".py",
|
| 350 |
+
"mimetype": "text/x-python",
|
| 351 |
+
"name": "python",
|
| 352 |
+
"nbconvert_exporter": "python",
|
| 353 |
+
"pygments_lexer": "ipython3",
|
| 354 |
+
"version": "3.12.0"
|
| 355 |
+
}
|
| 356 |
+
},
|
| 357 |
+
"nbformat": 4,
|
| 358 |
+
"nbformat_minor": 2
|
| 359 |
+
}
|