s1144662 commited on
Commit
3cdd433
·
verified ·
1 Parent(s): 153babc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -6,31 +6,37 @@ from typing import Optional
6
  from smolagents import CodeAgent, OpenAIServerModel, tool
7
 
8
  # --- 搜尋工具設定 ---
 
9
  try:
10
- from duckduckgo_search import DDGS
11
  except ImportError:
12
  import os
13
- os.system('pip install duckduckgo-search==6.4.2')
14
- from duckduckgo_search import DDGS
15
 
16
  @tool
17
  def web_search(query: str) -> str:
18
  """
19
- Search the web for information.
20
  Args:
21
  query: The search query string.
22
  """
23
- print(f"🕵️ [Debug] Searching: {query}")
24
  try:
25
- # 設定 10 秒逾時,只抓 2結果降低負擔
26
- results = DDGS(timeout=10).text(query, max_results=2)
 
 
 
 
 
27
  if not results:
28
- return "No results."
29
- return str(results)
 
30
  except Exception as e:
31
  print(f"❌ Search Error: {e}")
32
  return "Search failed."
33
-
34
  # -----------------------------------------------------------
35
 
36
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
6
  from smolagents import CodeAgent, OpenAIServerModel, tool
7
 
8
  # --- 搜尋工具設定 ---
9
+ # 把原本 import DDGS 的地方刪掉,換成這個:
10
  try:
11
+ from googlesearch import search
12
  except ImportError:
13
  import os
14
+ os.system('pip install googlesearch-python')
15
+ from googlesearch import search
16
 
17
  @tool
18
  def web_search(query: str) -> str:
19
  """
20
+ Search the web using Google.
21
  Args:
22
  query: The search query string.
23
  """
24
+ print(f"🕵️ [Debug] Google Searching: {query}")
25
  try:
26
+ # Google search 只抓 3網址Agent 沒辦法直接讀內容,
27
+ # smolagents 有時候會聰明地只看標題。
28
+ # 這是目前救急的方法。
29
+ results = []
30
+ for j in search(query, num_results=3, advanced=True):
31
+ results.append(f"Title: {j.title}\nDescription: {j.description}")
32
+
33
  if not results:
34
+ return "No results found."
35
+ return "\n---\n".join(results)
36
+
37
  except Exception as e:
38
  print(f"❌ Search Error: {e}")
39
  return "Search failed."
 
40
  # -----------------------------------------------------------
41
 
42
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"