├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── group.py ├── research.py └── simple.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 105 | __pypackages__/ 106 | 107 | # Celery stuff 108 | celerybeat-schedule 109 | celerybeat.pid 110 | 111 | # SageMath parsed files 112 | *.sage.py 113 | 114 | # Environments 115 | .env 116 | .venv 117 | env/ 118 | venv/ 119 | ENV/ 120 | env.bak/ 121 | venv.bak/ 122 | 123 | # Spyder project settings 124 | .spyderproject 125 | .spyproject 126 | 127 | # Rope project settings 128 | .ropeproject 129 | 130 | # mkdocs documentation 131 | /site 132 | 133 | # mypy 134 | .mypy_cache/ 135 | .dmypy.json 136 | dmypy.json 137 | 138 | # Pyre type checker 139 | .pyre/ 140 | 141 | # pytype static type analyzer 142 | .pytype/ 143 | 144 | # Cython debug symbols 145 | cython_debug/ 146 | 147 | # PyCharm 148 | # JetBrains specific template is maintainted in a separate JetBrains.gitignore that can 149 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 150 | # and can be added to the global gitignore or merged into this file. For a more nuclear 151 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 152 | #.idea/ 153 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Microsoft_AutoGen_Tutorial 2 | 3 | 4 | ## AI Tutorial 5 5 | ## 动画演示透彻解释 微软 AutoGen 框架概念 以及 实现Demo 6 | 7 | ## 涉及工具 8 | * Serper 9 | * Browserless 10 | 11 | ## 使用前 12 | * 运行前,请务必先自行完善 13 | * OAI_CONFIG_LIST 文件模版可从 AutoGen源码获取 14 | * .env文件中的参数。包括但不限于各类 API key\Secret Key等。 15 | * 如果有疑问, 欢迎 email: 798800165@qq.com 16 | 17 | ## 关于 Cost 18 | * Demo3 对 OpenAI 的依赖和消耗略微有些大,请关注,自行决定测试频率 19 | 20 | ## 视频讲解 21 | * B站 https://www.bilibili.com/video/BV1Fw41167v9/ 22 | * youtube https://youtu.be/hFYGI4MqAEc 23 | 24 | ## 小小声明 25 | 26 | ### 本项目代码均为演示 Demo 和授课讲解用,因为不是生产环境使用且时间仓促原因,代码质量请各位包涵,如果实际应用,请务必按照代码规范完善,谢谢 27 | -------------------------------------------------------------------------------- /group.py: -------------------------------------------------------------------------------- 1 | from autogen import config_list_from_json 2 | import autogen 3 | 4 | # 配置 api key 5 | config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST") 6 | llm_config = {"config_list": config_list, "seed": 42, "request_timeout": 120} 7 | 8 | # 创建 user proxy agent, coder, product manager 三个不同的角色 9 | user_proxy = autogen.UserProxyAgent( 10 | name="User_proxy", 11 | system_message="A human admin who will give the idea and run the code provided by Coder.", 12 | code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"}, #最多接收的响应的数量 13 | human_input_mode="ALWAYS", #注意这个 mode 的选择 14 | ) 15 | 16 | coder = autogen.AssistantAgent( 17 | name="Coder", 18 | llm_config=llm_config, 19 | ) 20 | 21 | pm = autogen.AssistantAgent( 22 | name="product_manager", 23 | system_message="You will help break down the initial idea into a well scoped requirement for the coder; Do not involve in future conversations or error fixing", 24 | llm_config=llm_config, 25 | ) 26 | 27 | # 创建 组 groupchat 28 | groupchat = autogen.GroupChat( 29 | agents=[user_proxy, coder, pm], messages=[]) 30 | manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config) 31 | 32 | # 初始化 开始干活 33 | user_proxy.initiate_chat( 34 | manager, message="Build a classic & basic pong game with 2 players in python") 35 | -------------------------------------------------------------------------------- /research.py: -------------------------------------------------------------------------------- 1 | import os 2 | from autogen import config_list_from_json 3 | import autogen 4 | 5 | import requests 6 | from bs4 import BeautifulSoup 7 | import json 8 | 9 | from langchain.agents import initialize_agent 10 | from langchain.chat_models import ChatOpenAI 11 | from langchain.text_splitter import RecursiveCharacterTextSplitter 12 | from langchain.chains.summarize import load_summarize_chain 13 | from langchain import PromptTemplate 14 | import openai 15 | from dotenv import load_dotenv 16 | 17 | # 获取各个 API Key 18 | load_dotenv() 19 | config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST") 20 | #具体可参见第三期视频 21 | openai.api_key = os.getenv("OPENAI_API_KEY") 22 | serper_api_key=os.getenv("SERPER_API_KEY") 23 | browserless_api_key=os.getenv("BROWSERLESS_API_KEY") 24 | 25 | # research 工具模块 26 | 27 | #调用 Google search by Serper 28 | def search(query): 29 | url = "https://google.serper.dev/search" 30 | 31 | payload = json.dumps({ 32 | "q": query 33 | }) 34 | headers = { 35 | 'X-API-KEY': serper_api_key, 36 | 'Content-Type': 'application/json' 37 | } 38 | 39 | response = requests.request("POST", url, headers=headers, data=payload) 40 | 41 | return response.json() 42 | 43 | 44 | #抓取网站内容 45 | def scrape(url: str): 46 | # scrape website, and also will summarize the content based on objective if the content is too large 47 | # objective is the original objective & task that user give to the agent, url is the url of the website to be scraped 48 | 49 | print("Scraping website...") 50 | # Define the headers for the request 51 | headers = { 52 | 'Cache-Control': 'no-cache', 53 | 'Content-Type': 'application/json', 54 | } 55 | 56 | # Define the data to be sent in the request 57 | data = { 58 | "url": url 59 | } 60 | 61 | # Convert Python object to JSON string 62 | data_json = json.dumps(data) 63 | 64 | # Send the POST request 65 | post_url = f"https://chrome.browserless.io/content?token={browserless_api_key}" 66 | response = requests.post( 67 | post_url, headers=headers, data=data_json) 68 | 69 | # Check the response status code 70 | if response.status_code == 200: 71 | soup = BeautifulSoup(response.content, "html.parser") 72 | text = soup.get_text() 73 | print("CONTENT:", text) 74 | if len(text) > 8000: 75 | output = summary(text) 76 | return output 77 | else: 78 | return text 79 | else: 80 | print(f"HTTP request failed with status code {response.status_code}") 81 | 82 | #总结网站内容 83 | def summary(content): 84 | llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-16k-0613") 85 | text_splitter = RecursiveCharacterTextSplitter( 86 | separators=["\n\n", "\n"], chunk_size=10000, chunk_overlap=500) 87 | docs = text_splitter.create_documents([content]) 88 | map_prompt = """ 89 | Write a detailed summary of the following text for a research purpose: 90 | "{text}" 91 | SUMMARY: 92 | """ 93 | map_prompt_template = PromptTemplate( 94 | template=map_prompt, input_variables=["text"]) 95 | 96 | summary_chain = load_summarize_chain( 97 | llm=llm, 98 | chain_type='map_reduce', #内容切片 防止超过 LLM 的 Token 限制 99 | map_prompt=map_prompt_template, 100 | combine_prompt=map_prompt_template, 101 | verbose=True 102 | ) 103 | 104 | output = summary_chain.run(input_documents=docs,) 105 | 106 | return output 107 | 108 | # 信息收集 109 | def research(query): 110 | llm_config_researcher = { 111 | "functions": [ 112 | { 113 | "name": "search", 114 | "description": "google search for relevant information", 115 | "parameters": { 116 | "type": "object", 117 | "properties": { 118 | "query": { 119 | "type": "string", 120 | "description": "Google search query", 121 | } 122 | }, 123 | "required": ["query"], 124 | }, 125 | }, 126 | { 127 | "name": "scrape", 128 | "description": "Scraping website content based on url", 129 | "parameters": { 130 | "type": "object", 131 | "properties": { 132 | "url": { 133 | "type": "string", 134 | "description": "Website url to scrape", 135 | } 136 | }, 137 | "required": ["url"], 138 | }, 139 | }, 140 | ], 141 | "config_list": config_list} 142 | 143 | researcher = autogen.AssistantAgent( 144 | name="researcher", 145 | system_message="Research about a given query, collect as many information as possible, and generate detailed research results with loads of technique details with all reference links attached; Add TERMINATE to the end of the research report;", 146 | llm_config=llm_config_researcher, 147 | ) 148 | 149 | user_proxy = autogen.UserProxyAgent( 150 | name="User_proxy", 151 | code_execution_config={"last_n_messages": 2, "work_dir": "research"}, 152 | is_termination_msg=lambda x: x.get("content", "") and x.get( 153 | "content", "").rstrip().endswith("TERMINATE"), 154 | human_input_mode="TERMINATE", 155 | function_map={ 156 | "search": search, 157 | "scrape": scrape, 158 | } 159 | ) 160 | 161 | user_proxy.initiate_chat(researcher, message=query) 162 | 163 | # set the receiver to be researcher, and get a summary of the research report 164 | user_proxy.stop_reply_at_receive(researcher) 165 | user_proxy.send( 166 | "Give me the research report that just generated again, return ONLY the report & reference links", researcher) 167 | 168 | # return the last message the expert received 169 | return user_proxy.last_message()["content"] 170 | 171 | 172 | # 编辑 配置不同的 agent 角色 173 | def write_content(research_material, topic): 174 | editor = autogen.AssistantAgent( 175 | name="editor", 176 | system_message="You are a senior editor of an AI blogger, you will define the structure of a short blog post based on material provided by the researcher, and give it to the writer to write the blog post", 177 | llm_config={"config_list": config_list}, 178 | ) 179 | 180 | writer = autogen.AssistantAgent( 181 | name="writer", 182 | system_message="You are a professional AI blogger who is writing a blog post about AI, you will write a short blog post based on the structured provided by the editor, and feedback from reviewer; After 2 rounds of content iteration, add TERMINATE to the end of the message", 183 | llm_config={"config_list": config_list}, 184 | ) 185 | 186 | reviewer = autogen.AssistantAgent( 187 | name="reviewer", 188 | system_message="You are a world class hash tech blog content critic, you will review & critic the written blog and provide feedback to writer.After 2 rounds of content iteration, add TERMINATE to the end of the message", 189 | llm_config={"config_list": config_list}, 190 | ) 191 | 192 | user_proxy = autogen.UserProxyAgent( 193 | name="admin", 194 | system_message="A human admin. Interact with editor to discuss the structure. Actual writing needs to be approved by this admin.", 195 | code_execution_config=False, 196 | is_termination_msg=lambda x: x.get("content", "") and x.get( 197 | "content", "").rstrip().endswith("TERMINATE"), 198 | human_input_mode="TERMINATE", #终止模式 199 | ) 200 | 201 | #创建 组 202 | groupchat = autogen.GroupChat( 203 | agents=[user_proxy, editor, writer, reviewer], 204 | messages=[], 205 | max_round=20) 206 | manager = autogen.GroupChatManager(groupchat=groupchat) 207 | 208 | #消息交换机制部分 重点 209 | user_proxy.initiate_chat( 210 | manager, message=f"Write a blog about {topic}, here are the material: {research_material}") 211 | 212 | user_proxy.stop_reply_at_receive(manager) 213 | user_proxy.send( 214 | "Give me the blog that just generated again, return ONLY the blog, and add TERMINATE in the end of the message", manager) 215 | 216 | # return the last message the expert received 217 | return user_proxy.last_message()["content"] 218 | 219 | 220 | # 出版 221 | llm_config_content_assistant = { 222 | "functions": [ 223 | { 224 | "name": "research", 225 | "description": "research about a given topic, return the research material including reference links", 226 | "parameters": { 227 | "type": "object", 228 | "properties": { 229 | "query": { 230 | "type": "string", 231 | "description": "The topic to be researched about", 232 | } 233 | }, 234 | "required": ["query"], 235 | }, 236 | }, 237 | { 238 | "name": "write_content", 239 | "description": "Write content based on the given research material & topic", 240 | "parameters": { 241 | "type": "object", 242 | "properties": { 243 | "research_material": { 244 | "type": "string", 245 | "description": "research material of a given topic, including reference links when available", 246 | }, 247 | "topic": { 248 | "type": "string", 249 | "description": "The topic of the content", 250 | } 251 | }, 252 | "required": ["research_material", "topic"], 253 | }, 254 | }, 255 | ], 256 | "config_list": config_list} 257 | 258 | writing_assistant = autogen.AssistantAgent( 259 | name="writing_assistant", 260 | system_message="You are a writing assistant, you can use research function to collect latest information about a given topic, and then use write_content function to write a very well written content; Reply TERMINATE when your task is done", 261 | llm_config=llm_config_content_assistant, 262 | ) 263 | 264 | user_proxy = autogen.UserProxyAgent( 265 | name="User_proxy", 266 | human_input_mode="TERMINATE", #注意此处的模式选择 267 | function_map={ 268 | "write_content": write_content, #调用编辑和信息 组 269 | "research": research, 270 | } 271 | ) 272 | 273 | #最初 需求 启动干活 274 | user_proxy.initiate_chat( 275 | writing_assistant, message="write a blog about autogen multi AI agent framework") 276 | -------------------------------------------------------------------------------- /simple.py: -------------------------------------------------------------------------------- 1 | from autogen import AssistantAgent, UserProxyAgent, config_list_from_json 2 | 3 | # 配置 openai api key 4 | config_list = config_list_from_json(env_or_file="OAI_CONFIG_LIST") 5 | 6 | # 创建 assistant agent 7 | assistant = AssistantAgent(name="assistant", llm_config={ 8 | "config_list": config_list}) 9 | 10 | # 创建 user proxy agent 11 | user_proxy = UserProxyAgent( 12 | name="user_proxy", 13 | code_execution_config={"work_dir": "sources"}) 14 | 15 | 16 | # 设置目标 开始干活 17 | user_proxy.initiate_chat( 18 | assistant, message="Plot a chart of NVDA and TESLA stock price change YTD.") 19 | --------------------------------------------------------------------------------