wt002 commited on
Commit
450a49d
·
verified ·
1 Parent(s): 069acab

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +15 -13
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 langchain_community.chat_models import ChatOpenAI
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(a: int, b: int, operation: str) -> float:
109
  """
110
  Perform a calculation between two numbers.
111
 
112
  Args:
113
- a: First number.
114
- b: Second number.
115
- operation: One of 'add', 'subtract', 'multiply', 'divide', 'modulus'.
116
  """
117
- operation = operation.lower()
 
 
 
118
  if operation == "add":
119
- return add(a, b)
120
  elif operation == "subtract":
121
- return subtract(a, b)
122
  elif operation == "multiply":
123
- return multiply(a, b)
124
  elif operation == "divide":
125
- return divide(a, b)
 
 
126
  elif operation == "modulus":
127
- return modulus(a, b)
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