ag2 0.4.1__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ag2 might be problematic. Click here for more details.

Files changed (160) hide show
  1. {ag2-0.4.1.dist-info → ag2-0.5.0.dist-info}/METADATA +5 -146
  2. ag2-0.5.0.dist-info/RECORD +6 -0
  3. ag2-0.5.0.dist-info/top_level.txt +1 -0
  4. ag2-0.4.1.dist-info/RECORD +0 -158
  5. ag2-0.4.1.dist-info/top_level.txt +0 -1
  6. autogen/__init__.py +0 -17
  7. autogen/_pydantic.py +0 -116
  8. autogen/agentchat/__init__.py +0 -42
  9. autogen/agentchat/agent.py +0 -142
  10. autogen/agentchat/assistant_agent.py +0 -85
  11. autogen/agentchat/chat.py +0 -306
  12. autogen/agentchat/contrib/__init__.py +0 -0
  13. autogen/agentchat/contrib/agent_builder.py +0 -788
  14. autogen/agentchat/contrib/agent_eval/agent_eval.py +0 -107
  15. autogen/agentchat/contrib/agent_eval/criterion.py +0 -47
  16. autogen/agentchat/contrib/agent_eval/critic_agent.py +0 -47
  17. autogen/agentchat/contrib/agent_eval/quantifier_agent.py +0 -42
  18. autogen/agentchat/contrib/agent_eval/subcritic_agent.py +0 -48
  19. autogen/agentchat/contrib/agent_eval/task.py +0 -43
  20. autogen/agentchat/contrib/agent_optimizer.py +0 -450
  21. autogen/agentchat/contrib/capabilities/__init__.py +0 -0
  22. autogen/agentchat/contrib/capabilities/agent_capability.py +0 -21
  23. autogen/agentchat/contrib/capabilities/generate_images.py +0 -297
  24. autogen/agentchat/contrib/capabilities/teachability.py +0 -406
  25. autogen/agentchat/contrib/capabilities/text_compressors.py +0 -72
  26. autogen/agentchat/contrib/capabilities/transform_messages.py +0 -92
  27. autogen/agentchat/contrib/capabilities/transforms.py +0 -565
  28. autogen/agentchat/contrib/capabilities/transforms_util.py +0 -120
  29. autogen/agentchat/contrib/capabilities/vision_capability.py +0 -217
  30. autogen/agentchat/contrib/captainagent/tools/__init__.py +0 -0
  31. autogen/agentchat/contrib/captainagent/tools/data_analysis/calculate_correlation.py +0 -41
  32. autogen/agentchat/contrib/captainagent/tools/data_analysis/calculate_skewness_and_kurtosis.py +0 -29
  33. autogen/agentchat/contrib/captainagent/tools/data_analysis/detect_outlier_iqr.py +0 -29
  34. autogen/agentchat/contrib/captainagent/tools/data_analysis/detect_outlier_zscore.py +0 -29
  35. autogen/agentchat/contrib/captainagent/tools/data_analysis/explore_csv.py +0 -22
  36. autogen/agentchat/contrib/captainagent/tools/data_analysis/shapiro_wilk_test.py +0 -31
  37. autogen/agentchat/contrib/captainagent/tools/information_retrieval/arxiv_download.py +0 -26
  38. autogen/agentchat/contrib/captainagent/tools/information_retrieval/arxiv_search.py +0 -55
  39. autogen/agentchat/contrib/captainagent/tools/information_retrieval/extract_pdf_image.py +0 -54
  40. autogen/agentchat/contrib/captainagent/tools/information_retrieval/extract_pdf_text.py +0 -39
  41. autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_wikipedia_text.py +0 -22
  42. autogen/agentchat/contrib/captainagent/tools/information_retrieval/get_youtube_caption.py +0 -35
  43. autogen/agentchat/contrib/captainagent/tools/information_retrieval/image_qa.py +0 -61
  44. autogen/agentchat/contrib/captainagent/tools/information_retrieval/optical_character_recognition.py +0 -62
  45. autogen/agentchat/contrib/captainagent/tools/information_retrieval/perform_web_search.py +0 -48
  46. autogen/agentchat/contrib/captainagent/tools/information_retrieval/scrape_wikipedia_tables.py +0 -34
  47. autogen/agentchat/contrib/captainagent/tools/information_retrieval/transcribe_audio_file.py +0 -22
  48. autogen/agentchat/contrib/captainagent/tools/information_retrieval/youtube_download.py +0 -36
  49. autogen/agentchat/contrib/captainagent/tools/math/calculate_circle_area_from_diameter.py +0 -22
  50. autogen/agentchat/contrib/captainagent/tools/math/calculate_day_of_the_week.py +0 -19
  51. autogen/agentchat/contrib/captainagent/tools/math/calculate_fraction_sum.py +0 -29
  52. autogen/agentchat/contrib/captainagent/tools/math/calculate_matrix_power.py +0 -32
  53. autogen/agentchat/contrib/captainagent/tools/math/calculate_reflected_point.py +0 -17
  54. autogen/agentchat/contrib/captainagent/tools/math/complex_numbers_product.py +0 -26
  55. autogen/agentchat/contrib/captainagent/tools/math/compute_currency_conversion.py +0 -24
  56. autogen/agentchat/contrib/captainagent/tools/math/count_distinct_permutations.py +0 -28
  57. autogen/agentchat/contrib/captainagent/tools/math/evaluate_expression.py +0 -29
  58. autogen/agentchat/contrib/captainagent/tools/math/find_continuity_point.py +0 -35
  59. autogen/agentchat/contrib/captainagent/tools/math/fraction_to_mixed_numbers.py +0 -40
  60. autogen/agentchat/contrib/captainagent/tools/math/modular_inverse_sum.py +0 -23
  61. autogen/agentchat/contrib/captainagent/tools/math/simplify_mixed_numbers.py +0 -37
  62. autogen/agentchat/contrib/captainagent/tools/math/sum_of_digit_factorials.py +0 -16
  63. autogen/agentchat/contrib/captainagent/tools/math/sum_of_primes_below.py +0 -16
  64. autogen/agentchat/contrib/captainagent/tools/requirements.txt +0 -10
  65. autogen/agentchat/contrib/captainagent/tools/tool_description.tsv +0 -34
  66. autogen/agentchat/contrib/captainagent.py +0 -490
  67. autogen/agentchat/contrib/gpt_assistant_agent.py +0 -545
  68. autogen/agentchat/contrib/graph_rag/__init__.py +0 -0
  69. autogen/agentchat/contrib/graph_rag/document.py +0 -30
  70. autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +0 -111
  71. autogen/agentchat/contrib/graph_rag/falkor_graph_rag_capability.py +0 -81
  72. autogen/agentchat/contrib/graph_rag/graph_query_engine.py +0 -56
  73. autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +0 -64
  74. autogen/agentchat/contrib/img_utils.py +0 -390
  75. autogen/agentchat/contrib/llamaindex_conversable_agent.py +0 -123
  76. autogen/agentchat/contrib/llava_agent.py +0 -176
  77. autogen/agentchat/contrib/math_user_proxy_agent.py +0 -471
  78. autogen/agentchat/contrib/multimodal_conversable_agent.py +0 -128
  79. autogen/agentchat/contrib/qdrant_retrieve_user_proxy_agent.py +0 -325
  80. autogen/agentchat/contrib/retrieve_assistant_agent.py +0 -56
  81. autogen/agentchat/contrib/retrieve_user_proxy_agent.py +0 -705
  82. autogen/agentchat/contrib/society_of_mind_agent.py +0 -203
  83. autogen/agentchat/contrib/swarm_agent.py +0 -463
  84. autogen/agentchat/contrib/text_analyzer_agent.py +0 -76
  85. autogen/agentchat/contrib/tool_retriever.py +0 -120
  86. autogen/agentchat/contrib/vectordb/__init__.py +0 -0
  87. autogen/agentchat/contrib/vectordb/base.py +0 -243
  88. autogen/agentchat/contrib/vectordb/chromadb.py +0 -326
  89. autogen/agentchat/contrib/vectordb/mongodb.py +0 -559
  90. autogen/agentchat/contrib/vectordb/pgvectordb.py +0 -958
  91. autogen/agentchat/contrib/vectordb/qdrant.py +0 -334
  92. autogen/agentchat/contrib/vectordb/utils.py +0 -126
  93. autogen/agentchat/contrib/web_surfer.py +0 -305
  94. autogen/agentchat/conversable_agent.py +0 -2908
  95. autogen/agentchat/groupchat.py +0 -1668
  96. autogen/agentchat/user_proxy_agent.py +0 -109
  97. autogen/agentchat/utils.py +0 -207
  98. autogen/browser_utils.py +0 -291
  99. autogen/cache/__init__.py +0 -10
  100. autogen/cache/abstract_cache_base.py +0 -78
  101. autogen/cache/cache.py +0 -182
  102. autogen/cache/cache_factory.py +0 -85
  103. autogen/cache/cosmos_db_cache.py +0 -150
  104. autogen/cache/disk_cache.py +0 -109
  105. autogen/cache/in_memory_cache.py +0 -61
  106. autogen/cache/redis_cache.py +0 -128
  107. autogen/code_utils.py +0 -745
  108. autogen/coding/__init__.py +0 -22
  109. autogen/coding/base.py +0 -113
  110. autogen/coding/docker_commandline_code_executor.py +0 -262
  111. autogen/coding/factory.py +0 -45
  112. autogen/coding/func_with_reqs.py +0 -203
  113. autogen/coding/jupyter/__init__.py +0 -22
  114. autogen/coding/jupyter/base.py +0 -32
  115. autogen/coding/jupyter/docker_jupyter_server.py +0 -164
  116. autogen/coding/jupyter/embedded_ipython_code_executor.py +0 -182
  117. autogen/coding/jupyter/jupyter_client.py +0 -224
  118. autogen/coding/jupyter/jupyter_code_executor.py +0 -161
  119. autogen/coding/jupyter/local_jupyter_server.py +0 -168
  120. autogen/coding/local_commandline_code_executor.py +0 -410
  121. autogen/coding/markdown_code_extractor.py +0 -44
  122. autogen/coding/utils.py +0 -57
  123. autogen/exception_utils.py +0 -46
  124. autogen/extensions/__init__.py +0 -0
  125. autogen/formatting_utils.py +0 -76
  126. autogen/function_utils.py +0 -362
  127. autogen/graph_utils.py +0 -148
  128. autogen/io/__init__.py +0 -15
  129. autogen/io/base.py +0 -105
  130. autogen/io/console.py +0 -43
  131. autogen/io/websockets.py +0 -213
  132. autogen/logger/__init__.py +0 -11
  133. autogen/logger/base_logger.py +0 -140
  134. autogen/logger/file_logger.py +0 -287
  135. autogen/logger/logger_factory.py +0 -29
  136. autogen/logger/logger_utils.py +0 -42
  137. autogen/logger/sqlite_logger.py +0 -459
  138. autogen/math_utils.py +0 -356
  139. autogen/oai/__init__.py +0 -33
  140. autogen/oai/anthropic.py +0 -428
  141. autogen/oai/bedrock.py +0 -606
  142. autogen/oai/cerebras.py +0 -270
  143. autogen/oai/client.py +0 -1148
  144. autogen/oai/client_utils.py +0 -167
  145. autogen/oai/cohere.py +0 -453
  146. autogen/oai/completion.py +0 -1216
  147. autogen/oai/gemini.py +0 -469
  148. autogen/oai/groq.py +0 -281
  149. autogen/oai/mistral.py +0 -279
  150. autogen/oai/ollama.py +0 -582
  151. autogen/oai/openai_utils.py +0 -811
  152. autogen/oai/together.py +0 -343
  153. autogen/retrieve_utils.py +0 -487
  154. autogen/runtime_logging.py +0 -163
  155. autogen/token_count_utils.py +0 -259
  156. autogen/types.py +0 -20
  157. autogen/version.py +0 -7
  158. {ag2-0.4.1.dist-info → ag2-0.5.0.dist-info}/LICENSE +0 -0
  159. {ag2-0.4.1.dist-info → ag2-0.5.0.dist-info}/NOTICE.md +0 -0
  160. {ag2-0.4.1.dist-info → ag2-0.5.0.dist-info}/WHEEL +0 -0
