24Arys11 commited on
Commit
851947c
·
1 Parent(s): fb0045e

defining the Solver's agents (part 1)

Browse files
Files changed (1) hide show
  1. solver.py +282 -90
solver.py CHANGED
@@ -10,26 +10,37 @@ from toolbox import Toolbox
10
  from args import Args
11
 
12
 
13
- class Solver:
14
  def __init__(self, temperature, max_tokens):
15
- system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "06_math_expert.txt")
 
16
  self.system_prompt = ""
17
  with open(system_prompt_path, "r") as file:
18
  self.system_prompt = file.read().strip()
 
19
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
20
- self.agent = AgentWorkflow.from_tools_or_functions(
21
- [
22
- Toolbox.math.symbolic_calc,
23
- Toolbox.math.unit_converter,
24
- ],
25
- llm=llm
26
- )
27
  self.ctx = Context(self.agent)
28
 
29
- def get_system_prompt(self):
 
 
 
 
 
 
30
  return self.system_prompt
31
 
32
  async def query(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
33
  response = await self.agent.run(question, ctx=self.ctx)
34
  response = str(response)
35
  return response
@@ -42,42 +53,96 @@ class Solver:
42
  self.ctx = Context(self.agent)
43
 
44
 
45
- class Summarizer:
46
  def __init__(self, temperature, max_tokens):
47
- system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "01_assistant.txt")
 
48
  self.system_prompt = ""
49
  with open(system_prompt_path, "r") as file:
50
  self.system_prompt = file.read().strip()
 
51
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
52
- self.agent = AgentWorkflow.setup_agent(llm=llm)
 
 
 
53
  self.ctx = Context(self.agent)
 
 
 
 
 
 
 
 
 
54
 
55
  async def query(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
56
  response = await self.agent.run(question, ctx=self.ctx)
57
  response = str(response)
58
  return response
59
 
 
 
 
 
 
 
 
60
 
61
- class MathExpert:
62
  def __init__(self, temperature, max_tokens):
63
- system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "06_math_expert.txt")
 
64
  self.system_prompt = ""
65
  with open(system_prompt_path, "r") as file:
66
  self.system_prompt = file.read().strip()
 
67
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
68
  self.agent = AgentWorkflow.from_tools_or_functions(
69
  [
70
- Toolbox.math.symbolic_calc,
71
- Toolbox.math.unit_converter,
 
 
 
 
72
  ],
73
  llm=llm
74
  )
75
  self.ctx = Context(self.agent)
 
 
 
76
 
77
- def get_system_prompt(self):
 
 
 
 
 
 
78
  return self.system_prompt
79
 
80
  async def query(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
81
  response = await self.agent.run(question, ctx=self.ctx)
82
  response = str(response)
83
  return response
@@ -86,137 +151,264 @@ class MathExpert:
86
  """
87
  Clears the current context of the agent, resetting any conversation history.
88
  This is useful when starting a new conversation or when the context needs to be refreshed.
 
89
  """
90
  self.ctx = Context(self.agent)
 
 
 
91
 
92
 
93
- class Researcher:
94
  def __init__(self, temperature, max_tokens):
95
- system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "04_researcher.txt")
 
96
  self.system_prompt = ""
97
  with open(system_prompt_path, "r") as file:
98
  self.system_prompt = file.read().strip()
 
99
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
100
-
101
  self.agent = AgentWorkflow.from_tools_or_functions(
102
- Toolbox.web_search.duck_duck_go_tools,
 
 
 
103
  llm=llm
104
  )
105
  self.ctx = Context(self.agent)
 
 
106
 
107
- def get_system_prompt(self):
 
 
 
 
 
 
108
  return self.system_prompt
109
 
110
  async def query(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
111
  response = await self.agent.run(question, ctx=self.ctx)
112
  response = str(response)
113
  return response
114
 
 
 
 
 
 
 
 
 
 
115
 
116
- class EncryptionExpert:
117
  def __init__(self, temperature, max_tokens):
118
- system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "05_encryption_expert.txt")
 
119
  self.system_prompt = ""
120
  with open(system_prompt_path, "r") as file:
121
  self.system_prompt = file.read().strip()
 
