TypeScript 快速开始
TypeScript 快速开始
Section titled “TypeScript 快速开始”npm install @hiddenpath/ai-lib-ts
# 或yarn add @hiddenpath/ai-lib-ts
# 或pnpm add @hiddenpath/ai-lib-ts库会自动在以下位置查找协议清单:
node_modules/ai-protocol/dist或node_modules/@hiddenpath/ai-protocol/dist../ai-protocol/dist或./protocols
Provider API 密钥
Section titled “Provider API 密钥”通过环境变量 <PROVIDER_ID>_API_KEY 设置 API 密钥:
export OPENAI_API_KEY="sk-..."export ANTHROPIC_API_KEY="sk-ant-..."export DEEPSEEK_API_KEY="..."import { AiClient, Message } from '@hiddenpath/ai-lib-ts';
const client = await AiClient.new('deepseek/deepseek-chat');
const response = await client .chat([ Message.system('你是一个有帮助的助手。'), Message.user('用简单的语言解释量子计算'), ]) .temperature(0.7) .maxTokens(500) .execute();
console.log(response.content);import { AiClient, Message } from '@hiddenpath/ai-lib-ts';
const client = await AiClient.new('anthropic/claude-3-5-sonnet');
const stream = client .chat([ Message.system('你是一个有帮助的助手。'), Message.user('讲一个短故事。'), ]) .stream() .executeStream();
for await (const event of stream) { if (event.event_type === 'PartialContentDelta') { process.stdout.write(event.content); }}import { AiClient, Message, Tool } from '@hiddenpath/ai-lib-ts';
const client = await AiClient.new('openai/gpt-4o');
const weatherTool = Tool.define( 'get_weather', { type: 'object', properties: { location: { type: 'string', description: '城市名称' }, }, required: ['location'], }, '获取指定位置的当前天气');
const response = await client .chat([Message.user('东京的天气怎么样?')]) .tools([weatherTool]) .execute();
if (response.toolCalls) { for (const tc of response.toolCalls) { console.log(`调用 ${tc.function.name}: ${tc.function.arguments}`); }}import { AiClient, Message } from '@hiddenpath/ai-lib-ts';
const client = await AiClient.new('anthropic/claude-3-5-sonnet');
const messages = [ Message.system('你是一个有帮助的编程助手。'), Message.user('TypeScript 中的闭包是什么?'),];
const response = await client .chat(messages) .execute();
console.log(response.content);获取统计信息
Section titled “获取统计信息”const { response, stats } = await client .chat([Message.user('你好!')]) .executeWithStats();
console.log('内容:', response.content);console.log('总 Token 数:', stats.totalTokens);console.log('延迟:', stats.latencyMs, 'ms');- AiClient API — 详细 API 参考
- 流式管道 — 流式输出工作原理
- 弹性模式 — 熔断器、限流、重试
- 高级功能 — 嵌入、缓存、插件、批处理