Ubik80 commited on
Commit
255f737
·
verified ·
1 Parent(s): 58937bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  os.system("pip install 'smolagents[openai]'") # ✅ Installa SmolAgents con OpenAI
3
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool, OpenAIServerModel
4
  import datetime
5
- import wikipediaapi
6
  import requests
7
  import pytz
8
  import yaml
@@ -42,25 +41,25 @@ final_answer = FinalAnswerTool()
42
  # 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:
43
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
44
 
 
45
  @tool
46
- def search_wikipedia(query: str, lang: str = "it") -> str:
47
- """Fetches a summary from Wikipedia for a given topic.
48
 
49
  Args:
50
- query: The search term for Wikipedia.
51
- lang: Language of the Wikipedia page (default: Italian).
52
 
53
  Returns:
54
- A short summary from Wikipedia.
55
  """
56
- user_agent = "MyAgent/1.0 (Contact: [email protected])" # ✅ Aggiungi un User-Agent personalizzato
57
- wiki_wiki = wikipediaapi.Wikipedia(lang, user_agent=user_agent) # 🔥 Ora Wikipedia accetterà la richiesta
58
- page = wiki_wiki.page(query)
59
 
60
- if not page.exists():
61
- return f"Nessuna pagina trovata per '{query}' su Wikipedia."
62
 
63
- return page.summary[:500] + "..." # Restituiamo un riassunto breve
 
64
 
65
 
66
  model = OpenAIServerModel(
@@ -85,7 +84,7 @@ with open("prompts.yaml", 'r') as stream:
85
 
86
  agent = CodeAgent(
87
  model=model,
88
- tools=[final_answer, get_current_time_in_timezone, image_generation_tool, search_wikipedia],
89
  max_steps=6,
90
  verbosity_level=1,
91
  grammar=None,
 
2
  os.system("pip install 'smolagents[openai]'") # ✅ Installa SmolAgents con OpenAI
3
  from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool, OpenAIServerModel
4
  import datetime
 
5
  import requests
6
  import pytz
7
  import yaml
 
41
  # 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:
42
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
43
 
44
+ # 1️⃣ Creiamo il Tool di ricerca su DuckDuckGo
45
  @tool
46
+ def search_duckduckgo(query: str) -> str:
47
+ """Performs a web search using DuckDuckGo and returns the first result summary.
48
 
49
  Args:
50
+ query: The search term.
 
51
 
52
  Returns:
53
+ A summary of the first result found.
54
  """
55
+ search_tool = DuckDuckGoSearchTool() # ✅ Inizializza il motore di ricerca
56
+ results = search_tool(query) # 🔍 Esegue la ricerca
 
57
 
58
+ if not results:
59
+ return f"Nessun risultato trovato per '{query}'."
60
 
61
+ top_result = results[0] # 📌 Prendiamo il primo risultato
62
+ return f"🔎 {top_result['title']}\n{top_result['snippet']}\n🔗 {top_result['link']}"
63
 
64
 
65
  model = OpenAIServerModel(
 
84
 
85
  agent = CodeAgent(
86
  model=model,
87
+ tools=[final_answer, get_current_time_in_timezone, image_generation_tool, search_duckduckgo],
88
  max_steps=6,
89
  verbosity_level=1,
90
  grammar=None,