122
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
123
-
124
- self.agent = AgentWorkflow.from_tools_or_functions(
125
- [
126
- Toolbox.encryption.base64_encode,
127
- Toolbox.encryption.base64_decode,
128
- Toolbox.encryption.caesar_cipher_encode,
129
- Toolbox.encryption.caesar_cipher_decode,
130
- Toolbox.encryption.reverse_string
131
- ],
132
- llm=llm
133
- )
134
  self.ctx = Context(self.agent)
135
-
136
- def get_system_prompt(self):
137
- return self.system_prompt
138
 
139
  async def query(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
140
  response = await self.agent.run(question, ctx=self.ctx)
141
  response = str(response)
142
  return response
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
  class ImageHandler:
146
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  class VideoHandler:
149
- pass
 
 
 
 
 
 
 
 
 
 
 
150
 
151
- class RecursiveSolverAgent:
152
- pass
 
 
153
 
 
 
 
 
 
 
 
154
 
155
- class Solver_2:
156
 
 
157
  def __init__(self, temperature, max_tokens):
158
- print("Agent initialized.")
159
- system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "01_assistant.txt")
160
  self.system_prompt = ""
161
  with open(system_prompt_path, "r") as file:
162
  self.system_prompt = file.read().strip()
 
163
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
164
  self.agent = AgentWorkflow.from_tools_or_functions(
165
  [
166
- FunctionTool.from_defaults(self.delegate_to_math_expert),
167
- FunctionTool.from_defaults(self.set_final_answer)
 
 
 
 
 
168
  ],
169
  llm=llm
170
  )
171
  self.ctx = Context(self.agent)
172
- self.final_answer = ""
173
-
174
- async def __call__(self, question: str) -> str:
175
- print(f"Agent received question (first 50 chars): {question[:50]}...")
176
- self.final_answer = ""
177
- response = await self.query(question)
178
- print(f"Agent processed the response: {response}")
179
- if self.final_answer == "":
180
- response = await self.query("I noticed the final_answer is an empty string. Have you forgot to set the final_answer ?")
181
- return self.final_answer
182
-
183
- def get_system_prompt(self):
184
- return self.system_prompt
185
 
186
- async def query(self, question: str) -> str:
187
- response = await self.agent.run(question, ctx=self.ctx)
188
- response = str(response)
189
 
190
- final_answer = response
 
 
 
191
 
192
- self.set_final_answer(final_answer)
193
- return response
194
-
195
- def set_final_answer(self, final_answer: str) -> str:
196
  """
197
- Sets the final answer for the current querry.
198
 
199
  Args:
200
- final_answer (str): The final answer to be set for the agent.
201
 
202
  Returns:
203
- str: The final answer that was set.
204
  """
205
- print("-> set_final_answer !")
206
- self.final_answer = final_answer
207
-
208
- def delegate_to_math_expert(self, question: str) -> str:
209
- print("-> delegated to math agent !")
210
- math_agent = MathExpert(temperature=0.7, max_tokens=100)
211
- return math_agent.query(question)
212
-
213
-
214
- if __name__ == "__main__":
215
- encryption_agent = EncryptionExpert(temperature=0.7, max_tokens=2000)
216
- # encryption_query = "Descifer this: 'Bmfy bfx ymj wjxzqy gjybjjs z-hqzo fsi zsnajwxnyfyjf-hwfntaf ns fuwnq 2025 ?'"
217
- encryption_query = ".rewsna eht sa ""tfel"" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI"
218
- # print(encryption_agent.get_system_prompt())
219
- # encoding = encryption_agent.caesar_cipher_encode(encryption_query, 5)
220
- # print(encoding)
221
- # print(encryption_agent.caesar_cipher_decode(encoding, 5))
222
- print(asyncio.run(encryption_agent.query(encryption_query)))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from args import Args
11
 
12
 
13
+ class Summarizer:
14
  def __init__(self, temperature, max_tokens):
15
+ # Load the system prompt from a file
16
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "04_summarizer.txt")
17
  self.system_prompt = ""
18
  with open(system_prompt_path, "r") as file:
19
  self.system_prompt = file.read().strip()
20
+ # Define the LLM and agent
21
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
22
+ self.agent = AgentWorkflow.setup_agent(llm=llm)
 
 
 
 
 
 
23
  self.ctx = Context(self.agent)
24
 
25
+ def get_system_prompt(self) -> str:
26
+ """
27
+ Retrieves the system prompt.
28
+
29
+ Returns:
30
+ str: The system prompt string.
31
+ """
32
  return self.system_prompt
33
 
34
  async def query(self, question: str) -> str:
