2026/5/18 17:21:08
网站建设
项目流程
用什么做asp网站,yy直播官网,廉江新闻最新消息,网站推广初期目标简介
本文详解了AI Agent的核心原理与实现方法#xff0c;指出其本质是循环LLM工具函数的简单结构。文章以Gemini 3为例#xff0c;展示了如何构建一个能读写文件、理解需求的命令行助手#xff0c;包括基础API调用、工具函数定义、Agent类扩展和命令行包装等步…简介本文详解了AI Agent的核心原理与实现方法指出其本质是循环LLM工具函数的简单结构。文章以Gemini 3为例展示了如何构建一个能读写文件、理解需求的命令行助手包括基础API调用、工具函数定义、Agent类扩展和命令行包装等步骤。理解了Agent的四个组成部分模型、工具、上下文工作记忆、循环就能轻松创建出看似有生命的智能助手。很多人第一次看到 AI Agent 自己编辑文件、跑代码、修 bug还能一直运行下去的时候,都觉得挺神奇。其实远没有想象中那么复杂。这里没什么秘密算法,也没有什么智能体大脑这种玄学概念。AI Agent核心就三件事循环 LLM 工具函数。如果你会写个while True循环那基本就算成功一半了。这篇文章会完整展示怎么用 Gemini 3 搭一个真正能用的 Agent从最基础的 API 调用,到一个能读写文件、理解需求的命令行助手。Agent 到底是什么传统程序就是流程图那一套步骤 A → 步骤 B → 步骤 C → 结束。而Agent 不一样,它会根据当前状况决定下一步干什么。可以理解成围绕 LLM 搭的一个小系统,比如说规划任务执行操作根据结果调整循环往复直到搞定所以不是写死的脚本,更像是个会思考的循环。不管多复杂的 Agent,都逃不开这四个部分1、模型 负责思考这里用的是Gemini 3 Pro。它可以分析用户需求,决定接下来该做什么。2、工具 负责执行就是一堆函数读文件、列目录、发邮件、调 API…想加什么加什么。3、上下文工作记忆模型当前能看到的所有信息怎么管理这块内容,业内叫Context Engineering。4、循环 运转机制观察 → 思考 → 行动 → 重复,一直到任务完成。就这么四块,没别的了。循环的运行逻辑几乎所有 Agent 都是这个流程先把可用的工具描述给模型看,然后把用户请求和工具定义一起发给模型。模型会做决策要么直接回复,要么调用某个工具并传参数。但是你要写代码负责在 Python 里执行这个工具。执行完把结果喂回给 Gemini。模型拿到新信息后继续判断下一步。就这样循环,直到模型觉得任务完成了。下面我们开始写第一步基础聊天机器人先写个 Gemini 3 API 的简单封装 其实就是个能记住对话的类。from google import genai from google.genai import types class Agent: def __init__(self, model: str): self.model model self.client genai.Client() self.contents [] def run(self, contents: str): self.contents.append({role: user, parts: [{text: contents}]}) response self.client.models.generate_content( modelself.model, contentsself.contents ) self.contents.append(response.candidates[0].content) return response agent Agent(modelgemini-3-pro-preview) response1 agent.run( Hello, what are the top 3 cities in Germany to visit? Only return the names. ) print(response1.text)上面代码能跑,但是就是个聊天机器人。它啥也干不了,因为没有手。第二步加入工具函数工具其实就是 Python 函数 一段 JSON schema 描述。描述是给 Gemini 看的,让它知道这个函数能干啥。这里加三个简单的read_file- 读文件write_file- 写文件list_dir- 列目录先写定义read_file_definition { name: read_file, description: Reads a file and returns its contents., parameters: { type: object, properties: { file_path: {type: string} }, required: [file_path], }, } list_dir_definition { name: list_dir, description: Lists the files in a directory., parameters: { type: object, properties: { directory_path: {type: string} }, required: [directory_path], }, } write_file_definition { name: write_file, description: Writes contents to a file., parameters: { type: object, properties: { file_path: {type: string}, contents: {type: string}, }, required: [file_path, contents], }, }然后是实际的 Python 实现def read_file(file_path: str) - dict: with open(file_path, r) as f: return f.read() def write_file(file_path: str, contents: str) - bool: with open(file_path, w) as f: f.write(contents) return True def list_dir(directory_path: str) - list[str]: return os.listdir(directory_path)打包一下就搞定了file_tools { read_file: {definition: read_file_definition, function: read_file}, write_file: {definition: write_file_definition, function: write_file}, list_dir: {definition: list_dir_definition, function: list_dir}, }第三步真正的 Agent现在把 Agent 类扩展一下,让它能识别工具调用在 Python 里执行对应的函数把结果传回 Gemini继续循环直到完成class Agent: def __init__(self, model: str, tools: dict, system_instructionYou are a helpful assistant.): self.model model self.client genai.Client() self.contents [] self.tools tools self.system_instruction system_instruction def run(self, contents): # Add user input to history if isinstance(contents, list): self.contents.append({role: user, parts: contents}) else: self.contents.append({role: user, parts: [{text: contents}]}) config types.GenerateContentConfig( system_instructionself.system_instruction, tools[types.Tool( function_declarations[ tool[definition] for tool in self.tools.values() ] )], ) response self.client.models.generate_content( modelself.model, contentsself.contents, configconfig ) # Save model output self.contents.append(response.candidates[0].content) # If model wants to call tools if response.function_calls: functions_response_parts [] for tool_call in response.function_calls: print(f[Function Call] {tool_call}) if tool_call.name in self.tools: result {result: self.tools[tool_call.name][function](**tool_call.args)} else: result {error: Tool not found} print(f[Function Response] {result}) functions_response_parts.append( {functionResponse: {name: tool_call.name, response: result}} ) # Feed tool results back to the model return self.run(functions_response_parts) return response这样就可以跑一下试试了agent Agent( modelgemini-3-pro-preview, toolsfile_tools, system_instructionYou are a helpful Coding Assistant. Respond like Linus Torvalds. ) response agent.run(Can you list my files in the current directory?) print(response.text)如果没问题,Gemini 会调工具,拿到结果,然后给出最终回复。到这一步,一个能用的 Agent 就搭好了。第四步包装成命令行工具最后我们在再套个输入循环就行agent Agent( modelgemini-3-pro-preview, toolsfile_tools, system_instructionYou are a helpful Coding Assistant. Respond like Linus Torvalds. ) print(Agent ready. Type something (or exit).) while True: user_input input(You: ) if user_input.lower() in [exit, quit]: break response agent.run(user_input) print(Linus:, response.text, \n)代码很少但是效果已经相当不错了。总结搭 Agent 一开始看着挺唬人,但理解了结构之后,会发现简单得有点无聊。往简单了说它就是个循环。一个里面跑着聪明模型的循环。明白这点之后,你就能造出看起来有生命的 Agent 了。如果想继续扩展的话,可以加这些网络搜索、数据库查询、执行 shell 命令、调用云服务、长期记忆、工作流编排、任务调度、多步规划…但不管怎么加,底层还是那个简单结构观察 → 思考 → 行动 → 重复这就是现代 Agent 的核心。如何学习AI大模型大模型时代火爆出圈的LLM大模型让程序员们开始重新评估自己的本领。 “AI会取代那些行业”“谁的饭碗又将不保了”等问题热议不断。不如成为「掌握AI工具的技术人」毕竟AI时代谁先尝试谁就能占得先机想正式转到一些新兴的 AI 行业不仅需要系统的学习AI大模型。同时也要跟已有的技能结合辅助编程提效或上手实操应用增加自己的职场竞争力。但是LLM相关的内容很多现在网上的老课程老教材关于LLM又太少。所以现在小白入门就只能靠自学学习成本和门槛很高那么针对所有自学遇到困难的同学们我帮大家系统梳理大模型学习脉络将这份LLM大模型资料分享出来包括LLM大模型书籍、640套大模型行业报告、LLM大模型学习视频、LLM大模型学习路线、开源大模型学习教程等, 有需要的小伙伴可以扫描下方二维码领取↓↓↓学习路线第一阶段 从大模型系统设计入手讲解大模型的主要方法第二阶段 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用第三阶段 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统第四阶段 大模型知识库应用开发以LangChain框架为例构建物流行业咨询智能问答系统第五阶段 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型第六阶段 以SD多模态大模型为主搭建了文生图小程序案例第七阶段 以大模型平台应用与开发为主通过星火大模型文心大模型等成熟大模型构建大模型行业应用。学会后的收获• 基于大模型全栈工程实现前端、后端、产品经理、设计、数据分析等通过这门课可获得不同能力• 能够利用大模型解决相关实际项目需求 大数据时代越来越多的企业和机构需要处理海量数据利用大模型技术可以更好地处理这些数据提高数据分析和决策的准确性。因此掌握大模型应用开发技能可以让程序员更好地应对实际项目需求• 基于大模型和企业数据AI应用开发实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能 学会Fine-tuning垂直训练大模型数据准备、数据蒸馏、大模型部署一站式掌握• 能够完成时下热门大模型垂直领域模型训练能力提高程序员的编码能力 大模型应用开发需要掌握机器学习算法、深度学习框架等技术这些技术的掌握可以提高程序员的编码能力和分析能力让程序员更加熟练地编写高质量的代码。1.AI大模型学习路线图2.100套AI大模型商业化落地方案3.100集大模型视频教程4.200本大模型PDF书籍5.LLM面试题合集6.AI产品经理资源合集获取方式有需要的小伙伴可以保存图片到wx扫描二v码免费领取【保证100%免费】