所有接口基地址为你的部署域名,例如 https://kf.yourdomain.com。
对话接口按 token 计费,余额不足返回 402。
<script src="https://kf.yourdomain.com/widget.js?key=YOUR_PUBLIC_KEY" defer></script>
可选参数:
| 参数 | 说明 | 示例 |
|---|---|---|
key | 必填,端口的 public key | &key=abc123 |
title | 窗口标题 | &title=在线客服 |
color | 主题色(需 URL 编码 #) | &color=%232563eb |
welcome | 首条欢迎语 | &welcome=您好 |
position | 悬浮位置 | &position=left |
POST /api/v1/chat?public_key=PK
Content-Type: application/json
{
"message": "门诊费用能报销吗", // 必填,≤4000 字
"conversation_id": null, // 首轮不传,之后传返回值实现多轮
"visitor_id": "visitor-001", // 可选,用于访客维度统计
"industry_code": "medical", // 可选,覆盖商户默认行业
"history": [] // 可选,自己维护的多轮上下文
}
{
"conversation_id": "3f2a9c...",
"answer": "**结论:线上购药暂不支持医保直接结算。**...",
"tags": ["医药", "医保报销"],
"citations": [
{"title": "医保能报销吗?", "url": "", "score": 0.87}
],
"model": "deepseek-chat",
"usage": {
"prompt_tokens": 812,
"completion_tokens": 156,
"total_tokens": 968,
"charged": 0.0011
},
"latency_ms": 1430
}
入参完全相同,返回 text/event-stream:
data: {"type":"meta","tags":["医药"],"citations":[{"title":"...","score":0.87}]}
data: {"type":"delta","text":"根据"}
data: {"type":"delta","text":"资料"}
data: {"type":"done","conversation_id":"3f2a9c...","usage":{...},"latency_ms":1204}
data: [DONE]
fetch + ReadableStream 读取;EventSource 不支持 POST。
经过 Nginx 反代时必须关闭 proxy_buffering,否则会攒成一次性输出。
| 状态码 | 含义 | 处理建议 |
|---|---|---|
| 400 | 参数错误(消息为空 / 超长) | 检查请求体 |
| 401 | 凭据无效或缺失 | 核对 public_key / API Key |
| 402 | 余额不足或商户停用 | 后台充值或联系服务商 |
| 403 | 会话不属于该商户 | 检查 conversation_id |
| 500 | 服务内部错误 | 查服务端日志 |
curl -X POST "https://kf.yourdomain.com/api/v1/chat" \
-H "X-API-Key: acs_xxxxxxxxxxxx" \
-H "Content-Type: application/json" \
-d '{"message":"退换货政策是什么","visitor_id":"u-1"}'
import httpx
def ask(msg, conv=None):
r = httpx.post(
"https://kf.yourdomain.com/api/v1/chat",
headers={"X-API-Key": "acs_xxxxxxxxxxxx"},
json={"message": msg, "conversation_id": conv},
timeout=120,
)
r.raise_for_status()
return r.json()
d = ask("医保能报销吗")
print(d["answer"], d["usage"]["charged"])
print(ask("那住院呢", d["conversation_id"])["answer"])
const res = await fetch("https://kf.yourdomain.com/api/v1/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-Key": "acs_xxxxxxxxxxxx",
},
body: JSON.stringify({ message: "怎么退货", visitor_id: "u-1" }),
});
const data = await res.json();
console.log(data.answer, data.tags, data.usage);
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create("https://kf.yourdomain.com/api/v1/chat"))
.header("Content-Type", "application/json")
.header("X-API-Key", "acs_xxxxxxxxxxxx")
.POST(HttpRequest.BodyPublishers.ofString(
"{\"message\":\"怎么退货\",\"visitor_id\":\"u-1\"}"))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient()
.send(req, HttpResponse.BodyHandlers.ofString());
完整字段定义与在线调试见 /docs(Swagger UI)。