35
+ """
36
+ Asynchronously queries the agent with a given question and returns the response.
37
+
38
+ Args:
39
+ question (str): The question to be sent to the agent.
40
+
41
+ Returns:
42
+ str: The response from the agent as a string.
43
+ """
44
  response = await self.agent.run(question, ctx=self.ctx)
45
  response = str(response)
46
  return response
 
53
  self.ctx = Context(self.agent)
54
 
55
 
56
+ class Researcher:
57
  def __init__(self, temperature, max_tokens):
58
+ # Load the system prompt from a file
59
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "05_researcher.txt")
60
  self.system_prompt = ""
61
  with open(system_prompt_path, "r") as file:
62
  self.system_prompt = file.read().strip()
63
+ # Define the LLM and agent
64
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
65
+ self.agent = AgentWorkflow.from_tools_or_functions(
66
+ Toolbox.web_search.duck_duck_go_tools,
67
+ llm=llm
68
+ )
69
  self.ctx = Context(self.agent)
70
+
71
+ def get_system_prompt(self) -> str:
72
+ """
73
+ Retrieves the system prompt.
74
+
75
+ Returns:
76
+ str: The system prompt string.
77
+ """
78
+ return self.system_prompt
79
 
80
  async def query(self, question: str) -> str:
81
+ """
82
+ Asynchronously queries the agent with a given question and returns the response.
83
+
84
+ Args:
85
+ question (str): The question to be sent to the agent.
86
+
87
+ Returns:
88
+ str: The response from the agent as a string.
89
+ """
90
  response = await self.agent.run(question, ctx=self.ctx)
91
  response = str(response)
92
  return response
93
 
94
+ def clear_context(self):
95
+ """
96
+ Clears the current context of the agent, resetting any conversation history.
97
+ This is useful when starting a new conversation or when the context needs to be refreshed.
98
+ """
99
+ self.ctx = Context(self.agent)
100
+
101
 
102
+ class EncryptionExpert:
103
  def __init__(self, temperature, max_tokens):
104
+ # Load the system prompt from a file
105
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "06_encryption_expert.txt")
106
  self.system_prompt = ""
107
  with open(system_prompt_path, "r") as file:
108
  self.system_prompt = file.read().strip()
109
+ # Define the LLM and agent
110
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
111
  self.agent = AgentWorkflow.from_tools_or_functions(
112
  [
113
+ Toolbox.encryption.base64_encode,
114
+ Toolbox.encryption.base64_decode,
115
+ Toolbox.encryption.caesar_cipher_encode,
116
+ Toolbox.encryption.caesar_cipher_decode,
117
+ Toolbox.encryption.reverse_string
118
+ # TODO: Add more encryption tools
119
  ],
120
  llm=llm
121
  )
122
  self.ctx = Context(self.agent)
123
+ # Initialize the tool agents
124
+ self.math_expert = MathExpert(temperature, max_tokens)
125
+ self.reasoner = Reasoner(temperature, max_tokens)
126
 
127
+ def get_system_prompt(self) -> str:
128
+ """
129
+ Retrieves the system prompt.
130
+
131
+ Returns:
132
+ str: The system prompt string.
133
+ """
134
  return self.system_prompt
135
 
136
  async def query(self, question: str) -> str:
137
+ """
138
+ Asynchronously queries the agent with a given question and returns the response.
139
+
140
+ Args:
141
+ question (str): The question to be sent to the agent.
142
+
143
+ Returns:
144
+ str: The response from the agent as a string.
145
+ """
146
  response = await self.agent.run(question, ctx=self.ctx)
147
  response = str(response)
148
  return response
 
151
  """
152
  Clears the current context of the agent, resetting any conversation history.
153
  This is useful when starting a new conversation or when the context needs to be refreshed.
154
+ Also clears the context of any tool agents.
155
  """
156
  self.ctx = Context(self.agent)
157
+ # Clear context for tool agents
158
+ self.math_expert.clear_context()
159
+ self.reasoner.clear_context()
160
 
161
 
162
+ class MathExpert:
163
  def __init__(self, temperature, max_tokens):
164
+ # Load the system prompt from a file
165
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "07_math_expert.txt")
166
  self.system_prompt = ""
167
  with open(system_prompt_path, "r") as file:
168
  self.system_prompt = file.read().strip()
169
+ # Define the LLM and agent
170
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
 
