-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.py
49 lines (43 loc) · 1.26 KB
/
tool.py
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
# File: tool.py
from textwrap import dedent
from typing import Literal, Type
from pydantic import BaseModel, Field
from pydantic import BaseModel as StudioBaseTool
class UserParameters(BaseModel):
pass
class CalculatorTool(StudioBaseTool):
class ToolParameters(BaseModel):
a: float = Field(
description="first number"
)
b: float = Field(
description="second number"
)
operator: Literal["+", "-", "*", "/"] = Field(
description="operator"
)
name: str = "Calculator Tool"
description: str = dedent(
"""
Calculator tool which can do basic addition, subtraction, multiplication, and division.
Division by 0 is not allowed.
"""
)
args_schema: Type[BaseModel] = ToolParameters
user_parameters: UserParameters
def _run(
self, a, b, operator
) -> str:
# Implementation for the tool goes here.
res = None
if operator == "+":
res = a + b
elif operator == "-":
res = a - b
elif operator == "*":
res = a * b
elif operator == "/":
res = float(a / b)
else:
raise ValueError("Invalid operator")
return str(res)