文档

结构化输出

您可以通过 LM Studio 的 REST API(或通过任何 OpenAI 客户端)向 /v1/chat/completions 端点提供 JSON schema,强制大型语言模型(LLM)以特定格式响应。


将 LM Studio 作为服务器启动

要从您自己的代码中以编程方式使用 LM Studio,请将 LM Studio 作为本地服务器运行。

您可以从 LM Studio 的“开发者”选项卡中,或通过 lms 命令行接口启动服务器。

lms server start
通过运行 npx lmstudio install-cli 来安装 lms

这将允许您通过类似 OpenAI 的 REST API 与 LM Studio 交互。要了解 LM Studio 类似 OpenAI 的 API 简介,请参阅将 LM Studio 作为服务器运行


结构化输出

当给定 JSON schema 时,API 支持通过 /v1/chat/completions 端点输出结构化 JSON。这样做将使大型语言模型(LLM)以符合所提供 schema 的有效 JSON 格式响应。

它遵循 OpenAI 最近发布的结构化输出 API 的相同格式,并预计可通过 OpenAI 客户端 SDK 正常工作。

使用 curl 的示例

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

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

curl http://{{hostname}}:{{port}}/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 schema 应在 response_formatjson_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 用户聊天,讨论大型语言模型、硬件等。