ドキュメント
LLMのローカル実行
プリセット
API
ユーザーインターフェース
高度な設定
LLMのローカル実行
プリセット
API
ユーザーインターフェース
高度な設定
構造化出力
LLMから特定の応答形式を強制するには、LM StudioのREST API(または任意のOpenAIクライアント)を介して、/v1/chat/completions
エンドポイントにJSONスキーマを提供します。
独自のコードから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は、/v1/chat/completions
エンドポイントにJSONスキーマが与えられた場合に、構造化された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_format
のjson_schema
フィールドで提供される必要があります。
JSONオブジェクトは、通常の応答フィールドchoices[0].message.content
にstring
形式で提供され、JSONオブジェクトにパースする必要があります。
python
を使用した例
from openai import OpenAI import json # Initialize OpenAI client that points to the local LM Studio server client = OpenAI( base_url="http://localhost: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サーバーで議論しましょう。
このページ
LM Studioをサーバーとして起動する
構造化出力
構造化出力エンジン
コミュニティ