forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
64 lines (47 loc) · 1.79 KB
/
decorator.py
File metadata and controls
64 lines (47 loc) · 1.79 KB
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
import asyncio
import json
import sys
import traceback
from urllib.parse import quote
import requests
from beeai_framework.agents import AgentExecutionConfig
from beeai_framework.agents.react import ReActAgent
from beeai_framework.backend import ChatModel
from beeai_framework.errors import FrameworkError
from beeai_framework.logger import Logger
from beeai_framework.memory import UnconstrainedMemory
from beeai_framework.tools import StringToolOutput, tool
logger = Logger(__name__)
# defining a tool using the `tool` decorator
@tool
def basic_calculator(expression: str) -> StringToolOutput:
"""
A calculator tool that performs mathematical operations.
Args:
expression: The mathematical expression to evaluate (e.g., "2 + 3 * 4").
Returns:
The result of the mathematical expression
"""
try:
encoded_expression = quote(expression)
math_url = f"https://newton.vercel.app/api/v2/simplify/{encoded_expression}"
response = requests.get(
math_url,
headers={"Accept": "application/json"},
)
response.raise_for_status()
return StringToolOutput(json.dumps(response.json()))
except Exception as e:
raise RuntimeError(f"Error evaluating expression: {e!s}") from Exception
async def main() -> None:
# using the tool in an agent
chat_model = ChatModel.from_name("ollama:granite3.1-dense:8b")
agent = ReActAgent(llm=chat_model, tools=[basic_calculator], memory=UnconstrainedMemory())
result = await agent.run("What is the square root of 36?", execution=AgentExecutionConfig(total_max_retries=10))
print(result.result.text)
if __name__ == "__main__":
try:
asyncio.run(main())
except FrameworkError as e:
traceback.print_exc()
sys.exit(e.explain())