HongyeYang commited on
Commit
b893ef5
·
verified ·
1 Parent(s): 64bea55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -66,6 +66,57 @@ def get_current_time_in_timezone(timezone: str) -> str:
66
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
67
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  final_answer = FinalAnswerTool()
70
 
71
  # 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:
@@ -87,7 +138,7 @@ with open("prompts.yaml", 'r') as stream:
87
 
88
  agent = CodeAgent(
89
  model=model,
90
- tools=[final_answer,calculator], ## add your tools here (don't remove final answer)
91
  max_steps=6,
92
  verbosity_level=1,
93
  grammar=None,
 
66
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
67
 
68
 
69
+
70
+ # 创建一个底层网页搜索工具
71
+ web_search = DuckDuckGoSearchTool(
72
+ max_results=8,
73
+ rate_limit=1.0
74
+ )
75
+
76
+
77
+ @tool
78
+ def search_today_news(topic: str) -> str:
79
+ """搜索指定主题的今日新闻,供 Agent 整理成1至2段新闻简报。
80
+
81
+ Args:
82
+ topic: 新闻主题,例如“人工智能”“全球财经”“中国科技”。
83
+ 输入“综合”时,搜索国内外综合新闻。
84
+ """
85
+ # 获取北京时间的当前日期
86
+ timezone = pytz.timezone("Asia/Shanghai")
87
+ today = datetime.datetime.now(timezone).strftime("%Y-%m-%d")
88
+
89
+ # 根据主题组织搜索语句
90
+ if topic.strip() == "综合":
91
+ query = (
92
+ f"{today} 今日重要新闻 "
93
+ f"国内新闻 国际新闻 科技新闻 财经新闻"
94
+ )
95
+ else:
96
+ query = f"{today} {topic} 今日最新新闻"
97
+
98
+ # 调用 DuckDuckGo 搜索
99
+ search_results = web_search(query)
100
+
101
+ # 将搜索结果返回给 Agent
102
+ return f"""
103
+ 当前日期:{today}
104
+ 新闻主题:{topic}
105
+
106
+ 以下是互联网搜索结果:
107
+
108
+ {search_results}
109
+
110
+ 请根据以上搜索结果,筛选最重要的新闻,
111
+ 用中文写成1至2段简短新闻简报。
112
+ 不要逐条罗列,不要编造搜索结果中没有的信息。
113
+ """
114
+
115
+
116
+
117
+
118
+
119
+
120
  final_answer = FinalAnswerTool()
121
 
122
  # 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:
 
138
 
139
  agent = CodeAgent(
140
  model=model,
141
+ tools=[final_answer,calculator,search_today_news], ## add your tools here (don't remove final answer)
142
  max_steps=6,
143
  verbosity_level=1,
144
  grammar=None,