Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, LiteLLMModel
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
load_dotenv()
|
| 5 |
+
|
| 6 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
|
| 8 |
+
@tool
|
| 9 |
+
def add(a: int, b: int) -> int:
|
| 10 |
+
"""
|
| 11 |
+
Adds two numbers together.
|
| 12 |
+
Args:
|
| 13 |
+
a (int): The first number.
|
| 14 |
+
b (int): The second number.
|
| 15 |
+
Returns:
|
| 16 |
+
int: The result of a + b.
|
| 17 |
+
"""
|
| 18 |
+
return a + b
|
| 19 |
+
|
| 20 |
+
@tool
|
| 21 |
+
def subtract(a: int, b: int) -> int:
|
| 22 |
+
"""
|
| 23 |
+
Subtracts the second number from the first.
|
| 24 |
+
Args:
|
| 25 |
+
a (int): The number to subtract from.
|
| 26 |
+
b (int): The number to subtract.
|
| 27 |
+
Returns:
|
| 28 |
+
int: The result of a - b.
|
| 29 |
+
"""
|
| 30 |
+
return a - b
|
| 31 |
+
|
| 32 |
+
@tool
|
| 33 |
+
def multiply(a: int, b: int) -> int:
|
| 34 |
+
"""
|
| 35 |
+
Multiplies two numbers.
|
| 36 |
+
Args:
|
| 37 |
+
a (int): The first number.
|
| 38 |
+
b (int): The second number.
|
| 39 |
+
Returns:
|
| 40 |
+
int: The product of a and b.
|
| 41 |
+
"""
|
| 42 |
+
return a * b
|
| 43 |
+
|
| 44 |
+
@tool
|
| 45 |
+
def divide(a: int, b: int) -> float:
|
| 46 |
+
"""
|
| 47 |
+
Divides the first number by the second.
|
| 48 |
+
Args:
|
| 49 |
+
a (int): The numerator.
|
| 50 |
+
b (int): The denominator.
|
| 51 |
+
Returns:
|
| 52 |
+
float: The result of a / b.
|
| 53 |
+
Raises:
|
| 54 |
+
ZeroDivisionError: If b is zero.
|
| 55 |
+
"""
|
| 56 |
+
return a / b
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class BasicAgent:
|
| 60 |
+
def __init__(self):
|
| 61 |
+
print("BasicAgent initialized.")
|
| 62 |
+
llm = LiteLLMModel("openai/gpt-4o-mini", api_key = api_key)
|
| 63 |
+
|
| 64 |
+
self.agent = CodeAgent(
|
| 65 |
+
tools=[add,subtract,multiply,divide,DuckDuckGoSearchTool()],
|
| 66 |
+
model=llm
|
| 67 |
+
)
|
| 68 |
+
def __call__(self, question: str) -> str:
|
| 69 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 70 |
+
fixed_answer = self.agent.run(question)
|
| 71 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 72 |
+
return fixed_answer
|