Create myagent.py
Browse files- myagent.py +59 -0
myagent.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from smolagents import CodeAgent, ToolCallingAgent
|
3 |
+
from smolagents import OpenAIServerModel
|
4 |
+
from tools.fetch import fetch_webpage
|
5 |
+
from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
|
6 |
+
import myprompts
|
7 |
+
|
8 |
+
# --- Basic Agent Definition ---
|
9 |
+
class BasicAgent:
|
10 |
+
def __init__(self):
|
11 |
+
print("BasicAgent initialized.")
|
12 |
+
def __call__(self, question: str) -> str:
|
13 |
+
|
14 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
15 |
+
|
16 |
+
try:
|
17 |
+
# Use the reviewer agent to determine if the question can be answered by a model or requires code
|
18 |
+
print("Calling reviewer agent...")
|
19 |
+
reviewer_answer = reviewer_agent.run(myprompts.review_prompt + "\nThe question is:\n" + question)
|
20 |
+
print(f"Reviewer agent answer: {reviewer_answer}")
|
21 |
+
|
22 |
+
question = question + '\n' + myprompts.output_format
|
23 |
+
fixed_answer = ""
|
24 |
+
|
25 |
+
if reviewer_answer == "code":
|
26 |
+
fixed_answer = gaia_agent.run(question)
|
27 |
+
print(f"Code agent answer: {fixed_answer}")
|
28 |
+
|
29 |
+
elif reviewer_answer == "model":
|
30 |
+
# If the reviewer agent suggests using the model, we can proceed with the model agent
|
31 |
+
print("Using model agent to answer the question.")
|
32 |
+
fixed_answer = model_agent.run(myprompts.model_prompt + "\nThe question is:\n" + question)
|
33 |
+
print(f"Model agent answer: {fixed_answer}")
|
34 |
+
|
35 |
+
return fixed_answer
|
36 |
+
except Exception as e:
|
37 |
+
error = f"An error occurred while processing the question: {e}"
|
38 |
+
print(error)
|
39 |
+
return error
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
model = OpenAIServerModel(
|
45 |
+
model_id="gpt-4.1-nano",
|
46 |
+
api_base="https://api.openai.com/v1",
|
47 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
48 |
+
)
|
49 |
+
|
50 |
+
reviewer_agent= ToolCallingAgent(model=model, tools=[])
|
51 |
+
model_agent = ToolCallingAgent(model=model,tools=[fetch_webpage])
|
52 |
+
gaia_agent = CodeAgent(tools=[fetch_webpage,get_youtube_title_description,get_youtube_transcript ], model=model)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
# Example usage
|
56 |
+
question = "What was the actual enrollment of the Malko competition in 2023?"
|
57 |
+
agent = BasicAgent()
|
58 |
+
answer = agent(question)
|
59 |
+
print(f"Answer: {answer}")
|