#API 接口定义
SDK 初始化完成后,可通过 window.$mujian_lite.openapi 获取访问后端 API 所需的配置:
interface OpenAPIConfig {
baseURL: string; // 如 "https://openapi.mujian.ai/v1"
apiKey: string; // 当前登录用户专属 Bearer Token
}#接口列表
| 接口 | 方法 | 路径 | 说明 |
|---|---|---|---|
GET /models | GET | /models | 符合 OpenAI API 规范 的模型列表接口,返回当前可用模型 ID 列表。 |
POST /chat/completions | POST | /chat/completions | 符合 OpenAI 规范的对话补全接口;具体参数请参考 OpenAI 官方文档。 |
#支持的模型
deepseek-v3.2
#认证方式
所有请求必须在 HTTP Header 中携带认证信息:
Authorization: Bearer <apiKey>其中 <apiKey> 来自 window.$mujian_lite.openapi.apiKey。
#请求示例(JavaScript/Fetch)
#获取模型列表
const { baseURL, apiKey } = window.$mujian_lite.openapi;
fetch(`${baseURL}/models`, {
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
})
.then((res) => res.json())
.then((data) => console.log("模型列表:", data.data));#调用模型(非流式)
const { baseURL, apiKey } = window.$mujian_lite.openapi;
async function callMujianOpenAPI(query) {
const response = await fetch(${baseURL}, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'deepseek-v3.2',
messages: [
{ role: 'system', content: '这里是系统提示词' },
{ role: 'user', content: query }
]
})
});
const data = await response.json();
console.log(data.choices[0].message.content); // 打印 AI 返回的内容
}
callMujianOpenAPI('用户输入');#调用模型(流式)
const { baseURL, apiKey } = window.$mujian_lite.openapi;
async function callMujianOpenAPI(query) {
try {
const response = await fetch(${baseURL}, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'deepseek-v3.2',
stream: true,
messages: [
{ role: 'system', content: '这里是系统提示词' },
{ role: 'user', content: query }
]
})
});
if (!response.body) {
throw new Error('No response body');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
// Read the stream
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n\n');
buffer = lines.pop() || ''; // Keep incomplete message in buffer
for (const line of lines) {
if (line.startsWith('data: ')) {
const jsonStr = line.slice(6);
if (jsonStr === '[DONE]') continue;
try {
const data = JSON.parse(jsonStr);
if (data.content) {
// 处理数据块
console.log(data.content); // 比如打印数据块
}
} catch (parseError) {
console.warn('Failed to parse SSE message:', line);
}
}
}
}
} catch (error) {
console.error("Streaming error:", error);
}
}
callMujianOpenAPI('用户输入');