ag2 0.3.2__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 (112) hide show
  1. ag2-0.3.2.dist-info/LICENSE +201 -0
  2. ag2-0.3.2.dist-info/METADATA +490 -0
  3. ag2-0.3.2.dist-info/NOTICE.md +19 -0
  4. ag2-0.3.2.dist-info/RECORD +112 -0
  5. ag2-0.3.2.dist-info/WHEEL +5 -0
  6. ag2-0.3.2.dist-info/top_level.txt +1 -0
  7. autogen/__init__.py +17 -0
  8. autogen/_pydantic.py +116 -0
  9. autogen/agentchat/__init__.py +26 -0
  10. autogen/agentchat/agent.py +142 -0
  11. autogen/agentchat/assistant_agent.py +85 -0
  12. autogen/agentchat/chat.py +306 -0
  13. autogen/agentchat/contrib/__init__.py +0 -0
  14. autogen/agentchat/contrib/agent_builder.py +785 -0
  15. autogen/agentchat/contrib/agent_optimizer.py +450 -0
  16. autogen/agentchat/contrib/capabilities/__init__.py +0 -0
  17. autogen/agentchat/contrib/capabilities/agent_capability.py +21 -0
  18. autogen/agentchat/contrib/capabilities/generate_images.py +297 -0
  19. autogen/agentchat/contrib/capabilities/teachability.py +406 -0
  20. autogen/agentchat/contrib/capabilities/text_compressors.py +72 -0
  21. autogen/agentchat/contrib/capabilities/transform_messages.py +92 -0
  22. autogen/agentchat/contrib/capabilities/transforms.py +565 -0
  23. autogen/agentchat/contrib/capabilities/transforms_util.py +120 -0
  24. autogen/agentchat/contrib/capabilities/vision_capability.py +217 -0
  25. autogen/agentchat/contrib/gpt_assistant_agent.py +545 -0
  26. autogen/agentchat/contrib/graph_rag/__init__.py +0 -0
  27. autogen/agentchat/contrib/graph_rag/document.py +24 -0
  28. autogen/agentchat/contrib/graph_rag/falkor_graph_query_engine.py +76 -0
  29. autogen/agentchat/contrib/graph_rag/graph_query_engine.py +50 -0
  30. autogen/agentchat/contrib/graph_rag/graph_rag_capability.py +56 -0
  31. autogen/agentchat/contrib/img_utils.py +390 -0
  32. autogen/agentchat/contrib/llamaindex_conversable_agent.py +114 -0
  33. autogen/agentchat/contrib/llava_agent.py +176 -0
  34. autogen/agentchat/contrib/math_user_proxy_agent.py +471 -0
  35. autogen/agentchat/contrib/multimodal_conversable_agent.py +128 -0
  36. autogen/agentchat/contrib/qdrant_retrieve_user_proxy_agent.py +325 -0
  37. autogen/agentchat/contrib/retrieve_assistant_agent.py +56 -0
  38. autogen/agentchat/contrib/retrieve_user_proxy_agent.py +701 -0
  39. autogen/agentchat/contrib/society_of_mind_agent.py +203 -0
  40. autogen/agentchat/contrib/text_analyzer_agent.py +76 -0
  41. autogen/agentchat/contrib/vectordb/__init__.py +0 -0
  42. autogen/agentchat/contrib/vectordb/base.py +243 -0
  43. autogen/agentchat/contrib/vectordb/chromadb.py +326 -0
  44. autogen/agentchat/contrib/vectordb/mongodb.py +559 -0
  45. autogen/agentchat/contrib/vectordb/pgvectordb.py +958 -0
  46. autogen/agentchat/contrib/vectordb/qdrant.py +334 -0
  47. autogen/agentchat/contrib/vectordb/utils.py +126 -0
  48. autogen/agentchat/contrib/web_surfer.py +305 -0
  49. autogen/agentchat/conversable_agent.py +2904 -0
  50. autogen/agentchat/groupchat.py +1666 -0
  51. autogen/agentchat/user_proxy_agent.py +109 -0
  52. autogen/agentchat/utils.py +207 -0
  53. autogen/browser_utils.py +291 -0
  54. autogen/cache/__init__.py +10 -0
  55. autogen/cache/abstract_cache_base.py +78 -0
  56. autogen/cache/cache.py +182 -0
  57. autogen/cache/cache_factory.py +85 -0
  58. autogen/cache/cosmos_db_cache.py +150 -0
  59. autogen/cache/disk_cache.py +109 -0
  60. autogen/cache/in_memory_cache.py +61 -0
  61. autogen/cache/redis_cache.py +128 -0
  62. autogen/code_utils.py +745 -0
  63. autogen/coding/__init__.py +22 -0
  64. autogen/coding/base.py +113 -0
  65. autogen/coding/docker_commandline_code_executor.py +262 -0
  66. autogen/coding/factory.py +45 -0
  67. autogen/coding/func_with_reqs.py +203 -0
  68. autogen/coding/jupyter/__init__.py +22 -0
  69. autogen/coding/jupyter/base.py +32 -0
  70. autogen/coding/jupyter/docker_jupyter_server.py +164 -0
  71. autogen/coding/jupyter/embedded_ipython_code_executor.py +182 -0
  72. autogen/coding/jupyter/jupyter_client.py +224 -0
  73. autogen/coding/jupyter/jupyter_code_executor.py +161 -0
  74. autogen/coding/jupyter/local_jupyter_server.py +168 -0
  75. autogen/coding/local_commandline_code_executor.py +410 -0
  76. autogen/coding/markdown_code_extractor.py +44 -0
  77. autogen/coding/utils.py +57 -0
  78. autogen/exception_utils.py +46 -0
  79. autogen/extensions/__init__.py +0 -0
  80. autogen/formatting_utils.py +76 -0
  81. autogen/function_utils.py +362 -0
  82. autogen/graph_utils.py +148 -0
  83. autogen/io/__init__.py +15 -0
  84. autogen/io/base.py +105 -0
  85. autogen/io/console.py +43 -0
  86. autogen/io/websockets.py +213 -0
  87. autogen/logger/__init__.py +11 -0
  88. autogen/logger/base_logger.py +140 -0
  89. autogen/logger/file_logger.py +287 -0
  90. autogen/logger/logger_factory.py +29 -0
  91. autogen/logger/logger_utils.py +42 -0
  92. autogen/logger/sqlite_logger.py +459 -0
  93. autogen/math_utils.py +356 -0
  94. autogen/oai/__init__.py +33 -0
  95. autogen/oai/anthropic.py +428 -0
  96. autogen/oai/bedrock.py +600 -0
  97. autogen/oai/cerebras.py +264 -0
  98. autogen/oai/client.py +1148 -0
  99. autogen/oai/client_utils.py +167 -0
  100. autogen/oai/cohere.py +453 -0
  101. autogen/oai/completion.py +1216 -0
  102. autogen/oai/gemini.py +469 -0
  103. autogen/oai/groq.py +281 -0
  104. autogen/oai/mistral.py +279 -0
  105. autogen/oai/ollama.py +576 -0
  106. autogen/oai/openai_utils.py +810 -0
  107. autogen/oai/together.py +343 -0
  108. autogen/retrieve_utils.py +487 -0
  109. autogen/runtime_logging.py +163 -0
  110. autogen/token_count_utils.py +257 -0
  111. autogen/types.py +20 -0
  112. autogen/version.py +7 -0
