Update myagent.py
Browse files- myagent.py +74 -58
myagent.py
CHANGED
@@ -1,59 +1,75 @@
|
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
)
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
print(f"Answer: {answer}")
|
|
|
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 |
+
|
51 |
+
MODEL_NAME = "meta-llama/Llama-3.2-3B" # 3B isn't released by Meta officially, but use 8B or a 3B variant like TinyLlama if needed
|
52 |
+
|
53 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
54 |
+
model_init = AutoModelForCausalLM.from_pretrained(
|
55 |
+
MODEL_NAME,
|
56 |
+
device_map="auto",
|
57 |
+
torch_dtype=torch.float16 # or bfloat16
|
58 |
+
)
|
59 |
+
|
60 |
+
def model(prompt: str, max_new_tokens=512):
|
61 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
|
62 |
+
output_ids = model.generate(input_ids, max_new_tokens=max_new_tokens)
|
63 |
+
output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
64 |
+
return output
|
65 |
+
|
66 |
+
reviewer_agent= ToolCallingAgent(model=model, tools=[])
|
67 |
+
model_agent = ToolCallingAgent(model=model,tools=[fetch_webpage])
|
68 |
+
gaia_agent = CodeAgent(tools=[fetch_webpage,get_youtube_title_description,get_youtube_transcript ], model=model)
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
# Example usage
|
72 |
+
question = "What was the actual enrollment of the Malko competition in 2023?"
|
73 |
+
agent = BasicAgent()
|
74 |
+
answer = agent(question)
|
75 |
print(f"Answer: {answer}")
|