A programming framework for agentic AI
Fix bug for windows and improve sample code (#38)
Chi Wang402c5bf8d176
diff --git a/.gitignore b/.gitignoreindex bb99e22a6..98517f9d6 100644--- a/.gitignore+++ b/.gitignore@@ -159,3 +159,8 @@ output/# local config files*.config.local+OAI_CONFIG_LIST+key_openai.txt+key_aoai.txt+base_aoai.txt+wolfram.txtdiff --git a/OAI_CONFIG_LIST_sample b/OAI_CONFIG_LIST_samplenew file mode 100644index 000000000..01608aeee--- /dev/null+++ b/OAI_CONFIG_LIST_sample@@ -0,0 +1,21 @@+// Please modify the content, remove this comment and rename this file to OAI_CONFIG_LIST to run the sample code.+[+ {+ "model": "gpt-4",+ "api_key": "<your OpenAI API key here>"+ },+ {+ "model": "gpt-4",+ "api_key": "<your Azure OpenAI API key here>",+ "api_base": "<your Azure OpenAI API base here>",+ "api_type": "azure",+ "api_version": "2023-07-01-preview"+ },+ {+ "model": "gpt-3.5-turbo",+ "api_key": "<your Azure OpenAI API key here>",+ "api_base": "<your Azure OpenAI API base here>",+ "api_type": "azure",+ "api_version": "2023-07-01-preview"+ }+]diff --git a/README.md b/README.mdindex e8853ffb2..005f63c60 100644--- a/README.md+++ b/README.md@@ -60,15 +60,24 @@ For LLM inference configurations, check the [FAQ](https://microsoft.github.io/au## Quickstart* Autogen enables the next-gen LLM applications with a generic multi-agent conversation framework. It offers customizable and conversable agents which integrate LLMs, tools and human.-By automating chat among multiple capable agents, one can easily make them collectively perform tasks autonomously or with human feedback, including tasks that require using tools via code. For example,+By automating chat among multiple capable agents, one can easily make them collectively perform tasks autonomously or with human feedback, including tasks that require using tools via code. For [example](https://github.com/microsoft/autogen/blob/main/test/twoagent.py),```python-from autogen import AssistantAgent, UserProxyAgent-assistant = AssistantAgent("assistant")-user_proxy = UserProxyAgent("user_proxy")-user_proxy.initiate_chat(assistant, message="Plot a chart of META and TESLA stock price change YTD.")+from autogen import AssistantAgent, UserProxyAgent, config_list_from_json+# Load LLM inference endpoints from an env variable or a file+# See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints+# and OAI_CONFIG_LIST_sample.json+config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")+assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})+user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding"})+user_proxy.initiate_chat(assistant, message="Plot a chart of NVDA and TESLA stock price change YTD.")# This initiates an automated chat between the two agents to solve the task```+This example can be run with+```python+python test/twoagent.py+```+After the repo is cloned.The figure below shows an example conversation flow with AutoGen.diff --git a/autogen/agentchat/assistant_agent.py b/autogen/agentchat/assistant_agent.pyindex c8528884c..cac322774 100644--- a/autogen/agentchat/assistant_agent.py+++ b/autogen/agentchat/assistant_agent.py@@ -16,7 +16,7 @@ class AssistantAgent(ConversableAgent):DEFAULT_SYSTEM_MESSAGE = """You are a helpful AI assistant.Solve tasks using your coding and language skills.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.- 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. 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.+ 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.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.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.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.diff --git a/autogen/code_utils.py b/autogen/code_utils.pyindex 1a6b970c6..e5e0a7e3a 100644--- a/autogen/code_utils.py+++ b/autogen/code_utils.py@@ -23,6 +23,8 @@ WORKING_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "extensiUNKNOWN = "unknown"TIMEOUT_MSG = "Timeout"DEFAULT_TIMEOUT = 600+WIN32 = sys.platform == "win32"+PATH_SEPARATOR = WIN32 and "\\" or "/"def infer_lang(code):@@ -193,10 +195,12 @@ def timeout_handler(signum, frame):def _cmd(lang):- if lang.startswith("python") or lang in ["bash", "sh"]:+ if lang.startswith("python") or lang in ["bash", "sh", "powershell"]:return lang- if lang == "shell":+ if lang in ["shell"]:return "sh"+ if lang in ["ps1"]:+ return "powershell"raise NotImplementedError(f"{lang} not recognized in code execution")@@ -242,6 +246,8 @@ def execute_code(assert code is not None or filename is not None, "Either code or filename must be provided."timeout = timeout or DEFAULT_TIMEOUToriginal_filename = filename+ if WIN32 and lang in ["sh", "shell"]:+ lang = "ps1"if filename is None:code_hash = md5(code.encode()).hexdigest()# create a file with a automatically generated name@@ -258,13 +264,17 @@ def execute_code(in_docker_container = os.path.exists("/.dockerenv")if not use_docker or in_docker_container:# already running in a docker container- cmd = [sys.executable if lang.startswith("python") else _cmd(lang), filename]- if sys.platform == "win32":+ cmd = [+ sys.executable if lang.startswith("python") else _cmd(lang),+ f".\\{filename}" if WIN32 else filename,+ ]+ if WIN32:logging.warning("SIGALRM is not supported on Windows. No timeout will be enforced.")result = subprocess.run(cmd,cwd=work_dir,capture_output=True,+ text=True,)else:signal.signal(signal.SIGALRM, timeout_handler)@@ -290,7 +300,7 @@ def execute_code(abs_path = str(pathlib.Path(filepath).absolute())logs = logs.replace(str(abs_path), "").replace(filename, "")else:- abs_path = str(pathlib.Path(work_dir).absolute()) + "/"+ abs_path = str(pathlib.Path(work_dir).absolute()) + PATH_SEPARATORlogs = logs.replace(str(abs_path), "")else:logs = result.stdoutdiff --git a/autogen/version.py b/autogen/version.pyindex b3f475621..ae7362549 100644--- a/autogen/version.py+++ b/autogen/version.py@@ -1 +1 @@-__version__ = "0.1.2"+__version__ = "0.1.3"diff --git a/notebook/agentchat_MathChat.ipynb b/notebook/agentchat_MathChat.ipynbindex bd7f75db5..226355c07 100644--- a/notebook/agentchat_MathChat.ipynb+++ b/notebook/agentchat_MathChat.ipynb@@ -5,7 +5,7 @@"cell_type": "markdown","metadata": {},"source": [- "<a href=\"https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/agentchat_MathChat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"+ "<a href=\"https://colab.research.google.com/github/microsoft/autogen/blob/main/notebook/agentchat_MathChat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"]},{@@ -43,7 +43,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"]},{diff --git a/notebook/agentchat_RetrieveChat.ipynb b/notebook/agentchat_RetrieveChat.ipynbindex 492f207d5..035dd01d8 100644--- a/notebook/agentchat_RetrieveChat.ipynb+++ b/notebook/agentchat_RetrieveChat.ipynb@@ -62,7 +62,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"]},{@@ -3684,6 +3684,7 @@]},{+ "attachments": {},"cell_type": "markdown","metadata": {},"source": [diff --git a/notebook/agentchat_auto_feedback_from_code_execution.ipynb b/notebook/agentchat_auto_feedback_from_code_execution.ipynbindex de81cf4f3..b26412f4f 100644--- a/notebook/agentchat_auto_feedback_from_code_execution.ipynb+++ b/notebook/agentchat_auto_feedback_from_code_execution.ipynb@@ -55,7 +55,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file.\n"]},{diff --git a/notebook/agentchat_chess.ipynb b/notebook/agentchat_chess.ipynbindex d859f4701..d104eeb0e 100644--- a/notebook/agentchat_chess.ipynb+++ b/notebook/agentchat_chess.ipynb@@ -56,7 +56,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."]},{diff --git a/notebook/agentchat_function_call.ipynb b/notebook/agentchat_function_call.ipynbindex 3c481fffd..4d0c2e933 100644--- a/notebook/agentchat_function_call.ipynb+++ b/notebook/agentchat_function_call.ipynb@@ -47,7 +47,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_models`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_models) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints for the provided list of models. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",+ "The [`config_list_from_models`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_models) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints for the provided list of models. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n","\n","- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n","- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n",diff --git a/notebook/agentchat_groupchat.ipynb b/notebook/agentchat_groupchat.ipynbindex 6964408eb..da769f3a2 100644--- a/notebook/agentchat_groupchat.ipynb+++ b/notebook/agentchat_groupchat.ipynb@@ -45,7 +45,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."]},{diff --git a/notebook/agentchat_groupchat_vis.ipynb b/notebook/agentchat_groupchat_vis.ipynbindex 4348d224d..1c619078f 100644--- a/notebook/agentchat_groupchat_vis.ipynb+++ b/notebook/agentchat_groupchat_vis.ipynb@@ -43,7 +43,7 @@"source": ["## Set your API Endpoint\n","\n",- "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."+ "The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."]},{@@ -1029,7 +1029,7 @@"name": "python","nbconvert_exporter": "python","pygments_lexer": "ipython3",- "version": "3.9.17"+ "version": "3.10.12"},"orig_nbformat": 4},diff --git a/notebook/agentchat_teaching.ipynb b/notebook/agentchat_teaching.ipynbindex 8c79cb969..f11a91ae1 100644--- a/notebook/agentchat_teaching.ipynb+++ b/notebook/agentchat_teaching.ipynb@@ -81,7 +81,7 @@"metadata": {},"outputs": [],"source": [- "from flaml import autogen\n",+ "import autogen\n","\n","llm_config={\n"," \"request_timeout\": 600,\n",@@ -95,6 +95,7 @@]},{+ "attachments": {},"cell_type": "markdown","metadata": {},"source": [@@ -104,6 +105,7 @@]},{+ "attachments": {},"cell_type": "markdown","metadata": {},"source": [@@ -138,6 +140,7 @@]},{+ "attachments": {},"cell_type": "markdown","metadata": {},"source": [@@ -697,6 +700,7 @@]},{+ "attachments": {},"cell_type": "markdown","metadata": {},"source": [@@ -825,6 +829,7 @@]},{+ "attachments": {},"cell_type": "markdown","metadata": {},"source": [diff --git a/notebook/oai_chatgpt_gpt4.ipynb b/notebook/oai_chatgpt_gpt4.ipynbindex c3f09943e..9f273db8b 100644--- a/notebook/oai_chatgpt_gpt4.ipynb+++ b/notebook/oai_chatgpt_gpt4.ipynb@@ -88,7 +88,7 @@"source": ["### Set your API Endpoint\n","\n",- "The [`config_list_openai_aoai`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_openai_aoai) function tries to create a list of Azure OpenAI endpoints and OpenAI endpoints. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",+ "The [`config_list_openai_aoai`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_openai_aoai) function tries to create a list of Azure OpenAI endpoints and OpenAI endpoints. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n","\n","- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n","- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n",diff --git a/notebook/oai_completion.ipynb b/notebook/oai_completion.ipynbindex 01cd77a49..029157490 100644--- a/notebook/oai_completion.ipynb+++ b/notebook/oai_completion.ipynb@@ -59,11 +59,11 @@"source": ["## Set your API Endpoint\n","\n",- "* The [`config_list_openai_aoai`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_openai_aoai) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",+ "* The [`config_list_openai_aoai`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_openai_aoai) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n"," - OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n"," - Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n"," - Azure OpenAI API base: os.environ[\"AZURE_OPENAI_API_BASE\"] or `aoai_api_base_file=\"base_aoai.txt\"`. Multiple bases can be stored, one per line.\n",- "* The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file. It first looks for environment variable `env_or_file` which needs to be a valid json string. If that variable is not found, it then looks for a json file with the same name. It filters the configs by filter_dict.\n",+ "* The [`config_list_from_json`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file. It first looks for environment variable `env_or_file` which needs to be a valid json string. If that variable is not found, it then looks for a json file with the same name. It filters the configs by filter_dict.\n","\n","It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base. If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose \"upload file\" icon.\n"]diff --git a/test/test_code.py b/test/test_code.pyindex ab2d5d591..3c967f65c 100644--- a/test/test_code.py+++ b/test/test_code.py@@ -9,6 +9,8 @@ from autogen.code_utils import (infer_lang,improve_code,improve_function,+ PATH_SEPARATOR,+ WIN32,)KEY_LOC = "notebook"@@ -214,36 +216,56 @@ print(f"Text: {text}")@pytest.mark.skipif(- sys.platform in ["darwin", "win32"],- reason="do not run on MacOS or windows",+ sys.platform in ["darwin"],+ reason="do not run on MacOS",)-def test_execute_code():+def test_execute_code(use_docker=None):try:import dockerexcept ImportError as exc:print(exc)docker = None- exit_code, msg, image = execute_code("print('hello world')", filename="tmp/codetest.py")+ if use_docker is None:+ use_docker = docker is not None+ exit_code, msg, image = execute_code("print('hello world')", filename="tmp/codetest.py", use_docker=use_docker)assert exit_code == 0 and msg == "hello world\n", msg# read a file- print(execute_code("with open('tmp/codetest.py', 'r') as f: a=f.read()"))+ print(execute_code("with open('tmp/codetest.py', 'r') as f: a=f.read()", use_docker=use_docker))# create a fileexit_code, msg, image = execute_code(- "with open('tmp/codetest.py', 'w') as f: f.write('b=1')", work_dir=f"{here}/my_tmp", filename="tmp2/codetest.py"+ "with open('tmp/codetest.py', 'w') as f: f.write('b=1')",+ work_dir=f"{here}/my_tmp",+ filename="tmp2/codetest.py",+ use_docker=use_docker,+ )+ assert exit_code and (+ 'File "tmp2/codetest.py"'.replace("/", PATH_SEPARATOR) in msg+ or 'File ".\\tmp2/codetest.py' in msg # py3.8 + win32+ ), msg+ print(+ execute_code(+ "with open('tmp/codetest.py', 'w') as f: f.write('b=1')", work_dir=f"{here}/my_tmp", use_docker=use_docker+ ))- assert exit_code and 'File "tmp2/codetest.py"' in msg, msg- print(execute_code("with open('tmp/codetest.py', 'w') as f: f.write('b=1')", work_dir=f"{here}/my_tmp"))# execute code in a file- print(execute_code(filename="tmp/codetest.py"))- print(execute_code("python tmp/codetest.py", lang="sh"))+ print(execute_code(filename="tmp/codetest.py", use_docker=use_docker))+ print(execute_code("python tmp/codetest.py", lang="sh", use_docker=use_docker))# execute code for assertion error- exit_code, msg, image = execute_code("assert 1==2")+ exit_code, msg, image = execute_code("assert 1==2", use_docker=use_docker)assert exit_code, msg- assert 'File ""' in msg+ assert 'File ""' in msg or 'File ".\\"' in msg # py3.8 + win32# execute code which takes a long time- exit_code, error, image = execute_code("import time; time.sleep(2)", timeout=1)- assert exit_code and error == "Timeout"- assert isinstance(image, str) or docker is None or os.path.exists("/.dockerenv")+ exit_code, error, image = execute_code("import time; time.sleep(2)", timeout=1, use_docker=use_docker)+ assert exit_code and error == "Timeout" or WIN32+ assert isinstance(image, str) or docker is None or os.path.exists("/.dockerenv") or use_docker is False+++ sys.platform in ["darwin"],+ reason="do not run on MacOS",+)+def test_execute_code_nodocker():+ test_execute_code(use_docker=False)def test_execute_code_no_docker():diff --git a/test/twoagent.py b/test/twoagent.pynew file mode 100644index 000000000..cc5c435d4--- /dev/null+++ b/test/twoagent.py@@ -0,0 +1,9 @@+from autogen import AssistantAgent, UserProxyAgent, config_list_from_json++# Load LLM inference endpoints from an env variable or a file+# See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints+# and OAI_CONFIG_LIST_sample.json+config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")+assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})+user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding"})+user_proxy.initiate_chat(assistant, message="Plot a chart of NVDA and TESLA stock price change YTD.")diff --git a/website/docs/Getting-Started.md b/website/docs/Getting-Started.mdindex d8ae61830..e39f3f572 100644--- a/website/docs/Getting-Started.md+++ b/website/docs/Getting-Started.md@@ -25,12 +25,17 @@ Install from pip: `pip install pyautogen`. Find more options in [Installation](/#### Multi-Agent Conversation FrameworkAutogen enables the next-gen LLM applications with a generic multi-agent conversation framework. It offers customizable and conversable agents which integrate LLMs, tools and human.-By automating chat among multiple capable agents, one can easily make them collectively perform tasks autonomously or with human feedback, including tasks that require using tools via code. For example,+By automating chat among multiple capable agents, one can easily make them collectively perform tasks autonomously or with human feedback, including tasks that require using tools via code. For [example](https://github.com/microsoft/autogen/blob/main/test/twoagent.py),```python-from autogen import AssistantAgent, UserProxyAgent-assistant = AssistantAgent("assistant")-user_proxy = UserProxyAgent("user_proxy")-user_proxy.initiate_chat(assistant, message="Plot a chart of META and TESLA stock price change YTD.")+from autogen import AssistantAgent, UserProxyAgent, config_list_from_json++# Load LLM inference endpoints from an env variable or a file+# See https://microsoft.github.io/autogen/docs/FAQ#set-your-api-endpoints+# and OAI_CONFIG_LIST_sample.json+config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST")+assistant = AssistantAgent("assistant", llm_config={"config_list": config_list})+user_proxy = UserProxyAgent("user_proxy", code_execution_config={"work_dir": "coding"})+user_proxy.initiate_chat(assistant, message="Plot a chart of NVDA and TESLA stock price change YTD.")# This initiates an automated chat between the two agents to solve the task```