与模型对话
向模型发送消息并接收响应。支持 MCP 集成。
POST /api/v1/chat
请求体
model : string
要使用的模型的唯一标识符。
input : string | array<object>
要发送给模型的消息。
输入文本 : string
消息的文本内容。
输入对象 : object
表示带有附加元数据的消息的对象。
文本输入 (可选) : object
用于提供用户消息的文本输入
type : "message"
输入项的类型。
content : string
消息的文本内容。
图片输入 (可选) : object
用于提供用户消息的图片输入
type : "image"
输入项的类型。
data_url : string
Base64 编码的数据 URL 格式的图片数据。
system_prompt (可选) : string
设置模型行为或指令的系统消息。
integrations (可选) : array<string | object>
为此请求启用的集成(插件、临时 MCP 服务器等)列表。
插件 ID : string
要使用的插件的唯一标识符。插件包含在 mcp.json 中安装的 MCP 服务器(ID 为 mcp/<server_label>)。这是无自定义配置的插件对象的简写。
插件 : object
要使用的插件的规范。插件包含在 mcp.json 中安装的 MCP 服务器(ID 为 mcp/<server_label>)。
type : "plugin"
集成类型。
id : string
插件的唯一标识符。
allowed_tools (可选) : array<string>
模型可以从此插件调用的工具名称列表。如果未提供,则允许使用该插件的所有工具。
临时 MCP 服务器规范 : object
临时 MCP 服务器的规范。允许即时定义 MCP 服务器,而无需在 mcp.json 中进行预配置。
type : "ephemeral_mcp"
集成类型。
server_label : string
用于标识 MCP 服务器的标签。
server_url : string
MCP 服务器的 URL。
allowed_tools (可选) : array<string>
模型可以从此服务器调用的工具名称列表。如果未提供,则允许使用该服务器的所有工具。
headers (可选) : object
发送到该服务器的请求中携带的自定义 HTTP 请求头。
stream (可选) : boolean
是否通过 SSE 流式传输部分输出。默认为 false。有关更多信息,请参阅流式事件。
temperature (可选) : number
Token 选择的随机性。0 表示确定性,更高的值会增加创造力 [0,1]。
top_p (可选) : number
下一个可能 Token 的最小累积概率 [0,1]。
top_k (可选) : integer
将下一个 Token 的选择限制为概率最高的前 k 个 Token。
min_p (可选) : number
被选择输出的 Token 的最小基础概率 [0,1]。
repeat_penalty (可选) : number
对重复 Token 序列的惩罚。1 表示不惩罚,更高的值会抑制重复。
max_output_tokens (可选) : integer
要生成的最大 Token 数量。
reasoning (可选) : "off" | "low" | "medium" | "high" | "on"
推理设置。如果所使用的模型不支持该推理设置,则会报错。默认为模型自动选择的设置。
context_length (可选) : integer
作为上下文考虑的 Token 数量。建议在使用 MCP 时设为更高的值。
store (可选) : boolean
是否存储对话。如果设置,响应将返回 "response_id" 字段。默认为 true。
previous_response_id (可选) : string
要追加到的现有响应的标识符。必须以 "resp_" 开头。
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "Tell me the top trending model on hugging face and navigate to https://lm-studio.cn",
"integrations": [
{
"type": "ephemeral_mcp",
"server_label": "huggingface",
"server_url": "https://hugging-face.cn/mcp",
"allowed_tools": [
"model_search"
]
},
{
"type": "plugin",
"id": "mcp/playwright",
"allowed_tools": [
"browser_navigate"
]
}
],
"context_length": 8000,
"temperature": 0
}'响应字段
model_instance_id : string
生成该响应的已加载模型实例的唯一标识符。
output : array<object>
生成的输出项数组。每个项可以是三种类型之一。
消息 : object
来自模型的文本消息。
type : "message"
输出项的类型。
content : string
消息的文本内容。
工具调用 : object
模型进行的工具调用。
type : "tool_call"
输出项的类型。
tool : string
调用的工具名称。
arguments : object
传递给工具的参数。根据工具定义,可以有任何键/值。
output : string
工具返回的结果。
provider_info : object
有关工具提供者的信息。
type : "plugin" | "ephemeral_mcp"
提供者类型。
plugin_id (可选) : string
插件的标识符(当 type 为 "plugin" 时)。
server_label (可选) : string
MCP 服务器的标签(当 type 为 "ephemeral_mcp" 时)。
推理 : object
来自模型的推理内容。
type : "reasoning"
输出项的类型。
content : string
推理的文本内容。
无效工具调用 : object
模型进行的无效工具调用 - 由于无效的工具名称或工具参数。
type : "invalid_tool_call"
输出项的类型。
reason : string
工具调用无效的原因。
metadata : object
关于无效工具调用的元数据。
type : "invalid_name" | "invalid_arguments"
发生的错误类型。
tool_name : string
尝试调用的工具名称。
arguments (可选) : object
传递给工具的参数(仅在 invalid_arguments 错误时存在)。
provider_info (可选) : object
有关工具提供者的信息(仅在 invalid_arguments 错误时存在)。
type : "plugin" | "ephemeral_mcp"
提供者类型。
plugin_id (可选) : string
插件的标识符(当 type 为 "plugin" 时)。
server_label (可选) : string
MCP 服务器的标签(当 type 为 "ephemeral_mcp" 时)。
stats : object
Token 使用情况和性能指标。
input_tokens : number
输入 Token 的数量。包括格式化、工具定义以及对话中先前的消息。
total_output_tokens : number
生成的输出 Token 总数。
reasoning_output_tokens : number
用于推理的 Token 数量。
tokens_per_second : number
生成速度(每秒 Token 数)。
time_to_first_token_seconds : number
生成第一个 Token 所需的时间(秒)。
model_load_time_seconds (可选) : number
为此请求加载模型所花费的时间(秒)。仅在模型尚未加载时存在。
response_id (可选) : string
后续请求的响应标识符。以 "resp_" 开头。当 store 为 true 时存在。
{
"model_instance_id": "ibm/granite-4-micro",
"output": [
{
"type": "tool_call",
"tool": "model_search",
"arguments": {
"sort": "trendingScore",
"query": "",
"limit": 1
},
"output": "...",
"provider_info": {
"server_label": "huggingface",
"type": "ephemeral_mcp"
}
},
{
"type": "message",
"content": "..."
},
{
"type": "tool_call",
"tool": "browser_navigate",
"arguments": {
"url": "https://lm-studio.cn"
},
"output": "...",
"provider_info": {
"plugin_id": "mcp/playwright",
"type": "plugin"
}
},
{
"type": "message",
"content": "**Top Trending Model on Hugging Face** ... Below is a quick snapshot of what’s on the landing page ... more details on the model or LM Studio itself!"
}
],
"stats": {
"input_tokens": 646,
"total_output_tokens": 586,
"reasoning_output_tokens": 0,
"tokens_per_second": 29.753900615398926,
"time_to_first_token_seconds": 1.088,
"model_load_time_seconds": 2.656
},
"response_id": "resp_4ef013eba0def1ed23f19dde72b67974c579113f544086de"
}