File size: 2,842 Bytes
9a6e431
6eaddf0
e5cabf3
 
 
a1cfa43
9a6e431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54c8f81
6a12f2e
9a6e431
 
 
ee0a948
a1cfa43
 
6a12f2e
 
9a6e431
 
6eaddf0
54c8f81
9a6e431
 
 
d08e75c
 
 
9a6e431
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel, OpenAIServerModel, SpeechToTextTool, WikipediaSearchTool
from dotenv import load_dotenv
load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

@tool
def add(a: int, b: int) -> int:
    """
    Adds two numbers together.
    Args:
        a (int): The first number.
        b (int): The second number.
    Returns:
        int: The result of a + b.
    """
    return a + b

@tool
def subtract(a: int, b: int) -> int:
    """
    Subtracts the second number from the first.
    Args:
        a (int): The number to subtract from.
        b (int): The number to subtract.
    Returns:
        int: The result of a - b.
    """
    return a - b

@tool
def multiply(a: int, b: int) -> int:
    """
    Multiplies two numbers.
    Args:
        a (int): The first number.
        b (int): The second number.
    Returns:
        int: The product of a and b.
    """
    return a * b

@tool
def divide(a: int, b: int) -> float:
    """
    Divides the first number by the second.
    Args:
        a (int): The numerator.
        b (int): The denominator.
    Returns:
        float: The result of a / b.
    Raises:
        ZeroDivisionError: If b is zero.
    """
    return a / b

prompt = "You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."

class BasicAgent:
    def __init__(self):
        print("BasicAgent initialized.")
        llm = OpenAIServerModel(
        model_id="gpt-4o",
        api_base="https://api.openai.com/v1",
        api_key=os.environ["OPENAI_API_KEY"],
        )
        
        self.agent = CodeAgent(
            tools=[add,subtract,multiply,divide,DuckDuckGoSearchTool(),WikipediaSearchTool(),SpeechToTextTool()],
            model=llm,
        )
    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        # Prepend the prompt to the question with a clear separator
        full_input = prompt + "\n\nQuestion: " + question
        fixed_answer = self.agent.run(full_input)
        print(f"Agent returning fixed answer: {fixed_answer}")
        return fixed_answer