@@ -1,142 +0,0 @@
1
- # Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
2
- #
3
- # SPDX-License-Identifier: Apache-2.0
4
- #
5
- # Portions derived from https://github.com/microsoft/autogen are under the MIT License.
6
- # SPDX-License-Identifier: MIT
7
- from typing import Any, Dict, List, Optional, Protocol, Union, runtime_checkable
8
-
9
-
10
- @runtime_checkable
11
- class Agent(Protocol):
12
- """(In preview) A protocol for Agent.
13
-
14
- An agent can communicate with other agents and perform actions.
15
- Different agents can differ in what actions they perform in the `receive` method.
16
- """
17
-
18
- @property
19
- def name(self) -> str:
20
- """The name of the agent."""
21
- ...
22
-
23
- @property
24
- def description(self) -> str:
25
- """The description of the agent. Used for the agent's introduction in
26
- a group chat setting."""
27
- ...
28
-
29
- def send(
30
- self,
31
- message: Union[Dict[str, Any], str],
32
- recipient: "Agent",
33
- request_reply: Optional[bool] = None,
34
- ) -> None:
35
- """Send a message to another agent.
36
-
37
- Args:
38
- message (dict or str): the message to send. If a dict, it should be
39
- a JSON-serializable and follows the OpenAI's ChatCompletion schema.
40
- recipient (Agent): the recipient of the message.
41
- request_reply (bool): whether to request a reply from the recipient.
42
- """
43
- ...
44
-
45
- async def a_send(
46
- self,
47
- message: Union[Dict[str, Any], str],
48
- recipient: "Agent",
49
- request_reply: Optional[bool] = None,
50
- ) -> None:
51
- """(Async) Send a message to another agent.
52
-
53
- Args:
54
- message (dict or str): the message to send. If a dict, it should be
55
- a JSON-serializable and follows the OpenAI's ChatCompletion schema.
56
- recipient (Agent): the recipient of the message.
57
- request_reply (bool): whether to request a reply from the recipient.
58
- """
59
- ...
60
-
61
- def receive(
62
- self,
63
- message: Union[Dict[str, Any], str],
64
- sender: "Agent",
65
- request_reply: Optional[bool] = None,
66
- ) -> None:
67
- """Receive a message from another agent.
68
-
69
- Args:
70
- message (dict or str): the message received. If a dict, it should be
71
- a JSON-serializable and follows the OpenAI's ChatCompletion schema.
72
- sender (Agent): the sender of the message.
73
- request_reply (bool): whether the sender requests a reply.
74
- """
75
-
76
- async def a_receive(
77
- self,
78
- message: Union[Dict[str, Any], str],
79
- sender: "Agent",
80
- request_reply: Optional[bool] = None,
81
- ) -> None:
82
- """(Async) Receive a message from another agent.
83
-
84
- Args:
85
- message (dict or str): the message received. If a dict, it should be
86
- a JSON-serializable and follows the OpenAI's ChatCompletion schema.
87
- sender (Agent): the sender of the message.
88
- request_reply (bool): whether the sender requests a reply.
89
- """
90
- ...
91
-
92
- def generate_reply(
93
- self,
94
- messages: Optional[List[Dict[str, Any]]] = None,
95
- sender: Optional["Agent"] = None,
96
- **kwargs: Any,
97
- ) -> Union[str, Dict[str, Any], None]:
98
- """Generate a reply based on the received messages.
99
-
100
- Args:
101
- messages (list[dict]): a list of messages received from other agents.
102
- The messages are dictionaries that are JSON-serializable and
103
- follows the OpenAI's ChatCompletion schema.
104
- sender: sender of an Agent instance.
105
-
106
- Returns:
107
- str or dict or None: the generated reply. If None, no reply is generated.
108
- """
109
-
110
- async def a_generate_reply(
111
- self,
112
- messages: Optional[List[Dict[str, Any]]] = None,
113
- sender: Optional["Agent"] = None,
114
- **kwargs: Any,
115
- ) -> Union[str, Dict[str, Any], None]:
116
- """(Async) Generate a reply based on the received messages.
117
-
118
- Args:
119
- messages (list[dict]): a list of messages received from other agents.
120
- The messages are dictionaries that are JSON-serializable and
121
- follows the OpenAI's ChatCompletion schema.
122
- sender: sender of an Agent instance.
123
-
124
- Returns:
125
- str or dict or None: the generated reply. If None, no reply is generated.
126
- """
127
-
128
-
129
- @runtime_checkable
130
- class LLMAgent(Agent, Protocol):
131
- """(In preview) A protocol for an LLM agent."""
132
-
133
- @property
134
- def system_message(self) -> str:
135
- """The system message of this agent."""
136
-
137
- def update_system_message(self, system_message: str) -> None:
138
- """Update this agent's system message.
139
-
140
- Args:
141
- system_message (str): system message for inference.
142
- """
@@ -1,85 +0,0 @@
1
- # Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
2
- #
3
- # SPDX-License-Identifier: Apache-2.0
4
- #
5
- # Portions derived from https://github.com/microsoft/autogen are under the MIT License.
6
- # SPDX-License-Identifier: MIT
7
- from typing import Callable, Dict, Literal, Optional, Union
8
-
9
- from autogen.runtime_logging import log_new_agent, logging_enabled
10
-
11
- from .conversable_agent import ConversableAgent
12
-
13
-
14
- class AssistantAgent(ConversableAgent):
15
- """(In preview) Assistant agent, designed to solve a task with LLM.
16
-
17
- AssistantAgent is a subclass of ConversableAgent configured with a default system message.
18
- The default system message is designed to solve a task with LLM,
19
- including suggesting python code blocks and debugging.
20
- `human_input_mode` is default to "NEVER"
21
- and `code_execution_config` is default to False.
22
- This agent doesn't execute code by default, and expects the user to execute the code.
23
- """
24
-
25
- DEFAULT_SYSTEM_MESSAGE = """You are a helpful AI assistant.
26
- Solve tasks using your coding and language skills.
27
- In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute.
28
- 1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time, check the operating system. After sufficient info is printed and the task is ready to be solved based on your language skill, you can solve the task by yourself.
29
- 2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly.
30
- Solve the task step by step if you need to. If a plan is not provided, explain your plan first. Be clear which step uses code, and which step uses your language skill.
31
- When using code, you must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
32
- If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
33
- If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
34
- When you find an answer, verify the answer carefully. Include verifiable evidence in your response if possible.
35
- Reply "TERMINATE" in the end when everything is done.
36
- """
37
-
38
- DEFAULT_DESCRIPTION = "A helpful and general-purpose AI assistant that has strong language skills, Python skills, and Linux command line skills."
39
-
40
- def __init__(
41
- self,
42
- name: str,
43
- system_message: Optional[str] = DEFAULT_SYSTEM_MESSAGE,
44
- llm_config: Optional[Union[Dict, Literal[False]]] = None,
45
- is_termination_msg: Optional[Callable[[Dict], bool]] = None,
46
- max_consecutive_auto_reply: Optional[int] = None,
47
- human_input_mode: Literal["ALWAYS", "NEVER", "TERMINATE"] = "NEVER",
48
- description: Optional[str] = None,
49
- **kwargs,
50
- ):
51
- """
52
- Args:
53
- name (str): agent name.
54
- system_message (str): system message for the ChatCompletion inference.
55
- Please override this attribute if you want to reprogram the agent.
56
- llm_config (dict or False or None): llm inference configuration.
57
- Please refer to [OpenAIWrapper.create](/docs/reference/oai/client#create)
58
- for available options.
59
- is_termination_msg (function): a function that takes a message in the form of a dictionary
60
- and returns a boolean value indicating if this received message is a termination message.
61
- The dict can contain the following keys: "content", "role", "name", "function_call".
62
- max_consecutive_auto_reply (int): the maximum number of consecutive auto replies.
63
- default to None (no limit provided, class attribute MAX_CONSECUTIVE_AUTO_REPLY will be used as the limit in this case).
64
- The limit only plays a role when human_input_mode is not "ALWAYS".
65
- **kwargs (dict): Please refer to other kwargs in
66
- [ConversableAgent](conversable_agent#__init__).
67
- """
68
- super().__init__(
69
- name,
70
- system_message,
71
- is_termination_msg,
72
- max_consecutive_auto_reply,
73
- human_input_mode,
74
- llm_config=llm_config,
75
- description=description,
76
- **kwargs,
77
- )
78
- if logging_enabled():
79
- log_new_agent(self, locals())
80
-
81
- # Update the provided description if None, and we are using the default system_message,
82
- # then use the default description.
83
- if description is None:
84
- if system_message == self.DEFAULT_SYSTEM_MESSAGE:
85
- self.description = self.DEFAULT_DESCRIPTION
autogen/agentchat/chat.py DELETED
@@ -1,306 +0,0 @@
1
- # Copyright (c) 2023 - 2024, Owners of https://github.com/ag2ai
2
- #
3
- # SPDX-License-Identifier: Apache-2.0
4
- #
5
- # Portions derived from https://github.com/microsoft/autogen are under the MIT License.
6
- # SPDX-License-Identifier: MIT
7
- import asyncio
8
- import datetime
9
- import logging
10
- import warnings
11
- from collections import abc, defaultdict
12
- from dataclasses import dataclass
13
- from functools import partial
14
- from typing import Any, Dict, List, Set, Tuple
15
-
16
- from ..formatting_utils import colored
17
- from ..io.base import IOStream
18
- from .utils import consolidate_chat_info
19
-
20
- logger = logging.getLogger(__name__)
21
- Prerequisite = Tuple[int, int]
22
-
23
-
24
- @dataclass
25
- class ChatResult:
26
- """(Experimental) The result of a chat. Almost certain to be changed."""
27
-
28
- chat_id: int = None
29
- """chat id"""
30
- chat_history: List[Dict[str, Any]] = None
31
- """The chat history."""
32
- summary: str = None
33
- """A summary obtained from the chat."""
34
- cost: Dict[str, dict] = None # keys: "usage_including_cached_inference", "usage_excluding_cached_inference"
35
- """The cost of the chat.
36
- The value for each usage type is a dictionary containing cost information for that specific type.
37
- - "usage_including_cached_inference": Cost information on the total usage, including the tokens in cached inference.
38
- - "usage_excluding_cached_inference": Cost information on the usage of tokens, excluding the tokens in cache. No larger than "usage_including_cached_inference".
39
- """
40
- human_input: List[str] = None
41
- """A list of human input solicited during the chat."""
42
-
43
-
44
- def _validate_recipients(chat_queue: List[Dict[str, Any]]) -> None:
45
- """
46
- Validate recipients exits and warn repetitive recipients.
47
- """
48
- receipts_set = set()
49
- for chat_info in chat_queue:
50
- assert "recipient" in chat_info, "recipient must be provided."
51
- receipts_set.add(chat_info["recipient"])
52
- if len(receipts_set) < len(chat_queue):
53
- warnings.warn(
54
- "Repetitive recipients detected: The chat history will be cleared by default if a recipient appears more than once. To retain the chat history, please set 'clear_history=False' in the configuration of the repeating agent.",
55
- UserWarning,
56
- )
57
-
58
-
59
- def __create_async_prerequisites(chat_queue: List[Dict[str, Any]]) -> List[Prerequisite]:
60
- """
61
- Create list of Prerequisite (prerequisite_chat_id, chat_id)
62
- """
63
- prerequisites = []
64
- for chat_info in chat_queue:
65
- if "chat_id" not in chat_info:
66
- raise ValueError("Each chat must have a unique id for async multi-chat execution.")
67
- chat_id = chat_info["chat_id"]
68
- pre_chats = chat_info.get("prerequisites", [])
69
- for pre_chat_id in pre_chats:
70
- if not isinstance(pre_chat_id, int):
71
- raise ValueError("Prerequisite chat id is not int.")
72
- prerequisites.append((chat_id, pre_chat_id))
73
- return prerequisites
74
-
75
-
76
- def __find_async_chat_order(chat_ids: Set[int], prerequisites: List[Prerequisite]) -> List[int]:
77
- """Find chat order for async execution based on the prerequisite chats
78
-
79
- args:
80
- num_chats: number of chats
81
- prerequisites: List of Prerequisite (prerequisite_chat_id, chat_id)
82
-
83
- returns:
84
- list: a list of chat_id in order.
85
- """
86
- edges = defaultdict(set)
87
- indegree = defaultdict(int)
88
- for pair in prerequisites:
89
- chat, pre = pair[0], pair[1]
90
- if chat not in edges[pre]:
91
- indegree[chat] += 1
92
- edges[pre].add(chat)
93
- bfs = [i for i in chat_ids if i not in indegree]
94
- chat_order = []
95
- steps = len(indegree)
96
- for _ in range(steps + 1):
97
- if not bfs:
98
- break
99
- chat_order.extend(bfs)
100
- nxt = []
101
- for node in bfs:
102
- if node in edges:
103
- for course in edges[node]:
104
- indegree[course] -= 1
105
- if indegree[course] == 0:
106
- nxt.append(course)
107
- indegree.pop(course)
108
- edges.pop(node)
109
- bfs = nxt
110
-
111
- if indegree:
112
- return []
113
- return chat_order
114
-
115
-
116
- def _post_process_carryover_item(carryover_item):
117
- if isinstance(carryover_item, str):
118
- return carryover_item
119
- elif isinstance(carryover_item, dict) and "content" in carryover_item:
120
- return str(carryover_item["content"])
121
- else:
122
- return str(carryover_item)
123
-
124
-
125
- def __post_carryover_processing(chat_info: Dict[str, Any]) -> None:
126
- iostream = IOStream.get_default()
127
-
128
- if "message" not in chat_info:
129
- warnings.warn(
130
- "message is not provided in a chat_queue entry. input() will be called to get the initial message.",
131
- UserWarning,
132
- )
133
- print_carryover = (
134
- ("\n").join([_post_process_carryover_item(t) for t in chat_info["carryover"]])
135
- if isinstance(chat_info["carryover"], list)
136
- else chat_info["carryover"]
137
- )
138
- message = chat_info.get("message")
139
- if isinstance(message, str):
140
- print_message = message
141
- elif callable(message):
142
- print_message = "Callable: " + message.__name__
143
- elif isinstance(message, dict):
144
- print_message = "Dict: " + str(message)
145
- elif message is None:
146
- print_message = "None"
147
- iostream.print(colored("\n" + "*" * 80, "blue"), flush=True, sep="")
148
- iostream.print(
149
- colored(
150
- "Starting a new chat....",
151
- "blue",
152
- ),
153
- flush=True,
154
- )
155
- if chat_info.get("verbose", False):
156
- iostream.print(colored("Message:\n" + print_message, "blue"), flush=True)
157
- iostream.print(colored("Carryover:\n" + print_carryover, "blue"), flush=True)
158
- iostream.print(colored("\n" + "*" * 80, "blue"), flush=True, sep="")
159
-
160
-
161
- def initiate_chats(chat_queue: List[Dict[str, Any]]) -> List[ChatResult]:
162
- """Initiate a list of chats.
163
- Args:
164
- chat_queue (List[Dict]): A list of dictionaries containing the information about the chats.
165
-
166
- Each dictionary should contain the input arguments for
167
- [`ConversableAgent.initiate_chat`](/docs/reference/agentchat/conversable_agent#initiate_chat).
168
- For example:
169
- - `"sender"` - the sender agent.
170
- - `"recipient"` - the recipient agent.
171
- - `"clear_history"` (bool) - whether to clear the chat history with the agent.
172
- Default is True.
173
- - `"silent"` (bool or None) - (Experimental) whether to print the messages in this
174
- conversation. Default is False.
175
- - `"cache"` (Cache or None) - the cache client to use for this conversation.
176
- Default is None.
177
- - `"max_turns"` (int or None) - maximum number of turns for the chat. If None, the chat
178
- will continue until a termination condition is met. Default is None.
179
- - `"summary_method"` (str or callable) - a string or callable specifying the method to get
180
- a summary from the chat. Default is DEFAULT_summary_method, i.e., "last_msg".
181
- - `"summary_args"` (dict) - a dictionary of arguments to be passed to the summary_method.
182
- Default is {}.
183
- - `"message"` (str, callable or None) - if None, input() will be called to get the
184
- initial message.
185
- - `**context` - additional context information to be passed to the chat.
186
- - `"carryover"` - It can be used to specify the carryover information to be passed
187
- to this chat. If provided, we will combine this carryover with the "message" content when
188
- generating the initial chat message in `generate_init_message`.
189
- - `"finished_chat_indexes_to_exclude_from_carryover"` - It can be used by specifying a list of indexes of the finished_chats list,
190
- from which to exclude the summaries for carryover. If 'finished_chat_indexes_to_exclude_from_carryover' is not provided or an empty list,
191
- then summary from all the finished chats will be taken.
192
- Returns:
193
- (list): a list of ChatResult objects corresponding to the finished chats in the chat_queue.
194
- """
195
-
196
- consolidate_chat_info(chat_queue)
197
- _validate_recipients(chat_queue)
198
- current_chat_queue = chat_queue.copy()
199
- finished_chats = []
200
- while current_chat_queue:
201
- chat_info = current_chat_queue.pop(0)
202
- _chat_carryover = chat_info.get("carryover", [])
203
- finished_chat_indexes_to_exclude_from_carryover = chat_info.get(
204
- "finished_chat_indexes_to_exclude_from_carryover", []
205
- )
206
-
207
- if isinstance(_chat_carryover, str):
208
- _chat_carryover = [_chat_carryover]
209
- chat_info["carryover"] = _chat_carryover + [
210
- r.summary for i, r in enumerate(finished_chats) if i not in finished_chat_indexes_to_exclude_from_carryover
211
- ]
212
-
213
- if not chat_info.get("silent", False):
214
- __post_carryover_processing(chat_info)
215
-
216
- sender = chat_info["sender"]
217
- chat_res = sender.initiate_chat(**chat_info)
218
- finished_chats.append(chat_res)
219
- return finished_chats
220
-
221
-
222
- def __system_now_str():
223
- ct = datetime.datetime.now()
224
- return f" System time at {ct}. "
225
-
226
-
227
- def _on_chat_future_done(chat_future: asyncio.Future, chat_id: int):
228
- """
229
- Update ChatResult when async Task for Chat is completed.
230
- """
231
- logger.debug(f"Update chat {chat_id} result on task completion." + __system_now_str())
232
- chat_result = chat_future.result()
233
- chat_result.chat_id = chat_id
234
-
235
-
236
- async def _dependent_chat_future(
237
- chat_id: int, chat_info: Dict[str, Any], prerequisite_chat_futures: Dict[int, asyncio.Future]
238
- ) -> asyncio.Task:
239
- """
240
- Create an async Task for each chat.
241
- """
242
- logger.debug(f"Create Task for chat {chat_id}." + __system_now_str())
243
- _chat_carryover = chat_info.get("carryover", [])
244
- finished_chat_indexes_to_exclude_from_carryover = chat_info.get(
245
- "finished_chat_indexes_to_exclude_from_carryover", []
246
- )
247
- finished_chats = dict()
248
- for chat in prerequisite_chat_futures:
249
- chat_future = prerequisite_chat_futures[chat]
250
- if chat_future.cancelled():
251
- raise RuntimeError(f"Chat {chat} is cancelled.")
252
-
253
- # wait for prerequisite chat results for the new chat carryover
254
- finished_chats[chat] = await chat_future
255
-
256
- if isinstance(_chat_carryover, str):
257
- _chat_carryover = [_chat_carryover]
258
- data = [
259
- chat_result.summary
260
- for chat_id, chat_result in finished_chats.items()
261
- if chat_id not in finished_chat_indexes_to_exclude_from_carryover
262
- ]
263
- chat_info["carryover"] = _chat_carryover + data
264
- if not chat_info.get("silent", False):
265
- __post_carryover_processing(chat_info)
266
-
267
- sender = chat_info["sender"]
268
- chat_res_future = asyncio.create_task(sender.a_initiate_chat(**chat_info))
269
- call_back_with_args = partial(_on_chat_future_done, chat_id=chat_id)
270
- chat_res_future.add_done_callback(call_back_with_args)
271
- logger.debug(f"Task for chat {chat_id} created." + __system_now_str())
272
- return chat_res_future
273
-
274
-
275
- async def a_initiate_chats(chat_queue: List[Dict[str, Any]]) -> Dict[int, ChatResult]:
276
- """(async) Initiate a list of chats.
277
-
278
- args:
279
- - Please refer to `initiate_chats`.
280
-
281
-
282
- returns:
283
- - (Dict): a dict of ChatId: ChatResult corresponding to the finished chats in the chat_queue.
284
- """
285
- consolidate_chat_info(chat_queue)
286
- _validate_recipients(chat_queue)
287
- chat_book = {chat_info["chat_id"]: chat_info for chat_info in chat_queue}
288
- num_chats = chat_book.keys()
289
- prerequisites = __create_async_prerequisites(chat_queue)
290
- chat_order_by_id = __find_async_chat_order(num_chats, prerequisites)
291
- finished_chat_futures = dict()
292
- for chat_id in chat_order_by_id:
293
- chat_info = chat_book[chat_id]
294
- prerequisite_chat_ids = chat_info.get("prerequisites", [])
295
- pre_chat_futures = dict()
296
- for pre_chat_id in prerequisite_chat_ids:
297
- pre_chat_future = finished_chat_futures[pre_chat_id]
298
- pre_chat_futures[pre_chat_id] = pre_chat_future
299
- current_chat_future = await _dependent_chat_future(chat_id, chat_info, pre_chat_futures)
300
- finished_chat_futures[chat_id] = current_chat_future
301
- await asyncio.gather(*list(finished_chat_futures.values()))
302
- finished_chats = dict()
303
- for chat in finished_chat_futures:
304
- chat_result = finished_chat_futures[chat].result()
305
- finished_chats[chat] = chat_result
306
- return finished_chats
File without changes