文档
集成
模型上下文协议 (MCP)
模型 (model.yaml)
API
OpenAI 兼容性 API
向聊天补全(文本和图像)、补全和嵌入端点发送请求
向聊天补全(文本和图像)、补全和嵌入端点发送请求。
LM Studio 接受对多个 OpenAI 端点的请求,并返回类似 OpenAI 的响应对象。
GET /v1/models POST /v1/chat/completions POST /v1/embeddings POST /v1/completions
您可以通过将“基本 URL”属性更改为指向您的 LM Studio 而不是 OpenAI 的服务器来重用现有的 OpenAI 客户端(在 Python、JS、C# 等中)。
基本 URL
更改为指向 LM Studio1234
from openai import OpenAI client = OpenAI( + base_url="https://:1234/v1" ) # ... the rest of your code ...
import OpenAI from 'openai'; const client = new OpenAI({ + baseUrl: "https://:1234/v1" }); // ... the rest of your code ...
- curl https://api.openai.com/v1/chat/completions \ + curl https://:1234/v1/chat/completions \ -H "Content-Type: application/json" \ -d '{ - "model": "gpt-4o-mini", + "model": "use the model identifier from LM Studio here", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7 }'
/v1/models
GET
请求curl https://:1234/v1/models
/v1/chat/completions
POST
请求lms log stream
以查看模型接收到的输入# Example: reuse your existing OpenAI setup from openai import OpenAI # Point to the local server client = OpenAI(base_url="https://:1234/v1", api_key="lm-studio") completion = client.chat.completions.create( model="model-identifier", messages=[ {"role": "system", "content": "Always answer in rhymes."}, {"role": "user", "content": "Introduce yourself."} ], temperature=0.7, ) print(completion.choices[0].message)
/v1/embeddings
POST
请求# Make sure to `pip install openai` first from openai import OpenAI client = OpenAI(base_url="https://:1234/v1", api_key="lm-studio") def get_embedding(text, model="model-identifier"): text = text.replace("\n", " ") return client.embeddings.create(input = [text], model=model).data[0].embedding print(get_embedding("Once upon a time, there was a cat."))
/v1/completions
OpenAI 不再支持此类似 OpenAI 的端点。LM Studio 继续支持它。
将此端点与经过聊天优化的模型一起使用可能会导致意外行为,例如模型发出多余的角色令牌。
为获得最佳结果,请使用基础模型。
POST
请求lms log stream
以查看模型接收到的输入有关每个参数的解释,请参阅https://platform.openai.com/docs/api-reference/chat/create。
model top_p top_k messages temperature max_tokens stream stop presence_penalty frequency_penalty logit_bias repeat_penalty seed
在LM Studio Discord 服务器上与其他 LM Studio 开发者聊天,讨论 LLM、硬件等。
此页面的源代码可在 GitHub 上获取
本页内容
类似 OpenAI 的 API 端点
- 支持的端点
重用现有 OpenAI 客户端
- 将基本 url 切换到 LM Studio
端点概览
- /v1/models
- /v1/chat/completions
- /v1/embeddings
- /v1/completions
支持的负载参数
社区