Help agents build conversational AI with AINative's Chat Completions API. Use when (1) Building a chatbot or AI assistant, (2) Implementing streaming responses, (3) Using function/tool calling, (4) Tracking credit usage per request, (5) Choosing between streaming and non-streaming. Covers raw API, Python SDK, React SDK, and Next.js patterns. Closes #1522.
POST https://api.ainative.studio/v1/public/chat/completions
认证方式: X-API-Key: ak_... 或 Authorization: Bearer
python
import requests
response = requests.post(
https://api.ainative.studio/v1/public/chat/completions,
headers={X-API-Key: akyourkey},
json={
model: claude-3-5-sonnet-20241022,
messages: [
{role: system, content: 你是一个乐于助人的助手。},
{role: user, content: 什么是ZeroDB?}
],
temperature: 0.7,
max_tokens: 1024
}
)
data = response.json()
print(data[choices][0][message][content])
print(f使用的令牌数:{data[usage][total_tokens]})
python
import requests
with requests.post(
https://api.ainative.studio/v1/public/chat/completions,
headers={X-API-Key: akyourkey},
json={model: claude-3-5-sonnet-20241022,
messages: [{role: user, content: 数到5}],
stream: True},
stream=True
) as resp:
for line in resp.iter_lines():
if line and line.startswith(bdata: ):
chunk = line[6:]
if chunk != b[DONE]:
import json
delta = json.loads(chunk)[choices][0][delta]
print(delta.get(content, ), end=, flush=True)
bash
npm install @ainative/react-sdk
tsx
import { AINativeProvider, useChat } from @ainative/react-sdk;
function App() {
return (
);
}
function ChatComponent() {
const { messages, sendMessage, isLoading, error } = useChat({
model: claude-3-5-sonnet-20241022,
systemPrompt: 你是一个乐于助人的助手。,
});
const handleSend = () => sendMessage(你好!);
return (
错误:{error.message}
}bash
npm install @ainative/next-sdk
typescript
// app/api/chat/route.ts
import { createServerClient } from @ainative/next-sdk/server;
export async function POST(request: Request) {
const { messages } = await request.json();
const client = createServerClient({ apiKey: process.env.AINATIVEAPIKEY! });
const stream = await client.chat.completions.create({
model: claude-3-5-sonnet-20241022,
messages,
stream: true,
});
return new Response(stream.body, {
headers: { Content-Type: text/event-stream }
});
}
| 模型 | 上下文长度 | 最佳用途 |
|---|---|---|
| claude-3-5-sonnet-20241022 | 200k | 通用(默认) |
| claude-3-5-haiku-20241022 |
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| model | 字符串 | 必填 | 模型ID |
| messages |
积分按令牌消耗。高量使用前请检查余额:
python
balance = requests.get(
https://api.ainative.studio/api/v1/public/credits/balance,
headers={X-API-Key: akyourkey}
).json()
print(f剩余积分:{balance[remaining_credits]})
| 状态码 | 含义 | 处理方式 |
|---|---|---|
| 401 | 无效的API密钥 | 检查密钥格式:ak_... |
| 402 |
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 ainative-chat-completions-1776064690 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 ainative-chat-completions-1776064690 技能
skillhub install ainative-chat-completions-1776064690
文件大小: 2.8 KB | 发布时间: 2026-4-14 10:52