aphilippov/python-server-api
0
1import re2import os3from pydantic import BaseModel, Extra, root_validator4from typing import Any, Callable, Dict, List, Optional, Union, Tuple5from time import sleep6 7from autogen._pydantic import PYDANTIC_V18from autogen.agentchat import Agent, UserProxyAgent9from autogen.code_utils import UNKNOWN, extract_code, execute_code, infer_lang10from autogen.math_utils import get_answer11 12 13PROMPTS = {14 # default15 "default": """Let's use Python to solve a math problem.16 17Query requirements:18You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.19You can use packages like sympy to help you.20You must follow the formats below to write your code:21```python22# your code23```24 25First state the key idea to solve the problem. You may choose from three ways to solve the problem:26Case 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.27Case 2: If the problem is mostly reasoning, you can solve it by yourself directly.28Case 3: If the problem cannot be handled in the above two ways, please follow this process:291. Solve the problem step by step (do not over-divide the steps).302. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).313. Wait for me to give the results.324. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.33 34After all the queries are run and you get the answer, put the answer in \\boxed{}.35 36Problem:37""",38 # select python or wolfram39 "two_tools": """Let's use two tools (Python and Wolfram alpha) to solve a math problem.40 41Query requirements:42You must follow the formats below to write your query:43For Wolfram Alpha:44```wolfram45# one wolfram query46```47For Python:48```python49# your code50```51When 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.52When using wolfram, give one query in each code block.53 54Please follow this process:551. Solve the problem step by step (do not over-divide the steps).562. 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).573. Wait for me to give the results.584. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.59 60After all the queries are run and you get the answer, put the final answer in \\boxed{}.61 62Problem: """,63 # use python step by step64 "python": """Let's use Python to solve a math problem.65 66Query requirements:67You should always use the 'print' function for the output and use fractions/radical forms instead of decimals.68You can use packages like sympy to help you.69You must follow the formats below to write your code:70```python71# your code72```73 74Please follow this process:751. Solve the problem step by step (do not over-divide the steps).762. Take out any queries that can be asked through Python (for example, any calculations or equations that can be calculated).773. Wait for me to give the results.784. Continue if you think the result is correct. If the result is invalid or unexpected, please correct your query or reasoning.79 80After all the queries are run and you get the answer, put the answer in \\boxed{}.81 82Problem: """,83}84 85 86def _is_termination_msg_mathchat(message):87 """Check if a message is a termination message."""88 if isinstance(message, dict):89 message = message.get("content")90 if message is None:91 return False92 cb = extract_code(message)93 contain_code = False94 for c in cb:95 if c[0] == "python" or c[0] == "wolfram":96 contain_code = True97 break98 return not contain_code and get_answer(message) is not None and get_answer(message) != ""99 100 101def _add_print_to_last_line(code):102 """Add print() to the last line of a string."""103 # 1. check if there is already a print statement104 if "print(" in code:105 return code106 # 2. extract the last line, enclose it in print() and return the new string107 lines = code.splitlines()108 last_line = lines[-1]109 if "\t" in last_line or "=" in last_line:110 return code111 if "=" in last_line:112 last_line = "print(" + last_line.split(" = ")[0] + ")"113 lines.append(last_line)114 else:115 lines[-1] = "print(" + last_line + ")"116 # 3. join the lines back together117 return "\n".join(lines)118 119 120def _remove_print(code):121 """remove all print statements from a string."""122 lines = code.splitlines()123 lines = [line for line in lines if not line.startswith("print(")]124 return "\n".join(lines)125 126 127class MathUserProxyAgent(UserProxyAgent):128 """(Experimental) A MathChat agent that can handle math problems."""129 130 MAX_CONSECUTIVE_AUTO_REPLY = 15 # maximum number of consecutive auto replies (subject to future change)131 DEFAULT_REPLY = "Continue. Please keep solving the problem until you need to query. (If you get to the answer, put it in \\boxed{}.)"132 133 def __init__(134 self,135 name: Optional[str] = "MathChatAgent", # default set to MathChatAgent136 is_termination_msg: Optional[137 Callable[[Dict], bool]138 ] = _is_termination_msg_mathchat, # terminate if \boxed{} in message139 human_input_mode: Optional[str] = "NEVER", # Fully automated140 default_auto_reply: Optional[Union[str, Dict, None]] = DEFAULT_REPLY,141 max_invalid_q_per_step=3, # a parameter needed in MathChat142 **kwargs,143 ):144 """145 Args:146 name (str): name of the agent147 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.148 The dict can contain the following keys: "content", "role", "name", "function_call".149 human_input_mode (str): whether to ask for human inputs every time a message is received.150 Possible values are "ALWAYS", "TERMINATE", "NEVER".151 (1) When "ALWAYS", the agent prompts for human input every time a message is received.152 Under this mode, the conversation stops when the human input is "exit",153 or when is_termination_msg is True and there is no human input.154 (2) When "TERMINATE", the agent only prompts for human input only when a termination message is received or155 the number of auto reply reaches the max_consecutive_auto_reply.156 (3) (Default) When "NEVER", the agent will never prompt for human input. Under this mode, the conversation stops157 when the number of auto reply reaches the max_consecutive_auto_reply or when is_termination_msg is True.158 default_auto_reply (str or dict or None): the default auto reply message when no code execution or llm based reply is generated.159 max_invalid_q_per_step (int): (ADDED) the maximum number of invalid queries per step.160 **kwargs (dict): other kwargs in [UserProxyAgent](../user_proxy_agent#__init__).161 """162 super().__init__(163 name=name,164 is_termination_msg=is_termination_msg,165 human_input_mode=human_input_mode,166 default_auto_reply=default_auto_reply,167 **kwargs,168 )169 self.register_reply([Agent, None], MathUserProxyAgent._generate_math_reply, position=2)170 # fixed var171 self._max_invalid_q_per_step = max_invalid_q_per_step172 173 # mutable174 self._valid_q_count = 0175 self._total_q_count = 0176 self._accum_invalid_q_per_step = 0177 self._previous_code = ""178 self.last_reply = None179 180 def generate_init_message(self, problem, prompt_type="default", customized_prompt=None):181 """Generate a prompt for the assistant agent with the given problem and prompt.182 183 Args:184 problem (str): the problem to be solved.185 prompt_type (str): the type of the prompt. Possible values are "default", "python", "wolfram".186 (1) "default": the prompt that allows the agent to choose between 3 ways to solve a problem:187 1. write a python program to solve it directly.188 2. solve it directly without python.189 3. solve it step by step with python.190 (2) "python":191 a simplified prompt from the third way of the "default" prompt, that asks the assistant192 to solve the problem step by step with python.193 (3) "two_tools":194 a simplified prompt similar to the "python" prompt, but allows the model to choose between195 Python and Wolfram Alpha to solve the problem.196 customized_prompt (str): a customized prompt to be used. If it is not None, the prompt_type will be ignored.197 198 Returns:199 str: the generated prompt ready to be sent to the assistant agent.200 """201 self._reset()202 if customized_prompt is not None:203 return customized_prompt + problem204 return PROMPTS[prompt_type] + problem205 206 def _reset(self):207 # super().reset()208 self._valid_q_count = 0209 self._total_q_count = 0210 self._accum_invalid_q_per_step = 0211 self._previous_code = ""212 self.last_reply = None213 214 def execute_one_python_code(self, pycode):215 """Execute python code blocks.216 217 Previous python code will be saved and executed together with the new code.218 the "print" function will also be added to the last line of the code if needed219 """220 # Need to replace all "; " with "\n" to avoid syntax error when adding `print` to the last line221 pycode = pycode.replace("; ", "\n").replace(";", "\n")222 pycode = self._previous_code + _add_print_to_last_line(pycode)223 224 return_code, output, _ = execute_code(pycode, **self._code_execution_config, timeout=5)225 is_success = return_code == 0226 227 if not is_success:228 # Remove the file information from the error string229 pattern = r'File "/[^"]+\.py", line \d+, in .+\n'230 if isinstance(output, str):231 output = re.sub(pattern, "", output)232 output = "Error: " + output233 elif output == "":234 # Check if there is any print statement235 if "print" not in pycode:236 output = "No output found. Make sure you print the results."237 is_success = False238 else:239 output = "No output found."240 is_success = True241 242 if len(output) > 2000:243 output = "Your requested query response is too long. You might have made a mistake. Please revise your reasoning and query."244 is_success = False245 246 if is_success:247 # remove print and check if it still works248 tmp = self._previous_code + "\n" + _remove_print(pycode) + "\n"249 rcode, _, _ = execute_code(tmp, **self._code_execution_config)250 else:251 # only add imports and check if it works252 tmp = self._previous_code + "\n"253 for line in pycode.split("\n"):254 if "import" in line:255 tmp += line + "\n"256 rcode, _, _ = execute_code(tmp, **self._code_execution_config)257 258 if rcode == 0:259 self._previous_code = tmp260 return output, is_success261 262 def execute_one_wolfram_query(self, query: str):263 """Run one wolfram query and return the output.264 265 Args:266 query: string of the query.267 268 Returns:269 output: string with the output of the query.270 is_success: boolean indicating whether the query was successful.271 """272 # wolfram query handler273 wolfram = WolframAlphaAPIWrapper()274 output, is_success = wolfram.run(query)275 if output == "":276 output = "Error: The wolfram query is invalid."277 is_success = False278 return output, is_success279 280 def _generate_math_reply(281 self,282 messages: Optional[List[Dict]] = None,283 sender: Optional[Agent] = None,284 config: Optional[Any] = None,285 ):286 """Generate an auto reply."""287 if messages is None:288 messages = self._oai_messages[sender]289 message = messages[-1]290 message = message.get("content", "")291 code_blocks = extract_code(message)292 293 if len(code_blocks) == 1 and code_blocks[0][0] == UNKNOWN:294 # no code block is found, lang should be `UNKNOWN``295 return True, self._default_auto_reply296 is_success, all_success = True, True297 reply = ""298 for code_block in code_blocks:299 lang, code = code_block300 if not lang:301 lang = infer_lang(code)302 if lang == "python":303 output, is_success = self.execute_one_python_code(code)304 elif lang == "wolfram":305 output, is_success = self.execute_one_wolfram_query(code)306 else:307 output = "Error: Unknown language."308 is_success = False309 310 reply += output + "\n"311 if not is_success:312 all_success = False313 self._valid_q_count -= 1 # count invalid queries314 315 reply = reply.strip()316 317 if self.last_reply == reply:318 return True, reply + "\nYour query or result is same from the last, please try a new approach."319 self.last_reply = reply320 321 if not all_success:322 self._accum_invalid_q_per_step += 1323 if self._accum_invalid_q_per_step > self._max_invalid_q_per_step:324 self._accum_invalid_q_per_step = 0325 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."326 327 return True, reply328 329 330# Modified based on langchain. Langchain is licensed under MIT License:331# The MIT License332 333# Copyright (c) Harrison Chase334 335# Permission is hereby granted, free of charge, to any person obtaining a copy336# of this software and associated documentation files (the "Software"), to deal337# in the Software without restriction, including without limitation the rights338# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell339# copies of the Software, and to permit persons to whom the Software is340# furnished to do so, subject to the following conditions:341 342# The above copyright notice and this permission notice shall be included in343# all copies or substantial portions of the Software.344 345# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR346# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,347# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE348# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER349# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,350# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN351# THE SOFTWARE.352 353 354def get_from_dict_or_env(data: Dict[str, Any], key: str, env_key: str, default: Optional[str] = None) -> str:355 """Get a value from a dictionary or an environment variable."""356 if key in data and data[key]:357 return data[key]358 elif env_key in os.environ and os.environ[env_key]:359 return os.environ[env_key]360 elif default is not None:361 return default362 else:363 raise ValueError(364 f"Did not find {key}, please add an environment variable"365 f" `{env_key}` which contains it, or pass"366 f" `{key}` as a named parameter."367 )368 369 370class WolframAlphaAPIWrapper(BaseModel):371 """Wrapper for Wolfram Alpha.372 373 Docs for using:374 375 1. Go to wolfram alpha and sign up for a developer account376 2. Create an app and get your APP ID377 3. Save your APP ID into WOLFRAM_ALPHA_APPID env variable378 4. pip install wolframalpha379 380 """381 382 wolfram_client: Any #: :meta private:383 wolfram_alpha_appid: Optional[str] = None384 385 class Config:386 """Configuration for this pydantic object."""387 388 if PYDANTIC_V1:389 extra = Extra.forbid390 391 @root_validator(skip_on_failure=True)392 def validate_environment(cls, values: Dict) -> Dict:393 """Validate that api key and python package exists in environment."""394 wolfram_alpha_appid = get_from_dict_or_env(values, "wolfram_alpha_appid", "WOLFRAM_ALPHA_APPID")395 values["wolfram_alpha_appid"] = wolfram_alpha_appid396 397 try:398 import wolframalpha399 400 except ImportError as e:401 raise ImportError("wolframalpha is not installed. Please install it with `pip install wolframalpha`") from e402 client = wolframalpha.Client(wolfram_alpha_appid)403 values["wolfram_client"] = client404 405 return values406 407 def run(self, query: str) -> Tuple[str, bool]:408 """Run query through WolframAlpha and parse result."""409 from urllib.error import HTTPError410 411 is_success = False # added412 res = None413 for _ in range(20):414 try:415 res = self.wolfram_client.query(query)416 break417 except HTTPError:418 sleep(1)419 except Exception:420 return (421 "Wolfram Alpha wasn't able to answer it. Please try a new query for wolfram or use python.",422 is_success,423 )424 if res is None:425 return (426 "Wolfram Alpha wasn't able to answer it (may due to web error), you can try again or use python.",427 is_success,428 )429 430 try:431 if not res["@success"]:432 return (433 "Your Wolfram query is invalid. Please try a new query for wolfram or use python.",434 is_success,435 )436 assumption = next(res.pods).text437 answer = ""438 for result in res["pod"]:439 if result["@title"] == "Solution":440 answer = result["subpod"]["plaintext"]441 if result["@title"] == "Results" or result["@title"] == "Solutions":442 for i, sub in enumerate(result["subpod"]):443 answer += f"ans {i}: " + sub["plaintext"] + "\n"444 break445 if answer == "":446 answer = next(res.results).text447 448 except Exception:449 return (450 "Wolfram Alpha wasn't able to answer it. Please try a new query for wolfram or use python.",451 is_success,452 )453 454 if answer is None or answer == "":455 # We don't want to return the assumption alone if answer is empty456 return "No good Wolfram Alpha Result was found", is_success457 is_success = True458 return f"Assumption: {assumption} \nAnswer: {answer}", is_success459 