Update multiagents.py
Browse files- multiagents.py +45 -96
multiagents.py
CHANGED
|
@@ -1,73 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
# a multi agent proposal to solve HF agent course final assignment
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
from smolagents import CodeAgent
|
| 6 |
-
from smolagents import OpenAIServerModel
|
| 7 |
from tools.fetch import fetch_webpage, search_web
|
| 8 |
-
from smolagents import PythonInterpreterTool
|
| 9 |
from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
|
| 10 |
from tools.stt import get_text_transcript_from_audio_file
|
| 11 |
from tools.image import analyze_image
|
| 12 |
from common.mylogger import mylog
|
| 13 |
-
import
|
|
|
|
| 14 |
|
| 15 |
dotenv.load_dotenv()
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
api_base="https://
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
vllm_model = OpenAIServerModel(
|
| 25 |
-
model_id="Qwen/Qwen2.5-1.5B-Instruct",
|
| 26 |
-
api_base="http://192.168.1.39:18000/v1",
|
| 27 |
-
api_key="token-abc123",
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
openai_41nano_model = OpenAIServerModel(
|
| 31 |
-
model_id="llama3-70b-8192",
|
| 32 |
-
api_base="https://api.groq.com/openai/v1",
|
| 33 |
-
api_key=os.environ["GROQ_API_KEY"],
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
model_id="llama3-70b-8192",
|
| 38 |
-
api_base="https://api.groq.com/openai/v1",
|
| 39 |
-
api_key=os.environ["GROQ_API_KEY"],
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def check_final_answer(final_answer, agent_memory) -> bool:
|
| 44 |
-
"""
|
| 45 |
-
Check if the final answer is correct.
|
| 46 |
-
basic check on the length of the answer.
|
| 47 |
-
"""
|
| 48 |
-
mylog("check_final_answer", final_answer)
|
| 49 |
-
# if return answer is more than 200 characters, we will assume it is not correct
|
| 50 |
-
if len(str(final_answer)) > 200:
|
| 51 |
-
return False
|
| 52 |
-
else:
|
| 53 |
-
return True
|
| 54 |
-
|
| 55 |
-
|
| 56 |
web_agent = CodeAgent(
|
| 57 |
-
model=
|
| 58 |
-
tools=[
|
| 59 |
-
search_web,
|
| 60 |
-
fetch_webpage,
|
| 61 |
-
],
|
| 62 |
name="web_agent",
|
| 63 |
-
description="
|
| 64 |
-
|
| 65 |
-
verbosity_level=1,
|
| 66 |
-
max_steps=7,
|
| 67 |
)
|
| 68 |
|
| 69 |
audiovideo_agent = CodeAgent(
|
| 70 |
-
model=
|
| 71 |
tools=[
|
| 72 |
get_youtube_transcript,
|
| 73 |
get_youtube_title_description,
|
|
@@ -75,69 +36,57 @@ audiovideo_agent = CodeAgent(
|
|
| 75 |
analyze_image
|
| 76 |
],
|
| 77 |
name="audiovideo_agent",
|
| 78 |
-
description="
|
| 79 |
-
|
| 80 |
verbosity_level=1,
|
| 81 |
-
max_steps=7,
|
| 82 |
)
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
manager_agent = CodeAgent(
|
| 87 |
-
model=
|
| 88 |
-
tools=[
|
| 89 |
-
managed_agents=[web_agent, audiovideo_agent],
|
| 90 |
-
additional_authorized_imports=["pandas", "numpy","bs4"],
|
| 91 |
-
planning_interval=5,
|
| 92 |
-
verbosity_level=2,
|
| 93 |
-
final_answer_checks=[check_final_answer],
|
| 94 |
-
max_steps=15,
|
| 95 |
name="manager_agent",
|
| 96 |
-
description="
|
|
|
|
|
|
|
|
|
|
| 97 |
)
|
| 98 |
|
|
|
|
| 99 |
class MultiAgent:
|
| 100 |
def __init__(self):
|
| 101 |
-
print("
|
| 102 |
|
| 103 |
def __call__(self, question: str) -> str:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
try:
|
| 107 |
-
|
| 108 |
-
"You are the
|
| 109 |
-
"
|
| 110 |
-
"
|
| 111 |
)
|
| 112 |
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
THE QUESTION:
|
| 116 |
{question.strip()}
|
| 117 |
|
| 118 |
-
{output_format.strip()}
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
assert isinstance(full_prompt, str), "Prompt must be a string."
|
| 122 |
-
|
| 123 |
-
print("Sending prompt to manager_agent:")
|
| 124 |
-
print(full_prompt)
|
| 125 |
-
|
| 126 |
-
result = manager_agent.run(full_prompt)
|
| 127 |
-
|
| 128 |
-
return result if isinstance(result, str) else str(result)
|
| 129 |
-
|
| 130 |
except Exception as e:
|
| 131 |
-
|
|
|
|
|
|
|
| 132 |
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
question = """
|
| 138 |
-
What was the actual enrollment of the Malko competition in 2023?
|
| 139 |
-
"""
|
| 140 |
agent = MultiAgent()
|
| 141 |
-
|
| 142 |
-
print(f"Answer: {answer}")
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from smolagents import CodeAgent, OpenAIServerModel, PythonInterpreterTool
|
|
|
|
|
|
|
| 3 |
from tools.fetch import fetch_webpage, search_web
|
|
|
|
| 4 |
from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
|
| 5 |
from tools.stt import get_text_transcript_from_audio_file
|
| 6 |
from tools.image import analyze_image
|
| 7 |
from common.mylogger import mylog
|
| 8 |
+
from myprompts import output_format
|
| 9 |
+
import dotenv
|
| 10 |
|
| 11 |
dotenv.load_dotenv()
|
| 12 |
|
| 13 |
+
# --- Models ---
|
| 14 |
+
openai_model = OpenAIServerModel(
|
| 15 |
+
model_id="gpt-3.5-turbo",
|
| 16 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
| 17 |
+
api_base="https://api.openai.com/v1",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# --- Tool Agents ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
web_agent = CodeAgent(
|
| 22 |
+
model=openai_model,
|
| 23 |
+
tools=[search_web, fetch_webpage],
|
|
|
|
|
|
|
|
|
|
| 24 |
name="web_agent",
|
| 25 |
+
description="Searches the web and fetches content from webpages.",
|
| 26 |
+
max_steps=6,
|
| 27 |
+
verbosity_level=1,
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
audiovideo_agent = CodeAgent(
|
| 31 |
+
model=openai_model,
|
| 32 |
tools=[
|
| 33 |
get_youtube_transcript,
|
| 34 |
get_youtube_title_description,
|
|
|
|
| 36 |
analyze_image
|
| 37 |
],
|
| 38 |
name="audiovideo_agent",
|
| 39 |
+
description="Processes audio, video, and image content.",
|
| 40 |
+
max_steps=6,
|
| 41 |
verbosity_level=1,
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
+
# --- Answer Checker ---
|
| 45 |
+
def check_final_answer(answer: str, _) -> bool:
|
| 46 |
+
return isinstance(answer, str) and len(answer.strip()) <= 200
|
| 47 |
|
| 48 |
+
# --- Manager Agent ---
|
| 49 |
manager_agent = CodeAgent(
|
| 50 |
+
model=openai_model,
|
| 51 |
+
tools=[PythonInterpreterTool()],
|
| 52 |
+
managed_agents=[web_agent, audiovideo_agent],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
name="manager_agent",
|
| 54 |
+
description="Manages sub-agents to answer complex queries.",
|
| 55 |
+
final_answer_checks=[check_final_answer],
|
| 56 |
+
verbosity_level=2,
|
| 57 |
+
max_steps=12,
|
| 58 |
)
|
| 59 |
|
| 60 |
+
# --- Entry Class ---
|
| 61 |
class MultiAgent:
|
| 62 |
def __init__(self):
|
| 63 |
+
print("✅ MultiAgent initialized")
|
| 64 |
|
| 65 |
def __call__(self, question: str) -> str:
|
| 66 |
+
mylog("MultiAgent", question)
|
|
|
|
| 67 |
try:
|
| 68 |
+
system_instruction = (
|
| 69 |
+
"You are the lead agent in a multi-agent AI system. Use the sub-agents wisely."
|
| 70 |
+
" Answer using reasoning, planning, and tools where needed."
|
| 71 |
+
" Stay concise and always format the answer exactly as required."
|
| 72 |
)
|
| 73 |
|
| 74 |
+
prompt = f"""
|
| 75 |
+
{system_instruction}
|
| 76 |
|
| 77 |
THE QUESTION:
|
| 78 |
{question.strip()}
|
| 79 |
|
| 80 |
+
{output_format.strip()}
|
| 81 |
+
"""
|
| 82 |
+
return str(manager_agent.run(prompt))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
+
error_msg = f"An error occurred while processing the question: {e}"
|
| 85 |
+
print(error_msg)
|
| 86 |
+
return error_msg
|
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
+
example_question = "What was the actual enrollment of the Malko competition in 2023?"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
agent = MultiAgent()
|
| 92 |
+
print(agent(example_question))
|
|
|
|
|
|