171
  self.agent = AgentWorkflow.from_tools_or_functions(
172
+ [
173
+ Toolbox.math.symbolic_calc,
174
+ Toolbox.math.unit_converter,
175
+ ],
176
  llm=llm
177
  )
178
  self.ctx = Context(self.agent)
179
+ # Initialize the tool agents
180
+ self.reasoner = Reasoner(temperature, max_tokens)
181
 
182
+ def get_system_prompt(self) -> str:
183
+ """
184
+ Retrieves the system prompt.
185
+
186
+ Returns:
187
+ str: The system prompt string.
188
+ """
189
  return self.system_prompt
190
 
191
  async def query(self, question: str) -> str:
192
+ """
193
+ Asynchronously queries the agent with a given question and returns the response.
194
+
195
+ Args:
196
+ question (str): The question to be sent to the agent.
197
+
198
+ Returns:
199
+ str: The response from the agent as a string.
200
+ """
201
  response = await self.agent.run(question, ctx=self.ctx)
202
  response = str(response)
203
  return response
204
 
205
+ def clear_context(self):
206
+ """
207
+ Clears the current context of the agent, resetting any conversation history.
208
+ This is useful when starting a new conversation or when the context needs to be refreshed.
209
+ Also clears the context of any tool agents.
210
+ """
211
+ self.ctx = Context(self.agent)
212
+ self.reasoner.clear_context()
213
+
214
 
215
+ class Reasoner:
216
  def __init__(self, temperature, max_tokens):
217
+ # Load the system prompt from a file
218
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "08_reasoner.txt")
219
  self.system_prompt = ""
220
  with open(system_prompt_path, "r") as file:
221
  self.system_prompt = file.read().strip()
222
+ # Define the LLM and agent
223
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
224
+ self.agent = AgentWorkflow.setup_agent(llm=llm)
 
 
 
 
 
 
 
 
 
 
225
  self.ctx = Context(self.agent)
 
 
 
226
 
227
  async def query(self, question: str) -> str:
228
+ """
229
+ Asynchronously queries the agent with a given question and returns the response.
230
+
231
+ Args:
232
+ question (str): The question to be sent to the agent.
233
+
234
+ Returns:
235
+ str: The response from the agent as a string.
236
+ """
237
  response = await self.agent.run(question, ctx=self.ctx)
238
  response = str(response)
239
  return response
240
 
241
+ def get_system_prompt(self) -> str:
242
+ """
243
+ Retrieves the system prompt.
244
+
245
+ Returns:
246
+ str: The system prompt string.
247
+ """
248
+ return self.system_prompt
249
+
250
+ def clear_context(self):
251
+ """
252
+ Clears the current context of the agent, resetting any conversation history.
253
+ This is useful when starting a new conversation or when the context needs to be refreshed.
254
+ """
255
+ self.ctx = Context(self.agent)
256
+
257
 
258
  class ImageHandler:
259
+ def __init__(self, temperature, max_tokens):
260
+ # Load the system prompt from a file
261
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "09_image_handler.txt")
262
+ self.system_prompt = ""
263
+ with open(system_prompt_path, "r") as file:
264
+ self.system_prompt = file.read().strip()
265
+ pass
266
+
267
+ def get_system_prompt(self) -> str:
268
+ """
269
+ Retrieves the system prompt.
270
+
271
+ Returns:
272
+ str: The system prompt string.
273
+ """
274
+ return self.system_prompt
275
+
276
+ def clear_context(self):
277
+ """
278
+ Clears the current context of the agent, resetting any conversation history.
279
+ This is useful when starting a new conversation or when the context needs to be refreshed.
280
+ """
281
+ if hasattr(self, 'ctx') and hasattr(self, 'agent'):
282
+ self.ctx = Context(self.agent)
283
+
284
 
285
  class VideoHandler:
286
+ def __init__(self, temperature, max_tokens):
287
+ # Load the system prompt from a file
288
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "10_video_handler.txt")
289
+ self.system_prompt = ""
290
+ with open(system_prompt_path, "r") as file:
291
+ self.system_prompt = file.read().strip()
292
+ # No implementation yet
293
+ pass
294
+
295
+ def get_system_prompt(self) -> str:
296
+ """
297
+ Retrieves the system prompt.
298
 
299
+ Returns:
300
+ str: The system prompt string.
301
+ """
302
+ return self.system_prompt
303
 
