Update multiagents.py
Browse files- multiagents.py +83 -52
multiagents.py
CHANGED
|
@@ -1,53 +1,73 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import os
|
| 3 |
import dotenv
|
| 4 |
-
|
| 5 |
-
from smolagents import
|
| 6 |
-
from smolagents.models.openai_server_model import OpenAIServerModel as BaseOpenAIServerModel
|
| 7 |
-
|
| 8 |
from tools.fetch import fetch_webpage, search_web
|
|
|
|
| 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 myprompts
|
| 14 |
|
| 15 |
-
# β
Load .env
|
| 16 |
dotenv.load_dotenv()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
def check_final_answer(final_answer, agent_memory)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
mylog("check_final_answer", final_answer)
|
| 36 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# β
Sub-agents
|
| 39 |
web_agent = CodeAgent(
|
| 40 |
-
model=
|
| 41 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
| 42 |
name="web_agent",
|
| 43 |
-
description="
|
| 44 |
-
additional_authorized_imports=["pandas", "numpy",
|
| 45 |
-
verbosity_level=1,
|
| 46 |
max_steps=7,
|
| 47 |
)
|
| 48 |
|
| 49 |
audiovideo_agent = CodeAgent(
|
| 50 |
-
model=
|
| 51 |
tools=[
|
| 52 |
get_youtube_transcript,
|
| 53 |
get_youtube_title_description,
|
|
@@ -55,52 +75,63 @@ audiovideo_agent = CodeAgent(
|
|
| 55 |
analyze_image
|
| 56 |
],
|
| 57 |
name="audiovideo_agent",
|
| 58 |
-
description="Extracts
|
| 59 |
-
additional_authorized_imports=["pandas", "numpy",
|
| 60 |
verbosity_level=1,
|
| 61 |
max_steps=7,
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
manager_agent = CodeAgent(
|
| 66 |
-
model=
|
| 67 |
-
tools=[PythonInterpreterTool()],
|
| 68 |
-
managed_agents=[web_agent, audiovideo_agent],
|
| 69 |
-
|
| 70 |
-
description="Coordinates other agents and returns a final answer.",
|
| 71 |
-
additional_authorized_imports=["pandas", "numpy", "bs4"],
|
| 72 |
planning_interval=5,
|
| 73 |
verbosity_level=2,
|
| 74 |
final_answer_checks=[check_final_answer],
|
| 75 |
max_steps=15,
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
-
# β
Multi-agent interface
|
| 79 |
class MultiAgent:
|
| 80 |
def __init__(self):
|
| 81 |
-
print("
|
| 82 |
|
| 83 |
def __call__(self, question: str) -> str:
|
| 84 |
-
mylog(self.__class__.__name__, question)
|
| 85 |
|
| 86 |
try:
|
| 87 |
-
prefix = """
|
| 88 |
-
You
|
| 89 |
-
You can use the web_agent to search the web, or the audiovideo_agent to extract
|
| 90 |
-
You
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
error = f"An error occurred while processing the question: {e}"
|
| 98 |
print(error)
|
| 99 |
return error
|
| 100 |
|
| 101 |
-
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
agent = MultiAgent()
|
| 105 |
answer = agent(question)
|
| 106 |
print(f"Answer: {answer}")
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# a multi agent proposal to solve HF agent course final assignment
|
| 3 |
import os
|
| 4 |
import dotenv
|
| 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 myprompts
|
| 14 |
|
|
|
|
| 15 |
dotenv.load_dotenv()
|
| 16 |
|
| 17 |
+
gemini_model = OpenAIServerModel(
|
| 18 |
+
model_id="gemini-2.0-flash",
|
| 19 |
+
api_key=os.environ["GEMINI_API_KEY"],
|
| 20 |
+
# Google Gemini OpenAI-compatible API base URL
|
| 21 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 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="gpt-4.1-nano",
|
| 32 |
+
api_base="https://api.openai.com/v1",
|
| 33 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
openai_41mini_model = OpenAIServerModel(
|
| 37 |
+
model_id="gpt-3.5-turbo",
|
| 38 |
+
api_base="https://api.openai.com/v1",
|
| 39 |
+
api_key=os.environ["OPENAI_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=openai_41nano_model,
|
| 58 |
+
tools=[
|
| 59 |
+
search_web,
|
| 60 |
+
fetch_webpage,
|
| 61 |
+
],
|
| 62 |
name="web_agent",
|
| 63 |
+
description="Use search engine to find webpages related to a subject and get the page content",
|
| 64 |
+
additional_authorized_imports=["pandas", "numpy","bs4"],
|
| 65 |
+
verbosity_level=1,
|
| 66 |
max_steps=7,
|
| 67 |
)
|
| 68 |
|
| 69 |
audiovideo_agent = CodeAgent(
|
| 70 |
+
model=openai_41nano_model,
|
| 71 |
tools=[
|
| 72 |
get_youtube_transcript,
|
| 73 |
get_youtube_title_description,
|
|
|
|
| 75 |
analyze_image
|
| 76 |
],
|
| 77 |
name="audiovideo_agent",
|
| 78 |
+
description="Extracts information from image, video or audio files from the web",
|
| 79 |
+
additional_authorized_imports=["pandas", "numpy","bs4", "requests"],
|
| 80 |
verbosity_level=1,
|
| 81 |
max_steps=7,
|
| 82 |
)
|
| 83 |
|
| 84 |
+
|
| 85 |
+
|
| 86 |
manager_agent = CodeAgent(
|
| 87 |
+
model=openai_41mini_model,
|
| 88 |
+
tools=[ PythonInterpreterTool()],
|
| 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="A manager agent that coordinates the work of other agents to answer questions.",
|
| 97 |
)
|
| 98 |
|
|
|
|
| 99 |
class MultiAgent:
|
| 100 |
def __init__(self):
|
| 101 |
+
print("BasicAgent initialized.")
|
| 102 |
|
| 103 |
def __call__(self, question: str) -> str:
|
| 104 |
+
mylog(self.__class__.__name__, question)
|
| 105 |
|
| 106 |
try:
|
| 107 |
+
prefix = """You are the top agent of a multi-agent system that can answer questions by coordinating the work of other agents.
|
| 108 |
+
You will receive a question and you will decide which agent to use to answer it.
|
| 109 |
+
You can use the web_agent to search the web for information and for fetching the content of a web page, or the audiovideo_agent to extract information from video or audio files.
|
| 110 |
+
You can also use your own knowledge to answer the question.
|
| 111 |
+
You need to respect the output format that is given to you.
|
| 112 |
+
Finding the correct answer to the question need reasoning and plannig, read the question carrefully, think step by step and do not skip any steps.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
question = prefix + "\nTHE QUESTION:\n" + question + '\n' + myprompts.output_format
|
| 116 |
+
|
| 117 |
+
fixed_answer = ""
|
| 118 |
+
|
| 119 |
+
fixed_answer = manager_agent.run(question)
|
| 120 |
+
|
| 121 |
+
return fixed_answer
|
| 122 |
except Exception as e:
|
| 123 |
error = f"An error occurred while processing the question: {e}"
|
| 124 |
print(error)
|
| 125 |
return error
|
| 126 |
|
| 127 |
+
|
| 128 |
if __name__ == "__main__":
|
| 129 |
+
# Example usage
|
| 130 |
+
|
| 131 |
+
question = """
|
| 132 |
+
What was the actual enrollment of the Malko competition in 2023?
|
| 133 |
+
"""
|
| 134 |
agent = MultiAgent()
|
| 135 |
answer = agent(question)
|
| 136 |
print(f"Answer: {answer}")
|
| 137 |
+
|