@celljs/ai-core 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +28 -97
  2. package/package.json +4 -5
package/README.md CHANGED
@@ -2,131 +2,62 @@
2
2
 
3
3
  ## 概览
4
4
 
5
- AI Core 模块是一个用于与 AI 模型服务交互的库,提供了生成聊天响应和嵌入向量的功能。通过简单易用的 API 接口,支持消息的创建、请求的发送和响应的处理。是所有 AI 模块的基础,提供了 AI 模块通用的 API 接口。
6
-
5
+ AI Core 定义了 Cell 中与 AI 模型交互的通用接口:聊天模型 `ChatModel`、嵌入模型 `EmbeddingModel`、提示词模板 `PromptTemplate`,以及按模型名路由到具体实现的 `ProxyChatModel`。它本身不调用任何模型服务,模型由实现这些接口的组件提供,目前是 [@celljs/ai-pi](../ai-pi)(基于 pi-ai,接入 20 多家模型服务商)。
7
6
 
8
7
  ## 特性
9
8
 
10
- - 生成聊天响应
11
- - 生成嵌入向量
12
- - 支持流式响应
13
- - 支持多种模型参数配置
9
+ - 生成聊天响应,支持流式响应
10
+ - 提示词模板(变量替换)
11
+ - `ProxyChatModel` 按 `<provider>:<model>` 把请求路由到对应的 `ChatModel` 实现
12
+ - 嵌入模型接口 `EmbeddingModel`(目前仓库内没有实现)
14
13
 
15
14
  ## 安装
16
15
 
17
- 使用 npm 安装 AI Core 模块:
18
-
19
- ```bash
20
- npm install @celljs/ai-core
21
- ```
22
-
23
- 或者使用 yarn:
24
-
25
16
  ```bash
26
- yarn add @celljs/ai-core
17
+ npm install @celljs/ai-core @celljs/ai-pi
27
18
  ```
28
19
 
29
- AI Core 模块是所有 AI 模块的基础,提供了 AI 模块通用的 API 接口,包括消息的创建、请求的发送和响应的处理。所以在使用 AI Core 模块之前,需要先安装厂商对应的模型服务适配模块。例如:`@celljs/ai-ollama`。
30
-
31
- ```bash
32
- npm install @celljs/ai-ollama
33
- ```
34
-
35
- 或者使用 yarn:
36
-
37
- ```bash
38
- yarn add @celljs/ai-ollama
39
- ```
20
+ 装上即生效,无需手动注册。模型凭据和服务商配置见 [@celljs/ai-pi](../ai-pi)。
40
21
 
41
22
  ## 快速开始
42
23
 
43
- 以下是一个简单的示例,展示如何使用 AI Ollama 模块生成聊天响应和嵌入向量:
44
-
45
24
  ```typescript
46
- import { AssistantMessage, PromptTemplate } from '@celljs/ai-core';
47
- import { Component, Autowired } from '@celljs/core';
25
+ import { type ChatModel, PromptTemplate, ProxyChatModel } from '@celljs/ai-core';
26
+ import { Autowired, Component } from '@celljs/core';
48
27
 
49
28
  @Component()
50
- export class OllamaDemo {
51
- @Autowired(OllamChatModel)
52
- private chatModel: ChatModel;
53
-
54
- @Autowired(EmbeddingModel)
55
- private embeddingModel: EmbeddingModel;
29
+ export class Assistant {
30
+ @Autowired(ProxyChatModel)
31
+ protected readonly chatModel: ChatModel;
56
32
 
57
33
  @Autowired(PromptTemplate)
58
- private promptTemplate: PromptTemplate;
59
-
60
- /**
61
- * Chat with Ollama
62
- */
63
- async chat() {
64
- const prompt = await this.promptTemplate.create(
65
- 'Hello {name}',
66
- {
67
- chatOptions: { model: 'llama3.2' },
68
- variables: { name: 'Ollama' }
69
- }
70
- );
34
+ protected readonly promptTemplate: PromptTemplate;
35
+
36
+ async chat(name: string): Promise<string> {
37
+ const prompt = await this.promptTemplate.create('Hello {name}', {
38
+ variables: { name },
39
+ // pi:<provider>/<model id>:ProxyChatModel 按第一个冒号前的 pi 交给 @celljs/ai-pi
40
+ chatOptions: { model: 'pi:anthropic/claude-sonnet-4-5' }
41
+ });
71
42
  const response = await this.chatModel.call(prompt);
72
- console.log(response.result.output);
43
+ return response.result.output.content;
73
44
  }
74
45
 
75
- /**
76
- * Stream chat response
77
- */
78
- async stream() {
79
- const prompt = await this.promptTemplate.create(
80
- 'Hello {name}',
81
- {
82
- chatOptions: { model: 'llama3.2' },
83
- variables: { name: 'Ollama' }
84
- }
85
- );
46
+ async stream(name: string): Promise<void> {
47
+ const prompt = await this.promptTemplate.create('Hello {name}', {
48
+ variables: { name },
49
+ chatOptions: { model: 'pi:anthropic/claude-sonnet-4-5' }
50
+ });
86
51
  const response$ = await this.chatModel.stream(prompt);
87
52
  response$.subscribe({
88
- next: response => console.log(response.result.output),
53
+ next: response => console.log(response.result.output.content),
89
54
  complete: () => console.log('Chat completed!')
90
55
  });
91
56
  }
92
-
93
- /**
94
- * Embed text to vector
95
- */
96
- async embed() {
97
- const response = await this.embeddingModel.call({
98
- inputs: ['text to embed'],
99
- options: { model: 'llama3.2' }
100
- });
101
- console.log(response.result.embeddings);
102
- }
103
57
  }
104
58
  ```
105
59
 
106
- 另外也可以使用 `ProxyChatModel` 类统一调用所有的模型服务商的模型:
107
-
108
- ```typescript
109
- import { ProxyChatModel } from '@celljs/ai-core';
110
- import { Component Autowired } from '@celljs/core';
111
-
112
- @Component()
113
- export class OllamaDemo {
114
- @Autowired(ProxyChatModel)
115
- private chatModel: ChatModel;
116
-
117
- /**
118
- * Chat with Ollama
119
- */
120
- async chat() {
121
- const response = await this.chatModel.call({
122
- prompt: 'Hello {name}',
123
- variables: { name: 'Ollama' },
124
- chatOptions: { model: 'ollama:llama3.2' }
125
- });
126
- console.log(response.result.output);
127
- }
128
- }
129
- ```
60
+ 模型名必须是 `<provider>:<model>` 形式,否则 `ProxyChatModel` 抛 `IllegalArgumentError`。
130
61
 
131
62
  ## 许可证
132
63
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ai-core",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "Core domain for AI programming",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -29,10 +29,9 @@
29
29
  "watch": "tsc -b -w --preserveWatchOutput"
30
30
  },
31
31
  "dependencies": {
32
- "@celljs/core": "4.0.0",
33
- "@celljs/http": "4.0.0",
34
- "rxjs": "^6.6.0",
35
- "tslib": "^2.8.0"
32
+ "@celljs/core": "4.1.0",
33
+ "@celljs/http": "4.1.0",
34
+ "rxjs": "^6.6.0"
36
35
  },
37
36
  "publishConfig": {
38
37
  "access": "public"