Spaces:
Sleeping
Sleeping
add custom tools
Browse fileswikipedia and dictionary
app.py
CHANGED
|
@@ -87,6 +87,113 @@ def get_ip_info(ip: str = "") -> str:
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error getting IP information: {str(e)}"
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
@tool
|
| 92 |
def string_utilities(action: str, text: str, additional_param: str = "") -> str:
|
|
@@ -163,7 +270,8 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 163 |
agent = CodeAgent(
|
| 164 |
model=model,
|
| 165 |
tools=[convert_currency,
|
| 166 |
-
|
|
|
|
| 167 |
string_utilities,
|
| 168 |
final_answer],
|
| 169 |
max_steps=6,
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error getting IP information: {str(e)}"
|
| 89 |
|
| 90 |
+
@tool
|
| 91 |
+
def dictionary_lookup(word: str) -> str:
|
| 92 |
+
"""A tool that provides definitions and information about English words using a free dictionary API.
|
| 93 |
+
|
| 94 |
+
Args:
|
| 95 |
+
word: The English word to look up.
|
| 96 |
+
"""
|
| 97 |
+
try:
|
| 98 |
+
word = word.lower().strip()
|
| 99 |
+
response = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}")
|
| 100 |
+
|
| 101 |
+
if response.status_code == 200:
|
| 102 |
+
data = response.json()
|
| 103 |
+
result = f"Definitions for '{word}':\n\n"
|
| 104 |
+
|
| 105 |
+
# Process the first entry
|
| 106 |
+
entry = data[0]
|
| 107 |
+
|
| 108 |
+
# Include phonetics if available
|
| 109 |
+
if 'phonetic' in entry and entry['phonetic']:
|
| 110 |
+
result += f"Pronunciation: {entry['phonetic']}\n\n"
|
| 111 |
+
|
| 112 |
+
# Process meanings
|
| 113 |
+
for meaning in entry.get('meanings', [])[:3]: # Limit to first 3 parts of speech
|
| 114 |
+
part_of_speech = meaning.get('partOfSpeech', 'unknown')
|
| 115 |
+
result += f"• {part_of_speech.capitalize()}:\n"
|
| 116 |
+
|
| 117 |
+
# Add definitions
|
| 118 |
+
for i, definition in enumerate(meaning.get('definitions', [])[:2], 1): # Limit to first 2 definitions
|
| 119 |
+
result += f" {i}. {definition.get('definition', 'No definition available')}\n"
|
| 120 |
+
|
| 121 |
+
# Add example if available
|
| 122 |
+
if 'example' in definition and definition['example']:
|
| 123 |
+
result += f" Example: \"{definition['example']}\"\n"
|
| 124 |
+
|
| 125 |
+
result += "\n"
|
| 126 |
+
|
| 127 |
+
# Add synonyms if available
|
| 128 |
+
synonyms = []
|
| 129 |
+
for meaning in entry.get('meanings', []):
|
| 130 |
+
for synonym in meaning.get('synonyms', []):
|
| 131 |
+
synonyms.append(synonym)
|
| 132 |
+
|
| 133 |
+
if synonyms:
|
| 134 |
+
result += f"Synonyms: {', '.join(synonyms[:5])}\n" # Limit to first 5 synonyms
|
| 135 |
+
|
| 136 |
+
return result
|
| 137 |
+
elif response.status_code == 404:
|
| 138 |
+
return f"Word '{word}' not found in the dictionary."
|
| 139 |
+
else:
|
| 140 |
+
return f"Error looking up word: HTTP Status {response.status_code}"
|
| 141 |
+
except Exception as e:
|
| 142 |
+
return f"Error performing dictionary lookup: {str(e)}"
|
| 143 |
+
|
| 144 |
+
@tool
|
| 145 |
+
def wikipedia_search(query: str, sentences: int = 3) -> str:
|
| 146 |
+
"""A tool that searches Wikipedia and returns a summary of the topic.
|
| 147 |
+
|
| 148 |
+
Args:
|
| 149 |
+
query: The topic to search for on Wikipedia.
|
| 150 |
+
sentences: Number of sentences to include in the summary (default: 3).
|
| 151 |
+
"""
|
| 152 |
+
try:
|
| 153 |
+
# Sanitize input
|
| 154 |
+
query = query.strip()
|
| 155 |
+
sentences = max(1, min(10, int(sentences))) # Ensure between 1-10 sentences
|
| 156 |
+
|
| 157 |
+
# Use Wikipedia's API
|
| 158 |
+
search_url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json"
|
| 159 |
+
search_response = requests.get(search_url)
|
| 160 |
+
|
| 161 |
+
if search_response.status_code != 200:
|
| 162 |
+
return f"Error: Failed to search Wikipedia. Status code: {search_response.status_code}"
|
| 163 |
+
|
| 164 |
+
search_data = search_response.json()
|
| 165 |
+
search_results = search_data.get('query', {}).get('search', [])
|
| 166 |
+
|
| 167 |
+
if not search_results:
|
| 168 |
+
return f"No Wikipedia articles found for '{query}'."
|
| 169 |
+
|
| 170 |
+
# Get the page ID of the first result
|
| 171 |
+
page_id = search_results[0]['pageid']
|
| 172 |
+
|
| 173 |
+
# Get the summary using the page ID
|
| 174 |
+
summary_url = f"https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro=true&explaintext=true&pageids={page_id}&format=json"
|
| 175 |
+
summary_response = requests.get(summary_url)
|
| 176 |
+
|
| 177 |
+
if summary_response.status_code != 200:
|
| 178 |
+
return f"Error: Failed to get Wikipedia summary. Status code: {summary_response.status_code}"
|
| 179 |
+
|
| 180 |
+
summary_data = summary_response.json()
|
| 181 |
+
page_data = summary_data.get('query', {}).get('pages', {}).get(str(page_id), {})
|
| 182 |
+
title = page_data.get('title', 'Unknown')
|
| 183 |
+
|
| 184 |
+
# Get the full extract
|
| 185 |
+
extract = page_data.get('extract', 'No summary available.')
|
| 186 |
+
|
| 187 |
+
# Split into sentences and limit
|
| 188 |
+
extract_sentences = extract.split('. ')
|
| 189 |
+
limited_extract = '. '.join(extract_sentences[:sentences])
|
| 190 |
+
if not limited_extract.endswith('.'):
|
| 191 |
+
limited_extract += '.'
|
| 192 |
+
|
| 193 |
+
return f"Wikipedia: {title}\n\n{limited_extract}\n\nSource: Wikipedia"
|
| 194 |
+
except Exception as e:
|
| 195 |
+
return f"Error searching Wikipedia: {str(e)}"
|
| 196 |
+
|
| 197 |
|
| 198 |
@tool
|
| 199 |
def string_utilities(action: str, text: str, additional_param: str = "") -> str:
|
|
|
|
| 270 |
agent = CodeAgent(
|
| 271 |
model=model,
|
| 272 |
tools=[convert_currency,
|
| 273 |
+
dictionary_lookup,
|
| 274 |
+
wikipedia_search,
|
| 275 |
string_utilities,
|
| 276 |
final_answer],
|
| 277 |
max_steps=6,
|