Use @ainative/react-sdk to add AI chat and credits to React apps. Use when (1) Installing @ainative/react-sdk, (2) Using the useChat hook for chat completions, (3) Displaying credit balance with useCredits, (4) Setting up AINativeProvider, (5) Handling loading/error states in chat UI. Published npm package v1.0.1.
用于 AINative 的 React hooks 和组件 — 聊天补全、积分追踪和托管会话。
bash
npm install @ainative/react-sdk
使用 provider 包裹你的应用(或子树):
tsx
import { AINativeProvider } from @ainative/react-sdk;
function App() {
return (
);
}
tsx
import { useChat } from @ainative/react-sdk;
import type { Message } from @ainative/react-sdk;
function ChatUI() {
const { messages, isLoading, error, sendMessage } = useChat({
model: claude-3-5-sonnet-20241022,
temperature: 0.7,
max_tokens: 1024,
});
const handleSend = async (input: string) => {
await sendMessage([
...messages,
{ role: user, content: input }
]);
};
return (
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| model | string | — | 模型 ID(例如 claude-3-5-sonnet-20241022) |
| temperature |
| 字段 | 类型 | 描述 |
|---|---|---|
| messages | Message[] | 完整对话历史 |
| isLoading |
tsx
import { useCredits } from @ainative/react-sdk;
function CreditsBar() {
const { balance, isLoading, error, refetch } = useCredits();
if (isLoading) return
return (
| 字段 | 类型 | 描述 | |
|---|---|---|---|
| balance | CreditBalance \ | null | 余额数据 |
| isLoading |
CreditBalance 结构:{ remaining_credits: number, plan: string, ... }
typescript
import {
AINativeProvider,
useChat,
useCredits,
useAINativeContext, // 访问原始配置/baseUrl
type Message,
type ChatCompletionResponse,
type CreditBalance,
type AINativeError,
type UseChatOptions,
} from @ainative/react-sdk;
bash
REACTAPPAINATIVEAPIKEY=akyourkey # CRA
VITEAINATIVEAPIKEY=akyour_key # Vite
tsx
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 ainative-react-sdk-1776063677 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 ainative-react-sdk-1776063677 技能
skillhub install ainative-react-sdk-1776063677
文件大小: 2.23 KB | 发布时间: 2026-4-14 10:02