304
+ def clear_context(self):
305
+ """
306
+ Clears the current context of the agent, resetting any conversation history.
307
+ This is useful when starting a new conversation or when the context needs to be refreshed.
308
+ """
309
+ if hasattr(self, 'ctx') and hasattr(self, 'agent'):
310
+ self.ctx = Context(self.agent)
311
 
 
312
 
313
+ class Solver:
314
  def __init__(self, temperature, max_tokens):
315
+ # Load the system prompt from a file
316
+ system_prompt_path = os.path.join(os.getcwd(), "system_prompts", "03_solver.txt")
317
  self.system_prompt = ""
318
  with open(system_prompt_path, "r") as file:
319
  self.system_prompt = file.read().strip()
320
+ # Define the LLM and agent
321
  llm = LLMFactory.create(Args.primary_llm_interface, self.system_prompt, temperature, max_tokens)
322
  self.agent = AgentWorkflow.from_tools_or_functions(
323
  [
324
+ self.call_summarizer,
325
+ self.call_researcher,
326
+ self.call_encryption_expert,
327
+ self.call_math_expert,
328
+ self.call_reasoner,
329
+ self.call_image_handler,
330
+ self.call_video_handler
331
  ],
332
  llm=llm
333
  )
334
  self.ctx = Context(self.agent)
335
+ # Initialize the tool agents
336
+ self.summarizer = Summarizer(temperature, max_tokens)
337
+ self.researcher = Researcher(temperature, max_tokens)
338
+ self.encryption_expert = EncryptionExpert(temperature, max_tokens)
339
+ self.math_expert = MathExpert(temperature, max_tokens)
340
+ self.reasoner = Reasoner(temperature, max_tokens)
341
+ self.image_handler = ImageHandler(temperature, max_tokens)
342
+ self.video_handler = VideoHandler(temperature, max_tokens)
 
 
 
 
 
343
 
344
+ def get_system_prompt(self) -> str:
345
+ """
346
+ Retrieves the system prompt.
347
 
348
+ Returns:
349
+ str: The system prompt string.
350
+ """
351
+ return self.system_prompt
352
 
353
+ async def query(self, question: str) -> str:
 
 
 
354
  """
355
+ Asynchronously queries the agent with a given question and returns the response.
356
 
357
  Args:
358
+ question (str): The question to be sent to the agent.
359
 
360
  Returns:
361
+ str: The response from the agent as a string.
362
  """
363
+ response = await self.agent.run(question, ctx=self.ctx)
364
+ response = str(response)
365
+ return response
366
+
367
+ def clear_context(self):
368
+ """
369
+ Clears the current context of the agent, resetting any conversation history.
370
+ This is useful when starting a new conversation or when the context needs to be refreshed.
371
+ Also clears the context of all tool agents.
372
+ """
373
+ self.ctx = Context(self.agent)
374
+ # Clear context for all tool agents
375
+ self.summarizer.clear_context()
376
+ self.researcher.clear_context()
377
+ self.encryption_expert.clear_context()
378
+ self.math_expert.clear_context()
379
+ self.reasoner.clear_context()
380
+ self.image_handler.clear_context()
381
+ self.video_handler.clear_context()
382
+
383
+ async def call_summarizer(self, question: str) -> str:
384
+ return await self.summarizer.query(question)
385
+
386
+ async def call_researcher(self, question: str) -> str:
387
+ return await self.researcher.query(question)
388
+
389
+ async def call_encryption_expert(self, question: str) -> str:
390
+ return await self.encryption_expert.query(question)
391
+
392
+ async def call_math_expert(self, question: str) -> str:
393
+ return await self.math_expert.query(question)
394
+
395
+ async def call_reasoner(self, question: str) -> str:
396
+ return await self.reasoner.query(question)
397
+
398
+ async def call_image_handler(self, question: str) -> str:
399
+ # ImageHandler may not have a query method yet, but following the pattern
400
+ if hasattr(self.image_handler, 'query'):
401
+ return await self.image_handler.query(question)
402
+ return "Image handling is not implemented yet."
403
+ # TODO
404
+
405
+ async def call_video_handler(self, question: str) -> str:
406
+ # VideoHandler may not have a query method yet, but following the pattern
407
+ if hasattr(self.video_handler, 'query'):
408
+ return await self.video_handler.query(question)
409
+ return "Video handling is not implemented yet."
410
+ # TODO
411
+
412
+
413
+ # if __name__ == "__main__":
414
+ # pass