Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,31 +6,37 @@ from typing import Optional
|
|
| 6 |
from smolagents import CodeAgent, OpenAIServerModel, tool
|
| 7 |
|
| 8 |
# --- 搜尋工具設定 ---
|
|
|
|
| 9 |
try:
|
| 10 |
-
from
|
| 11 |
except ImportError:
|
| 12 |
import os
|
| 13 |
-
os.system('pip install
|
| 14 |
-
from
|
| 15 |
|
| 16 |
@tool
|
| 17 |
def web_search(query: str) -> str:
|
| 18 |
"""
|
| 19 |
-
Search the web
|
| 20 |
Args:
|
| 21 |
query: The search query string.
|
| 22 |
"""
|
| 23 |
-
print(f"🕵️ [Debug] Searching: {query}")
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
if not results:
|
| 28 |
-
return "No results."
|
| 29 |
-
return
|
|
|
|
| 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"
|