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}"