跟大模型聊天人人都会,但你有没有想过——为什么ai能帮你订餐、查航班、写代码,而你调用api只会一问一答?
差别就两个字:agent。
大模型是大脑,但没手没脚没记忆。你问它"北京天气怎么样",它只会说"我无法访问实时数据"。agent就是给大脑装上四肢、记忆和执行力——让它自己判断该用什么工具,执行完再思考下一步。
听起来很复杂?其实核心就一个公式:
agent = llm(大脑)+ tools(双手)+ memory(记忆)+ loop(循环)
今天用100行python代码,把这四个模块从零搭一遍。不依赖任何框架,跑完你就明白agent到底是怎么回事。
环境准备
pip install openai
用openai sdk,兼容deepseek、智谱等国产api,改个base_url就行。这篇用deepseek做demo——便宜,而且支持按量付费。
你需要去注册拿一个api key。
llm调用 —— 给agent装个大脑
第一块拼图:能跟大模型对话。
from openai import openai
from dotenv import load_dotenv
import os
load_dotenv()
client = openai(
# api_key="your-api-key", # 换成你的deepseek api key
api_key=os.getenv("deepseek_api_key"), # 或使用env方式存储apikey
base_url="https://api.deepseek.com"
)
def ask_llm(messages, tools=none):
response = client.chat.completions.create(
model="deepseek-v4-flash",
messages=messages,
tools=tools,
tool_choice="auto",
)
return response.choices[0].message
res = ask_llm([{"role":"user","content":"你好"}])
print(res)这段代码本身没什么特别的,就是标准的api调用。但注意tools这个参数——这是agent和普通聊天的核心分界线。
不传tools,模型只能聊天。传了tools,模型就知道"我还有这些工具可以用",在需要的时候会主动要求调用。tool_choice="auto"的意思是让模型自己判断:这个问题我直接答,还是得用工具?
踩坑提示 deepseek的deepseek-chat模型已废弃,对应deepseek-v4-flash模型中的非思考模式
工具定义 —— 给agent装双手
工具就是普通的python函数,外加一份给llm看的"说明书"。
def calculator(expression: str) -> str:
try:
return str(eval(expression))
except:
return "计算错误"
def get_weather(city: str) -> str:
mock = {"北京": "晴 25℃", "上海": "多云 28℃", "深圳": "雷阵雨 30℃"}
return mock.get(city, f"暂无{city}天气数据")
tools = [
{"type": "function", "function": {
"name": "calculator",
"description": "执行数学计算,输入数学表达式",
"parameters": {"type": "object", "properties": {
"expression": {"type": "string", "description": "数学表达式,如 2+3*4"}
}, "required": ["expression"]}
}},
{"type": "function", "function": {
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {"type": "object", "properties": {
"city": {"type": "string", "description": "城市名称"}
}, "required": ["city"]}
}}
]
tool_map = {"calculator": calculator, "get_weather": get_weather}这里有个关键认知:llm不直接执行代码。
它只做一件事——输出一段json,告诉你"我想调用calculator工具,参数是123 * 456 + 789"。真正执行计算的是你的代码。llm的角色更像一个调度员,决定调用什么工具、传什么参数,但脏活累活都是你的程序干。
description字段很重要,写得越清晰,模型调用越准确。模型是靠读这个描述来理解工具用途的——你说"执行数学计算",它就知道算术题该找这个工具。
记忆 —— 让agent记住上下文
class memory:
def __init__(self):
self.messages = []
def add(self, role, content, **kwargs):
msg = {"role": role, "content": content}
msg.update(kwargs)
self.messages.append(msg)
def get_messages(self):
return self.messages.copy()就这?就这么简单。
很多人觉得agent的"记忆"很高深,其实本质就是维护一个messages列表。每次调用llm时,把完整的对话历史传过去——llm本身没有状态,所谓的"记住上下文"就是每轮都把历史消息全部塞进请求里。
这也解释了为什么聊天越长,api费用越高——因为每次请求都带着之前所有的对话。
agent循环 —— 让agent会思考
这是整个agent的心脏。核心思路叫react模式:思考→行动→观察→重复,直到任务完成。
def run_agent(user_input, max_steps=10):
memory = memory()
memory.add("system", "你是一个有用的助手,可以调用工具回答问题。")
memory.add("user", user_input)
for step in range(max_steps):
print(f"\n--- 第{step + 1}步 ---")
response = ask_llm(memory.get_messages(), tools=tools)
if not response.tool_calls:
print(f"agent回答:{response.content}")
return response.content
memory.add("assistant", response.content or "", tool_calls=response.tool_calls)
for tool_call in response.tool_calls:
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"调用工具:{name}({args})")
result = tool_map[name](**args)
print(f"工具返回:{result}")
memory.add("tool", result, tool_call_id=tool_call.id)
return "达到最大步数,agent停止"拆解这个循环:
- 思考:把对话历史和工具列表发给llm,llm决定下一步干什么
- 判断:如果llm没有请求调用工具,说明它觉得可以直接回答了,循环结束
- 行动:llm说"我要查北京天气",你的代码就执行
get_weather("北京") - 观察:把工具执行结果塞回messages,让llm看到结果
- 重复:llm拿到结果后再思考,可能还需要调别的工具,也可能可以直接回答了
max_steps是安全阀,防止agent陷入死循环。实际使用中10步足够处理大部分任务。
跑起来看效果
# 测试1:简单计算
run_agent("帮我算一下 123 * 456 + 789")输出:

