bwilkie commited on
Commit
7d8f8f7
·
verified ·
1 Parent(s): 170b178

Create multiagents.py

Browse files
Files changed (1) hide show
  1. multiagents.py +183 -0
multiagents.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, InferenceClientModel
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
+ from smolagents import LiteLLMModel # Import LiteLLMModel instead of OpenAIServerModel
14
+ import os
15
+ #from huggingface_hub import InferenceClient
16
+ import myprompts
17
+
18
+ from groq_api import GrokApi
19
+
20
+
21
+ dotenv.load_dotenv()
22
+
23
+ # gemini_model = OpenAIServerModel(
24
+ # model_id="gemini-2.0-flash",
25
+ # api_key=os.environ["GEMINI_API_KEY"],
26
+ # # Google Gemini OpenAI-compatible API base URL
27
+ # api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
28
+ # )
29
+
30
+ # vllm_model = OpenAIServerModel(
31
+ # model_id="Qwen/Qwen2.5-1.5B-Instruct",
32
+ # api_base="http://192.168.1.39:18000/v1",
33
+ # api_key="token-abc123",
34
+ # )
35
+
36
+ # openai_41nano_model = OpenAIServerModel(
37
+ # model_id="gpt-4.1-nano",
38
+ # api_base="https://api.openai.com/v1",
39
+ # api_key=os.environ["OPENAI_API_KEY"],
40
+ # )
41
+
42
+ # openai_41mini_model = OpenAIServerModel(
43
+ # model_id="gpt-4.1-mini",
44
+ # api_base="https://api.openai.com/v1",
45
+ # api_key=os.environ["OPENAI_API_KEY"],
46
+ # )
47
+
48
+
49
+
50
+ # # --- Agent wrappers ---
51
+ # groq_model = LiteLLMModel(
52
+ # model_id="groq/qwen3-32b", # or any other Groq model like groq/mixtral-8x7b-32768
53
+ # api_key = os.getenv("GROQ_API_KEY"),
54
+ # temperature=0.1,
55
+ # max_tokens=4000,
56
+ # )
57
+
58
+ # SETUP AND TEST
59
+
60
+
61
+
62
+ grok_api_key = os.getenv("groq_api")
63
+
64
+ #InferenceClientModel InferenceClient
65
+ My_Agent = InferenceClientModel(
66
+ provider="groq",
67
+ api_key=grok_api_key,
68
+ model_id = "qwen/qwen3-32b"
69
+ )
70
+
71
+ # test_messages = [{"role": "user", "content": "What are the 3 laws of robotics"}]
72
+ # output_test = My_Agent(test_messages)
73
+ # print('1', output_test)
74
+
75
+
76
+
77
+ # My_Agent = client.chat.completions.create(
78
+ # model="qewn/qwen3-32b",
79
+ # messages=[
80
+ # {
81
+ # "role": "user",
82
+ # "content": "How many 'G's in 'huggingface'?"
83
+ # }
84
+ # ],
85
+ # )
86
+
87
+
88
+
89
+ def check_final_answer(final_answer, agent_memory) -> bool:
90
+ """
91
+ Check if the final answer is correct.
92
+ basic check on the length of the answer.
93
+ """
94
+ mylog("check_final_answer", final_answer)
95
+ # if return answer is more than 200 characters, we will assume it is not correct
96
+ if len(str(final_answer)) > 200:
97
+ return False
98
+ else:
99
+ return True
100
+
101
+
102
+ web_agent = CodeAgent(
103
+ model=My_Agent,
104
+ tools=[
105
+ search_web,
106
+ fetch_webpage,
107
+ ],
108
+ name="web_agent",
109
+ description="Use search engine to find webpages related to a subject and get the page content",
110
+ additional_authorized_imports=["pandas", "numpy","bs4"],
111
+ verbosity_level=1,
112
+ max_steps=7,
113
+ )
114
+
115
+ audiovideo_agent = CodeAgent(
116
+ model=My_Agent,
117
+ tools=[
118
+ get_youtube_transcript,
119
+ get_youtube_title_description,
120
+ get_text_transcript_from_audio_file,
121
+ analyze_image
122
+ ],
123
+ name="audiovideo_agent",
124
+ description="Extracts information from image, video or audio files from the web",
125
+ additional_authorized_imports=["pandas", "numpy","bs4", "requests"],
126
+ verbosity_level=1,
127
+ max_steps=7,
128
+ )
129
+
130
+
131
+
132
+ manager_agent = CodeAgent(
133
+ model=My_Agent,
134
+ tools=[ PythonInterpreterTool()],
135
+ managed_agents=[web_agent, audiovideo_agent],
136
+ additional_authorized_imports=["pandas", "numpy","bs4"],
137
+ planning_interval=5,
138
+ verbosity_level=2,
139
+ final_answer_checks=[check_final_answer],
140
+ max_steps=15,
141
+ name="manager_agent",
142
+ description="A manager agent that coordinates the work of other agents to answer questions.",
143
+ )
144
+
145
+ class MultiAgent:
146
+ def __init__(self):
147
+ print("BasicAgent initialized.")
148
+
149
+ def __call__(self, question: str) -> str:
150
+ mylog(self.__class__.__name__, question)
151
+
152
+ try:
153
+ prefix = """You are the top agent of a multi-agent system that can answer questions by coordinating the work of other agents.
154
+ You will receive a question and you will decide which agent to use to answer it.
155
+ 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.
156
+ You can also use your own knowledge to answer the question.
157
+ You need to respect the output format that is given to you.
158
+ 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.
159
+ """
160
+
161
+ question = prefix + "\nTHE QUESTION:\n" + question + '\n' + myprompts.output_format
162
+
163
+ fixed_answer = ""
164
+
165
+ fixed_answer = manager_agent.run(question)
166
+
167
+ return fixed_answer
168
+ except Exception as e:
169
+ error = f"An error occurred while processing the question: {e}"
170
+ print(error)
171
+ return error
172
+
173
+
174
+ if __name__ == "__main__":
175
+ # Example usage
176
+
177
+ asyncio.run(main())
178
+ question = """
179
+ What was the actual enrollment of the Malko competition in 2023?
180
+ """
181
+ agent = MultiAgent()
182
+ answer = agent(question)
183
+ print(f"Answer: {answer}")