使用 Needle(一款仅 14MB 的函数调用型大语言模型)将文本输入转化为操作
在本文中,Cactus Compute 团队展示了 Needle 2(一款仅 14MB 的函数调用模型)如何仅凭 Raspberry Pi 5 的 CPU,将日常英语指令转化为本地操作。您还可查看Needle 3——最新发布版本。
输入“打开 LED”,78 毫秒后,Raspberry Pi 5 上的 LED 即被点亮。这是 Needle 2 仅在 CPU 上运行的结果——无需专用 AI HAT。

我们先从 Needle 是什么讲起先拔下连接器的一端。它并非聊天机器人。相反,Cactus Compute团队仅针对一项任务对其进行了训练:在设备端实现可靠、结构化的操作。
“Needle 2 表现相当出色”
——Eben Upton,Raspberry Pi 首席执行官
设置非常简单——您只需声明 Python 函数,然后 Needle 将自动选择调用哪一个,并填充相应参数。我们已用它来编写本地笔记、查询vcgencmd, interact with the LEDs, and more on a CPU-only Raspberry Pi 5.
python3 -m venv needle-env
source needle-env/bin/activate
python -m pip install cactus-needle
首次下载完成后,Needle 即可完全离线本地运行,无需云 API 或网络连接。本示例无需额外硬件。
import needle
from pathlib import Path
import subprocess
notes_path = Path("needle-notes.txt")
该Needle class accepts functions decorated with @needle.tool. The decorator uses each function’s name, docstring, and type annotations to build its tool schema. In save_note below, the annotation tells Needle that text must be a string.
@needle.tool
def save_note(text: str):
"""将一条笔记追加到本地文本文件中。"""
with notes_path.open("a", encoding="utf-8") as notes:
notes.write(text + "\n")
return {"text": text, "path": str(notes_path)}
@needle.tool
def get_temperature():
"""返回当前 Raspberry Pi 的 CPU 温度(摄氏度)。"""
out = subprocess.check_output(["vcgencmd", "measure_temp"], text=True)
return {"temperature_c": float(out.split("=")[1].split("'")[0])}
agent = needle.Needle(tools=[save_note, get_temperature])
response = agent.run("保存一条笔记,内容为‘冷却器正在工作’。")
其中包含run(), the model first produces this tool selection (output trimmed for clarity):
{
"type": "call",
"function_calls": [
{
"name": "save_note",
"arguments": { "text": "the cooler is working" }
}
]
}
run() executes the function and includes its return value in response["results"]:
[
{
"text": "the cooler is working",
"path": "needle-notes.txt"
}
]
在本次运行中,初始complete() inside run() selected the function and produced its arguments in 107ms.
我们还可以进一步询问:
agent.reset()
response = agent.run("这台 Raspberry Pi 当前温度有多高?")
Needle 调用:
{
"type": "call",
"function_calls": [
{"name": "get_temperature", "arguments": {}}
]
}
run() executes vcgencmd and includes the reading in its results:
{ "temperature_c": 49.9 }
我们还声明了set_led(on), blink_led(times), and take_photo() via rpicam-still to test tool selection. Needle chooses the tool; ordinary Python code decides what happens next. You can similarly decorate any Python function that has a clear name, type annotations, and description with @needle.tool.
Needle 故意设计得较为精简。例如提问“法国的首都是什么?”,它会返回{ "function_calls": [] } in 92ms because none of the tools we gave it can answer that question. For an action model, refusing an unrelated request is the correct response.
Needle 的原生会话内存占用约为 28MB。在此 Python 演示中,整个流程(含解释器)的内存峰值介于 43MB 至 46.4MB 之间。以下是若干次运行结果:
| 提示词 | 调用 | 预填充(词元/秒) | 解码(词元/秒) | 耗时(毫秒) |
| 打开 LED。 | set_led | 488 | 296 | 78 |
| 这块树莓派的温度有多高? | get_temperature | 487 | 303 | 149 |
| 让 LED 闪烁 2 次。 | blink_led | 475 | 314 | 83 |
| 拍照。 | take_photo | 461 | 248 | 76 |
| 保存一条备注,说明散热器正在正常工作。 | save_note | 475 | 305 | 107 |
| 法国的首都是什么? | (无) | 470 | 297 | 92 |
这些示例在树莓派 5(8GB)、树莓派操作系统(Raspberry Pi OS)上运行,仅使用 CPU。cactus-needle 2.0.7. 预填充和解码是 Needle 的会话计数器。每一项延迟值所用时间列中的数值为单次操作的挂钟时间(wall-clock time)。complete() call, before the selected Python function runs.
如果你已编写了 GPIO Zero 函数,可对其添加装饰器后传入;其他应用逻辑同理。此外,Needle 还可在笔记本电脑上针对特定工具集进行本地微调。
你可在以下地址找到模型权重与代码:huggingface.co/Cactus-Compute/needle2, github.com/cactus-compute/needle两者均按 Apache 2.0 许可协议发布。
7条评论
跳转到评论表单
罗马
感谢提供此功能!非常期待借助树莓派实现端侧(on-device)体验。
期待在此处解答各位的任何问题
查尔斯·罗斯
这极其实用,尤其因为无需特殊 HAT!万分感谢分享。
大天狗
那是我的笔记本电脑吗?我在相同位置贴了相同的贴纸。卡尔!你知道我对贴纸有多反感!
兰德尔
得由比我更聪明的人来帮我从 A 直接走到 C……我实在看不出,用大语言模型(LLM)处理确定性逻辑、实现本已极其简单且被广泛文档化的功能,到底有何实用价值。这感觉就像一个“为找问题而生的解决方案”,还额外增加了冗余和一层不必要的、含糊的抽象。
Anders
其目的是将接口转化为轻松自然的日常语言,让模型自行解析用户请求的含义,而非强制用户遵循严格的 API 调用协议。
兰德尔
但在这一切发生之前,你仍需在代码中显式地向模型注册该函数,对吧?我不太确定。LLM 在资源受限场景下确实存在一些真正令人兴奋的过渡性应用和用例,但这个似乎并不属于其中之一。
crumble
如果该系统能长期获得支持,你现在就可以构建语音控制的玩具或住宅。智能家居对残障人士大有裨益。
如果你是残障人士,你需要的是稳定可靠、可信赖使用多年的产品;它绝不能在下一次更新后就失效。