Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
class FinalAnswerTool(Tool):
|
| 5 |
name = "final_answer"
|
| 6 |
description = "Provides a final answer to the given problem."
|
|
@@ -11,4 +15,43 @@ class FinalAnswerTool(Tool):
|
|
| 11 |
return answer
|
| 12 |
|
| 13 |
def __init__(self, *args, **kwargs):
|
| 14 |
-
self.is_initialized = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
|
| 4 |
+
simplify_system_message = """
|
| 5 |
+
You are a general AI assistant. I will give you a question and answer, you should simplify answer. Your answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
class FinalAnswerTool(Tool):
|
| 9 |
name = "final_answer"
|
| 10 |
description = "Provides a final answer to the given problem."
|
|
|
|
| 15 |
return answer
|
| 16 |
|
| 17 |
def __init__(self, *args, **kwargs):
|
| 18 |
+
self.is_initialized = False
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class SimplifyAnswerTool(Tool):
|
| 22 |
+
name = "simplify_answer"
|
| 23 |
+
description = (
|
| 24 |
+
"Simplify the final answer."
|
| 25 |
+
"Be sure to use this as the last step to prepare the final answer."
|
| 26 |
+
)
|
| 27 |
+
inputs = {
|
| 28 |
+
"question": {
|
| 29 |
+
"type": "string",
|
| 30 |
+
"description": "The question from the user.",
|
| 31 |
+
},
|
| 32 |
+
"answer": {
|
| 33 |
+
"type": "string",
|
| 34 |
+
"description": "The existing answer to be simplified.",
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
output_type = "string"
|
| 38 |
+
|
| 39 |
+
def __init__(self, api_model: ApiModel):
|
| 40 |
+
super().__init__()
|
| 41 |
+
self.api_model = api_model
|
| 42 |
+
|
| 43 |
+
def forward(self, question: str, answer: str) -> str:
|
| 44 |
+
try:
|
| 45 |
+
response_message = self.api_model(messages=[
|
| 46 |
+
{"role": MessageRole.SYSTEM, "content": [{"type": "text", "text": simplify_system_message }]},
|
| 47 |
+
{"role": MessageRole.ASSISTANT, "content": [{"type": "text", "text": f"QUESTION: {question}"}]},
|
| 48 |
+
{"role": MessageRole.ASSISTANT, "content": [{"type": "text", "text": f"ANSWER: {answer}"}]}
|
| 49 |
+
])
|
| 50 |
+
return response_message.content
|
| 51 |
+
|
| 52 |
+
except requests.exceptions.Timeout:
|
| 53 |
+
return "The request timed out. Please try again later or check the URL."
|
| 54 |
+
except RequestException as e:
|
| 55 |
+
return f"Error fetching the webpage: {str(e)}"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"An unexpected error occurred: {str(e)}"
|