sirine1712 commited on
Commit
01b4c4a
Β·
verified Β·
1 Parent(s): 3e5205a

Update multiagents.py

Browse files
Files changed (1) hide show
  1. multiagents.py +28 -18
multiagents.py CHANGED
@@ -1,8 +1,10 @@
1
- # multiagent.py β€” GAIA-compliant smolagents setup using Groq
2
-
3
  import os
4
  import dotenv
5
- from smolagents import CodeAgent, OpenAIServerModel, PythonInterpreterTool
 
 
 
6
  from tools.fetch import fetch_webpage, search_web
7
  from tools.yttranscript import get_youtube_transcript, get_youtube_title_description
8
  from tools.stt import get_text_transcript_from_audio_file
@@ -10,32 +12,40 @@ from tools.image import analyze_image
10
  from common.mylogger import mylog
11
  import myprompts
12
 
 
13
  dotenv.load_dotenv()
14
 
15
- # βœ… Use Groq’s LLaMA3 (OpenAI-compatible and fast)
16
- groq_model = OpenAIServerModel(
 
 
 
 
 
 
 
 
17
  model_id="llama3-70b-8192",
18
  api_key=os.environ["GROQ_API_KEY"],
19
  api_base="https://api.groq.com/openai/v1",
20
  )
21
 
22
- # βœ… Final answer validation
23
  def check_final_answer(final_answer, agent_memory) -> bool:
24
  mylog("check_final_answer", final_answer)
25
  return len(str(final_answer)) <= 200
26
 
27
- # βœ… Web agent for search and scraping
28
  web_agent = CodeAgent(
29
  model=groq_model,
30
  tools=[search_web, fetch_webpage],
31
  name="web_agent",
32
- description="Use search engine to find webpages related to a subject and get the page content.",
33
  additional_authorized_imports=["pandas", "numpy", "bs4"],
34
  verbosity_level=1,
35
  max_steps=7,
36
  )
37
 
38
- # βœ… Audio/Video/Image processing agent
39
  audiovideo_agent = CodeAgent(
40
  model=groq_model,
41
  tools=[
@@ -45,30 +55,30 @@ audiovideo_agent = CodeAgent(
45
  analyze_image
46
  ],
47
  name="audiovideo_agent",
48
- description="Extracts information from image, video or audio files from the web.",
49
  additional_authorized_imports=["pandas", "numpy", "bs4", "requests"],
50
  verbosity_level=1,
51
  max_steps=7,
52
  )
53
 
54
- # βœ… Manager agent (planner/coordinator)
55
  manager_agent = CodeAgent(
56
  model=groq_model,
57
  tools=[PythonInterpreterTool()],
58
  managed_agents=[web_agent, audiovideo_agent],
 
 
59
  additional_authorized_imports=["pandas", "numpy", "bs4"],
60
  planning_interval=5,
61
  verbosity_level=2,
62
  final_answer_checks=[check_final_answer],
63
  max_steps=15,
64
- name="manager_agent",
65
- description="A manager agent that coordinates the work of other agents to answer questions.",
66
  )
67
 
68
- # βœ… Multi-agent system wrapper class
69
  class MultiAgent:
70
  def __init__(self):
71
- print("MultiAgent system initialized.")
72
 
73
  def __call__(self, question: str) -> str:
74
  mylog(self.__class__.__name__, question)
@@ -80,15 +90,15 @@ You can use the web_agent to search the web, or the audiovideo_agent to extract
80
  You must reason step by step and respect the required output format.
81
  Only return the final answer in the correct format.
82
  """
83
- full_prompt = prefix.strip() + "\nTHE QUESTION:\n" + question.strip() + "\n" + myprompts.output_format.strip()
84
- answer = manager_agent.run(full_prompt)
85
  return answer
86
  except Exception as e:
87
  error = f"An error occurred while processing the question: {e}"
88
  print(error)
89
  return error
90
 
91
- # βœ… Manual test (can be triggered from HF Space too)
92
  if __name__ == "__main__":
93
  question = "What was the actual enrollment of the Malko competition in 2023?"
94
  agent = MultiAgent()
 
1
+ # multiagent.py β€” GAIA-compliant multi-agent system using Groq (patched)
 
2
  import os
3
  import dotenv
4
+
5
+ from smolagents import CodeAgent, PythonInterpreterTool
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
 
12
  from common.mylogger import mylog
13
  import myprompts
14
 
15
+ # βœ… Load .env
16
  dotenv.load_dotenv()
17
 
18
+ # βœ… Monkeypatch: Ensure message['content'] is always a string
19
+ class PatchedOpenAIServerModel(BaseOpenAIServerModel):
20
+ def complete_chat(self, messages, **kwargs):
21
+ for msg in messages:
22
+ if not isinstance(msg.get("content", ""), str):
23
+ msg["content"] = str(msg["content"])
24
+ return super().complete_chat(messages, **kwargs)
25
+
26
+ # βœ… Groq model (OpenAI-compatible)
27
+ groq_model = PatchedOpenAIServerModel(
28
  model_id="llama3-70b-8192",
29
  api_key=os.environ["GROQ_API_KEY"],
30
  api_base="https://api.groq.com/openai/v1",
31
  )
32
 
33
+ # βœ… Final answer checker
34
  def check_final_answer(final_answer, agent_memory) -> bool:
35
  mylog("check_final_answer", final_answer)
36
  return len(str(final_answer)) <= 200
37
 
38
+ # βœ… Sub-agents
39
  web_agent = CodeAgent(
40
  model=groq_model,
41
  tools=[search_web, fetch_webpage],
42
  name="web_agent",
43
+ description="Uses search engine and scrapes webpages for content.",
44
  additional_authorized_imports=["pandas", "numpy", "bs4"],
45
  verbosity_level=1,
46
  max_steps=7,
47
  )
48
 
 
49
  audiovideo_agent = CodeAgent(
50
  model=groq_model,
51
  tools=[
 
55
  analyze_image
56
  ],
57
  name="audiovideo_agent",
58
+ description="Extracts data from audio, video, or images.",
59
  additional_authorized_imports=["pandas", "numpy", "bs4", "requests"],
60
  verbosity_level=1,
61
  max_steps=7,
62
  )
63
 
64
+ # βœ… Manager agent
65
  manager_agent = CodeAgent(
66
  model=groq_model,
67
  tools=[PythonInterpreterTool()],
68
  managed_agents=[web_agent, audiovideo_agent],
69
+ name="manager_agent",
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("MultiAgent initialized.")
82
 
83
  def __call__(self, question: str) -> str:
84
  mylog(self.__class__.__name__, question)
 
90
  You must reason step by step and respect the required output format.
91
  Only return the final answer in the correct format.
92
  """
93
+ prompt = prefix.strip() + "\nTHE QUESTION:\n" + question.strip() + "\n" + myprompts.output_format.strip()
94
+ answer = manager_agent.run(prompt)
95
  return answer
96
  except Exception as e:
97
  error = f"An error occurred while processing the question: {e}"
98
  print(error)
99
  return error
100
 
101
+ # βœ… Local test
102
  if __name__ == "__main__":
103
  question = "What was the actual enrollment of the Malko competition in 2023?"
104
  agent = MultiAgent()