File size: 9,543 Bytes
8792035 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | import inspect
import requests
from datetime import datetime
from typing import Optional, List
from smolagents import tool, DuckDuckGoSearchTool
import html2text
from src.client import ask
def generate_docstring(description):
def inner(obj):
obj.__doc__ = """
{description}
""".format(description=description)
return obj
return inner
###
### BASE TOOL DESCRIPTIONS
###
current_time_and_date_tool_description = """
Used at beginning of the flow to get the current time and date of the user.
Returns:
The current time and date of the user.
"""
###
### REASONING TOOL DESCRIPTIONS
###
planning_tool_description = """
Generate a detailed, reasoned plan to solve a given problem.
Args:
problem: A string describing the problem to be solved.
Returns:
A structured plan outlining steps, considerations, and reasoning to address the problem effectively.
"""
analysis_tool_description = """
Analyze a given task and provide insights or suggestions for improvement.
Args:
task_description: A string describing the task to be analyzed.
Returns:
A string providing insights, recommendations, or a breakdown of the task.
"""
decision_making_tool_description = """" \
Assist in making a decision by evaluating different options based on given criteria.
Args:
options: A list of options to be considered.
criteria: A dictionary where keys are criteria names, and values are their respective weights or importance.
constraints: A list of constraints or limitations that the decision must adhere to (optional).
Returns:
A string suggesting the best option based on the evaluation of the criteria and constraints.
"""
problem_solving_tool_description = """
Help solve a problem by breaking it down into smaller, manageable steps and providing potential solutions.
Args:
problem_description: A string outlining the problem to be solved.
context: Additional context or background information relevant to the problem (optional).
Returns:
A string describing a step-by-step approach to solving the problem, including possible solutions.
"""
self_reflection_tool_description = """
Facilitate self-reflection by prompting the user with introspective questions and summarizing insights.
Args:
focus_area: The area of life or work to reflect on (e.g., personal growth, career, relationships).
Returns:
A string summarizing insights gained through the reflection process and offering suggestions for improvement.
"""
task_goal_tool_description = """
Assist in setting clear and achievable goals for a given task and outline steps to accomplish them.
Args:
task_description: A string describing the task or project to set goals for.
Returns:
A string outlining clear, actionable goals for the task along with suggested steps to achieve them.
"""
prioritization_tool_description = """
Help prioritize tasks or options based on urgency, importance, and available resources.
Args:
tasks: A list of tasks or options to be prioritized.
Returns:
A string providing a prioritized list of tasks or options based on the evaluation of the criteria and resources.
"""
###
### MATH TOOL DESCRIPTIONS
###
sum_tool_description = """
This tool helps in summing up two numbers.
Args:
a: A number to be summed.
b: A number to be summed.
Returns:
A string containing the result of the sum.
"""
subtraction_tool_description = """
This tool helps in subtracting up two numbers.
Args:
a: A number to be subtracted.
b: A number to be subtracted.
Returns:
A string containing the result of the subtraction.
"""
multiplication_tool_description = """
This tool helps in multiplicating a number for another number.
Args:
a: A number to be multiplied.
b: A number to be multiplied.
Returns:
A string containing the result of the multiplication.
"""
division_tool_description = """
This tool helps in divide a number for another number.
Args:
a: A number to be divided.
b: A number to be divided.
Returns:
A string containing the result of the division.
"""
###
### WEB TOOL DESCRIPTIONS
###
visit_webpage_tool_description = """Visits a web link at the given URL and returns its content as a markdown string.
Args:
url: The URL of the webpage to visit.
Returns:
The content of the webpage converted to Markdown, or an error message if the request fails.
"""
class Toolkit:
###
### BASE TOOLS
###
def get(self):
return [func for name, func in inspect.getmembers(self) if name.endswith("_tool") ]
@tool
@generate_docstring(current_time_and_date_tool_description)
def date_and_time_tool() -> str:
try:
fnow = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return fnow
except Exception as e:
return f"Fail to plan on this task: Date and Time error: {e}"
###
### REASONING TOOLS
###
@tool
@generate_docstring(planning_tool_description)
def planning_tool(problem: str) -> str:
try:
return ask(system=planning_tool_description, prompt=problem, task_title="Planning")
except Exception as e:
return f"Fail to plan on this task: Planning error: {e}"
@tool
@generate_docstring(analysis_tool_description)
def analysis_tool(task_description: str) -> str:
try:
return ask(system=analysis_tool_description, prompt=task_description, task_title="Analysis")
except Exception as e:
return f"Fail to plan on this task: Analysis error: {e}"
@tool
@generate_docstring(decision_making_tool_description)
def decision_making_tool(options: str, criteria:str, constraints:Optional[str] = []) -> str:
prompt = f"Options: {options}\nCriteria: {criteria}\nConstraints: {constraints}"
try:
return ask(system=decision_making_tool_description, prompt=prompt, task_title="Decision Making")
except Exception as e:
return f"Fail to plan on this task: Decision Making error: {e}"
@tool
@generate_docstring(problem_solving_tool_description)
def problem_solving_tool(problem_description: str, context: Optional[str] = []) -> str:
prompt = f"Problem Description: {problem_description}\Context: {context}"
try:
return ask(system=problem_solving_tool_description, prompt=prompt, task_title="Problem Solving")
except Exception as e:
return f"Fail to plan on this task: Problem Solving error: {e}"
@tool
@generate_docstring(self_reflection_tool_description)
def self_reflection_tool(focus_area: str) -> str:
try:
return ask(system=self_reflection_tool_description, prompt=focus_area, task_title="Reflection Tool")
except Exception as e:
return f"Fail to plan on this task: Reflection Tool error: {e}"
@tool
@generate_docstring(task_goal_tool_description)
def task_goal_tool(task_description: str) -> str:
try:
return ask(system=task_goal_tool_description, prompt=task_description, task_title="Goal Tool")
except Exception as e:
return f"Fail to plan on this task: Goal Tool error: {e}"
@tool
@generate_docstring(prioritization_tool_description)
def prioritization_tool(tasks: List[str]) -> str:
try:
return ask(system=prioritization_tool_description, prompt=tasks, task_title="Prioritization Tool")
except Exception as e:
return f"Fail to plan on this task: Prioritization Tool error: {e}"
###
### MATH TOOLS
###
@tool
@generate_docstring(sum_tool_description)
def sum_tool(a:float, b:float) -> str:
try:
result = a + b
return f"{result}"
except Exception as e:
return f"Fail to plan on this task: Sum Tool error: {e}"
@tool
@generate_docstring(subtraction_tool_description)
def subtraction_tool(a:float, b:float) -> str:
try:
result = a - b
return f"{result}"
except Exception as e:
return f"Fail to plan on this task: Subtraction Tool error: {e}"
@tool
@generate_docstring(multiplication_tool_description)
def multiplication_tool(a:float, b:float) -> str:
try:
result = a * b
return f"{result}"
except Exception as e:
return f"Fail to plan on this task: Multiplication Tool error: {e}"
@tool
@generate_docstring(division_tool_description)
def division_tool(a:float, b:float) -> str:
try:
result = a / b
return f"{result}"
except Exception as e:
return f"Fail to plan on this task: Division Tool error: {e}"
###
### WEB TOOLS
###
def duck_duck_search_tool():
# placeholder
pass
duck_duck_search_tool = DuckDuckGoSearchTool()
@tool
@generate_docstring(visit_webpage_tool_description)
def visit_webpage_tool(url: str) -> str:
print("visit_webpage_tool", url)
try:
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
response = requests.get(url, headers=headers)
response.raise_for_status()
text_content = html2text.html2text(response.text)
return text_content
except Exception as e:
return f"Fail to plan on this task: Visit Web Page error: {e}"
|