ag2 0.3.2b2__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.2b2.dist-info/LICENSE +201 -0
  2. ag2-0.3.2b2.dist-info/METADATA +490 -0
  3. ag2-0.3.2b2.dist-info/NOTICE.md +19 -0
  4. ag2-0.3.2b2.dist-info/RECORD +112 -0
  5. ag2-0.3.2b2.dist-info/WHEEL +5 -0
  6. ag2-0.3.2b2.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
@@ -0,0 +1,176 @@
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 json
8
+ import logging
9
+ from typing import List, Optional, Tuple
10
+
11
+ import replicate
12
+ import requests
13
+
14
+ from autogen.agentchat.agent import Agent
15
+ from autogen.agentchat.contrib.img_utils import get_image_data, llava_formatter
16
+ from autogen.agentchat.contrib.multimodal_conversable_agent import MultimodalConversableAgent
17
+ from autogen.code_utils import content_str
18
+
19
+ from ...formatting_utils import colored
20
+
21
+ logger = logging.getLogger(__name__)
22
+
23
+ # we will override the following variables later.
24
+ SEP = "###"
25
+
26
+ DEFAULT_LLAVA_SYS_MSG = "You are an AI agent and you can view images."
27
+
28
+
29
+ class LLaVAAgent(MultimodalConversableAgent):
30
+ def __init__(
31
+ self,
32
+ name: str,
33
+ system_message: Optional[Tuple[str, List]] = DEFAULT_LLAVA_SYS_MSG,
34
+ *args,
35
+ **kwargs,
36
+ ):
37
+ """
38
+ Args:
39
+ name (str): agent name.
40
+ system_message (str): system message for the ChatCompletion inference.
41
+ Please override this attribute if you want to reprogram the agent.
42
+ **kwargs (dict): Please refer to other kwargs in
43
+ [ConversableAgent](../conversable_agent#__init__).
44
+ """
45
+ super().__init__(
46
+ name,
47
+ system_message=system_message,
48
+ *args,
49
+ **kwargs,
50
+ )
51
+
52
+ assert self.llm_config is not None, "llm_config must be provided."
53
+ self.register_reply([Agent, None], reply_func=LLaVAAgent._image_reply, position=2)
54
+
55
+ def _image_reply(self, messages=None, sender=None, config=None):
56
+ # Note: we did not use "llm_config" yet.
57
+
58
+ if all((messages is None, sender is None)):
59
+ error_msg = f"Either {messages=} or {sender=} must be provided."
60
+ logger.error(error_msg)
61
+ raise AssertionError(error_msg)
62
+
63
+ if messages is None:
64
+ messages = self._oai_messages[sender]
65
+
66
+ # The formats for LLaVA and GPT are different. So, we manually handle them here.
67
+ images = []
68
+ prompt = content_str(self.system_message) + "\n"
69
+ for msg in messages:
70
+ role = "Human" if msg["role"] == "user" else "Assistant"
71
+ # pdb.set_trace()
72
+ images += [d["image_url"]["url"] for d in msg["content"] if d["type"] == "image_url"]
73
+ content_prompt = content_str(msg["content"])
74
+ prompt += f"{SEP}{role}: {content_prompt}\n"
75
+ prompt += "\n" + SEP + "Assistant: "
76
+
77
+ # TODO: PIL to base64
78
+ images = [get_image_data(im) for im in images]
79
+ print(colored(prompt, "blue"))
80
+
81
+ out = ""
82
+ retry = 10
83
+ while len(out) == 0 and retry > 0:
84
+ # image names will be inferred automatically from llava_call
85
+ out = llava_call_binary(
86
+ prompt=prompt,
87
+ images=images,
88
+ config_list=self.llm_config["config_list"],
89
+ temperature=self.llm_config.get("temperature", 0.5),
90
+ max_new_tokens=self.llm_config.get("max_new_tokens", 2000),
91
+ )
92
+ retry -= 1
93
+
94
+ assert out != "", "Empty response from LLaVA."
95
+
96
+ return True, out
97
+
98
+
99
+ def _llava_call_binary_with_config(
100
+ prompt: str, images: list, config: dict, max_new_tokens: int = 1000, temperature: float = 0.5, seed: int = 1
101
+ ):
102
+ if config["base_url"].find("0.0.0.0") >= 0 or config["base_url"].find("localhost") >= 0:
103
+ llava_mode = "local"
104
+ else:
105
+ llava_mode = "remote"
106
+
107
+ if llava_mode == "local":
108
+ headers = {"User-Agent": "LLaVA Client"}
109
+ pload = {
110
+ "model": config["model"],
111
+ "prompt": prompt,
112
+ "max_new_tokens": max_new_tokens,
113
+ "temperature": temperature,
114
+ "stop": SEP,
115
+ "images": images,
116
+ }
117
+
118
+ response = requests.post(
119
+ config["base_url"].rstrip("/") + "/worker_generate_stream", headers=headers, json=pload, stream=False
120
+ )
121
+
122
+ for chunk in response.iter_lines(chunk_size=8192, decode_unicode=False, delimiter=b"\0"):
123
+ if chunk:
124
+ data = json.loads(chunk.decode("utf-8"))
125
+ output = data["text"].split(SEP)[-1]
126
+ elif llava_mode == "remote":
127
+ # The Replicate version of the model only support 1 image for now.
128
+ img = "data:image/jpeg;base64," + images[0]
129
+ response = replicate.run(
130
+ config["base_url"], input={"image": img, "prompt": prompt.replace("<image>", " "), "seed": seed}
131
+ )
132
+ # The yorickvp/llava-13b model can stream output as it's running.
133
+ # The predict method returns an iterator, and you can iterate over that output.
134
+ output = ""
135
+ for item in response:
136
+ # https://replicate.com/yorickvp/llava-13b/versions/2facb4a474a0462c15041b78b1ad70952ea46b5ec6ad29583c0b29dbd4249591/api#output-schema
137
+ output += item
138
+
139
+ # Remove the prompt and the space.
140
+ output = output.replace(prompt, "").strip().rstrip()
141
+ return output
142
+
143
+
144
+ def llava_call_binary(
145
+ prompt: str, images: list, config_list: list, max_new_tokens: int = 1000, temperature: float = 0.5, seed: int = 1
146
+ ):
147
+ # TODO 1: add caching around the LLaVA call to save compute and cost
148
+ # TODO 2: add `seed` to ensure reproducibility. The seed is not working now.
149
+
150
+ for config in config_list:
151
+ try:
152
+ return _llava_call_binary_with_config(prompt, images, config, max_new_tokens, temperature, seed)
153
+ except Exception as e:
154
+ print(f"Error: {e}")
155
+ continue
156
+
157
+
158
+ def llava_call(prompt: str, llm_config: dict) -> str:
159
+ """
160
+ Makes a call to the LLaVA service to generate text based on a given prompt
161
+ """
162
+
163
+ prompt, images = llava_formatter(prompt, order_image_tokens=False)
164
+
165
+ for im in images:
166
+ if len(im) == 0:
167
+ raise RuntimeError("An image is empty!")
168
+
169
+ return llava_call_binary(
170
+ prompt,
171
+ images,
172
+ config_list=llm_config["config_list"],
173
+ max_new_tokens=llm_config.get("max_new_tokens", 2000),
174
+ temperature=llm_config.get("temperature", 0.5),
175
+ seed=llm_config.get("seed", None),
176
+ )
@@ -0,0 +1,471 @@
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 os
8
+ import re
9
+ from time import sleep
10
+ from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Union
11
+
12
+ from pydantic import BaseModel, Extra, root_validator
13
+
14
+ from autogen._pydantic import PYDANTIC_V1
15
+ from autogen.agentchat import Agent, UserProxyAgent
16
+ from autogen.code_utils import UNKNOWN, execute_code, extract_code, infer_lang
17
+ from autogen.math_utils import get_answer
18
+
19
+ PROMPTS = {
20
+ # default
21
+ "default": """Let's use Python to solve a math problem.
22
+
23
+ Query requirements:
24
+ You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.
25
+ You can use packages like sympy to help you.
26
+ You must follow the formats below to write your code:
27
+ ```python
28
+ # your code
29
+ ```
30
+
31
+ First state the key idea to solve the problem. You may choose from three ways to solve the problem:
32
+ Case 1: If the problem can be solved with Python code directly, please write a program to solve it. You can enumerate all possible arrangements if needed.
33
+ Case 2: If the problem is mostly reasoning, you can solve it by yourself directly.
34
+ Case 3: If the problem cannot be handled in the above two ways, please follow this process:
35
+ 1. Solve the problem step by step (do not over-divide the steps).
36
+ 2. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).
37
+ 3. Wait for me to give the results.
38
+ 4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.
39
+
40
+ After all the queries are run and you get the answer, put the answer in \\boxed{}.
41
+
42
+ Problem:
43
+ """,
44
+ # select python or wolfram
45
+ "two_tools": """Let's use two tools (Python and Wolfram alpha) to solve a math problem.
46
+
47
+ Query requirements:
48
+ You must follow the formats below to write your query:
49
+ For Wolfram Alpha:
50
+ ```wolfram
51
+ # one wolfram query
52
+ ```
53
+ For Python:
54
+ ```python
55
+ # your code
56
+ ```
57
+ When using Python, you should always use the 'print' function for the output and use fractions/radical forms instead of decimals. You can use packages like sympy to help you.
58
+ When using wolfram, give one query in each code block.
59
+
60
+ Please follow this process:
61
+ 1. Solve the problem step by step (do not over-divide the steps).
62
+ 2. Take out any queries that can be asked through Python or Wolfram Alpha, select the most suitable tool to be used (for example, any calculations or equations that can be calculated).
63
+ 3. Wait for me to give the results.
64
+ 4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.
65
+
66
+ After all the queries are run and you get the answer, put the final answer in \\boxed{}.
67
+
68
+ Problem: """,
69
+ # use python step by step
70
+ "python": """Let's use Python to solve a math problem.
71
+
72
+ Query requirements:
73
+ You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.
74
+ You can use packages like sympy to help you.
75
+ You must follow the formats below to write your code:
76
+ ```python
77
+ # your code
78
+ ```
79
+
80
+ Please follow this process:
81
+ 1. Solve the problem step by step (do not over-divide the steps).
82
+ 2. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).
83
+ 3. Wait for me to give the results.
84
+ 4. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.
85
+
86
+ After all the queries are run and you get the answer, put the answer in \\boxed{}.
87
+
88
+ Problem: """,
89
+ }
90
+
91
+
92
+ def _is_termination_msg_mathchat(message):
93
+ """Check if a message is a termination message."""
94
+ if isinstance(message, dict):
95
+ message = message.get("content")
96
+ if message is None:
97
+ return False
98
+ cb = extract_code(message)
99
+ contain_code = False
100
+ for c in cb:
101
+ if c[0] == "python" or c[0] == "wolfram":
102
+ contain_code = True
103
+ break
104
+ return not contain_code and get_answer(message) is not None and get_answer(message) != ""
105
+
106
+
107
+ def _add_print_to_last_line(code):
108
+ """Add print() to the last line of a string."""
109
+ # 1. check if there is already a print statement
110
+ if "print(" in code:
111
+ return code
112
+ # 2. extract the last line, enclose it in print() and return the new string
113
+ lines = code.splitlines()
114
+ last_line = lines[-1]
115
+ if "\t" in last_line or "=" in last_line:
116
+ return code
117
+ if "=" in last_line:
118
+ last_line = "print(" + last_line.split(" = ")[0] + ")"
119
+ lines.append(last_line)
120
+ else:
121
+ lines[-1] = "print(" + last_line + ")"
122
+ # 3. join the lines back together
123
+ return "\n".join(lines)
124
+
125
+
126
+ def _remove_print(code):
127
+ """remove all print statements from a string."""
128
+ lines = code.splitlines()
129
+ lines = [line for line in lines if not line.startswith("print(")]
130
+ return "\n".join(lines)
131
+
132
+
133
+ class MathUserProxyAgent(UserProxyAgent):
134
+ """(Experimental) A MathChat agent that can handle math problems."""
135
+
136
+ MAX_CONSECUTIVE_AUTO_REPLY = 15 # maximum number of consecutive auto replies (subject to future change)
137
+ DEFAULT_REPLY = "Continue. Please keep solving the problem until you need to query. (If you get to the answer, put it in \\boxed{}.)"
138
+
139
+ def __init__(
140
+ self,
141
+ name: Optional[str] = "MathChatAgent", # default set to MathChatAgent
142
+ is_termination_msg: Optional[
143
+ Callable[[Dict], bool]
144
+ ] = _is_termination_msg_mathchat, # terminate if \boxed{} in message
145
+ human_input_mode: Literal["ALWAYS", "NEVER", "TERMINATE"] = "NEVER", # Fully automated
146
+ default_auto_reply: Optional[Union[str, Dict, None]] = DEFAULT_REPLY,
147
+ max_invalid_q_per_step=3, # a parameter needed in MathChat
148
+ **kwargs,
149
+ ):
150
+ """
151
+ Args:
152
+ name (str): name of the agent
153
+ is_termination_msg (function): a function that takes a message in the form of a dictionary and returns a boolean value indicating if this received message is a termination message.
154
+ The dict can contain the following keys: "content", "role", "name", "function_call".
155
+ human_input_mode (str): whether to ask for human inputs every time a message is received.
156
+ Possible values are "ALWAYS", "TERMINATE", "NEVER".
157
+ (1) When "ALWAYS", the agent prompts for human input every time a message is received.
158
+ Under this mode, the conversation stops when the human input is "exit",
159
+ or when is_termination_msg is True and there is no human input.
160
+ (2) When "TERMINATE", the agent only prompts for human input only when a termination message is received or
161
+ the number of auto reply reaches the max_consecutive_auto_reply.
162
+ (3) (Default) When "NEVER", the agent will never prompt for human input. Under this mode, the conversation stops
163
+ when the number of auto reply reaches the max_consecutive_auto_reply or when is_termination_msg is True.
164
+ default_auto_reply (str or dict or None): the default auto reply message when no code execution or llm based reply is generated.
165
+ max_invalid_q_per_step (int): (ADDED) the maximum number of invalid queries per step.
166
+ **kwargs (dict): other kwargs in [UserProxyAgent](../user_proxy_agent#__init__).
167
+ """
168
+ super().__init__(
169
+ name=name,
170
+ is_termination_msg=is_termination_msg,
171
+ human_input_mode=human_input_mode,
172
+ default_auto_reply=default_auto_reply,
173
+ **kwargs,
174
+ )
175
+ self.register_reply([Agent, None], MathUserProxyAgent._generate_math_reply, position=2)
176
+ # fixed var
177
+ self._max_invalid_q_per_step = max_invalid_q_per_step
178
+
179
+ # mutable
180
+ self._valid_q_count = 0
181
+ self._total_q_count = 0
182
+ self._accum_invalid_q_per_step = 0
183
+ self._previous_code = ""
184
+ self.last_reply = None
185
+
186
+ @staticmethod
187
+ def message_generator(sender, recipient, context):
188
+ """Generate a prompt for the assistant agent with the given problem and prompt.
189
+
190
+ Args:
191
+ sender (Agent): the sender of the message.
192
+ recipient (Agent): the recipient of the message.
193
+ context (dict): a dictionary with the following fields:
194
+ problem (str): the problem to be solved.
195
+ prompt_type (str, Optional): the type of the prompt. Possible values are "default", "python", "wolfram".
196
+ (1) "default": the prompt that allows the agent to choose between 3 ways to solve a problem:
197
+ 1. write a python program to solve it directly.
198
+ 2. solve it directly without python.
199
+ 3. solve it step by step with python.
200
+ (2) "python":
201
+ a simplified prompt from the third way of the "default" prompt, that asks the assistant
202
+ to solve the problem step by step with python.
203
+ (3) "two_tools":
204
+ a simplified prompt similar to the "python" prompt, but allows the model to choose between
205
+ Python and Wolfram Alpha to solve the problem.
206
+ customized_prompt (str, Optional): a customized prompt to be used. If it is not None, the prompt_type will be ignored.
207
+
208
+ Returns:
209
+ str: the generated prompt ready to be sent to the assistant agent.
210
+ """
211
+ sender._reset()
212
+ problem = context.get("problem")
213
+ prompt_type = context.get("prompt_type", "default")
214
+ customized_prompt = context.get("customized_prompt", None)
215
+ if customized_prompt is not None:
216
+ return customized_prompt + problem
217
+ return PROMPTS[prompt_type] + problem
218
+
219
+ def _reset(self):
220
+ # super().reset()
221
+ self._valid_q_count = 0
222
+ self._total_q_count = 0
223
+ self._accum_invalid_q_per_step = 0
224
+ self._previous_code = ""
225
+ self.last_reply = None
226
+
227
+ def execute_one_python_code(self, pycode):
228
+ """Execute python code blocks.
229
+
230
+ Previous python code will be saved and executed together with the new code.
231
+ the "print" function will also be added to the last line of the code if needed
232
+ """
233
+ # Need to replace all "; " with "\n" to avoid syntax error when adding `print` to the last line
234
+ pycode = pycode.replace("; ", "\n").replace(";", "\n")
235
+ pycode = self._previous_code + _add_print_to_last_line(pycode)
236
+
237
+ return_code, output, _ = execute_code(pycode, **self._code_execution_config, timeout=5)
238
+ is_success = return_code == 0
239
+
240
+ if not is_success:
241
+ # Remove the file information from the error string
242
+ pattern = r'File "/[^"]+\.py", line \d+, in .+\n'
243
+ if isinstance(output, str):
244
+ output = re.sub(pattern, "", output)
245
+ output = "Error: " + output
246
+ elif output == "":
247
+ # Check if there is any print statement
248
+ if "print" not in pycode:
249
+ output = "No output found. Make sure you print the results."
250
+ is_success = False
251
+ else:
252
+ output = "No output found."
253
+ is_success = True
254
+
255
+ if len(output) > 2000:
256
+ output = "Your requested query response is too long. You might have made a mistake. Please revise your reasoning and query."
257
+ is_success = False
258
+
259
+ if is_success:
260
+ # remove print and check if it still works
261
+ tmp = self._previous_code + "\n" + _remove_print(pycode) + "\n"
262
+ rcode, _, _ = execute_code(tmp, **self._code_execution_config)
263
+ else:
264
+ # only add imports and check if it works
265
+ tmp = self._previous_code + "\n"
266
+ for line in pycode.split("\n"):
267
+ if "import" in line:
268
+ tmp += line + "\n"
269
+ rcode, _, _ = execute_code(tmp, **self._code_execution_config)
270
+
271
+ if rcode == 0:
272
+ self._previous_code = tmp
273
+ return output, is_success
274
+
275
+ def execute_one_wolfram_query(self, query: str):
276
+ """Run one wolfram query and return the output.
277
+
278
+ Args:
279
+ query: string of the query.
280
+
281
+ Returns:
282
+ output: string with the output of the query.
283
+ is_success: boolean indicating whether the query was successful.
284
+ """
285
+ # wolfram query handler
286
+ wolfram = WolframAlphaAPIWrapper()
287
+ output, is_success = wolfram.run(query)
288
+ if output == "":
289
+ output = "Error: The wolfram query is invalid."
290
+ is_success = False
291
+ return output, is_success
292
+
293
+ def _generate_math_reply(
294
+ self,
295
+ messages: Optional[List[Dict]] = None,
296
+ sender: Optional[Agent] = None,
297
+ config: Optional[Any] = None,
298
+ ):
299
+ """Generate an auto reply."""
300
+ if messages is None:
301
+ messages = self._oai_messages[sender]
302
+ message = messages[-1]
303
+ message = message.get("content", "")
304
+ code_blocks = extract_code(message)
305
+
306
+ if len(code_blocks) == 1 and code_blocks[0][0] == UNKNOWN:
307
+ # no code block is found, lang should be `UNKNOWN``
308
+ return True, self._default_auto_reply
309
+ is_success, all_success = True, True
310
+ reply = ""
311
+ for code_block in code_blocks:
312
+ lang, code = code_block
313
+ if not lang:
314
+ lang = infer_lang(code)
315
+ if lang == "python":
316
+ output, is_success = self.execute_one_python_code(code)
317
+ elif lang == "wolfram":
318
+ output, is_success = self.execute_one_wolfram_query(code)
319
+ else:
320
+ output = "Error: Unknown language."
321
+ is_success = False
322
+
323
+ reply += output + "\n"
324
+ if not is_success:
325
+ all_success = False
326
+ self._valid_q_count -= 1 # count invalid queries
327
+
328
+ reply = reply.strip()
329
+
330
+ if self.last_reply == reply:
331
+ return True, reply + "\nYour query or result is same from the last, please try a new approach."
332
+ self.last_reply = reply
333
+
334
+ if not all_success:
335
+ self._accum_invalid_q_per_step += 1
336
+ if self._accum_invalid_q_per_step > self._max_invalid_q_per_step:
337
+ self._accum_invalid_q_per_step = 0
338
+ reply = "Please revisit the problem statement and your reasoning. If you think this step is correct, solve it yourself and continue the next step. Otherwise, correct this step."
339
+
340
+ return True, reply
341
+
342
+
343
+ # Modified based on langchain. Langchain is licensed under MIT License:
344
+ # The MIT License
345
+
346
+ # Copyright (c) Harrison Chase
347
+
348
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
349
+ # of this software and associated documentation files (the "Software"), to deal
350
+ # in the Software without restriction, including without limitation the rights
351
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
352
+ # copies of the Software, and to permit persons to whom the Software is
353
+ # furnished to do so, subject to the following conditions:
354
+
355
+ # The above copyright notice and this permission notice shall be included in
356
+ # all copies or substantial portions of the Software.
357
+
358
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
359
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
360
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
361
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
362
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
363
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
364
+ # THE SOFTWARE.
365
+
366
+
367
+ def get_from_dict_or_env(data: Dict[str, Any], key: str, env_key: str, default: Optional[str] = None) -> str:
368
+ """Get a value from a dictionary or an environment variable."""
369
+ if key in data and data[key]:
370
+ return data[key]
371
+ elif env_key in os.environ and os.environ[env_key]:
372
+ return os.environ[env_key]
373
+ elif default is not None:
374
+ return default
375
+ else:
376
+ raise ValueError(
377
+ f"Did not find {key}, please add an environment variable"
378
+ f" `{env_key}` which contains it, or pass"
379
+ f" `{key}` as a named parameter."
380
+ )
381
+
382
+
383
+ class WolframAlphaAPIWrapper(BaseModel):
384
+ """Wrapper for Wolfram Alpha.
385
+
386
+ Docs for using:
387
+
388
+ 1. Go to wolfram alpha and sign up for a developer account
389
+ 2. Create an app and get your APP ID
390
+ 3. Save your APP ID into WOLFRAM_ALPHA_APPID env variable
391
+ 4. pip install wolframalpha
392
+
393
+ """
394
+
395
+ wolfram_client: Any #: :meta private:
396
+ wolfram_alpha_appid: Optional[str] = None
397
+
398
+ class Config:
399
+ """Configuration for this pydantic object."""
400
+
401
+ if PYDANTIC_V1:
402
+ extra = Extra.forbid
403
+
404
+ @root_validator(skip_on_failure=True)
405
+ def validate_environment(cls, values: Dict) -> Dict:
406
+ """Validate that api key and python package exists in environment."""
407
+ wolfram_alpha_appid = get_from_dict_or_env(values, "wolfram_alpha_appid", "WOLFRAM_ALPHA_APPID")
408
+ values["wolfram_alpha_appid"] = wolfram_alpha_appid
409
+
410
+ try:
411
+ import wolframalpha
412
+
413
+ except ImportError as e:
414
+ raise ImportError("wolframalpha is not installed. Please install it with `pip install wolframalpha`") from e
415
+ client = wolframalpha.Client(wolfram_alpha_appid)
416
+ values["wolfram_client"] = client
417
+
418
+ return values
419
+
420
+ def run(self, query: str) -> Tuple[str, bool]:
421
+ """Run query through WolframAlpha and parse result."""
422
+ from urllib.error import HTTPError
423
+
424
+ is_success = False # added
425
+ res = None
426
+ for _ in range(20):
427
+ try:
428
+ res = self.wolfram_client.query(query)
429
+ break
430
+ except HTTPError:
431
+ sleep(1)
432
+ except Exception:
433
+ return (
434
+ "Wolfram Alpha wasn't able to answer it. Please try a new query for wolfram or use python.",
435
+ is_success,
436
+ )
437
+ if res is None:
438
+ return (
439
+ "Wolfram Alpha wasn't able to answer it (may due to web error), you can try again or use python.",
440
+ is_success,
441
+ )
442
+
443
+ try:
444
+ if not res["@success"]:
445
+ return (
446
+ "Your Wolfram query is invalid. Please try a new query for wolfram or use python.",
447
+ is_success,
448
+ )
449
+ assumption = next(res.pods).text
450
+ answer = ""
451
+ for result in res["pod"]:
452
+ if result["@title"] == "Solution":
453
+ answer = result["subpod"]["plaintext"]
454
+ if result["@title"] == "Results" or result["@title"] == "Solutions":
455
+ for i, sub in enumerate(result["subpod"]):
456
+ answer += f"ans {i}: " + sub["plaintext"] + "\n"
457
+ break
458
+ if answer == "":
459
+ answer = next(res.results).text
460
+
461
+ except Exception:
462
+ return (
463
+ "Wolfram Alpha wasn't able to answer it. Please try a new query for wolfram or use python.",
464
+ is_success,
465
+ )
466
+
467
+ if answer is None or answer == "":
468
+ # We don't want to return the assumption alone if answer is empty
469
+ return "No good Wolfram Alpha Result was found", is_success
470
+ is_success = True
471
+ return f"Assumption: {assumption} \nAnswer: {answer}", is_success