File size: 3,808 Bytes
966e533
 
107693b
966e533
 
 
 
 
 
 
 
 
107693b
966e533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107693b
966e533
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from smolagents import Tool, tool, CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool
import time
import os
import requests
import markdownify
#for the mp3 file reading
import whisper
import tempfile
import io


class Mod4Agent:
    
    def __init__(self):        
        self.api_key=os.getenv("OPENAI_KEY")
        
        #base model
        self.model = OpenAIServerModel(
            model_id="gpt-4o",
            api_base="https://api.openai.com/v1",
            temperature=0.0,
            api_key=self.api_key)

        #base_prompt
        self.base_prompt="""
        You are an agent with a set of tools for answering to questions.
        You need to be accurate and get the best possible answer in the simplest possible way.
        You need to think step-by-step, and if at some point there is an error, backtrack and use a different method.
        It is important to adhere to the instructions of the question as close as possible.
        IMPORTANT: always answer according to the format required to the best of your abilities. Stating that you do not know, or explaining why, will give a score of 0 therefore it is to be avoided. 
        You can do it!

        Question: 
        """


        @tool
        def audio_interpreter(input: bytes)->str:
            """
            Function to transcribe an mp3 file from raw bytes or file path into the corresponding text

            Args:
                input: raw bytes content of the input mp3 file, or its file path

            Return:
                str: a string with the text corresponding to the mp3 input file
            """

            model = whisper.load_model("tiny")
        
            if isinstance(input, bytes):
                with tempfile.NamedTemporaryFile(suffix=".mp3", delete=True) as tmp:
                    tmp.write(input)
                    tmp.flush()
                    result = model.transcribe(tmp.name)
                    
            elif isinstance(input, str) and os.path.exists(input):
            # Safe if the HF environment mounts the file
                result = model.transcribe(input)
                
            else:
                raise TypeError("Unsupported input type. Expected bytes or a valid file path.")

            return result["text"]

            


        
        self.list_tools=[DuckDuckGoSearchTool(), audio_interpreter]
        
        self.agent = CodeAgent(tools=self.list_tools, 
                          model=self.model, 
                          additional_authorized_imports=['pandas','io', 'requests','markdownify'], 
                          max_steps=10,  
                          add_base_tools=True  # Add any additional base tools
                          #planning_interval=3   # Enable planning every 3 steps)  #-1 to suppress display of reasoning steps
                        )
        
        print("BasicAgent initialized.")

    
    #Retry policy if quota exceeded
    def retry(self, prompt):
        backoff = 20
        while True:
            try:
                response = self.agent.run(prompt)
                return response
                break  # Success
            except Exception as e:
                if "429" in str(e):
                    print(f"Rate limit hit. Sleeping for {backoff} seconds...")
                    time.sleep(backoff)
                    backoff = min(backoff * 2, 80)  # max backoff = 80 seconds
                else:
                    print("Error:", e)
                    break

        
    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        prompt=f'{self.base_prompt}\n {question}'
        answer = self.retry(prompt)
        print(f"Agent returning fixed answer: {answer}")
        return answer