autogen/math_utils.py ADDED
@@ -0,0 +1,356 @@
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 Optional
8
+
9
+ from autogen import DEFAULT_MODEL, oai
10
+
11
+ _MATH_PROMPT = "{problem} Solve the problem carefully. Simplify your answer as much as possible. Put the final answer in \\boxed{{}}."
12
+ _MATH_CONFIG = {
13
+ "model": DEFAULT_MODEL,
14
+ "prompt": _MATH_PROMPT,
15
+ }
16
+
17
+
18
+ def solve_problem(problem: str, **config) -> str:
19
+ """(openai<1) Solve the math problem.
20
+
21
+ Args:
22
+ problem (str): The problem statement.
23
+ config (Optional, dict): The configuration for the API call.
24
+
25
+ Returns:
26
+ str: The solution to the problem.
27
+ """
28
+ params = {**_MATH_CONFIG, **config}
29
+ response = oai.Completion.create({"problem": problem}, **params)
30
+ results = eval_math_responses(oai.Completion.extract_text(response))
31
+ return results.get("voted_answer"), response["cost"]
32
+
33
+
34
+ def remove_boxed(string: str) -> Optional[str]:
35
+ """Source: https://github.com/hendrycks/math
36
+ Extract the text within a \\boxed{...} environment.
37
+ Example:
38
+
39
+ > remove_boxed("\\boxed{\\frac{2}{3}}")
40
+
41
+ \\frac{2}{3}
42
+ """
43
+ left = "\\boxed{"
44
+ try:
45
+ if not all((string[: len(left)] == left, string[-1] == "}")):
46
+ raise AssertionError
47
+
48
+ return string[len(left) : -1]
49
+ except Exception:
50
+ return None
51
+
52
+
53
+ def last_boxed_only_string(string: str) -> Optional[str]:
54
+ """Source: https://github.com/hendrycks/math
55
+ Extract the last \\boxed{...} or \\fbox{...} element from a string.
56
+ """
57
+ idx = string.rfind("\\boxed")
58
+ if idx < 0:
59
+ idx = string.rfind("\\fbox")
60
+ if idx < 0:
61
+ return None
62
+
63
+ i = idx
64
+ right_brace_idx = None
65
+ num_left_braces_open = 0
66
+ while i < len(string):
67
+ if string[i] == "{":
68
+ num_left_braces_open += 1
69
+ if string[i] == "}":
70
+ num_left_braces_open -= 1
71
+ if num_left_braces_open == 0:
72
+ right_brace_idx = i
73
+ break
74
+ i += 1
75
+
76
+ if right_brace_idx is None:
77
+ retval = None
78
+ else:
79
+ retval = string[idx : right_brace_idx + 1]
80
+
81
+ return retval
82
+
83
+
84
+ def _fix_fracs(string: str) -> str:
85
+ """Source: https://github.com/hendrycks/math
86
+ Reformat fractions.
87
+ Examples:
88
+ >>> _fix_fracs("\\frac1b")
89
+ \frac{1}{b}
90
+ >>> _fix_fracs("\\frac12")
91
+ \frac{1}{2}
92
+ >>> _fix_fracs("\\frac1{72}")
93
+ \frac{1}{72}
94
+ """
95
+ substrs = string.split("\\frac")
96
+ new_str = substrs[0]
97
+ if len(substrs) > 1:
98
+ substrs = substrs[1:]
99
+ for substr in substrs:
100
+ new_str += "\\frac"
101
+ if substr[0] == "{":
102
+ new_str += substr
103
+ else:
104
+ try:
105
+ if not len(substr) >= 2:
106
+ raise AssertionError
107
+ except Exception:
108
+ return string
109
+ a = substr[0]
110
+ b = substr[1]
111
+ if b != "{":
112
+ if len(substr) > 2:
113
+ post_substr = substr[2:]
114
+ new_str += "{" + a + "}{" + b + "}" + post_substr
115
+ else:
116
+ new_str += "{" + a + "}{" + b + "}"
117
+ else:
118
+ if len(substr) > 2:
119
+ post_substr = substr[2:]
120
+ new_str += "{" + a + "}" + b + post_substr
121
+ else:
122
+ new_str += "{" + a + "}" + b
123
+ string = new_str
124
+ return string
125
+
126
+
127
+ def _fix_a_slash_b(string: str) -> str:
128
+ """Source: https://github.com/hendrycks/math
129
+ Reformat fractions formatted as a/b to \\frac{a}{b}.
130
+ Example:
131
+ >>> _fix_a_slash_b("2/3")
132
+ \frac{2}{3}
133
+ """
134
+ if len(string.split("/")) != 2:
135
+ return string
136
+ a_str = string.split("/")[0]
137
+ b_str = string.split("/")[1]
138
+ try:
139
+ a = int(a_str)
140
+ b = int(b_str)
141
+ if not string == "{}/{}".format(a, b):
142
+ raise AssertionError
143
+ new_string = "\\frac{" + str(a) + "}{" + str(b) + "}"
144
+ return new_string
145
+ except Exception:
146
+ return string
147
+
148
+
149
+ def _remove_right_units(string: str) -> str:
150
+ """Source: https://github.com/hendrycks/math
151
+ Remove units (on the right).
152
+ "\\text{ " only ever occurs (at least in the val set) when describing units.
153
+ """
154
+ if "\\text{ " in string:
155
+ splits = string.split("\\text{ ")
156
+ if not len(splits) == 2:
157
+ raise AssertionError
158
+ return splits[0]
159
+ else:
160
+ return string
161
+
162
+
163
+ def _fix_sqrt(string: str) -> str:
164
+ """Source: https://github.com/hendrycks/math
165
+ Reformat square roots.
166
+ Example:
167
+ >>> _fix_sqrt("\\sqrt3")
168
+ \\sqrt{3}
169
+ """
170
+ if "\\sqrt" not in string:
171
+ return string
172
+ splits = string.split("\\sqrt")
173
+ new_string = splits[0]
174
+ for split in splits[1:]:
175
+ if split[0] != "{":
176
+ a = split[0]
177
+ new_substr = "\\sqrt{" + a + "}" + split[1:]
178
+ else:
179
+ new_substr = "\\sqrt" + split
180
+ new_string += new_substr
181
+ return new_string
182
+
183
+
184
+ def _strip_string(string: str) -> str:
185
+ """Source: https://github.com/hendrycks/math
186
+ Apply the reformatting helper functions above.
187
+ """
188
+ # linebreaks
189
+ string = string.replace("\n", "")
190
+ # print(string)
191
+
192
+ # remove inverse spaces
193
+ string = string.replace("\\!", "")
194
+ # print(string)
195
+
196
+ # replace \\ with \
197
+ string = string.replace("\\\\", "\\")
198
+ # print(string)
199
+
200
+ # replace tfrac and dfrac with frac
201
+ string = string.replace("tfrac", "frac")
202
+ string = string.replace("dfrac", "frac")
203
+ # print(string)
204
+
205
+ # remove \left and \right
206
+ string = string.replace("\\left", "")
207
+ string = string.replace("\\right", "")
208
+ # print(string)
209
+
210
+ # Remove circ (degrees)
211
+ string = string.replace("^{\\circ}", "")
212
+ string = string.replace("^\\circ", "")
213
+
214
+ # remove dollar signs
215
+ string = string.replace("\\$", "")
216
+
217
+ # remove units (on the right)
218
+ string = _remove_right_units(string)
219
+
220
+ # remove percentage
221
+ string = string.replace("\\%", "")
222
+ string = string.replace("%", "")
223
+
224
+ # " 0." equivalent to " ." and "{0." equivalent to "{." Alternatively, add "0" if "." is the start of the string
225
+ string = string.replace(" .", " 0.")
226
+ string = string.replace("{.", "{0.")
227
+ # if empty, return empty string
228
+ if len(string) == 0:
229
+ return string
230
+ if string[0] == ".":
231
+ string = "0" + string
232
+
233
+ # to consider: get rid of e.g. "k = " or "q = " at beginning
234
+ if len(string.split("=")) == 2:
235
+ if len(string.split("=")[0]) <= 2:
236
+ string = string.split("=")[1]
237
+
238
+ # fix sqrt3 --> sqrt{3}
239
+ string = _fix_sqrt(string)
240
+
241
+ # remove spaces
242
+ string = string.replace(" ", "")
243
+
244
+ # \frac1b or \frac12 --> \frac{1}{b} and \frac{1}{2}, etc.
245
+ # Even works with \frac1{72} (but not \frac{72}1).
246
+ # Also does a/b --> \\frac{a}{b}
247
+ string = _fix_fracs(string)
248
+
249
+ # manually change 0.5 --> \frac{1}{2}
250
+ if string == "0.5":
251
+ string = "\\frac{1}{2}"
252
+
253
+ # NOTE: X/Y changed to \frac{X}{Y} in dataset, but in simple cases fix in case the model output is X/Y
254
+ string = _fix_a_slash_b(string)
255
+
256
+ return string
257
+
258
+
259
+ def get_answer(solution: Optional[str]) -> Optional[str]:
260
+ if solution is None:
261
+ return None
262
+ last_boxed = last_boxed_only_string(solution)
263
+ if last_boxed is None:
264
+ return None
265
+ answer = remove_boxed(last_boxed)
266
+ if answer is None:
267
+ return None
268
+ return answer
269
+
270
+
271
+ def is_equiv(str1: Optional[str], str2: Optional[str]) -> float:
272
+ """Returns (as a float) whether two strings containing math are equivalent up to differences of formatting in
273
+ - units
274
+ - fractions
275
+ - square roots
276
+ - superfluous LaTeX.
277
+ Source: https://github.com/hendrycks/math
278
+ """
279
+ if str1 is None and str2 is None:
280
+ print("WARNING: Both None")
281
+ return 1.0
282
+ if str1 is None or str2 is None:
283
+ return 0.0
284
+
285
+ try:
286
+ ss1 = _strip_string(str1)
287
+ ss2 = _strip_string(str2)
288
+ return float(ss1 == ss2)
289
+ except Exception:
290
+ return float(str1 == str2)
291
+
292
+
293
+ def is_equiv_chain_of_thought(str1: str, str2: str) -> float:
294
+ """Strips the solution first before calling `is_equiv`."""
295
+ ans1 = get_answer(str1)
296
+ ans2 = get_answer(str2)
297
+
298
+ return is_equiv(ans1, ans2)
299
+
300
+
301
+ def voting_counts(responses):
302
+ answers = {}
303
+ for i in range(len(responses)):
304
+ equiv = i
305
+ if get_answer(responses[i]) is None:
306
+ # ignore None answers
307
+ continue
308
+ for j in answers:
309
+ if is_equiv_chain_of_thought(responses[i], responses[j]):
310
+ equiv = j
311
+ break
312
+ if equiv in answers:
313
+ answers[equiv] += 1
314
+ else:
315
+ answers[equiv] = 1
316
+ return answers
317
+
318
+
319
+ def eval_math_responses(responses, solution=None, **args):
320
+ """Select a response for a math problem using voting, and check if the response is correct if the solution is provided.
321
+
322
+ Args:
323
+ responses (list): The list of responses.
324
+ solution (str): The canonical solution.
325
+
326
+ Returns:
327
+ dict: The success metrics.
328
+ """
329
+ n = len(responses)
330
+ if not n:
331
+ return {
332
+ "expected_success": 0,
333
+ "success": False,
334
+ "success_vote": 0,
335
+ "voted_answer": None,
336
+ "votes": 0,
337
+ }
338
+ success_list = []
339
+ if solution is not None:
340
+ for i in range(n):
341
+ response = responses[i]
342
+ succeed = is_equiv_chain_of_thought(response, solution)
343
+ success_list.append(succeed)
344
+ # voting
345
+ answers = voting_counts(responses)
346
+ # find the answer with highest votes in answers
347
+ answer, votes = max(answers.items(), key=lambda x: x[1], default=(0, 0))
348
+ # check if the answer is correct
349
+ success_vote = is_equiv_chain_of_thought(responses[answer], solution)
350
+ return {
351
+ "expected_success": 1 - pow(1 - sum(success_list) / n, n),
352
+ "success": any(s for s in success_list),
353
+ "success_vote": success_vote,
354
+ "voted_answer": responses[answer],
355
+ "votes": votes,
356
+ }
@@ -0,0 +1,33 @@
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 autogen.cache.cache import Cache
8
+ from autogen.oai.client import ModelClient, OpenAIWrapper
9
+ from autogen.oai.completion import ChatCompletion, Completion
10
+ from autogen.oai.openai_utils import (
11
+ config_list_from_dotenv,
12
+ config_list_from_json,
13
+ config_list_from_models,
14
+ config_list_gpt4_gpt35,
15
+ config_list_openai_aoai,
16
+ filter_config,
17
+ get_config_list,
18
+ )
19
+
20
+ __all__ = [
21
+ "OpenAIWrapper",
22
+ "ModelClient",
23
+ "Completion",
24
+ "ChatCompletion",
25
+ "get_config_list",
26
+ "config_list_gpt4_gpt35",
27
+ "config_list_openai_aoai",
28
+ "config_list_from_models",
29
+ "config_list_from_json",
30
+ "config_list_from_dotenv",
31
+ "filter_config",
32
+ "Cache",
33
+ ]