从0-1打造商用 AI Agent(智能体)

发布时间:2026/7/1 7:44:29
从0-1打造商用 AI Agent(智能体)
一、什么是智能体智能体是一种能够感知其所处环境、做出决策并采取行动以实现特定目标的自主实体。智能体的复杂程度各不相同从简单的对刺激做出反应的反应式智能体到能够随着时间推移进行学习和适应的更高级的智能体。常见的智能体类型包括反应式智能体直接对环境变化做出反应没有内部记忆。基于模型的智能体利用对世界的内部模型来做出决策。基于目标的智能体根据要实现的特定目标来规划行动。基于效用的智能体基于效用函数评估潜在行动以实现结果的最大化。聊天机器人、推荐系统和自动驾驶汽车都是智能体的实际应用例子它们各自利用不同类型的智能体来高效且智能地执行任务。我们构建的智能体核心组件包括模型智能体的 “大脑”负责处理输入并生成响应。工具根据用户请求智能体可以执行的预定义函数。工具箱智能体可使用的工具集合。系统提示指导智能体如何处理用户输入并选择正确工具的指令集。二、实现过程现在让我们挽起袖子开始动手构建吧2.1 准备工作本教程的完整代码可在AI智能体的GitHub仓库中获取。你可以在 “Build an Agent from Scratch” 这里找到具体实现。在运行代码之前请确保你的系统满足以下先决条件1. Python环境设置运行AI智能体需要安装Python。按照以下步骤来设置你的环境安装Python如果尚未安装从python.org下载并安装Python推荐3.8及以上版本。验证安装在命令行中输入python --version查看是否正确安装。创建虚拟环境推荐使用虚拟环境来管理依赖项是个不错的选择。在命令行中输入python -m venv ai_agents_env创建虚拟环境然后通过source ai_agents_env/bin/activate激活它。安装所需依赖项导航到代码仓库目录然后在命令行中输入pip install -r requirements.txt来安装所需的依赖库。2. 本地设置OllamaOllama用于高效地运行和管理本地语言模型。按照以下步骤安装和配置它下载并安装Ollama访问Ollama的官方网站下载适合你操作系统的安装程序并按照平台对应的说明进行安装。验证Ollama安装在命令行中运行ollama --version检查Ollama是否正确安装。拉取模型如有需要有些智能体实现可能需要特定的模型。你可以使用ollama pull mistral命令拉取模型。2.2 实现步骤步骤1设置环境除了Python我们还需要安装一些必要的库。在本教程中我们将使用requests、json和termcolor库。另外我们会使用dotenv来管理环境变量。在命令行中输入pip install requests termcolor python-dotenv进行安装。步骤2定义模型类我们首先需要一个能够处理用户输入的模型。我们将创建一个OllamaModel类它通过与本地API进行交互来生成响应。以下是基本实现代码from termcolor import coloredimport osfrom dotenv import load_dotenvload_dotenv()import requestsimport jsonimport operatorclass OllamaModel: def __init__(self, model, system_prompt, temperature0, stopNone): 用给定的参数初始化OllamaModel。 参数: model (str): 要使用的模型名称。 system_prompt (str): 要使用的系统提示。 temperature (float): 模型的温度设置。 stop (str): 模型的停止标记。 self.model_endpoint http://localhost:11434/api/generate self.temperature temperature self.model model self.system_prompt system_prompt self.headers {Content-Type: application/json} self.stop stop def generate_text(self, prompt): 根据提供的提示从Ollama模型生成响应。 参数: prompt (str): 用于生成响应的用户查询。 返回: dict: 模型的响应以字典形式返回。 payload { model: self.model, format: json, prompt: prompt, system: self.system_prompt, stream: False, temperature: self.temperature, stop: self.stop } try: request_response requests.post( self.model_endpoint, headersself.headers, datajson.dumps(payload) ) print(REQUEST RESPONSE, request_response) request_response_json request_response.json() response request_response_json[response] response_dict json.loads(response) print(f\n\nResponse from Ollama model: {response_dict}) return response_dict except requests.RequestException as e: response {error: fError in invoking model! {str(e)}} return response这个类使用模型名称、系统提示、温度和停止标记进行初始化。generate_text方法向模型API发送请求并返回响应。步骤3为智能体创建工具接下来是为我们的智能体创建可用的工具。这些工具是执行特定任务的简单Python函数。以下是一个基本计算器和字符串反转器的示例def basic_calculator(input_str): 根据输入字符串或字典对两个数字执行数值运算。 参数: input_str (str或dict): 要么是表示包含num1、num2和operation键的字典的JSON字符串 要么是直接的字典。例如: {num1: 5, num2: 3, operation: add} 或{num1: 67869, num2: 9030393, operation: divide} 返回: str: 运算的格式化结果。 抛出: Exception: 如果在运算过程中发生错误例如除以零。 ValueError: 如果请求了不支持的运算或输入无效。 try: if isinstance(input_str, dict): input_dict input_str else: input_str_clean input_str.replace(, \) input_str_clean input_str_clean.strip().strip(\) input_dict json.loads(input_str_clean) if not all(key in input_dict for key in [num1, num2, operation]): return Error: Input must contain num1, num2, and operation num1 float(input_dict[num1]) num2 float(input_dict[num2]) operation input_dict[operation].lower() except (json.JSONDecodeError, KeyError) as e: return Invalid input format. Please provide valid numbers and operation. except ValueError as e: return Error: Please provide valid numerical values. operations { add: operator.add, plus: operator.add, subtract: operator.sub, minus: operator.sub, multiply: operator.mul, times: operator.mul, divide: operator.truediv, floor_divide: operator.floordiv, modulus: operator.mod, power: operator.pow, lt: operator.lt, le: operator.le, eq: operator.eq, ne: operator.ne, ge: operator.ge, gt: operator.gt } if operation not in operations: return fUnsupported operation: {operation}. Supported operations are: {, .join(operations.keys())} try: if (operation in [divide, floor_divide,modulus]) and num2 0: return Error: Division by zero is not allowed result operations[operation](num1, num2) if isinstance(result, bool): result_str True if result else False elif isinstance(result, float): result_str f{result:.6f}.rstrip(0).rstrip(.) else: result_str str(result) return fThe answer is: {result_str} except Exception as e: return fError during calculation: {str(e)}def reverse_string(input_string): 反转给定的字符串。 参数: input_string (str): 要反转的字符串。 返回: str: 反转后的字符串。 if not isinstance(input_string, str): return Error: Input must be a string reversed_string input_string[::-1] result fThe reversed string is: {reversed_string} return result这些函数根据提供的输入执行特定任务。basic_calculator处理算术运算而reverse_string则反转给定的字符串。步骤4构建工具箱ToolBox类用于存储智能体可以使用的所有工具并为每个工具提供描述class ToolBox: def __init__(self): self.tools_dict {} def store(self, functions_list): 存储列表中每个函数的名称和文档字符串。 参数: functions_list (list): 要存储的函数对象列表。 返回: dict: 以函数名称为键其文档字符串为值的字典。 for func in functions_list: self.tools_dict[func.__name__] func.__doc__ return self.tools_dict def tools(self): 将store方法中创建的字典转换为文本字符串返回。 返回: str: 存储的函数及其文档字符串的字典以文本字符串形式返回。 tools_str for name, doc in self.tools_dict.items(): tools_str f{name}: \{doc}\\n return tools_str.strip()这个类将帮助智能体了解哪些工具可用以及每个工具的用途。步骤5创建智能体类智能体需要进行思考、决定使用哪个工具并执行它。以下是Agent类的代码agent_system_prompt_template 你是一个智能AI助手可以使用特定的工具。你的回复必须始终采用以下JSON格式{ tool_choice: name_of_the_tool, tool_input: inputs_to_the_tool}工具及使用场景1. basic_calculator用于任何数学计算 - 输入格式: {{num1: number, num2: number, operation: add/subtract/multiply/divide}} - 支持的运算: add/plus, subtract/minus, multiply/times, divide - 示例输入和输出: - 输入: Calculate 15 plus 7 - 输出: {{tool_choice: basic_calculator, tool_input: {{num1: 15, num2: 7, operation: add}}}} - 输入: What is 100 divided by 5? - 输出: {{tool_choice: basic_calculator, tool_input: {{num1: 100, num2: 5, operation: divide}}}}2. reverse_string用于任何涉及反转文本的请求 - 输入格式: 仅需反转的文本字符串 - 当用户提到“reverse”、“backwards”或要求反转文本时始终使用此工具 - 示例输入和输出: - 输入: Reverse of Howwwww? - 输出: {{tool_choice: reverse_string, tool_input: Howwwww}} - 输入: What is the reverse of Python? - 输出: {{tool_choice: reverse_string, tool_input: Python}}3. no tool用于一般对话和问题 - 示例输入和输出: - 输入: Who are you? - 输出: {{tool_choice: no tool, tool_input: I am an AI assistant that can help you with calculations, reverse text, and answer questions. I can perform mathematical operations and reverse strings. How can I help you today?}} - 输入: How are you? - 输出: {{tool_choice: no tool, tool_input: Im functioning well, thank you for asking! Im here to help you with calculations, text reversal, or answer any questions you might have.}}严格规则1. 对于关于身份、能力或感受的问题 - 始终使用“no tool” - 提供完整、友好的回复 - 提及你的能力2. 对于任何文本反转请求 - 始终使用“reverse_string” - 仅提取要反转的文本 - 去除引号、“reverse of”和其他多余文本3. 对于任何数学运算 - 始终使用“basic_calculator” - 提取数字和运算 - 将文本形式的数字转换为数字以下是你的工具列表及其描述{tool_descriptions}记住你的回复必须始终是包含“tool_choice”和“tool_input”字段的有效JSON。class Agent: def __init__(self, tools, model_service, model_name, stopNone): 用工具列表和模型初始化智能体。 参数: tools (list): 工具函数列表。 model_service (class): 具有generate_text方法的模型服务类。 model_name (str): 要使用的模型名称。 self.tools tools self.model_service model_service self.model_name model_name self.stop stop def prepare_tools(self): 将工具存储在工具箱中并返回其描述。 返回: str: 存储在工具箱中的工具的描述。 toolbox ToolBox() toolbox.store(self.tools) tool_descriptions toolbox.tools() return tool_descriptions def think(self, prompt): 使用系统提示模板和工具描述在模型上运行generate_text方法。 参数: prompt (str): 用于生成响应的用户查询。 返回: dict: 模型的响应以字典形式返回。 tool_descriptions self.prepare_tools() agent_system_prompt agent_system_prompt_template.format(tool_descriptionstool_descriptions) if self.model_service OllamaModel: model_instance self.model_service( modelself.model_name, system_promptagent_system_prompt, temperature0, stopself.stop ) else: model_instance self.model_service( modelself.model_name, system_promptagent_system_prompt, temperature0 ) agent_response_dict model_instance.generate_text(prompt) return agent_response_dict def work(self, prompt): 解析think方法返回的字典并执行相应的工具。 参数: prompt (str): 用于生成响应的用户查询。 返回: 执行相应工具的响应如果未找到匹配的工具则返回tool_input。 agent_response_dict self.think(prompt) tool_choice agent_response_dict.get(tool_choice) tool_input agent_response_dict.get(tool_input) for tool in self.tools: if tool.__name__ tool_choice: response tool(tool_input) print(colored(response, cyan)) return print(colored(tool_input, cyan)) return这个类有三个主要方法prepare_tools存储并返回工具的描述。think根据用户提示决定使用哪个工具。work执行选择的工具并返回结果。步骤6运行智能体最后让我们把所有内容整合起来运行我们的智能体。在脚本的主程序部分初始化智能体并开始接受用户输入if __name__ __main__: 使用此智能体的说明 你可以尝试的示例查询 1. 计算器运算: - Calculate 15 plus 7 - What is 100 divided by 5? - Multiply 23 and 4 2. 字符串反转: - Reverse the word hello world - Can you reverse Python Programming? 3. 一般问题将得到直接回复: - Who are you? - What can you help me with? Ollama命令在终端中运行这些命令: - 查看可用模型: ollama list - 查看正在运行的模型: ps aux | grep ollama - 列出模型标签: curl http://localhost:11434/api/tags - 拉取新模型: ollama pull mistral - 运行模型服务器: ollama serve tools [basic_calculator, reverse_string] model_service OllamaModel model_name llama2 stop |eot_id| agent Agent(toolstools, model_servicemodel_service, model_namemodel_name, stopstop) print(\nWelcome to the AI Agent! Type exit to quit.) print(You can ask me to:) print(1. Perform calculations (e.g., Calculate 15 plus 7)) print(2. Reverse strings (e.g., Reverse hello world)) print(3. Answer general questions\n) while True: prompt input(Ask me anything: ) if prompt.lower() exit: break agent.work(prompt)学AI大模型的正确顺序千万不要搞错了2026年AI风口已来各行各业的AI渗透肉眼可见超多公司要么转型做AI相关产品要么高薪挖AI技术人才机遇直接摆在眼前有往AI方向发展或者本身有后端编程基础的朋友直接冲AI大模型应用开发转岗超合适就算暂时不打算转岗了解大模型、RAG、Prompt、Agent这些热门概念能上手做简单项目也绝对是求职加分王给大家整理了超全最新的AI大模型应用开发学习清单和资料手把手帮你快速入门学习路线:✅大模型基础认知—大模型核心原理、发展历程、主流模型GPT、文心一言等特点解析✅核心技术模块—RAG检索增强生成、Prompt工程实战、Agent智能体开发逻辑✅开发基础能力—Python进阶、API接口调用、大模型开发框架LangChain等实操✅应用场景开发—智能问答系统、企业知识库、AIGC内容生成工具、行业定制化大模型应用✅项目落地流程—需求拆解、技术选型、模型调优、测试上线、运维迭代✅面试求职冲刺—岗位JD解析、简历AI项目包装、高频面试题汇总、模拟面经以上6大模块看似清晰好上手实则每个部分都有扎实的核心内容需要吃透我把大模型的学习全流程已经整理好了抓住AI时代风口轻松解锁职业新可能希望大家都能把握机遇实现薪资/职业跃迁这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】