praisonaiagents 0.0.22__py3-none-any.whl → 0.0.24__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- praisonaiagents/agent/agent.py +22 -33
- praisonaiagents/agents/agents.py +18 -4
- praisonaiagents/tools/__init__.py +165 -2
- praisonaiagents/tools/arxiv_tools.py +292 -0
- praisonaiagents/tools/calculator_tools.py +278 -0
- praisonaiagents/tools/csv_tools.py +266 -0
- praisonaiagents/tools/duckdb_tools.py +268 -0
- praisonaiagents/tools/duckduckgo_tools.py +52 -0
- praisonaiagents/tools/excel_tools.py +310 -0
- praisonaiagents/tools/file_tools.py +274 -0
- praisonaiagents/tools/json_tools.py +515 -0
- praisonaiagents/tools/newspaper_tools.py +354 -0
- praisonaiagents/tools/pandas_tools.py +326 -0
- praisonaiagents/tools/python_tools.py +423 -0
- praisonaiagents/tools/shell_tools.py +278 -0
- praisonaiagents/tools/spider_tools.py +431 -0
- praisonaiagents/tools/test.py +56 -0
- praisonaiagents/tools/tools.py +5 -36
- praisonaiagents/tools/wikipedia_tools.py +272 -0
- praisonaiagents/tools/xml_tools.py +498 -0
- praisonaiagents/tools/yaml_tools.py +417 -0
- praisonaiagents/tools/yfinance_tools.py +213 -0
- {praisonaiagents-0.0.22.dist-info → praisonaiagents-0.0.24.dist-info}/METADATA +1 -1
- praisonaiagents-0.0.24.dist-info/RECORD +42 -0
- praisonaiagents-0.0.22.dist-info/RECORD +0 -24
- {praisonaiagents-0.0.22.dist-info → praisonaiagents-0.0.24.dist-info}/WHEEL +0 -0
- {praisonaiagents-0.0.22.dist-info → praisonaiagents-0.0.24.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,272 @@
|
|
1
|
+
"""Wikipedia tools for accessing and searching Wikipedia content.
|
2
|
+
|
3
|
+
Usage:
|
4
|
+
from praisonaiagents.tools import wikipedia_tools
|
5
|
+
summary = wikipedia_tools.get_summary("Python programming language")
|
6
|
+
page = wikipedia_tools.get_page("Python programming language")
|
7
|
+
results = wikipedia_tools.search("Python programming")
|
8
|
+
|
9
|
+
or
|
10
|
+
from praisonaiagents.tools import get_wikipedia_summary, get_wikipedia_page
|
11
|
+
summary = get_wikipedia_summary("Python programming language")
|
12
|
+
"""
|
13
|
+
|
14
|
+
import logging
|
15
|
+
from typing import List, Dict, Union, Any
|
16
|
+
from importlib import util
|
17
|
+
import json
|
18
|
+
|
19
|
+
class WikipediaTools:
|
20
|
+
"""Tools for accessing and searching Wikipedia content."""
|
21
|
+
|
22
|
+
def __init__(self):
|
23
|
+
"""Initialize WikipediaTools and check for wikipedia package."""
|
24
|
+
self._check_wikipedia()
|
25
|
+
|
26
|
+
def _check_wikipedia(self):
|
27
|
+
"""Check if wikipedia package is installed."""
|
28
|
+
if util.find_spec("wikipedia") is None:
|
29
|
+
raise ImportError("wikipedia package is not available. Please install it using: pip install wikipedia")
|
30
|
+
global wikipedia
|
31
|
+
import wikipedia
|
32
|
+
# Set default language to English
|
33
|
+
wikipedia.set_lang("en")
|
34
|
+
|
35
|
+
def search(
|
36
|
+
self,
|
37
|
+
query: str,
|
38
|
+
results: int = 10,
|
39
|
+
suggestion: bool = True
|
40
|
+
) -> Union[List[str], Dict[str, str]]:
|
41
|
+
"""
|
42
|
+
Search Wikipedia for a query.
|
43
|
+
|
44
|
+
Args:
|
45
|
+
query: Search query
|
46
|
+
results: Maximum number of results to return
|
47
|
+
suggestion: Whether to suggest similar queries
|
48
|
+
|
49
|
+
Returns:
|
50
|
+
List[str] or Dict: List of search results or error dict
|
51
|
+
"""
|
52
|
+
try:
|
53
|
+
search_results = wikipedia.search(query, results=results, suggestion=suggestion)
|
54
|
+
if isinstance(search_results, tuple):
|
55
|
+
# If suggestion is True, returns (results, suggestion)
|
56
|
+
return {
|
57
|
+
"results": search_results[0],
|
58
|
+
"suggestion": search_results[1] if search_results[1] else None
|
59
|
+
}
|
60
|
+
return {"results": search_results, "suggestion": None}
|
61
|
+
except Exception as e:
|
62
|
+
error_msg = f"Error searching Wikipedia for '{query}': {str(e)}"
|
63
|
+
logging.error(error_msg)
|
64
|
+
return {"error": error_msg}
|
65
|
+
|
66
|
+
def get_summary(
|
67
|
+
self,
|
68
|
+
title: str,
|
69
|
+
sentences: int = 5,
|
70
|
+
auto_suggest: bool = True
|
71
|
+
) -> Union[str, Dict[str, str]]:
|
72
|
+
"""
|
73
|
+
Get a summary of a Wikipedia page.
|
74
|
+
|
75
|
+
Args:
|
76
|
+
title: Title of the Wikipedia page
|
77
|
+
sentences: Number of sentences to return
|
78
|
+
auto_suggest: Whether to auto-suggest similar titles
|
79
|
+
|
80
|
+
Returns:
|
81
|
+
str or Dict: Page summary if successful, error dict if failed
|
82
|
+
"""
|
83
|
+
try:
|
84
|
+
return wikipedia.summary(title, sentences=sentences, auto_suggest=auto_suggest)
|
85
|
+
except wikipedia.exceptions.DisambiguationError as e:
|
86
|
+
return {
|
87
|
+
"error": "Disambiguation page",
|
88
|
+
"options": e.options[:10] # Limit to first 10 options
|
89
|
+
}
|
90
|
+
except Exception as e:
|
91
|
+
error_msg = f"Error getting summary for '{title}': {str(e)}"
|
92
|
+
logging.error(error_msg)
|
93
|
+
return {"error": error_msg}
|
94
|
+
|
95
|
+
def get_page(
|
96
|
+
self,
|
97
|
+
title: str,
|
98
|
+
auto_suggest: bool = True
|
99
|
+
) -> Union[Dict[str, Any], Dict[str, str]]:
|
100
|
+
"""
|
101
|
+
Get detailed information about a Wikipedia page.
|
102
|
+
|
103
|
+
Args:
|
104
|
+
title: Title of the Wikipedia page
|
105
|
+
auto_suggest: Whether to auto-suggest similar titles
|
106
|
+
|
107
|
+
Returns:
|
108
|
+
Dict: Page information if successful, error dict if failed
|
109
|
+
"""
|
110
|
+
try:
|
111
|
+
page = wikipedia.page(title, auto_suggest=auto_suggest)
|
112
|
+
return {
|
113
|
+
"title": page.title,
|
114
|
+
"url": page.url,
|
115
|
+
"content": page.content,
|
116
|
+
"summary": page.summary,
|
117
|
+
"references": page.references,
|
118
|
+
"categories": page.categories,
|
119
|
+
"links": page.links,
|
120
|
+
"images": page.images,
|
121
|
+
"sections": self.get_sections(page)
|
122
|
+
}
|
123
|
+
except wikipedia.exceptions.DisambiguationError as e:
|
124
|
+
return {
|
125
|
+
"error": "Disambiguation page",
|
126
|
+
"options": e.options[:10] # Limit to first 10 options
|
127
|
+
}
|
128
|
+
except Exception as e:
|
129
|
+
error_msg = f"Error getting page for '{title}': {str(e)}"
|
130
|
+
logging.error(error_msg)
|
131
|
+
return {"error": error_msg}
|
132
|
+
|
133
|
+
def get_sections(self, page: Any) -> List[Dict[str, Any]]:
|
134
|
+
"""
|
135
|
+
Extract sections and their content from a Wikipedia page.
|
136
|
+
|
137
|
+
Args:
|
138
|
+
page: Wikipedia page object
|
139
|
+
|
140
|
+
Returns:
|
141
|
+
List[Dict]: List of sections with their titles and content
|
142
|
+
"""
|
143
|
+
sections = []
|
144
|
+
content = page.content
|
145
|
+
section_titles = [s.strip() for s in content.split("==")[1::2]]
|
146
|
+
section_contents = [s.strip() for s in content.split("==")[2::2]]
|
147
|
+
|
148
|
+
# Add introduction section
|
149
|
+
intro = content.split("==")[0].strip()
|
150
|
+
if intro:
|
151
|
+
sections.append({
|
152
|
+
"title": "Introduction",
|
153
|
+
"level": 0,
|
154
|
+
"content": intro
|
155
|
+
})
|
156
|
+
|
157
|
+
# Process remaining sections
|
158
|
+
for title, content in zip(section_titles, section_contents):
|
159
|
+
# Count leading '=' to determine section level
|
160
|
+
level = title.count("=") // 2
|
161
|
+
clean_title = title.strip("= ")
|
162
|
+
|
163
|
+
sections.append({
|
164
|
+
"title": clean_title,
|
165
|
+
"level": level,
|
166
|
+
"content": content.strip()
|
167
|
+
})
|
168
|
+
|
169
|
+
return sections
|
170
|
+
|
171
|
+
def get_random(self, pages: int = 1) -> Union[List[str], Dict[str, str]]:
|
172
|
+
"""
|
173
|
+
Get random Wikipedia page titles.
|
174
|
+
|
175
|
+
Args:
|
176
|
+
pages: Number of random pages to return
|
177
|
+
|
178
|
+
Returns:
|
179
|
+
List[str] or Dict: List of random page titles or error dict
|
180
|
+
"""
|
181
|
+
try:
|
182
|
+
return wikipedia.random(pages)
|
183
|
+
except Exception as e:
|
184
|
+
error_msg = f"Error getting random pages: {str(e)}"
|
185
|
+
logging.error(error_msg)
|
186
|
+
return {"error": error_msg}
|
187
|
+
|
188
|
+
def set_language(self, language: str) -> bool:
|
189
|
+
"""
|
190
|
+
Set the language for Wikipedia searches.
|
191
|
+
|
192
|
+
Args:
|
193
|
+
language: Language code (e.g., 'en' for English, 'es' for Spanish)
|
194
|
+
|
195
|
+
Returns:
|
196
|
+
bool: True if successful, False if failed
|
197
|
+
"""
|
198
|
+
try:
|
199
|
+
wikipedia.set_lang(language)
|
200
|
+
return True
|
201
|
+
except Exception as e:
|
202
|
+
error_msg = f"Error setting language to '{language}': {str(e)}"
|
203
|
+
logging.error(error_msg)
|
204
|
+
return False
|
205
|
+
|
206
|
+
# Create instance for direct function access
|
207
|
+
_wikipedia_tools = WikipediaTools()
|
208
|
+
search = _wikipedia_tools.search
|
209
|
+
get_summary = _wikipedia_tools.get_summary
|
210
|
+
get_page = _wikipedia_tools.get_page
|
211
|
+
get_random = _wikipedia_tools.get_random
|
212
|
+
set_language = _wikipedia_tools.set_language
|
213
|
+
|
214
|
+
if __name__ == "__main__":
|
215
|
+
# Example usage
|
216
|
+
print("\n==================================================")
|
217
|
+
print("WikipediaTools Demonstration")
|
218
|
+
print("==================================================\n")
|
219
|
+
|
220
|
+
# 1. Search for a topic
|
221
|
+
print("1. Searching Wikipedia")
|
222
|
+
print("------------------------------")
|
223
|
+
search_results = search("Python programming language")
|
224
|
+
print("Search results:")
|
225
|
+
print(json.dumps(search_results, indent=2))
|
226
|
+
print()
|
227
|
+
|
228
|
+
# 2. Get a page summary
|
229
|
+
print("2. Getting Page Summary")
|
230
|
+
print("------------------------------")
|
231
|
+
summary = get_summary("Python programming language", sentences=3)
|
232
|
+
print("Summary:")
|
233
|
+
print(summary)
|
234
|
+
print()
|
235
|
+
|
236
|
+
# 3. Get detailed page information
|
237
|
+
print("3. Getting Full Page Information")
|
238
|
+
print("------------------------------")
|
239
|
+
page = get_page("Python programming language")
|
240
|
+
if isinstance(page, dict) and "error" not in page:
|
241
|
+
print("Page sections:")
|
242
|
+
for section in page["sections"]:
|
243
|
+
print(f"- {section['title']} (Level {section['level']})")
|
244
|
+
print(f"\nNumber of references: {len(page['references'])}")
|
245
|
+
print(f"Number of categories: {len(page['categories'])}")
|
246
|
+
print(f"Number of links: {len(page['links'])}")
|
247
|
+
print(f"Number of images: {len(page['images'])}")
|
248
|
+
else:
|
249
|
+
print("Error getting page:", page.get("error"))
|
250
|
+
print()
|
251
|
+
|
252
|
+
# 4. Get random pages
|
253
|
+
print("4. Getting Random Pages")
|
254
|
+
print("------------------------------")
|
255
|
+
random_pages = get_random(3)
|
256
|
+
print("Random page titles:")
|
257
|
+
print(json.dumps(random_pages, indent=2))
|
258
|
+
print()
|
259
|
+
|
260
|
+
# 5. Try different language
|
261
|
+
print("5. Changing Language")
|
262
|
+
print("------------------------------")
|
263
|
+
success = set_language("es")
|
264
|
+
if success:
|
265
|
+
summary = get_summary("Python (lenguaje de programación)", sentences=1)
|
266
|
+
print("Spanish summary:")
|
267
|
+
print(summary)
|
268
|
+
print()
|
269
|
+
|
270
|
+
print("\n==================================================")
|
271
|
+
print("Demonstration Complete")
|
272
|
+
print("==================================================")
|