@topthink/ai
v1.0.7
Published
ThinkAI SDK — TypeScript client for the ThinkAI platform
Readme
@topthink/ai
ThinkAI SDK & CLI — TypeScript 客户端,同时提供命令行工具 tai。
安装
npm install @topthink/aiCLI
npm install -g @topthink/ai支持环境变量 THINKAI_API_KEY 和 THINKAI_BASE_URL,全局选项可覆盖:
--api-key <key> API key (or set THINKAI_API_KEY env var)
--base-url <url> API base URL (or set THINKAI_BASE_URL env var)tai chat
tai chat --model gpt-4 --message "Hello!"
tai chat --model gpt-4 --system "You are a poet." --message "写首诗" --stream
tai chat --model gpt-4 --message "Hello!" --jsontai text
tai text embedding --model text-embedding-v3 --input "Hello!"
tai text embedding --model text-embedding-v3 --input "a" --input "b"
tai text rerank --model gte-rerank-v2 --query "什么是AI?" --documents ./documents.json --score 0.5tai image
tai image generations --model dall-e-3 --prompt "A cat" --size 1024x1024
tai image generations --model dall-e-3 --prompt "A cat" --image ./ref.png --out cat.png
tai image generations --model dall-e-3 --prompt "A cat" --n 2 --ratio 16:9 --style vividtai audio
tai audio speech --model tts-1 --input "你好世界" --voice alloy --out hello.mp3
tai audio voice --model tts-1tai music
tai music song --model xxx --prompt "流行摇滚" --lyrics "歌词..." --out song.mp3
tai music bgm --model xxx --prompt "史诗管弦" --duration 60 --out bgm.mp3
tai music query --model xxx --id 123456tai video
tai video generations --model kling-video-o1 --prompt "A sunset" --out sunset.mp4
tai video generations --model xxx --prompt "..." --first-frame ./start.png --last-frame ./end.png --audio
tai video query --model kling-video-o1 --id 123456 --out result.mp4tai avatar
tai avatar video --model digital-human-v1 --anchor figure-1 --text "欢迎来到直播间" --voice voice-1 --out welcome.mp4
tai avatar query --model digital-human-v1 --id 123456 --out result.mp4tai model
tai model list
tai model list --type chat
tai model show chat gpt-4tai decision
tai decision evaluate --model jev-latest --state "Help! My payouts have been failing for 3 days." --questions ./questions.json
tai decision evaluate --model jev-latest --state '{"user":{"plan":"pro"}}' --questions '{"is_churn_risk":{"type":"noul","instructions":"Is this user likely to churn?"}}'--state 支持纯文本、内联 JSON 或 JSON 文件路径;--questions 支持内联 JSON 或 JSON 文件路径。
SDK
import { Client } from '@topthink/ai';
const client = new Client({
apiKey: 'sk-xxx',
baseUrl: 'https://ai.topthink.com',
});apiKey 和 baseUrl 可省略,自动从 THINKAI_API_KEY / THINKAI_BASE_URL 环境变量读取。
Chat
// 非流式
const result = await client.chat.completions({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
});
// result.message.content
// 流式
const stream = await client.chat.completions({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
stream: true,
});
for await (const event of stream) {
console.log(event.delta.content);
}Text
const { embeddings } = await client.text.embedding({
model: 'text-embedding-v3',
input: ['a', 'b'],
});
// embeddings
const { documents } = await client.text.rerank({
model: 'gte-rerank-v2',
query: 'What is AI?',
documents: ['AI is...', '...'],
score: 0.5,
});
// documentsImage
const result = await client.image.generations({
model: 'dall-e-3',
prompt: 'A cat in a spacesuit',
n: 2,
size: '1024x1024',
});
// result.images[0].urlAudio
const speech = await client.audio.speech({
model: 'tts-1',
input: 'Hello, world!',
voice: 'alloy',
});
// speech.audio.data (base64)
const voices = await client.audio.voice({ model: 'tts-1' });Music
const result = await client.music.song({
model: 'music-2.6',
prompt: 'A pop rock song',
lyrics: '[verse] La da dee...',
});
// result.id or result.audios[0].url
const bgm = await client.music.bgm({ model: 'music-2.6', prompt: 'Epic orchestral' });
const task = await client.music.query({ model: 'music-2.6', id: '...' });Video
const task = await client.video.generations({
model: 'kling-video-o1',
prompt: 'A sunset',
});
// task.id
const result = await client.video.query({ model: 'kling-video-o1', id: task.id });
// result.videos[0].urlAvatar
const task = await client.avatar.video({
model: 'digital-human-v1',
anchor: 'figure-1',
text: '欢迎来到直播间',
voice: 'voice-1',
});
// task.id
const result = await client.avatar.query({ model: 'digital-human-v1', id: task.id });
// result.status: processing | success | fail;success 时 result.videoModel
const models = await client.model.list('chat');
const model = await client.model.get('chat', 'gpt-4');Decision
const result = await client.decision.evaluate({
model: 'jev-latest',
state: 'Help! My payouts have been failing for 3 days.',
questions: {
is_urgent: { type: 'noul', instructions: 'Does this convey urgency?' },
department: {
type: 'choice',
instructions: 'Which team should handle this?',
criteria: { billing: 'Payments, invoicing', technical: 'Bugs, outages' },
},
frustration: {
type: 'score',
instructions: 'How frustrated is the customer?',
criteria: ['Calm', 'Frustrated', 'Very angry'],
},
},
});
// result.answers.is_urgent -> { type: 'noul', noul: 0.95 }
// result.answers.department -> { type: 'choice', choice: 'billing', probabilities: {...}, confidence: 0.81 }
// result.answers.frustration -> { type: 'score', score: 1.05, legend: {...}, probabilities: {...}, confidence: 0.92 }错误处理
import { Exception } from '@topthink/ai';
try {
const result = await client.chat.completions({ ... });
} catch (err) {
if (err instanceof Exception) {
console.error(err.status, err.message);
}
}License
MIT
