Примеры кода
curl
curl -N https://api.cyberai.help/chat \
-H 'Content-Type: application/json' \
-H 'X-Api-Key: ВАШ_КЛЮЧ_API' \
-d '{"message":"Какие условия доставки?"}'JavaScript (fetch и разбор SSE)
async function ask(apiKey, message, history = [], conversationId = null) {
const res = await fetch('https://api.cyberai.help/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Api-Key': apiKey },
body: JSON.stringify({ message, history, conversation_id: conversationId })
});
const reader = res.body.getReader();
const dec = new TextDecoder();
let buf = '', answer = '', convId = conversationId;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buf += dec.decode(value, { stream: true });
const parts = buf.split('\n\n'); buf = parts.pop();
for (const p of parts) {
if (!p.startsWith('data: ')) continue;
const o = JSON.parse(p.slice(6));
if (o.type === 'token') answer += o.text; // склеиваем ответ
else if (o.type === 'done') convId = o.conversation; // сохраняем id сессии
else if (o.type === 'error') throw new Error(o.error);
}
}
return { answer, conversationId: convId };
}Python (httpx, стриминг)
import json, httpx
def ask(api_key, message, history=None, conversation_id=None):
payload = {"message": message,
"history": history or [], "conversation_id": conversation_id}
answer, conv = "", conversation_id
with httpx.stream("POST", "https://api.cyberai.help/chat", json=payload, headers={"X-Api-Key": api_key}, timeout=60) as r:
for line in r.iter_lines():
if not line.startswith("data: "):
continue
o = json.loads(line[6:])
if o["type"] == "token":
answer += o["text"]
elif o["type"] == "done":
conv = o["conversation"]
elif o["type"] == "error":
raise RuntimeError(o["error"])
return answer, convПамять диалога. Чтобы бот помнил разговор: в первом запросе не передавайте
conversation_id, возьмите его из события done и передавайте в следующих запросах, а history собирайте из пар вопрос-ответ. Так вся сессия будет одним диалогом и попадёт в раздел «Диалоги» кабинета.