292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656 | class WiseAgentContext():
from typing import List
''' A WiseAgentContext is a class that represents a context in which agents can communicate with each other.
'''
_message_trace : List[WiseAgentMessage] = []
_participants : List[WiseAgent] = []
# Maps a chat uuid to a list of chat completion messages
_llm_chat_completion : Dict[str, List[ChatCompletionMessageParam]] = {}
# Maps a chat uuid to a list of tool names that need to be executed
_llm_required_tool_call : Dict[str, List[str]] = {}
# Maps a chat uuid to a list of available tools in chat
_llm_available_tools_in_chat : Dict[str, List[ChatCompletionToolParam]] = {}
# Maps a chat uuid to a list of agent names that need to be executed in sequence
# Used by a sequential coordinator
_agents_sequence : Dict[str, List[str]] = {}
# Maps a chat uuid to the agent where the final response should be routed to
# Used by both a sequential coordinator and a phased coordinator
_route_response_to : Dict[str, str] = {}
# Maps a chat uuid to a list that contains a list of agent names to be executed for each phase
# Used by a phased coordinator
_agent_phase_assignments : Dict[str, List[List[str]]] = {}
# Maps a chat uuid to the current phase. Used by a phased coordinator.
_current_phase : Dict[str, int] = {}
# Maps a chat uuid to a list of agent names that need to be executed for the current phase
# Used by a phased coordinator
_required_agents_for_current_phase : Dict[str, List[str]] = {}
# Maps a chat uuid to a list containing the queries attempted for each iteration executed by
# the phased coordinator
_queries : Dict[str, List[str]] = {}
def __init__(self, name: str):
''' Initialize the context with the given name.
Args:
name (str): the name of the context'''
self._name = name
WiseAgentRegistry.register_context(self)
@property
def name(self) -> str:
"""Get the name of the context."""
return self._name
@property
def message_trace(self) -> List[WiseAgentMessage]:
"""Get the message trace of the context."""
return self._message_trace
@property
def participants(self) -> List[WiseAgent]:
"""Get the participants of the context."""
return self._participants
@property
def llm_chat_completion(self) -> Dict[str, List[ChatCompletionMessageParam]]:
"""Get the LLM chat completion of the context."""
return self._llm_chat_completion
def add_participant(self, agent: WiseAgent):
'''Add a participant to the context.
Args:
agent (WiseAgent): the agent to add'''
if agent not in self._participants:
self._participants.append(agent)
def append_chat_completion(self, chat_uuid: str, messages: Iterable[ChatCompletionMessageParam]):
'''Append chat completion to the context.
Args:
chat_uuid (str): the chat uuid
messages (Iterable[ChatCompletionMessageParam]): the messages to append'''
if chat_uuid not in self._llm_chat_completion:
self._llm_chat_completion[chat_uuid] = []
self._llm_chat_completion[chat_uuid].append(messages)
@property
def llm_required_tool_call(self) -> Dict[str, List[str]]:
"""Get the LLM required tool call of the context.
return Dict[str, List[str]]"""
return self._llm_required_tool_call
def append_required_tool_call(self, chat_uuid: str, tool_name: str):
'''Append required tool call to the context.
Args:
chat_uuid (str): the chat uuid
tool_name (str): the tool name to append'''
if chat_uuid not in self._llm_required_tool_call:
self._llm_required_tool_call[chat_uuid] = []
self._llm_required_tool_call[chat_uuid].append(tool_name)
def remove_required_tool_call(self, chat_uuid: str, tool_name: str):
'''Remove required tool call from the context.
Args:
chat_uuid (str): the chat uuid
tool_name (str): the tool name to remove'''
if chat_uuid in self._llm_required_tool_call:
self._llm_required_tool_call[chat_uuid].remove(tool_name)
if len(self._llm_required_tool_call[chat_uuid]) == 0:
self._llm_required_tool_call.pop(chat_uuid)
def get_required_tool_calls(self, chat_uuid: str) -> List[str]:
'''Get required tool calls from the context.
Args:
chat_uuid (str): the chat uuid
return List[str]'''
if chat_uuid in self._llm_required_tool_call:
return self._llm_required_tool_call[chat_uuid]
else:
return []
@property
def llm_available_tools_in_chat(self) -> Dict[str, List[ChatCompletionToolParam]]:
"""Get the LLM available tools in chat of the context."""
return self._llm_available_tools_in_chat
def append_available_tool_in_chat(self, chat_uuid: str, tools: Iterable[ChatCompletionToolParam]):
'''Append available tool in chat to the context.
Args:
chat_uuid (str): the chat uuid
tools (Iterable[ChatCompletionToolParam]): the tools to append'''
if chat_uuid not in self._llm_available_tools_in_chat:
self._llm_available_tools_in_chat[chat_uuid] = []
self._llm_available_tools_in_chat[chat_uuid].append(tools)
def get_available_tools_in_chat(self, chat_uuid: str) -> List[ChatCompletionToolParam]:
'''Get available tools in chat from the context.
Args:
chat_uuid (str): the chat uuid
return List[ChatCompletionToolParam]'''
if chat_uuid in self._llm_available_tools_in_chat:
return self._llm_available_tools_in_chat[chat_uuid]
else:
return []
def get_agents_sequence(self, chat_uuid: str) -> List[str]:
"""
Get the sequence of agents for the given chat uuid for this context. This is used by a sequential
coordinator to execute its agents in a specific order, passing the output from one agent in the sequence
to the next agent in the sequence.
Args:
chat_uuid (str): the chat uuid
Returns:
List[str]: the sequence of agents names or an empty list if no sequence has been set for this context
"""
if chat_uuid in self._agents_sequence:
return self._agents_sequence[chat_uuid]
return []
def set_agents_sequence(self, chat_uuid: str, agents_sequence: List[str]):
"""
Set the sequence of agents for the given chat uuid for this context. This is used by
a sequential coordinator to execute its agents in a specific order, passing the output
from one agent in the sequence to the next agent in the sequence.
Args:
chat_uuid (str): the chat uuid
agents_sequence (List[str]): the sequence of agent names
"""
self._agents_sequence[chat_uuid] = agents_sequence
def get_route_response_to(self, chat_uuid: str) -> Optional[str]:
"""
Get the name of the agent where the final response should be routed to for the given chat uuid for this
context. This is used by a sequential coordinator and a phased coordinator.
Returns:
Optional[str]: the name of the agent where the final response should be routed to or None if no agent is set
"""
if chat_uuid in self._route_response_to:
return self._route_response_to[chat_uuid]
else:
return None
def set_route_response_to(self, chat_uuid: str, agent: str):
"""
Set the name of the agent where the final response should be routed to for the given chat uuid for this
context. This is used by a sequential coordinator and a phased coordinator.
Args:
chat_uuid (str): the chat uuid
agent (str): the name of the agent where the final response should be routed to
"""
self._route_response_to[chat_uuid] = agent
def get_next_agent_in_sequence(self, chat_uuid: str, current_agent: str):
"""
Get the name of the next agent in the sequence of agents for the given chat uuid for this context.
This is used by a sequential coordinator to determine the name of the next agent to execute.
Args:
chat_uuid (str): the chat uuid
current_agent (str): the name of the current agent
Returns:
str: the name of the next agent in the sequence after the current agent or None if there are no remaining
agents in the sequence after the current agent
"""
agents_sequence = self.get_agents_sequence(chat_uuid)
if current_agent in agents_sequence:
current_agent_index = agents_sequence.index(current_agent)
next_agent_index = current_agent_index + 1
if next_agent_index < len(agents_sequence):
return agents_sequence[next_agent_index]
return None
def get_agent_phase_assignments(self, chat_uuid: str) -> List[List[str]]:
"""
Get the agents to be executed in each phase for the given chat uuid for this context. This is used
by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
Returns:
List[List[str]]: The agents to be executed in each phase, represented as a list of lists, where the
size of the outer list corresponds to the number of phases and each element in the list is a list of
agent names for that phase. An empty list is returned if no phases have been set for the
given chat uuid
"""
if chat_uuid in self._agent_phase_assignments:
return self._agent_phase_assignments.get(chat_uuid)
return []
def set_agent_phase_assignments(self, chat_uuid: str, agent_phase_assignments: List[List[str]]):
"""
Set the agents to be executed in each phase for the given chat uuid for this context. This is used
by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
agent_phase_assignments (List[List[str]]): The agents to be executed in each phase, represented as a
list of lists, where the size of the outer list corresponds to the number of phases and each element
in the list is a list of agent names for that phase.
"""
self._agent_phase_assignments[chat_uuid] = agent_phase_assignments
def get_current_phase(self, chat_uuid: str) -> int:
"""
Get the current phase for the given chat uuid for this context. This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
Returns:
int: the current phase, represented as an integer in the zero-indexed list of phases
"""
return self._current_phase.get(chat_uuid)
def set_current_phase(self, chat_uuid: str, phase: int):
"""
Set the current phase for the given chat uuid for this context. This method also
sets the required agents for the current phase. This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
phase (int): the current phase, represented as an integer in the zero-indexed list of phases
"""
self._current_phase[chat_uuid] = phase
self._required_agents_for_current_phase[chat_uuid] = copy.deepcopy(self._agent_phase_assignments[chat_uuid][phase])
def get_agents_for_next_phase(self, chat_uuid: str) -> Optional[List]:
"""
Get the list of agents to be executed for the next phase for the given chat uuid for this context.
This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
Returns:
Optional[List[str]]: the list of agent names for the next phase or None if there are no more phases
"""
current_phase = self.get_current_phase(chat_uuid)
next_phase = current_phase + 1
if next_phase < len(self._agent_phase_assignments[chat_uuid]):
self.set_current_phase(chat_uuid, next_phase)
return self._agent_phase_assignments[chat_uuid][next_phase]
return None
def get_required_agents_for_current_phase(self, chat_uuid: str) -> List[str]:
"""
Get the list of agents that still need to be executed for the current phase for the given chat uuid for this
context. This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
Returns:
List[str]: the list of agent names that still need to be executed for the current phase or an empty list
if there are no remaining agents that need to be executed for the current phase
"""
if chat_uuid in self._required_agents_for_current_phase:
return self._required_agents_for_current_phase.get(chat_uuid)
return []
def remove_required_agent_for_current_phase(self, chat_uuid: str, agent_name: str):
"""
Remove the given agent from the list of required agents for the current phase for the given chat uuid for this
context. This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
agent_name (str): the name of the agent to remove
"""
if chat_uuid in self._required_agents_for_current_phase:
self._required_agents_for_current_phase.get(chat_uuid).remove(agent_name)
def get_current_query(self, chat_uuid: str) -> Optional[str]:
"""
Get the current query for the given chat uuid for this context. This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
Returns:
Optional[str]: the current query or None if there is no current query
"""
if chat_uuid in self._queries:
if self._queries.get(chat_uuid):
# return the last query
return self._queries.get(chat_uuid)[-1]
else:
return None
def add_query(self, chat_uuid: str, query: str):
"""
Add the current query for the given chat uuid for this context. This is used by a phased coordinator.
Args:
chat_uuid (str): the chat uuid
query (str): the current query
"""
if chat_uuid not in self._queries:
self._queries[chat_uuid] = []
self._queries[chat_uuid].append(query)
def get_queries(self, chat_uuid: str) -> List[str]:
"""
Get the queries attempted for the given chat uuid for this context. This is used by a phased coordinator.
Returns:
List[str]: the queries attempted for the given chat uuid for this context
"""
if chat_uuid in self._queries:
return self._queries.get(chat_uuid)
else:
return []
|