Shortcuts

Calculator

class agentlego.tools.Calculator(timeout=2, toolmeta=None)[源代码]

A calculator based on Python expression.

参数:
  • timeout (int) – The timeout value to interrupt calculation. Defaults to 2.

  • toolmeta (None | dict | ToolMeta) – The additional info of the tool. Defaults to None.

默认工具信息

  • 名称: Calculator

  • 描述: A calculator tool. The input must be a single Python expression and you cannot import packages. You can use functions in the math package without import.

  • 输入:

    • expression (str)

  • 输出:

    • str

Examples

Use the tool directly (without agent)

from agentlego.apis import load_tool

# load tool
tool = load_tool('Calculator')

# apply tool
tool('e ** cos(pi)')

With Lagent

from agentlego.apis import load_tool
from lagent import ReAct, GPTAPI, ActionExecutor

# load tools and build agent
# please set `OPENAI_API_KEY` in your environment variable.
tool = load_tool('Calculator').to_lagent()
agent = ReAct(GPTAPI(temperature=0.), action_executor=ActionExecutor([tool]))

# agent running with the tool.
ret = agent.chat(f'pi and 3.2, which is greater?')
for step in ret.inner_steps[1:]:
    print('------')
    print(step['content'])