Hanson commited on
Commit
735d45f
·
1 Parent(s): 9436bac

fix: modify prompt templates and add web tools

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -10,7 +10,9 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
- from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool
 
 
14
  import os
15
  import spaces
16
 
@@ -21,19 +23,39 @@ def fake_gpu_function():
21
  class BasicAgent:
22
  def __init__(self):
23
  self.openrouter_key = os.getenv("OPENROUTER_API_KEY")
24
-
25
- # 💡 將預設模型改為 OpenRouter 提供的免費 Llama 3.1 模型
26
- self.model_id = os.getenv("HF_MODEL_ID", "openrouter/meta-llama/llama-3.1-8b-instruct:free")
27
 
28
  def __call__(self, question: str) -> str:
29
  try:
30
- model = LiteLLMModel(
31
  model_id=self.model_id,
32
  api_key=self.openrouter_key,
33
  api_base="https://openrouter.ai/api/v1"
34
  )
 
35
  search_tool = DuckDuckGoSearchTool()
36
- agent = CodeAgent(tools=[search_tool], model=model)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  return str(agent.run(question))
39
  except Exception as e:
 
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
+ from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool, VisitWebpageTool
14
+ # 💡 導入 smolagents 內建的系統提示詞結構
15
+ from smolagents.agents import SYSTEM_PROMPT_WITH_CODE
16
  import os
17
  import spaces
18
 
 
23
  class BasicAgent:
24
  def __init__(self):
25
  self.openrouter_key = os.getenv("OPENROUTER_API_KEY")
26
+ self.model_id = os.getenv("HF_MODEL_ID", "google/gemini-2.5-flash")
 
 
27
 
28
  def __call__(self, question: str) -> str:
29
  try:
30
+ model = OpenAIServerModel(
31
  model_id=self.model_id,
32
  api_key=self.openrouter_key,
33
  api_base="https://openrouter.ai/api/v1"
34
  )
35
+
36
  search_tool = DuckDuckGoSearchTool()
37
+ visit_tool = VisitWebpageTool()
38
+ tools = [search_tool, visit_tool]
39
+
40
+ # 💡 精準修正:灌輸 Agent 解題邏輯,並提醒它留意當前目錄下的附加檔案(如 .xlsx, .mp3, .py 等)
41
+ custom_prompt = (
42
+ "You are an expert AI Assistant capable of running Python code and web searches to solve complex tasks.\n"
43
+ "When a question requires factual data, always double check the facts using search tools.\n"
44
+ "If the first search yields no good results, try different keywords.\n"
45
+ "If the question mentions an attached file (e.g., .mp3, .xlsx, .py), check the current directory, "
46
+ "and write Python code to read/analyze the file content directly.\n"
47
+ "Be concise and output only the final correct answer without unnecessary prose."
48
+ )
49
+
50
+ # 💡 正確傳遞 Prompt 的方式:替換預設的 system_prompt 範本
51
+ prompt_templates = {"system_prompt": SYSTEM_PROMPT_WITH_CODE + "\n\nSpecific Rules:\n" + custom_prompt}
52
+
53
+ agent = CodeAgent(
54
+ tools=tools,
55
+ model=model,
56
+ max_steps=8,
57
+ prompt_templates=prompt_templates # 💡 修正參數名稱
58
+ )
59
 
60
  return str(agent.run(question))
61
  except Exception as e: