文档

OpenAI 兼容端点

结构化输出

使用 JSON 模式强制执行 LLM 响应格式。

你可以通过 LM Studio 的 REST API(或任何 OpenAI 客户端)向 /v1/chat/completions 端点提供 JSON 模式,从而强制 LLM 使用特定的响应格式。


将 LM Studio 作为服务器启动

若要通过代码以编程方式使用 LM Studio,请将 LM Studio 作为本地服务器运行。

你可以从 LM Studio 的“开发者 (Developer)”选项卡中开启服务器,或通过 lms CLI 启动。

lms server start
运行 npx lmstudio install-cli 即可安装 lms

这将允许你通过 REST API 与 LM Studio 进行交互。有关 LM Studio REST API 的简介,请参阅 REST API 概述

结构化输出

该 API 在提供 JSON 模式时,通过 /v1/chat/completions 端点支持结构化 JSON 输出。执行此操作将使 LLM 以符合所提供模式的有效 JSON 格式进行响应。

它遵循与 OpenAI 最近宣布的 结构化输出 (Structured Output) API 相同的格式,并预期可通过 OpenAI 客户端 SDK 使用。

使用 curl 的示例

此示例演示了如何使用 curl 工具进行结构化输出请求。

要在 Mac 或 Linux 上运行此示例,请使用任意终端。在 Windows 上,请使用 Git Bash

curl https://:1234/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "{{model}}",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful jokester."
      },
      {
        "role": "user",
        "content": "Tell me a joke."
      }
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "joke_response",
        "strict": "true",
        "schema": {
          "type": "object",
          "properties": {
            "joke": {
              "type": "string"
            }
          },
          "required": ["joke"]
        }
      }
    },
    "temperature": 0.7,
    "max_tokens": 50,
    "stream": false
  }'

/v1/chat/completions 可识别的所有参数都将被支持,且 JSON 模式应提供在 response_format 字段下的 json_schema 字段中。

JSON 对象将以 string(字符串)形式出现在典型的响应字段 choices[0].message.content 中,你需要将其解析为 JSON 对象。

使用 python 的示例

from openai import OpenAI
import json

# Initialize OpenAI client that points to the local LM Studio server
client = OpenAI(
    base_url="https://:1234/v1",
    api_key="lm-studio"
)

# Define the conversation with the AI
messages = [
    {"role": "system", "content": "You are a helpful AI assistant."},
    {"role": "user", "content": "Create 1-3 fictional characters"}
]

# Define the expected response structure
character_schema = {
    "type": "json_schema",
    "json_schema": {
        "name": "characters",
        "schema": {
            "type": "object",
            "properties": {
                "characters": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "name": {"type": "string"},
                            "occupation": {"type": "string"},
                            "personality": {"type": "string"},
                            "background": {"type": "string"}
                        },
                        "required": ["name", "occupation", "personality", "background"]
                    },
                    "minItems": 1,
                }
            },
            "required": ["characters"]
        },
    }
}

# Get response from AI
response = client.chat.completions.create(
    model="your-model",
    messages=messages,
    response_format=character_schema,
)

# Parse and display the results
results = json.loads(response.choices[0].message.content)
print(json.dumps(results, indent=2))

重要提示:并非所有模型都具备结构化输出能力,特别是 7B 参数以下的 LLM。

如果不确定模型是否支持结构化输出,请查阅模型卡的 README 文件。

结构化输出引擎

  • 对于 GGUF 模型:使用 llama.cpp 的基于语法的采样 API。
  • 对于 MLX 模型:使用 Outlines

MLX 实现可在 Github 上获取:lmstudio-ai/mlx-engine


社区...

LM Studio Discord 服务器上与其他 LM Studio 用户聊天,讨论大型语言模型、硬件等。

本页源代码可在 GitHub 上获取