Update app.py
#636
by Jarine - opened
app.py
CHANGED
|
@@ -1,69 +1,65 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
| 24 |
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., '
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
-
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
#
|
| 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 |
-
#
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
with open("prompts.yaml", 'r') as stream:
|
| 54 |
-
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
| 9 |
+
# ==========================================
|
| 10 |
+
# 1. 定义工具 (必须在 Agent 之前)
|
| 11 |
+
# ==========================================
|
| 12 |
+
|
| 13 |
@tool
|
| 14 |
+
def suggest_nearby_food(location: str, food_type: str) -> str:
|
| 15 |
+
"""A tool that searches for restaurants based on location and food type.
|
|
|
|
| 16 |
Args:
|
| 17 |
+
location: The user's current city or district (e.g., '上海静安区').
|
| 18 |
+
food_type: The type of food or vibe (e.g., '下午茶', '夜宵', '点心').
|
| 19 |
"""
|
| 20 |
+
# 这个工具负责把用户的需求转化成搜索关键词
|
| 21 |
+
search_query = f"{location} {food_type} 推荐 评价高"
|
| 22 |
+
return f"我正在为您寻找:{search_query}。请稍等,我将结合实时搜索结果为您分析..."
|
| 23 |
|
| 24 |
@tool
|
| 25 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 26 |
"""A tool that fetches the current local time in a specified timezone.
|
| 27 |
Args:
|
| 28 |
+
timezone: A string representing a valid timezone (e.g., 'Asia/Shanghai').
|
| 29 |
"""
|
| 30 |
try:
|
|
|
|
| 31 |
tz = pytz.timezone(timezone)
|
|
|
|
| 32 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 33 |
return f"The current local time in {timezone} is: {local_time}"
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 36 |
|
| 37 |
+
# 这里保留你原本可能有的 final_answer 工具
|
| 38 |
final_answer = FinalAnswerTool()
|
| 39 |
|
| 40 |
+
# ==========================================
|
| 41 |
+
# 2. 实例化基础组件
|
| 42 |
+
# ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
model = HfApiModel()
|
| 45 |
+
search_tool = DuckDuckGoSearchTool()
|
| 46 |
|
| 47 |
+
# ==========================================
|
| 48 |
+
# 3. 初始化 Agent (这里是核心,它引用了上面的所有工具)
|
| 49 |
+
# ==========================================
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
agent = CodeAgent(
|
| 52 |
model=model,
|
| 53 |
+
# 这里的列表包含了你上面定义的所有工具名
|
| 54 |
+
tools=[suggest_nearby_food, get_current_time_in_timezone, search_tool, final_answer],
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
+
executor_type='local',
|
| 58 |
+
can_answer_telemetry=True
|
|
|
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# ==========================================
|
| 62 |
+
# 4. 启动界面 (必须在 Agent 实例化之后)
|
| 63 |
+
# ==========================================
|
| 64 |
|
| 65 |
GradioUI(agent).launch()
|