Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -43,10 +43,8 @@ from io import StringIO
|
|
43 |
from transformers import BertTokenizer, BertModel
|
44 |
import torch
|
45 |
import torch.nn.functional as F
|
46 |
-
#from langchain.tools import Tool
|
47 |
from langchain.agents import initialize_agent, AgentType
|
48 |
-
from
|
49 |
-
#from langchain_openai import ChatOpenAI
|
50 |
from langchain_community.tools import Tool
|
51 |
|
52 |
import time
|
@@ -104,27 +102,31 @@ def modulus(a: int, b: int) -> int:
|
|
104 |
"""
|
105 |
return a % b
|
106 |
|
|
|
107 |
@tool
|
108 |
-
def calculator(
|
109 |
"""
|
110 |
Perform a calculation between two numbers.
|
111 |
|
112 |
Args:
|
113 |
-
|
114 |
-
b: Second number.
|
115 |
-
operation: One of 'add', 'subtract', 'multiply', 'divide', 'modulus'.
|
116 |
"""
|
117 |
-
|
|
|
|
|
|
|
118 |
if operation == "add":
|
119 |
-
return
|
120 |
elif operation == "subtract":
|
121 |
-
return
|
122 |
elif operation == "multiply":
|
123 |
-
return
|
124 |
elif operation == "divide":
|
125 |
-
|
|
|
|
|
126 |
elif operation == "modulus":
|
127 |
-
return
|
128 |
else:
|
129 |
raise ValueError(f"Unsupported operation: {operation}")
|
130 |
|
|
|
43 |
from transformers import BertTokenizer, BertModel
|
44 |
import torch
|
45 |
import torch.nn.functional as F
|
|
|
46 |
from langchain.agents import initialize_agent, AgentType
|
47 |
+
from langchain_openai import ChatOpenAI
|
|
|
48 |
from langchain_community.tools import Tool
|
49 |
|
50 |
import time
|
|
|
102 |
"""
|
103 |
return a % b
|
104 |
|
105 |
+
|
106 |
@tool
|
107 |
+
def calculator(data: dict) -> float:
|
108 |
"""
|
109 |
Perform a calculation between two numbers.
|
110 |
|
111 |
Args:
|
112 |
+
data: A dictionary with keys 'a', 'b', and 'operation'
|
|
|
|
|
113 |
"""
|
114 |
+
a = data.get("a")
|
115 |
+
b = data.get("b")
|
116 |
+
operation = data.get("operation", "").lower()
|
117 |
+
|
118 |
if operation == "add":
|
119 |
+
return a + b
|
120 |
elif operation == "subtract":
|
121 |
+
return a - b
|
122 |
elif operation == "multiply":
|
123 |
+
return a * b
|
124 |
elif operation == "divide":
|
125 |
+
if b == 0:
|
126 |
+
raise ValueError("Cannot divide by zero.")
|
127 |
+
return a / b
|
128 |
elif operation == "modulus":
|
129 |
+
return a % b
|
130 |
else:
|
131 |
raise ValueError(f"Unsupported operation: {operation}")
|
132 |
|