ドキュメント

構造化出力

LM StudioのREST API(またはOpenAIクライアント)を介して、/v1/chat/completions エンドポイントにJSONスキーマを提供することで、LLMから特定のレスポンス形式を強制できます。


LM Studioをサーバーとして起動する

独自のコードからLM Studioをプログラムで使用する場合は、LM Studioをローカルサーバーとして実行します。

LM Studioの「開発者」タブ、またはlms CLIからサーバーを起動できます。

lms server start
npx lmstudio install-cliを実行してlmsをインストールします

これにより、OpenAIのようなREST APIを介してLM Studioと対話できます。 LM StudioのOpenAIのようなAPIの概要については、LM Studioをサーバーとして実行するを参照してください。


構造化出力

APIは、JSONスキーマが指定されている場合、/v1/chat/completionsエンドポイントを介して構造化JSON出力をサポートします。 これにより、LLMは提供されたスキーマに準拠した有効な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スキーマはresponse_formatjson_schemaフィールドに提供する必要があります。

JSONオブジェクトは、通常のレスポンスフィールド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ユーザーとチャットしたり、LLM、ハードウェアなどについてLM Studio Discordサーバーで話し合ったりできます。