交互 API(Beta)
使用 Convai 的交互 API 启用实时对话式 AI。向你的 AI 角色发送文本消息,并通过服务器发送事件(SSE)接收自然的流式响应。
最后更新于
这有帮助吗?
这有帮助吗?
curl -X POST https://live.convai.com/connect/stream \\
-H "X-API-Key: YOUR_API_KEY" \\
-F "character_id=7bd3274c-1745-11ee-a3af-42010a400002" \\
-F "text_input=Hello, how are you?" \\
--no-bufferimport httpx
import json
async with httpx.AsyncClient(timeout=30.0) as client:
async with client.stream(
'POST',
'https://live.convai.com/connect/stream',
headers={'X-API-Key': 'YOUR_API_KEY'},
data={
'character_id': '7bd3274c-1745-11ee-a3af-42010a400002',
'text_input': 'Hello, how are you?'
}
) as response:
async for line in response.aiter_lines():
if line.startswith('data: '):
data = json.loads(line[6:])
if data.get('type') == 'bot-llm-text':
print(data['data']['text'], end='', flush=True)
const formData = new FormData();
formData.append('character_id', '7bd3274c-1745-11ee-a3af-42010a400002');
formData.append('text_input', 'Hello, how are you?');
const response = await fetch('https://live.convai.com/connect/stream', {
method: 'POST',
headers: { 'X-API-Key': 'YOUR_API_KEY' },
body: formData,
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
if (data.type === 'bot-llm-text') {
console.log(data.data.text);
}
}
}
}data: {"type": "connection-started", "message": {"session_id": "abc123", "transport": "sse", "character_session_id": "def456"}}
data: {"label": "rtvi-ai", "type": "bot-llm-started"}
data: {"label": "rtvi-ai", "type": "bot-llm-text", "data": {"text": "你好"}}
data: {"label": "rtvi-ai", "type": "bot-llm-text", "data": {"text": "!我很好,谢谢你的关心。"}}
data: {"label": "rtvi-ai", "type": "bot-transcription", "data": {"text": "你好!我很好,谢谢你的关心。"}}
data: {"label": "rtvi-ai", "type": "bot-llm-stopped"}
data: {"type": "connection-stoppped"}curl -X POST https://live.convai.com/connect/stream \\
-H "X-API-Key: YOUR_API_KEY" \\
-F "character_id=7bd3274c-1745-11ee-a3af-42010a400002" \\
-F "text_input=My name is Alice" \\
--no-buffer{"type": "connection-started", "message": {"character_session_id": "9a98ab9b-5a6c-406e-9721-cc5bb0d527bb", ...}}curl -X POST https://live.convai.com/connect/stream \\
-H "X-API-Key: YOUR_API_KEY" \\
-F "character_id=7bd3274c-1745-11ee-a3af-42010a400002" \\
-F "text_input=What is my name?" \\
-F "character_session_id=9a98ab9b-5a6c-406e-9721-cc5bb0d527bb" \\
--no-bufferimport httpx
import json
import asyncio
session_id = None
async def chat(message):
global session_id
data = {
'character_id': '7bd3274c-1745-11ee-a3af-42010a400002',
'text_input': message,
}
if session_id:
data['character_session_id'] = session_id
async with httpx.AsyncClient(timeout=30.0) as client:
async with client.stream(
'POST',
'https://live.convai.com/connect/stream',
headers={'X-API-Key': 'YOUR_API_KEY'},
data=data,
) as response:
async for line in response.aiter_lines():
if line.startswith('data: '):
msg = json.loads(line[6:])
if msg.get('type') == 'connection-started':
session_id = msg['message']['character_session_id']
elif msg.get('type') == 'bot-llm-text':
print(msg['data']['text'], end='', flush=True)
print()
# 用法
await chat('My name is Alice')
await chat('What is my name?') # 机器人记得:"Your name is Alice"let sessionId = null;
async function chat(message) {
const formData = new FormData();
formData.append('character_id', '7bd3274c-1745-11ee-a3af-42010a400002');
formData.append('text_input', message);
if (sessionId) {
formData.append('character_session_id', sessionId);
}
const response = await fetch('https://live.convai.com/connect/stream', {
method: 'POST',
headers: { 'X-API-Key': 'YOUR_API_KEY' },
body: formData,
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\\n');
buffer = lines.pop();
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
if (data.type === 'connection-started') {
sessionId = data.message.character_session_id;
} else if (data.type === 'bot-llm-text') {
process.stdout.write(data.data.text);
}
}
}
}
console.log();
}
// 用法
await chat('My name is Alice');
await chat('What is my name?'); // 机器人记得:"Your name is Alice"{
"type": "connection-started",
"message": {
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"transport": "sse",
"character_session_id": "660e8400-e29b-41d4-a716-446655440001"
}
}{
"label": "rtvi-ai",
"type": "bot-llm-started"
}{
"label": "rtvi-ai",
"type": "bot-llm-text",
"data": {
"text": "你好呀!"
}
}{
"label": "rtvi-ai",
"type": "bot-transcription",
"data": {
"text": "你好呀!完整的响应文本。"
}
}{
"label": "rtvi-ai",
"type": "bot-llm-stopped"
}{
"type": "connection-stoppped"
}{
"detail": "描述出错原因的错误消息"
}{
"detail": "API 密钥无效"
}