文档
Anthropic 兼容端点
Anthropic 兼容端点
安装并启动 LM Studio。
然后,确保通过开发者页面左上角的切换开关,或通过终端中的 lms 启动了服务器。
lms server start
默认情况下,服务器可在 https://:1234 访问。
如果您还没有下载模型,可以先下载模型
lms get ibm/granite-4-micro
默认情况下,LM Studio API 服务器不要求身份验证。为了提高安全性,您可以在服务器设置中将服务器配置为需要 API 令牌进行身份验证。
要验证 API 请求,请在 LM Studio 的开发者页面生成 API 令牌,并将其包含在请求的 Authorization 标头中,格式如下:Authorization: Bearer $LM_API_TOKEN。在此阅读更多关于身份验证的信息。
使用聊天端点向模型发送消息。默认情况下,如果模型尚未加载,系统将自动加载它。
/api/v1/chat 端点是有状态的,这意味着您不需要在每个请求中传递完整的历史记录。在此阅读更多相关说明。
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "Write a short haiku about sunrise."
}'
请参阅完整的聊天文档以获取更多详细信息。
通过在 integrations 字段中指定服务器,使模型能够在 /api/v1/chat 中与临时模型上下文协议 (MCP) 服务器交互。
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "What is the top trending model on hugging face?",
"integrations": [
{
"type": "ephemeral_mcp",
"server_label": "huggingface",
"server_url": "https://hugging-face.cn/mcp",
"allowed_tools": ["model_search"]
}
],
"context_length": 8000
}'
您还可以通过 integrations 字段使用本地配置的 MCP 插件(来自您的 mcp.json)。使用本地运行的 MCP 插件需要通过 Authorization 标头传递 API 令牌进行身份验证。在此阅读更多关于身份验证的信息。
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "Open lmstudio.ai",
"integrations": [
{
"type": "plugin",
"id": "mcp/playwright",
"allowed_tools": ["browser_navigate"]
}
],
"context_length": 8000
}'
请参阅完整的聊天文档以获取更多详细信息。
使用下载端点通过标识符从 LM Studio 模型目录下载模型,或通过 Hugging Face 模型 URL 下载。
curl https://:1234/api/v1/models/download \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro"
}'
响应将返回一个 job_id,您可以使用它来跟踪下载进度。
curl -H "Authorization: Bearer $LM_API_TOKEN" \
https://:1234/api/v1/models/download/status/{job_id}
本页面源码可在 GitHub 上获取