Ultralytics YOLO27:

Ultralytics LLM 接口#

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 为默认 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 的提供商配合使用,包括 DeepSeekKimiZ.AI GLMOpenRouter。设置提供商的 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_urlNone可选的兼容 OpenAI 的端点
api_keyNoneAPI 密钥;否则 SDK 会读取 OPENAI_API_KEY
promptNone添加到纯文本和图像请求前面的指令
**kwargs默认 SDK 请求参数;每次调用的参数会覆盖构造函数中对应的值

source 接受文本、原生消息列表或图像对象。image 接受图像 URL、数据 URI、路径、NumPy 数组或 PIL 图像。使用已配置的 prompt 调用 model() 时,只会发送提示词本身。

LLM 仅用于推理,不会通过 yolo CLI 暴露。它不实现 trainvalexporttrackbenchmark

常见问题#

  • LLM 默认使用 Responses API。对于 Chat Completions 端点,请设置 api="chat.completions"

  • 你可以使用 OpenAI 或其他提供商提供的兼容 API,例如 DeepSeekKimiZ.AI GLMOpenRouter。请使用提供商的模型名称、基础 URL 和 API 密钥。

  • 将提示词作为第一个参数传入,并通过 image 传入本地路径、URL、数据 URI、NumPy 数组或 PIL 图像。图像支持取决于所选模型和提供商。

  • 不可以。LLM 仅提供同步和异步推理。对于计算机视觉训练、验证、预测、导出、跟踪和基准测试,请使用 Ultralytics 的 YOLO

贡献者

评论