muhammadnst commited on
Commit
4d42038
·
verified ·
1 Parent(s): fd47354

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
2
  import spaces
3
  import gradio as gr
4
  import datetime
@@ -14,6 +15,10 @@ def run_agent(prompt):
14
 
15
  from Gradio_UI import GradioUI
16
 
 
 
 
 
17
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
18
  @tool
19
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
@@ -40,6 +45,35 @@ def get_current_time_in_timezone(timezone: str) -> str:
40
  except Exception as e:
41
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  final_answer = FinalAnswerTool()
45
 
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ from tavily import TavilyClient
3
  import spaces
4
  import gradio as gr
5
  import datetime
 
15
 
16
  from Gradio_UI import GradioUI
17
 
18
+ # Initialize Tavily Client
19
+ tavily_api_key = os.getenv("TAVILY_API_KEY")
20
+ tavily_client = TavilyClient(api_key=tavily_api_key) if tavily_api_key else None
21
+
22
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
23
  @tool
24
  def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
 
45
  except Exception as e:
46
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
47
 
48
+ @tool
49
+ def web_search(query: str) -> str:
50
+ """A tool that performs a web search using Tavily API to fetch up-to-date information.
51
+
52
+ Args:
53
+ query: The search term or question to look up on the web.
54
+ """
55
+ if not tavily_client:
56
+ return "Error: TAVILY_API_KEY is not set in Secrets."
57
+
58
+ try:
59
+ response = tavily_client.search(query=query, max_results=5)
60
+ results = response.get("results", [])
61
+
62
+ if not results:
63
+ return "No results found for the query."
64
+
65
+ # Format output agar mudah dibaca oleh Agent
66
+ formatted_results = []
67
+ for result in results:
68
+ title = result.get("title", "")
69
+ url = result.get("url", "")
70
+ content = result.get("content", "")
71
+ formatted_results.append(f"Title: {title}\nURL: {url}\nContent: {content}\n")
72
+
73
+ return "\n---\n".join(formatted_results)
74
+ except Exception as e:
75
+ return f"Error executing web search: {str(e)}"
76
+
77
 
78
  final_answer = FinalAnswerTool()
79