# metagpt/tools/libs/calculate_factorial.py
import math
from metagpt.tools.tool_registry import register_tool
# Register tool with the decorator
@register_tool()
def calculate_factorial(n):
"""
Calculate the factorial of a non-negative integer.
"""
if n < 0:
raise ValueError("Input must be a non-negative integer")
return math.factorial(n)
import asyncio
from metagpt.roles.di.data_interpreter import DataInterpreter
from metagpt.tools.libs import calculate_factorial
async def main(requirement: str):
role = DataInterpreter(tools=["calculate_factorial"]) # integrate the tool
await role.run(requirement)
if __name__ == "__main__":
requirement = "Please calculate the factorial of 5."
asyncio.run(main(requirement))
tools can return a result but just print in CLI (print 5! = 120 in CLI )
how to get returned data and assign it to a variable?
for example,,
var1 = 120 (calculated by this tool...)
var1 = return data of asyncio.run(main(requirement))
Would you consider updating the requirement to "Please calculate the factorial of 5 and save the result to 'result.txt'"? This modification allows you to retrieve the calculated result from the 'result.txt' file.
https://docs.deepwisdom.ai/main/en/guide/tutorials/create_and_use_tools.html
tools can return a result but just print in CLI (print 5! = 120 in CLI ) how to get returned data and assign it to a variable? for example,, var1 = 120 (calculated by this tool...) var1 = return data of asyncio.run(main(requirement))
thanks a lot