Skip to content

Cell - AI Core Component ​

Not translated yet

This page is only written in Chinese so far; the original is shown below.

概览 ​

AI Core 定义了 Cell 中与 AI 模型交互的通用接口:聊天模型 ChatModel、嵌入模型 EmbeddingModel、提示词模板 PromptTemplate,以及按模型名路由到具体实现的 ProxyChatModel。它本身不调用任何模型服务,模型由实现这些接口的组件提供,目前是 @celljs/ai-pi(基于 pi-ai,接入 20 多家模型服务商)。

特性 ​

  • 生成聊天响应,支持流式响应
  • 提示词模板(变量替换)
  • ProxyChatModel 按 <provider>:<model> 把请求路由到对应的 ChatModel 实现
  • 嵌入模型接口 EmbeddingModel(目前仓库内没有实现)

安装 ​

bash
npm install @celljs/ai-core @celljs/ai-pi

装上即生效,无需手动注册。模型凭据和服务商配置见 @celljs/ai-pi。

快速开始 ​

typescript
import { type ChatModel, PromptTemplate, ProxyChatModel } from '@celljs/ai-core';
import { Autowired, Component } from '@celljs/core';

@Component()
export class Assistant {
    @Autowired(ProxyChatModel)
    protected readonly chatModel: ChatModel;

    @Autowired(PromptTemplate)
    protected readonly promptTemplate: PromptTemplate;

    async chat(name: string): Promise<string> {
        const prompt = await this.promptTemplate.create('Hello {name}', {
            variables: { name },
            // pi:<provider>/<model id>:ProxyChatModel 按第一个冒号前的 pi 交给 @celljs/ai-pi
            chatOptions: { model: 'pi:anthropic/claude-sonnet-4-5' }
        });
        const response = await this.chatModel.call(prompt);
        return response.result.output.content;
    }

    async stream(name: string): Promise<void> {
        const prompt = await this.promptTemplate.create('Hello {name}', {
            variables: { name },
            chatOptions: { model: 'pi:anthropic/claude-sonnet-4-5' }
        });
        const response$ = await this.chatModel.stream(prompt);
        response$.subscribe({
            next: response => console.log(response.result.output.content),
            complete: () => console.log('Chat completed!')
        });
    }
}

模型名必须是 <provider>:<model> 形式,否则 ProxyChatModel 抛 IllegalArgumentError。

许可证 ​

本项目采用 MIT 许可证。