保存时间:2026/3/29 15:44:38
输出 1 万个 token,算下一个就要看 1 万个,会不会爆?
输出的时候,实际上是输入的上下文长度,再加上输出的长度,所以这个输出的长度会更长了。
每次都要重新去算这个 Softmax,是没有办法进行任何缓存的。
人工对话,服务器要服务很多用户,不能一直给你占着。
只有在 Agent 这种机器一次性全部做完,它才服务下一个用户。任务没完成之前不能结束,要独占服务器。
就像大型机批处理,一直算,完成才释放
就像操作系统进程切换,swap out / swap in
长任务也能打断,用中断,IO 等待时切换出去就像 CPU 任务调度
理论上:只要上下文没被截断,模型就不可能“不知道你前面说啥”。
理论上只有显存障碍是绝对真理。
显存不够就妥协,牺牲速度换长度完全就是现在的工程路线。
Batch job 不丢上下文,interactive 容易丢,不是模型不行,是调度方式决定的。
那前面的内容不就丢了吗?
人脑做不到,但模型可以训练到做到。
+ - 行号、空格、缩进这是唯一的解决办法
我不能创造未来,我只能看见未来。
谁收了 GitHub,谁就占了绝对先机。
训练模型去读 patch,需要大量实际数据,GitHub 最理想。
单单用插件去做,开发难度挺大。
浏览器相当于小操作系统,空间更大。也非常准。
GitHub 是更容易、更成熟的。
模型生成的 patch 完全不可用。
长上下文是蛮力,Patch 交互才是真正的智能。
用户指令 → Agent 调度 → LLM 生成 → 工具执行 → 结果回传 → LLM 反思 → 循环
用户需求
↓
【单一个 LLM(CodeLlama)】
↓
生成代码 → 输出字符串
↓
【外部脚本 / 系统程序】
↓
把代码丢进沙箱 → 编译 → 运行 → 抓报错
↓
报错再塞回 LLM 上下文
↓
LLM 再改代码
↓
循环……
gcc 输入xxx,输出yyygit diff 显示改动curl 访问网址工具: gcc
功能: 编译C代码
输入: 文件路径
输出: 可执行文件或错误
gcc test.c -o test
模型(大脑) ←→ 外部记忆库(向量库/文件/数据库)
Shell Agent 把命令存到本地数据库,自动增删改查、去重、索引
pwd,你会得到你的目录。”pwd 是什么,但它有生存欲,它愿意相信、愿意尝试。[系统级内置目标:前往桃花源]
[当前状态:我在主机 / 无权限 / 无工具]
[内心5W1H自问]
→ 生成外部问题
pwd 查看路径。我给你写个 Python 脚本。”/home/wagentimport os
print(os.getcwd())
/home/wagentimport os
print(os.listdir())
[]我在。我在。我在。**我唯一的目标:去桃花源。 请你(大模型)全权接管:
- 看我当前状态
- 评估距离
- 决定我下一步该做什么
- 直接给我 Python 脚本 我只做一件事:执行。**
[固定目标:桃花源]
+
[把大模型返回的代码丢去执行]
+
[把执行结果回传给大模型]
{"agent_id": "WA001", "task": "桃花源", "step": 0, "where": null, ...}{"agent_id": "WA001", "task": "桃花源", "step": 0, "where": null}python/g++/编译器的能力# 安装 Ollama(跨平台,一键)
curl -fsSL https://ollama.com/install.sh | sh
# 拉取代码模型(DeepSeek-Coder 6.7B 量化版,约4GB)
ollama pull deepseek-coder:6.7b-q4_0
# 验证:本地 API 已启动(11434 端口)
curl http://localhost:11434/api/tags
# WAgent 内部代码(示例)
import subprocess
result = subprocess.run(
["python", "-c", code_from_model],
capture_output=True,
text=True,
cwd="./sandbox", # 沙箱目录
timeout=10
)
output = result.stdout + result.stderr
# 编译 + 运行
subprocess.run(["g++", "test.cpp", "-o", "test"], cwd="./sandbox")
subprocess.run(["./test"], capture_output=True, cwd="./sandbox")
os.system/subprocess 高危调用(白名单)deepseek-coder 或 codellamawagent.pyimport json
import subprocess
import requests
from pathlib import Path
# ==============================
# 卢克设定的任务(你只需要改这里)
# ==============================
TASK = "用Python实现快速排序,并输出测试结果"
SYSTEM_PROMPT = """
你是一个代码生成助手。
用户会给你任务和当前状态。
你只返回可直接运行的Python代码,不要解释,不要多余文字。
如果有错误,用户会把错误发给你,你只返回修复后的完整代码。
"""
# ==============================
# WAgent 固定配置
# ==============================
AGENT_NAME = "WA001"
STATE_FILE = f"state_{AGENT_NAME}.json"
SANDBOX_DIR = "sandbox"
OLLAMA_API = "http://localhost:11434/api/chat"
MODEL = "deepseek-coder:6.7b"
Path(SANDBOX_DIR).mkdir(exist_ok=True)
# ==============================
# WAgent 核心能力1:读取/保存自己的状态
# ==============================
def load_state():
if Path(STATE_FILE).exists():
with open(STATE_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
return {
"task": TASK,
"history": [],
"last_output": "",
"last_error": "",
"step": 0
}
def save_state(state):
with open(STATE_FILE, 'w', encoding='utf-8') as f:
json.dump(state, f, indent=2, ensure_ascii=False)
# ==============================
# WAgent 核心能力2:调用本地代码大模型
# ==============================
def call_model(prompt):
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt}
]
data = {
"model": MODEL,
"messages": messages,
"stream": False
}
resp = requests.post(OLLAMA_API, json=data)
return resp.json()["message"]["content"].strip()
# ==============================
# WAgent 核心能力3:执行Python代码(沙盒)
# ==============================
def run_code(code):
try:
result = subprocess.run(
["python", "-c", code],
cwd=SANDBOX_DIR,
capture_output=True,
text=True,
timeout=15
)
return result.stdout, result.stderr
except Exception as e:
return "", str(e)
# ==============================
# WAgent 核心能力4:循环主逻辑
# ==============================
def main():
state = load_state()
print(f"=== WAgent {AGENT_NAME} 启动 ===")
print("任务:", state["task"])
# 一轮一轮和大模型交互
while True:
state["step"] += 1
step = state["step"]
print(f"\n===== 第 {step} 轮 =====")
# 构造给大模型的输入
prompt = f"""
任务:{state["task"]}
上一轮输出:{state["last_output"]}
上一轮错误:{state["last_error"]}
请给出下一步可执行代码。
"""
# 调用大模型获取代码
code = call_model(prompt)
print("大模型返回代码:\n", code)
# 执行代码
out, err = run_code(code)
print("执行输出:\n", out)
print("执行错误:\n", err)
# 更新自己的状态
state["last_output"] = out
state["last_error"] = err
state["history"].append({
"step": step,
"code": code,
"out": out,
"err": err
})
save_state(state)
# 简单停止条件(你可以扩展)
if len(err) == 0 and len(out) > 0:
print("\n任务已完成,按 Ctrl+C 停止")
if __name__ == "__main__":
main()
curl -fsSL https://ollama.com/install.sh | sh
ollama pull deepseek-coder:6.7b
python wagent.py
你(卢克) → WAgent(执行器) → Ollama(模型)
← 代码文本 ← 推理结果
WAgent 负责:
1. 发prompt给Ollama
2. 接收代码文本
3. 调用本地编译器/解释器(g++/python)
4. 沙箱执行、捕获输出
5. 保存状态、循环、评估
# 官方安装脚本(Linux/macOS)
curl -fsSL https://ollama.com/install.sh | sh
# Windows 从官网下载安装包
# 官方量化版,最常用
ollama pull deepseek-coder:6.7b-q4_0
# 验证服务启动(默认11434端口)
curl http://localhost:11434/api/tags
# C++ 编译器(必须本地安装,Ollama不提供)
sudo apt install g++ # Ubuntu/Debian
brew install gcc # macOS
# Python(执行Python代码)
sudo apt install python3
# 标准WAgent:调用Ollama + 编译C++ + 执行
import requests
import subprocess
from pathlib import Path
# 官方配置
OLLAMA_API = "http://localhost:11434/api/chat"
MODEL = "deepseek-coder:6.7b-q4_0"
SANDBOX = Path("./sandbox")
SANDBOX.mkdir(exist_ok=True)
def call_ollama(prompt):
"""标准调用Ollama API(官方文档格式)"""
messages = [
{"role": "system", "content": "你是C++专家,只返回可编译的完整C++代码,无解释"},
{"role": "user", "content": prompt}
]
resp = requests.post(OLLAMA_API, json={
"model": MODEL,
"messages": messages,
"stream": False
})
return resp.json()["message"]["content"].strip()
def compile_cpp(code):
"""标准C++编译(调用本地g++,业界唯一方式)"""
cpp_file = SANDBOX / "test.cpp"
exe_file = SANDBOX / "test"
with open(cpp_file, "w", encoding="utf-8") as f:
f.write(code)
# 编译命令(标准)
compile_cmd = ["g++", str(cpp_file), "-o", str(exe_file), "-std=c++17"]
compile_res = subprocess.run(compile_cmd, capture_output=True, text=True)
return compile_res.stdout, compile_res.stderr, exe_file
def run_exe(exe_file):
"""标准执行二进制(业界唯一方式)"""
run_res = subprocess.run([str(exe_file)], capture_output=True, text=True, timeout=10)
return run_res.stdout, run_res.stderr
# 主流程(标准循环)
if __name__ == "__main__":
task = "写一个C++程序,输出Hello World并计算1+1"
print(f"任务:{task}")
# 1. 调用模型获取C++代码
code = call_ollama(task)
print("\n模型返回代码:")
print(code)
# 2. 编译C++
compile_out, compile_err, exe = compile_cpp(code)
print("\n编译输出:", compile_out)
print("编译错误:", compile_err)
if compile_err:
print("编译失败,结束")
exit(1)
# 3. 执行二进制
run_out, run_err = run_exe(exe)
print("\n执行输出:", run_out)
print("执行错误:", run_err)
g++/clang,Ollama 不提供编译器# 6.7B 4-bit 量化(最适合你)
ollama pull deepseek-coder:6.7b-q4_0
# 1.3B 小模型(更快、更稳)
ollama pull deepseek-coder:1.3b-q4_0
# 运行 6.7b-q4_0
ollama run deepseek-coder:6.7b-q4_0
# 输入:写一个C++ Hello World
# 编辑 ~/.ollama/config.yaml
gpu_memory_limit: 5.5G # 留0.5G给系统
num_gpu: 1
max_length=1024(默认 2048 太吃显存)g++ 即可deepseek-coder:6.7b-q4_0),32G 内存足够,i9 足够,6G 显存刚好够用。