两步完成:第一步调计算器,第二步直接回答。
# 测试2:多步骤任务
run_agent("北京和上海哪个温度高?并帮我算一下高多少?")输出:

总结
写完这100行代码,最大的感受是:agent一点都不神秘。
网上铺天盖地的agent框架——langchain、autogen、crewai——把这件事包装得很复杂。但你把核心扒开看,就是一个while循环:llm决定调什么工具,你的代码执行,结果传回去,循环直到完成。框架做的事,无非是帮你把这个循环封装好,再加上一些工程化的东西(重试、超时、日志)。
我的建议:先手写一遍,再用框架。
不是框架不好,是你不理解原理直接用框架,出了问题完全不知道怎么排查。你不知道tool_calls的格式长什么样,不知道messages里每条消息的role有什么讲究,调试的时候就是两眼一抹黑。
还有一点:别急着给agent加复杂的planning和multi-agent协作。先把这个最小闭环跑通,加一个你自己业务场景需要的工具(比如查数据库、调内部api),让它真正干一件有用的事。能稳定跑一周,再考虑扩展。
agent的价值不在于架构多精巧,在于它能不能稳定地把一件事干好。
跑通代码后,可以试试这些扩展方向:
- 换个工具:把
get_weather换成查数据库、调内部api、发邮件——只要是个python函数就能当工具 - 加rag:给agent接一个知识库,让它能查文档回答问题
- 多agent协作:多个agent分工合作,比如一个负责搜索、一个负责写作
- 试试框架:理解原理后再用langchain或crewai,你会发现它们其实就是帮你封装了这套循环
完整代码
"""
最小ai agent实现 —— 100行代码搞定
agent = llm + tools + memory + loop
"""
from openai import openai
import json
from dotenv import load_dotenv
import os
load_dotenv()
# ============ 1. llm调用(大脑)============
client = openai(
# api_key="your-api-key", # 换成你的deepseek api key
api_key=os.getenv("deepseek_api_key"), # 或使用env方式存储apikey
base_url="https://api.deepseek.com"
)
def ask_llm(messages, tools=none):
response = client.chat.completions.create(
model="deepseek-v4-flash",
messages=messages,
tools=tools,
tool_choice="auto",
)
return response.choices[0].message
# ============ 2. 工具定义(双手)============
def calculator(expression: str) -> str:
try:
return str(eval(expression))
except:
return "计算错误"
def get_weather(city: str) -> str:
mock = {"北京": "晴 25℃", "上海": "多云 28℃", "深圳": "雷阵雨 30℃"}
return mock.get(city, f"暂无{city}天气数据")
tools = [
{"type": "function", "function": {
"name": "calculator",
"description": "执行数学计算,输入数学表达式",
"parameters": {"type": "object", "properties": {
"expression": {"type": "string", "description": "数学表达式,如 2+3*4"}
}, "required": ["expression"]}
}},
{"type": "function", "function": {
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {"type": "object", "properties": {
"city": {"type": "string", "description": "城市名称"}
}, "required": ["city"]}
}}
]
tool_map = {"calculator": calculator, "get_weather": get_weather}
# ============ 3. 记忆(对话历史)============
class memory:
def __init__(self):
self.messages = []
def add(self, role, content, **kwargs):
msg = {"role": role, "content": content}
msg.update(kwargs)
self.messages.append(msg)
def get_messages(self):
return self.messages.copy()
# ============ 4. agent循环(心脏)============
def run_agent(user_input, max_steps=10):
memory = memory()
memory.add("system", "你是一个有用的助手,可以调用工具回答问题。")
memory.add("user", user_input)
for step in range(max_steps):
print(f"\n--- 第{step + 1}步 ---")
response = ask_llm(memory.get_messages(), tools=tools)
if not response.tool_calls:
print(f"agent回答:{response.content}")
return response.content
memory.add("assistant", response.content or "", tool_calls=response.tool_calls)
for tool_call in response.tool_calls:
name = tool_call.function.name
args = json.loads(tool_call.function.arguments)
print(f"调用工具:{name}({args})")
result = tool_map[name](**args)
print(f"工具返回:{result}")
memory.add("tool", result, tool_call_id=tool_call.id)
return "达到最大步数,agent停止"
# ============ 5. 测试 ============
if __name__ == "__main__":
run_agent("北京和上海哪个温度高?并帮我算一下高多少?")以上就是100行python代码带你搭建一个能干活的ai agent的详细内容,更多关于python搭建ai agent的资料请关注代码网其它相关文章!
发表评论