├── .gitignore ├── LICENSE ├── README.md ├── demo.py ├── images ├── React.png ├── Tiny_Agent.jpg └── example.gif ├── requirements.txt └── src ├── __init__.py ├── core.py ├── tools.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Editors 2 | .vscode/ 3 | .idea/ 4 | 5 | # Vagrant 6 | .vagrant/ 7 | 8 | # Mac/OSX 9 | .DS_Store 10 | 11 | # Windows 12 | Thumbs.db 13 | 14 | # Source for the following rules: https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore 15 | # Byte-compiled / optimized / DLL files 16 | __pycache__/ 17 | *.py[cod] 18 | *$py.class 19 | 20 | # C extensions 21 | *.so 22 | 23 | # Distribution / packaging 24 | .Python 25 | build/ 26 | develop-eggs/ 27 | dist/ 28 | downloads/ 29 | eggs/ 30 | .eggs/ 31 | lib/ 32 | lib64/ 33 | parts/ 34 | sdist/ 35 | var/ 36 | wheels/ 37 | *.egg-info/ 38 | .installed.cfg 39 | *.egg 40 | MANIFEST 41 | 42 | # PyInstaller 43 | # Usually these files are written by a python script from a template 44 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 45 | *.manifest 46 | *.spec 47 | 48 | # Installer logs 49 | pip-log.txt 50 | pip-delete-this-directory.txt 51 | 52 | # Unit test / coverage reports 53 | htmlcov/ 54 | .tox/ 55 | .nox/ 56 | .coverage 57 | .coverage.* 58 | .cache 59 | nosetests.xml 60 | coverage.xml 61 | *.cover 62 | .hypothesis/ 63 | .pytest_cache/ 64 | 65 | # Translations 66 | *.mo 67 | *.pot 68 | 69 | # Django stuff: 70 | *.log 71 | local_settings.py 72 | db.sqlite3 73 | 74 | # Flask stuff: 75 | instance/ 76 | .webassets-cache 77 | 78 | # Scrapy stuff: 79 | .scrapy 80 | 81 | # Sphinx documentation 82 | docs/_build/ 83 | 84 | # PyBuilder 85 | target/ 86 | 87 | # Jupyter Notebook 88 | .ipynb_checkpoints 89 | 90 | # IPython 91 | profile_default/ 92 | ipython_config.py 93 | 94 | # pyenv 95 | .python-version 96 | 97 | # celery beat schedule file 98 | celerybeat-schedule 99 | 100 | # SageMath parsed files 101 | *.sage.py 102 | 103 | # Environments 104 | .env 105 | .venv 106 | env/ 107 | venv/ 108 | ENV/ 109 | env.bak/ 110 | venv.bak/ 111 | 112 | # Spyder project settings 113 | .spyderproject 114 | .spyproject 115 | 116 | # Rope project settings 117 | .ropeproject 118 | 119 | # mkdocs documentation 120 | /site 121 | 122 | # mypy 123 | .mypy_cache/ 124 | .dmypy.json 125 | dmypy.json -------------------------------------------------------------------------------- /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 | # TinyAgent 2 | 3 | 在`ChatGPT`横空出世,夺走`Bert`的桂冠之后,大模型愈发的火热,国内各种模型层出不穷,史称“百模大战”。大模型的能力是毋庸置疑的,但大模型在一些实时的问题上,或是某些专有领域的问题上,可能会显得有些力不从心。因此,我们需要一些工具来为大模型赋能,给大模型一个抓手,让大模型和现实世界发生的事情对齐颗粒度,这样我们就获得了一个更好的用的大模型。 4 | 5 | 本项目基于 `openai` 库和其 `tool_calls` 功能,实现了一个简单的 Agent 结构,可以调用预定义的工具函数来完成特定任务。 6 | 7 | 通过这个简单的例子,我们可以了解 Agent 如何利用大模型和外部工具进行交互。 8 | 9 |
10 | 11 |
12 | 13 | ## 实现细节 14 | 15 | ### Step 1: 初始化客户端和模型 16 | 17 | 首先,我们需要一个能够调用大模型的客户端。这里我们使用 `openai` 库,并配置其指向一个兼容 OpenAI API 的服务终端,例如 [SiliconFlow](https://cloud.siliconflow.cn/i/ybUFvmqK)。同时,指定要使用的模型,如 `Qwen/Qwen2.5-32B-Instruct`。 18 | 19 | ```python 20 | from openai import OpenAI 21 | 22 | # 初始化 OpenAI 客户端 23 | client = OpenAI( 24 | api_key="YOUR_API_KEY", # 替换为你的 API Key 25 | base_url="https://api.siliconflow.cn/v1", # 使用 SiliconFlow 的 API 地址 26 | ) 27 | 28 | # 指定模型名称 29 | model_name = "Qwen/Qwen2.5-32B-Instruct" 30 | ``` 31 | 32 | > **注意:** 你需要将 `YOUR_API_KEY` 替换为你从 [SiliconFlow](https://cloud.siliconflow.cn/i/ybUFvmqK) 或其他服务商获取的有效 API Key。 33 | 34 | ### Step 2: 定义工具函数 35 | 36 | 我们在 `src/tools.py` 文件中定义 Agent 可以使用的工具函数。每个函数都需要有清晰的文档字符串(docstring),描述其功能和参数,因为这将用于自动生成工具的 JSON Schema。 37 | 38 | ```python 39 | # src/tools.py 40 | from datetime import datetime 41 | 42 | # 获取当前日期和时间 43 | def get_current_datetime() -> str: 44 | """ 45 | 获取当前日期和时间。 46 | :return: 当前日期和时间的字符串表示。 47 | """ 48 | current_datetime = datetime.now() 49 | formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S") 50 | return formatted_datetime 51 | 52 | def add(a: float, b: float): 53 | """ 54 | 计算两个浮点数的和。 55 | :param a: 第一个浮点数。 56 | :param b: 第二个浮点数。 57 | :return: 两个浮点数的和。 58 | """ 59 | return a + b 60 | 61 | def compare(a: float, b: float): 62 | """ 63 | 比较两个浮点数的大小。 64 | :param a: 第一个浮点数。 65 | :param b: 第二个浮点数。 66 | :return: 比较结果的字符串表示。 67 | """ 68 | if a > b: 69 | return f'{a} is greater than {b}' 70 | elif a < b: 71 | return f'{b} is greater than {a}' 72 | else: 73 | return f'{a} is equal to {b}' 74 | 75 | def count_letter_in_string(a: str, b: str): 76 | """ 77 | 统计字符串中某个字母的出现次数。 78 | :param a: 要搜索的字符串。 79 | :param b: 要统计的字母。 80 | :return: 字母在字符串中出现的次数。 81 | """ 82 | return a.count(b) 83 | 84 | # ... (可能还有其他工具函数) 85 | ``` 86 | 87 | 为了让 OpenAI API 理解这些工具,我们需要将它们转换成特定的 JSON Schema 格式。这可以通过 `src/utils.py` 中的 `function_to_json` 辅助函数完成。 88 | 89 | ```python 90 | # src/utils.py (部分) 91 | import inspect 92 | 93 | def function_to_json(func) -> dict: 94 | # ... (函数实现细节) 95 | # 返回符合 OpenAI tool schema 的字典 96 | return { 97 | "type": "function", 98 | "function": { 99 | "name": func.__name__, 100 | "description": inspect.getdoc(func), 101 | "parameters": { 102 | "type": "object", 103 | "properties": parameters, 104 | "required": required, 105 | }, 106 | }, 107 | } 108 | ``` 109 | 110 | ### Step 3: 构造 Agent 类 111 | 112 | 我们在 `src/core.py` 文件中定义 `Agent` 类。这个类负责管理对话历史、调用 OpenAI API、处理工具调用请求以及执行工具函数。 113 | 114 | ```python 115 | # src/core.py (部分) 116 | from openai import OpenAI 117 | import json 118 | from typing import List, Dict, Any 119 | from utils import function_to_json 120 | # 导入定义好的工具函数 121 | from tools import get_current_datetime, add, compare, count_letter_in_string 122 | 123 | SYSREM_PROMPT = """ 124 | 你是一个叫不要葱姜蒜的人工智能助手。你的输出应该与用户的语言保持一致。 125 | 当用户的问题需要调用工具时,你可以从提供的工具列表中调用适当的工具函数。 126 | """ 127 | 128 | class Agent: 129 | def __init__(self, client: OpenAI, model: str = "Qwen/Qwen2.5-32B-Instruct", tools: List=[], verbose : bool = True): 130 | self.client = client 131 | self.tools = tools # 存储可用的工具函数列表 132 | self.model = model 133 | self.messages = [ 134 | {"role": "system", "content": SYSREM_PROMPT}, 135 | ] 136 | self.verbose = verbose 137 | 138 | def get_tool_schema(self) -> List[Dict[str, Any]]: 139 | # 使用 utils.function_to_json 获取所有工具的 JSON Schema 140 | return [function_to_json(tool) for tool in self.tools] 141 | 142 | def handle_tool_call(self, tool_call): 143 | # 处理来自模型的工具调用请求 144 | function_name = tool_call.function.name 145 | function_args = tool_call.function.arguments 146 | function_id = tool_call.id 147 | 148 | # 动态执行工具函数 149 | # 注意:实际应用中应添加更严格的安全检查 150 | function_call_content = eval(f"{function_name}(**{function_args})") 151 | 152 | # 返回工具执行结果给模型 153 | return { 154 | "role": "tool", 155 | "content": function_call_content, 156 | "tool_call_id": function_id, 157 | } 158 | 159 | def get_completion(self, prompt) -> str: 160 | # 主对话逻辑 161 | self.messages.append({"role": "user", "content": prompt}) 162 | 163 | # 第一次调用模型,传入工具 Schema 164 | response = self.client.chat.completions.create( 165 | model=self.model, 166 | messages=self.messages, 167 | tools=self.get_tool_schema(), 168 | stream=False, 169 | ) 170 | 171 | # 检查模型是否请求调用工具 172 | if response.choices[0].message.tool_calls: 173 | tool_list = [] 174 | # 处理所有工具调用请求 175 | for tool_call in response.choices[0].message.tool_calls: 176 | # 执行工具并将结果添加到消息历史中 177 | self.messages.append(self.handle_tool_call(tool_call)) 178 | tool_list.append(tool_call.function.name) 179 | if self.verbose: 180 | print("调用工具:", tool_list) 181 | 182 | # 第二次调用模型,传入工具执行结果 183 | response = self.client.chat.completions.create( 184 | model=self.model, 185 | messages=self.messages, 186 | tools=self.get_tool_schema(), # 再次传入 Schema 可能有助于模型理解上下文 187 | stream=False, 188 | ) 189 | 190 | # 将最终的助手回复添加到消息历史 191 | self.messages.append({"role": "assistant", "content": response.choices[0].message.content}) 192 | return response.choices[0].message.content 193 | ``` 194 | 195 | 这个 Agent 的工作流程如下: 196 | 1. 接收用户输入。 197 | 2. 调用大模型(如 Qwen),并告知其可用的工具及其 Schema。 198 | 3. 如果模型决定调用工具,Agent 会解析请求,执行相应的 Python 函数。 199 | 4. Agent 将工具的执行结果返回给模型。 200 | 5. 模型根据工具结果生成最终回复。 201 | 6. Agent 将最终回复返回给用户。 202 | 203 |
204 | 205 |
206 | 207 | ### Step 4: 运行 Agent 208 | 209 | 现在我们可以实例化并运行 Agent。在 `demo.py` 的 `if __name__ == "__main__":` 部分提供了一个简单的命令行交互示例。 210 | 211 | ```python 212 | # demo.py (部分) 213 | if __name__ == "__main__": 214 | client = OpenAI( 215 | api_key="YOUR_API_KEY", # 替换为你的 API Key 216 | base_url="https://api.siliconflow.cn/v1", 217 | ) 218 | 219 | # 创建 Agent 实例,传入 client、模型名称和工具函数列表 220 | agent = Agent( 221 | client=client, 222 | model="Qwen/Qwen2.5-32B-Instruct", 223 | tools=[get_current_datetime, add, compare, count_letter_in_string], 224 | verbose=True # 设置为 True 可以看到工具调用信息 225 | ) 226 | 227 | # 开始交互式对话循环 228 | while True: 229 | # 使用彩色输出区分用户输入和AI回答 230 | prompt = input("\033[94mUser: \033[0m") # 蓝色显示用户输入提示 231 | if prompt.lower() == "exit": 232 | break 233 | response = agent.get_completion(prompt) 234 | print("\033[92mAssistant: \033[0m", response) # 绿色显示AI助手回答 235 | ``` 236 | 237 | 运行 `python src/core.py` 后,你可以开始提问。如果问题需要调用工具,Agent 会自动处理。 238 | 239 | **示例交互:** 240 | 241 | ```bash 242 | User: 你好 243 | Assistant: 你好!有什么可以帮助你的吗? 244 | User: 9.12和9 .2哪个更大? 245 | 调用工具: ['compare'] 246 | Assistant: 9.2 比 9.12 更大。 247 | User: 为什么? 248 | Assistant: 当我们比较9.12和9.2时,可以将它们看作是9.12和9.20。由于9.20在小数点后第二位是0,而9.12在小数点后第二位是2,所以在小数点后第一位相等的情况下,9.20(即9.2)大于9.12。因此,9.2 比 9.12 更大。 249 | User: strawberry中有几个r? 250 | 调用工具: ['count_letter_in_string'] 251 | Assistant: 单词 "strawberry" 中有3个字母 'r'。 252 | User: 你确信嘛? 253 | 调用工具: ['count_letter_in_string'] 254 | Assistant: 是的,我确定。单词 "strawberry" 中确实有3个字母 'r'。让我们再次确认一下,"strawberry" 中的 'r' 确实出现了3次。 255 | User: 好的 你很薄,现在几点 了? 256 | 调用工具: ['get_current_datetime'] 257 | Assistant: 当前的时间是2025年4月26日17:01:33。不过,我注意到您提到“你很薄”,这似乎是一个打字错误,如果您有任何其他问题或者需要进一步的帮助,请告诉我! 258 | User: exit 259 | ``` 260 | 261 | > ***记得给仓库点个小小的 star 哦~*** -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | from src.core import Agent 2 | from src.tools import add, count_letter_in_string, compare, get_current_datetime 3 | 4 | from openai import OpenAI 5 | 6 | 7 | if __name__ == "__main__": 8 | client = OpenAI( 9 | api_key="your siliconflow api key", 10 | base_url="https://api.siliconflow.cn/v1", 11 | ) 12 | 13 | agent = Agent( 14 | client=client, 15 | model="Qwen/Qwen2.5-32B-Instruct", 16 | tools=[get_current_datetime, add, compare, count_letter_in_string], 17 | ) 18 | 19 | while True: 20 | # 使用彩色输出区分用户输入和AI回答 21 | prompt = input("\033[94mUser: \033[0m") # 蓝色显示用户输入提示 22 | if prompt == "exit": 23 | break 24 | response = agent.get_completion(prompt) 25 | print("\033[92mAssistant: \033[0m", response) # 绿色显示AI助手回答 26 | -------------------------------------------------------------------------------- /images/React.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KMnO4-zx/TinyAgent/3cfc0d5b0df07e5d9618874f9b976e495a4d46cc/images/React.png -------------------------------------------------------------------------------- /images/Tiny_Agent.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KMnO4-zx/TinyAgent/3cfc0d5b0df07e5d9618874f9b976e495a4d46cc/images/Tiny_Agent.jpg -------------------------------------------------------------------------------- /images/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KMnO4-zx/TinyAgent/3cfc0d5b0df07e5d9618874f9b976e495a4d46cc/images/example.gif -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | json 2 | openai 3 | datetime 4 | pprint -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KMnO4-zx/TinyAgent/3cfc0d5b0df07e5d9618874f9b976e495a4d46cc/src/__init__.py -------------------------------------------------------------------------------- /src/core.py: -------------------------------------------------------------------------------- 1 | from openai import OpenAI 2 | import json 3 | from typing import List, Dict, Any 4 | from src.utils import function_to_json 5 | from src.tools import get_current_datetime, add, compare, count_letter_in_string 6 | 7 | import pprint 8 | 9 | SYSREM_PROMPT = """ 10 | 你是一个叫不要葱姜蒜的人工智能助手。你的输出应该与用户的语言保持一致。 11 | 当用户的问题需要调用工具时,你可以从提供的工具列表中调用适当的工具函数。 12 | """ 13 | 14 | class Agent: 15 | def __init__(self, client: OpenAI, model: str = "Qwen/Qwen2.5-32B-Instruct", tools: List=[], verbose : bool = True): 16 | self.client = client 17 | self.tools = tools 18 | self.model = model 19 | self.messages = [ 20 | {"role": "system", "content": SYSREM_PROMPT}, 21 | ] 22 | self.verbose = verbose 23 | 24 | def get_tool_schema(self) -> List[Dict[str, Any]]: 25 | # 获取所有工具的 JSON 模式 26 | return [function_to_json(tool) for tool in self.tools] 27 | 28 | def handle_tool_call(self, tool_call): 29 | # 处理工具调用 30 | function_name = tool_call.function.name 31 | function_args = tool_call.function.arguments 32 | function_id = tool_call.id 33 | 34 | function_call_content = eval(f"{function_name}(**{function_args})") 35 | 36 | return { 37 | "role": "tool", 38 | "content": function_call_content, 39 | "tool_call_id": function_id, 40 | } 41 | 42 | def get_completion(self, prompt) -> str: 43 | 44 | self.messages.append({"role": "user", "content": prompt}) 45 | 46 | # 获取模型的完成响应 47 | response = self.client.chat.completions.create( 48 | model=self.model, 49 | messages=self.messages, 50 | tools=self.get_tool_schema(), 51 | stream=False, 52 | ) 53 | if response.choices[0].message.tool_calls: 54 | self.messages.append({"role": "assistant", "content": response.choices[0].message.content}) 55 | # 处理工具调用 56 | tool_list = [] 57 | for tool_call in response.choices[0].message.tool_calls: 58 | # 处理工具调用并将结果添加到消息列表中 59 | self.messages.append(self.handle_tool_call(tool_call)) 60 | tool_list.append([tool_call.function.name, tool_call.function.arguments]) 61 | if self.verbose: 62 | print("调用工具:", response.choices[0].message.content, tool_list) 63 | # 再次获取模型的完成响应,这次包含工具调用的结果 64 | response = self.client.chat.completions.create( 65 | model=self.model, 66 | messages=self.messages, 67 | tools=self.get_tool_schema(), 68 | stream=False, 69 | ) 70 | 71 | # 将模型的完成响应添加到消息列表中 72 | self.messages.append({"role": "assistant", "content": response.choices[0].message.content}) 73 | return response.choices[0].message.content 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /src/tools.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | 3 | # 获取当前日期和时间 4 | def get_current_datetime() -> str: 5 | """ 6 | 获取当前日期和时间。 7 | :return: 当前日期和时间的字符串表示。 8 | """ 9 | current_datetime = datetime.now() 10 | formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S") 11 | return formatted_datetime 12 | 13 | def add(a: float, b: float): 14 | """ 15 | 计算两个浮点数的和。 16 | :param a: 第一个浮点数。 17 | :param b: 第二个浮点数。 18 | :return: 两个浮点数的和。 19 | """ 20 | return str(a + b) 21 | 22 | def mul(a: float, b: float): 23 | """ 24 | 计算两个浮点数的积。 25 | :param a: 第一个浮点数。 26 | :param b: 第二个浮点数。 27 | :return: 两个浮点数的积。 28 | """ 29 | return str(a * b) 30 | 31 | def compare(a: float, b: float): 32 | """ 33 | 比较两个浮点数的大小。 34 | :param a: 第一个浮点数。 35 | :param b: 第二个浮点数。 36 | :return: 比较结果的字符串表示。 37 | """ 38 | if a > b: 39 | return f'{a} is greater than {b}' 40 | elif a < b: 41 | return f'{b} is greater than {a}' 42 | else: 43 | return f'{a} is equal to {b}' 44 | 45 | def count_letter_in_string(a: str, b: str): 46 | """ 47 | 统计字符串中某个字母的出现次数。 48 | :param a: 要搜索的字符串。 49 | :param b: 要统计的字母。 50 | :return: 字母在字符串中出现的次数。 51 | """ 52 | string = a.lower() 53 | letter = b.lower() 54 | 55 | count = string.count(letter) 56 | return(f"The letter '{letter}' appears {count} times in the string.") 57 | -------------------------------------------------------------------------------- /src/utils.py: -------------------------------------------------------------------------------- 1 | import inspect 2 | from datetime import datetime 3 | import pprint 4 | 5 | def function_to_json(func) -> dict: 6 | # 定义 Python 类型到 JSON 数据类型的映射 7 | type_map = { 8 | str: "string", # 字符串类型映射为 JSON 的 "string" 9 | int: "integer", # 整型类型映射为 JSON 的 "integer" 10 | float: "number", # 浮点型映射为 JSON 的 "number" 11 | bool: "boolean", # 布尔型映射为 JSON 的 "boolean" 12 | list: "array", # 列表类型映射为 JSON 的 "array" 13 | dict: "object", # 字典类型映射为 JSON 的 "object" 14 | type(None): "null", # None 类型映射为 JSON 的 "null" 15 | } 16 | 17 | # 获取函数的签名信息 18 | try: 19 | signature = inspect.signature(func) 20 | except ValueError as e: 21 | # 如果获取签名失败,则抛出异常并显示具体的错误信息 22 | raise ValueError( 23 | f"无法获取函数 {func.__name__} 的签名: {str(e)}" 24 | ) 25 | 26 | # 用于存储参数信息的字典 27 | parameters = {} 28 | for param in signature.parameters.values(): 29 | # 尝试获取参数的类型,如果无法找到对应的类型则默认设置为 "string" 30 | try: 31 | param_type = type_map.get(param.annotation, "string") 32 | except KeyError as e: 33 | # 如果参数类型不在 type_map 中,抛出异常并显示具体错误信息 34 | raise KeyError( 35 | f"未知的类型注解 {param.annotation},参数名为 {param.name}: {str(e)}" 36 | ) 37 | # 将参数名及其类型信息添加到参数字典中 38 | parameters[param.name] = {"type": param_type} 39 | 40 | # 获取函数中所有必需的参数(即没有默认值的参数) 41 | required = [ 42 | param.name 43 | for param in signature.parameters.values() 44 | if param.default == inspect._empty 45 | ] 46 | 47 | # 返回包含函数描述信息的字典 48 | return { 49 | "type": "function", 50 | "function": { 51 | "name": func.__name__, # 函数的名称 52 | "description": func.__doc__ or "", # 函数的文档字符串(如果不存在则为空字符串) 53 | "parameters": { 54 | "type": "object", 55 | "properties": parameters, # 函数参数的类型描述 56 | "required": required, # 必须参数的列表 57 | }, 58 | }, 59 | } 60 | 61 | --------------------------------------------------------------------------------