API 参考
对话接口
LMU AI 的 OpenAI 兼容对话接口。支持流式与非流式响应、chatId 上下文管理、知识库检索引用。第三方项目改 Base URL 即可接入。
本站为 LMU AI 应用接入文档,并非 API Token 中转服务。如果你需要 API Token 服务,可前往 灵眸 AI。
对话接口完全兼容 OpenAI 的 Chat Completions。如果你已有第三方项目,只需修改 Base URL 和 Authorization 即可调用 LMU AI 应用。
接口地址
POST https://ai.lmuai.com/api/openapi/v1/chat/completions鉴权
使用 apiKey-appId 组合作为 Bearer Token(见 鉴权):
Authorization: Bearer {apiKey}-{appId}请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
messages | array | 是 | 对话消息列表,与 OpenAI 一致:[{ "role": "user", "content": "..." }],role 可为 system / user / assistant。 |
stream | boolean | 否 | 是否流式返回,默认 false。 |
chatId | string | 否 | 上下文标识,见下方说明。 |
appId | string | 否 | 应用 ID。已包含在 Bearer Token 中时可省略。 |
temperature | number | 否 | 采样温度,兼容 OpenAI。 |
max_tokens | number | 否 | 最大生成 token,兼容 OpenAI。 |
chatId 的三种取值
| 取值 | 行为 |
|---|---|
| 不传(undefined) | 不使用服务端历史,完全通过传入的 messages 构建上下文。 |
空字符串 "" | 表示新会话的第一次对话,响应中会返回一个 newChatId。 |
| 非空字符串 | 使用该 chatId 继续对话,服务端自动取历史记录拼接上下文。 |
请求示例
curl https://ai.lmuai.com/api/openapi/v1/chat/completions \
-H "Authorization: Bearer sk-v45kgtllywo9kol9io3eic04-642adec15f04d67d4613efdb" \
-H "Content-Type: application/json" \
-d '{
"chatId": "",
"stream": false,
"messages": [
{ "role": "user", "content": "导演是谁" }
]
}'非流式响应
{
"rawSearch": [
{
"id": "19339",
"q": "电影《铃芽之旅》的导演是谁?",
"a": "电影《铃芽之旅》的导演是新海诚。",
"source": ""
}
],
"newChatId": "648f0fc12e0d47315f3bc30e",
"id": "",
"model": "gpt-3.5-turbo-16k",
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 373
},
"choices": [
{
"message": { "role": "assistant", "content": "电影《铃芽之旅》的导演是新海诚。" },
"finish_reason": "stop",
"index": 0
}
]
}特殊字段:
| 字段 | 说明 |
|---|---|
newChatId | 新会话 ID。下次对话传入它即可获得上下文。 |
rawSearch | 本次命中的知识库原始数据。 |
quoteLen | 本次引用知识库的条数(流式响应中通过 chatResponse 事件返回)。 |
流式响应(SSE)
设置 "stream": true 后,响应为 Server-Sent Events 事件流:
chatResponse事件:携带newChatId与quoteLen,通常最先返回。answer事件:携带增量内容choices[0].delta.content。- 以
data: [DONE]结束。
event: chatResponse
data: {"newChatId": "648f0fc12e0d47315f3bc30e", "quoteLen": 5}
event: answer
data: {"id":"","object":"","created":0,"choices":[{"delta":{"content":"电"},"index":0,"finish_reason":null}]}
event: answer
data: {"id":"","object":"","created":0,"choices":[{"delta":{"content":"影"},"index":0,"finish_reason":null}]}
event: answer
data: [DONE]浏览器端解析示例
const response = await fetch(
'https://ai.lmuai.com/api/openapi/v1/chat/completions',
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer sk-v45kgtllywo9kol9io3eic04-642adec15f04d67d4613efdb',
},
body: JSON.stringify({
messages: [{ role: 'user', content: 'LMU 是什么?' }],
chatId: '',
stream: true,
}),
},
);
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let responseText = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
for (const line of chunk.split('\n\n').filter(Boolean)) {
const parts = line.split('\n');
const event = parts.length === 2 ? parts[0].replace('event: ', '') : '';
const dataStr = (parts[parts.length - 1] || '').replace('data: ', '');
if (dataStr === '[DONE]') continue;
let data;
try { data = JSON.parse(dataStr); } catch { continue; }
if (event === 'answer') {
responseText += data?.choices?.[0]?.delta?.content || '';
console.log(responseText);
} else if (event === 'chatResponse') {
console.log('newChatId:', data.newChatId, 'quoteLen:', data.quoteLen);
}
}
}Python 示例
import requests
resp = requests.post(
"https://ai.lmuai.com/api/openapi/v1/chat/completions",
headers={
"Authorization": "Bearer sk-v45kgtllywo9kol9io3eic04-642adec15f04d67d4613efdb",
"Content-Type": "application/json",
},
json={
"chatId": "",
"stream": False,
"messages": [{"role": "user", "content": "导演是谁"}],
},
)
print(resp.json())因为接口兼容 OpenAI,你也可以直接用 openai SDK:把 base_url 设为 https://ai.lmuai.com/api/openapi/v1,api_key 设为 apiKey-appId 组合即可。