Update my_tools.py
Browse files- my_tools.py +22 -5
my_tools.py
CHANGED
|
@@ -1,6 +1,23 @@
|
|
| 1 |
-
from smolagents import DuckDuckGoSearchTool,
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import DuckDuckGoSearchTool, Tool
|
| 2 |
+
import wikipediaapi
|
| 3 |
|
| 4 |
+
class WikipediaSearchTool(Tool):
|
| 5 |
+
name = "wikipedia_search"
|
| 6 |
+
description = "查找英文维基百科的页面简介,输入应为一个词或短词组"
|
| 7 |
+
inputs = {
|
| 8 |
+
"query": {"type": "string", "description": "维基百科搜索关键词,例如人名/专名"}
|
| 9 |
+
}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
def __init__(self, lang="en"):
|
| 12 |
+
super().__init__()
|
| 13 |
+
self.wiki = wikipediaapi.Wikipedia(language=lang, user_agent="celum")
|
| 14 |
+
def forward(self, query: str):
|
| 15 |
+
page = self.wiki.page(query)
|
| 16 |
+
if not page.exists():
|
| 17 |
+
return "No Wikipedia page found."
|
| 18 |
+
return page.summary[:1000]
|
| 19 |
+
|
| 20 |
+
wiki_tool = WikipediaSearchTool()
|
| 21 |
+
ddg_tool = DuckDuckGoSearchTool() # smolagents 自带
|
| 22 |
+
|
| 23 |
+
my_tool_list = [wiki_tool, ddg_tool]
|