M96820 commited on
Commit
7ce893b
·
unverified ·
1 Parent(s): fa6e9fa

feat: add search for papers tool

Browse files
Files changed (1) hide show
  1. app.py +46 -19
app.py CHANGED
@@ -1,22 +1,49 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
  import datetime
3
- import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
 
 
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -37,33 +64,33 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  final_answer = FinalAnswerTool()
38
 
39
  # 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:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
 
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- custom_role_conversions=None,
47
  )
48
 
49
 
50
  # Import tool from Hub
51
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
52
 
53
- with open("prompts.yaml", 'r') as stream:
54
  prompt_templates = yaml.safe_load(stream)
55
-
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
63
  name=None,
64
  description=None,
65
- prompt_templates=prompt_templates
66
  )
67
 
68
 
69
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
2
  import datetime
 
3
  import pytz
4
  import yaml
5
+ import requests
6
  from tools.final_answer import FinalAnswerTool
7
+ from tools.web_search import DuckDuckGoSearchTool
8
+ from tools.visit_webpage import VisitWebpageTool
9
 
10
  from Gradio_UI import GradioUI
11
 
12
+
13
  @tool
14
+ def search_arxiv(query: str) -> str:
15
+ """A tool that searches arXiv for papers matching the query using web search
 
16
  Args:
17
+ query: The search query string
 
18
  """
19
+ try:
20
+ # Initialize tools
21
+ web_search = DuckDuckGoSearchTool(max_results=5)
22
+ webpage_reader = VisitWebpageTool()
23
+
24
+ # Perform web search with site:arxiv.org to restrict to arXiv
25
+ search_query = f"site:arxiv.org {query}"
26
+ search_results = web_search.forward(search_query)
27
+
28
+ # Extract URLs and visit each page
29
+ import re
30
+ arxiv_urls = re.findall(r'\((https://arxiv\.org/[^)]+)\)', search_results)
31
+
32
+ if not arxiv_urls:
33
+ return "No arXiv papers found for the given query."
34
+
35
+ papers_content = []
36
+ for url in arxiv_urls[:3]: # Limit to first 3 papers
37
+ try:
38
+ content = webpage_reader.forward(url)
39
+ papers_content.append(f"## Paper from {url}\n{content}\n")
40
+ except Exception as e:
41
+ papers_content.append(f"Failed to fetch content from {url}: {str(e)}")
42
+
43
+ return "\n\n".join(papers_content)
44
+ except Exception as e:
45
+ return f"Error searching arXiv: {str(e)}"
46
+
47
 
48
  @tool
49
  def get_current_time_in_timezone(timezone: str) -> str:
 
64
  final_answer = FinalAnswerTool()
65
 
66
  # 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:
67
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
68
 
69
  model = HfApiModel(
70
+ max_tokens=2096,
71
+ temperature=0.5,
72
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # it is possible that this model may be overloaded
73
+ custom_role_conversions=None,
74
  )
75
 
76
 
77
  # Import tool from Hub
78
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
79
 
80
+ with open("prompts.yaml", "r") as stream:
81
  prompt_templates = yaml.safe_load(stream)
82
+
83
  agent = CodeAgent(
84
  model=model,
85
+ tools=[final_answer], ## add your tools here (don't remove final answer)
86
  max_steps=6,
87
  verbosity_level=1,
88
  grammar=None,
89
  planning_interval=None,
90
  name=None,
91
  description=None,
92
+ prompt_templates=prompt_templates,
93
  )
94
 
95
 
96
+ GradioUI(agent).launch()