File size: 2,639 Bytes
7ce893b
9b5b26a
 
c19d193
7ce893b
6aae614
7ce893b
 
3753dba
8fe992b
9b5b26a
 
7ce893b
9b5b26a
7ce893b
 
9b5b26a
7ce893b
9b5b26a
7ce893b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6aae614
2e39bf9
ae7a494
 
7ce893b
ae7a494
e121372
7ce893b
 
 
 
13d500a
8c01ffb
 
9b5b26a
 
8c01ffb
7ce893b
861422e
7ce893b
8c01ffb
8fe992b
2e39bf9
8c01ffb
 
 
 
 
 
7ce893b
8fe992b
 
9b5b26a
7ce893b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from smolagents import CodeAgent, HfApiModel, load_tool, tool
import datetime
import pytz
import yaml
import requests
from tools.final_answer import FinalAnswerTool
from tools.web_search import DuckDuckGoSearchTool
from tools.visit_webpage import VisitWebpageTool
from smolagents.default_tools import UserInputTool

from Gradio_UI import GradioUI


@tool
def search_arxiv(query: str) -> str:
    """A tool that searches arXiv for papers matching the query using web search
    Args:
        query: The search query string
    """
    try:
        # Initialize tools
        web_search = DuckDuckGoSearchTool(max_results=5)
        webpage_reader = VisitWebpageTool()
        
        # Perform web search with site:arxiv.org to restrict to arXiv
        search_query = f"site:arxiv.org {query}"
        search_results = web_search.forward(search_query)
        
        # Extract URLs and visit each page
        import re
        arxiv_urls = re.findall(r'\((https://arxiv\.org/[^)]+)\)', search_results)
        
        if not arxiv_urls:
            return "No arXiv papers found for the given query."
            
        papers_content = []
        for url in arxiv_urls[:3]:  # Limit to first 3 papers
            try:
                content = webpage_reader.forward(url)
                papers_content.append(f"## Paper from {url}\n{content}\n")
            except Exception as e:
                papers_content.append(f"Failed to fetch content from {url}: {str(e)}")
        
        return "\n\n".join(papers_content)
    except Exception as e:
        return f"Error searching arXiv: {str(e)}"

final_answer = FinalAnswerTool()
user_input = UserInputTool()

# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id="Qwen/Qwen2.5-Coder-32B-Instruct",  # it is possible that this model may be overloaded
    custom_role_conversions=None,
)


# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", "r") as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    tools=[final_answer, user_input, search_arxiv],  ## add your tools here (don't remove final answer)
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates,
)


GradioUI(agent).launch()