@vimo-ai/ai-platform-sdk
v0.0.1-beta.1
Published
SDK for AI Platform Server - 统一 AI 调用层,支持多渠道、流式传输、工具调用
Maintainers
Readme
AI Platform Chat SDK
这是一个专用于与 AI Platform Server 的聊天功能进行交互的轻量级 Node.js SDK。它全面使用 fetch API,并为流式响应提供了一流的支持。
安装
如果您在同一个 monorepo 中使用此 SDK,您可以使用本地路径依赖。在您另一个项目的 package.json 中添加:
"dependencies": {
"ai-platform-sdk": "file:../sdk"
}然后运行 npm install 或 pnpm install。
在此之后,您需要先编译 SDK:
cd sdk
pnpm build在 NestJS 中使用
为了在 NestJS 应用中实现依赖注入和单例模式,我们提供了 AIPlatformSdkModule。
1. 导入模块
在您项目的根模块(例如 app.module.ts)中导入 AIPlatformSdkModule。
// in your-app/src/app.module.ts
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { AIPlatformSdkModule } from 'ai-platform-sdk';
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
AIPlatformSdkModule.forRootAsync(),
],
})
export class AppModule {}2. 配置环境变量
在您项目的 .env 文件中添加以下变量:
AI_PLATFORM_BASE_URL=http://localhost:3000
AI_PLATFORM_API_KEY=your_secret_api_key3. 在服务中注入客户端
// in your-app/src/some.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { AIPlatformClient, AI_PLATFORM_CLIENT } from 'ai-platform-sdk';
@Injectable()
export class SomeService {
constructor(
@Inject(AI_PLATFORM_CLIENT) private readonly aiPlatformClient: AIPlatformClient,
) {}
// 示例:调用流式API
async streamExample() {
const stream = this.aiPlatformClient.chat.streamChat({ input: '给我讲个关于程序员的笑话' });
console.log('AI 回复:');
for await (const chunk of stream) {
process.stdout.write(chunk);
}
console.log(); // for a newline
}
// 示例:调用非流式API
async nonStreamExample() {
const reply = await this.aiPlatformClient.chat.chat({ input: '你好!' });
console.log('AI 回复:', reply);
}
}API 参考
所有方法都位于 client.chat 命名空间下。
client.chat.chat(params)
进行一次性的、非流式的聊天调用。
- 参数:
{ input: string; channel?: PROVIDER_CHANNEL; model?: string; } - 返回:
Promise<string>- AI 的完整回复。
client.chat.streamChat(params)
进行基于 GET 请求的流式聊天。
- 参数:
{ input: string; channel?: PROVIDER_CHANNEL; model?: string; } - 返回:
AsyncIterable<string>- 一个可以for...await...of循环的异步迭代器,每次迭代返回一个数据块(字符串)。
client.chat.streamChatWithToolsAndMessages(body)
进行基于 POST 请求的、支持完整消息历史和工具的流式聊天。
参数:
{ messages: ChatCompletionMessageParam[], tools: ChatCompletionTool[], ... }返回:
AsyncIterable<string>- 同上。
