Ultralytics LLM Interface#
LLM 是一个轻量级接口,用于通过兼容 OpenAI 的 API 调用语言和视觉模型。它准备文本或图像输入,将请求参数转发给官方的 OpenAI Python SDK,并返回 SDK 的原生响应对象。这使得在 YOLO 管道旁边添加语言或视觉推理变得轻而易举,无需包装特定提供商的响应类型。
默认 API 是 responses;对于仅实现 Chat Completions 的提供商,请使用 api="chat.completions"。
安装#
通过可选的 LLM 依赖项安装 Ultralytics:
pip install "ultralytics[llm]"对于 OpenAI,请在环境中设置 API 密钥:
export OPENAI_API_KEY="your-api-key"Responses API#
Responses API 是默认设置。传入提示词并读取 output_text:
from ultralytics import LLM
llm = LLM("gpt-5.6-luna")
response = llm("What is YOLO?")
print(response.output_text)多模态输入#
通过 image 传入路径、HTTP URL、数据 URI、NumPy 数组或 PIL 图像:
from ultralytics import LLM
llm = LLM("gpt-5.6-luna")
response = llm("What is happening in this image?", image="https://ultralytics.com/images/bus.jpg")
print(response.output_text)本地文件和图像对象被编码为 JPEG 数据 URI。NumPy 数组使用 OpenCV 的 BGR 通道顺序。作为 source 传入的字符串(例如 llm("bus.jpg"))将被视为文本;请使用 image 参数来发送图像。
使用 prompt 传递每个请求共有的指令:
llm = LLM("gpt-5.6-luna", prompt="Answer in one sentence.")
response = llm("Describe this image.", image="bus.jpg")Chat Completions#
在端点要求时选择 Chat Completions,并读取其原生响应形状:
from ultralytics import LLM
llm = LLM("gpt-5.6-luna", api="chat.completions")
response = llm("What is non-maximum suppression?")
print(response.choices[0].message.content)相同的接口接受多模态输入:
response = llm("Describe this image.", image="bus.jpg")
print(response.choices[0].message.content)当你需要对话历史时,传入原生消息对象。它们将被原封不动地转发。
流式传输和异步调用#
SDK 请求参数将原封不动地传递,包括 stream=True:
llm = LLM("gpt-5.6-luna")
for event in llm("Explain object detection.", stream=True):
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)使用 async_call 进行并发请求:
import asyncio
from ultralytics import LLM
llm = LLM("gpt-5.6-luna")
async def main():
response = await llm.async_call("What is YOLO?")
print(response.output_text)
asyncio.run(main())兼容 OpenAI 的提供商#
LLM 适用于公开兼容 OpenAI 的 Responses 或 Chat Completions API 的提供商,包括 DeepSeek、Kimi、Z.AI GLM 和 OpenRouter。设置提供商的 base_url、模型名称和 API 密钥:
from ultralytics import LLM
llm = LLM(
"provider-model",
api="chat.completions",
base_url="https://provider.example/v1",
api_key="your-api-key",
)
response = llm("What tasks does YOLO support?")
print(response.choices[0].message.content)相同的配置也适用于兼容的本地服务器。模型名称、图像支持、请求参数和支持的 API 因提供商而异,请查阅其相关文档。
结合 YOLO 和 LLM#
首先运行 YOLO,然后仅在检测到人时调用 LLM:
from ultralytics import LLM, YOLO
yolo = YOLO("yolo26n.pt")
llm = LLM("gpt-5.6-luna")
image = "https://ultralytics.com/images/bus.jpg"
result = yolo(image)[0]
# Call the LLM only when YOLO detects a person.
if any(result.names[int(cls)] == "person" for cls in result.boxes.cls):
response = llm("Describe the scene.", image=image)
print(response.output_text)API 参考#
| 参数 | 默认值 | 描述 |
|---|---|---|
model | "gpt-5.6-luna" | 发送到所选端点的模型标识符 |
api | "responses" | API 格式:"responses" 或 "chat.completions" |
base_url | None | 可选的兼容 OpenAI 的端点 |
api_key | None | API 密钥;否则 SDK 会读取 OPENAI_API_KEY |
prompt | None | 预置在纯文本和图像请求之前的指令 |
**kwargs | 默认的 SDK 请求参数;每次调用的参数会覆盖匹配的构造函数值 |
source 接受文本、原生消息列表或图像对象。image 接受图像 URL、数据 URI、路径、NumPy 数组或 PIL 图像。使用配置好的 prompt 调用 model() 将单独发送提示词。
LLM 仅用于推理,不会通过 yolo CLI 公开。它不实现 train、val、export、track 或 benchmark。
常见问题解答#
LLM默认使用 Responses API。为 Chat Completions 端点设置api="chat.completions"。你可以使用 OpenAI 或其他提供兼容 API 的提供商,例如 DeepSeek、Kimi、Z.AI GLM 或 OpenRouter。使用提供商的模型名称、基础 URL 和 API 密钥。
将提示词作为第一个参数传入,并通过
image传入本地路径、URL、数据 URI、NumPy 数组或 PIL 图像。图像支持情况取决于所选的模型和提供商。不能。
LLM仅提供同步和异步推理。请使用 UltralyticsYOLO进行计算机视觉训练、验证、预测、导出、追踪和基准测试。