@topthink/ai
v1.0.1
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 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 model
tai model list
tai model list --type chat
tai model show chat gpt-4SDK
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);
}Image
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].urlModel
const models = await client.model.list('chat');
const model = await client.model.get('chat', 'gpt-4');错误处理
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
