@celljs/ai-core 3.9.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.
- package/README.md +28 -97
- package/package.json +14 -21
package/README.md
CHANGED
|
@@ -2,131 +2,62 @@
|
|
|
2
2
|
|
|
3
3
|
## 概览
|
|
4
4
|
|
|
5
|
-
AI Core
|
|
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
|
-
|
|
17
|
+
npm install @celljs/ai-core @celljs/ai-pi
|
|
27
18
|
```
|
|
28
19
|
|
|
29
|
-
|
|
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 {
|
|
47
|
-
import {
|
|
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
|
|
51
|
-
@Autowired(
|
|
52
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
43
|
+
return response.result.output.content;
|
|
73
44
|
}
|
|
74
45
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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": "
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "Core domain for AI programming",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -29,96 +29,89 @@
|
|
|
29
29
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@celljs/core": "
|
|
33
|
-
"@celljs/http": "
|
|
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"
|
|
39
38
|
},
|
|
40
39
|
"exports": {
|
|
41
40
|
".": {
|
|
42
|
-
"webpack": "./lib/common/index.js",
|
|
43
41
|
"import": "./esm/common/index.js",
|
|
44
42
|
"default": "./lib/common/index.js"
|
|
45
43
|
},
|
|
46
44
|
"./package.json": "./package.json",
|
|
47
45
|
"./lib/common": {
|
|
48
|
-
"webpack": "./lib/common/index.js",
|
|
49
46
|
"import": "./esm/common/index.js",
|
|
50
47
|
"default": "./lib/common/index.js"
|
|
51
48
|
},
|
|
52
49
|
"./lib/common/chat": {
|
|
53
|
-
"webpack": "./lib/common/chat/index.js",
|
|
54
50
|
"import": "./esm/common/chat/index.js",
|
|
55
51
|
"default": "./lib/common/chat/index.js"
|
|
56
52
|
},
|
|
57
53
|
"./lib/common/chat/message": {
|
|
58
|
-
"webpack": "./lib/common/chat/message/index.js",
|
|
59
54
|
"import": "./esm/common/chat/message/index.js",
|
|
60
55
|
"default": "./lib/common/chat/message/index.js"
|
|
61
56
|
},
|
|
62
57
|
"./lib/common/chat/metadata": {
|
|
63
|
-
"webpack": "./lib/common/chat/metadata/index.js",
|
|
64
58
|
"import": "./esm/common/chat/metadata/index.js",
|
|
65
59
|
"default": "./lib/common/chat/metadata/index.js"
|
|
66
60
|
},
|
|
67
61
|
"./lib/common/chat/model": {
|
|
68
|
-
"webpack": "./lib/common/chat/model/index.js",
|
|
69
62
|
"import": "./esm/common/chat/model/index.js",
|
|
70
63
|
"default": "./lib/common/chat/model/index.js"
|
|
71
64
|
},
|
|
72
65
|
"./lib/common/chat/prompt": {
|
|
73
|
-
"webpack": "./lib/common/chat/prompt/index.js",
|
|
74
66
|
"import": "./esm/common/chat/prompt/index.js",
|
|
75
67
|
"default": "./lib/common/chat/prompt/index.js"
|
|
76
68
|
},
|
|
77
69
|
"./lib/common/chat/tool": {
|
|
78
|
-
"webpack": "./lib/common/chat/tool/index.js",
|
|
79
70
|
"import": "./esm/common/chat/tool/index.js",
|
|
80
71
|
"default": "./lib/common/chat/tool/index.js"
|
|
81
72
|
},
|
|
82
73
|
"./lib/common/embedding": {
|
|
83
|
-
"webpack": "./lib/common/embedding/index.js",
|
|
84
74
|
"import": "./esm/common/embedding/index.js",
|
|
85
75
|
"default": "./lib/common/embedding/index.js"
|
|
86
76
|
},
|
|
87
77
|
"./lib/common/error": {
|
|
88
|
-
"webpack": "./lib/common/error/index.js",
|
|
89
78
|
"import": "./esm/common/error/index.js",
|
|
90
79
|
"default": "./lib/common/error/index.js"
|
|
91
80
|
},
|
|
92
81
|
"./lib/common/model": {
|
|
93
|
-
"webpack": "./lib/common/model/index.js",
|
|
94
82
|
"import": "./esm/common/model/index.js",
|
|
95
83
|
"default": "./lib/common/model/index.js"
|
|
96
84
|
},
|
|
97
85
|
"./lib/common/model/function": {
|
|
98
|
-
"webpack": "./lib/common/model/function/index.js",
|
|
99
86
|
"import": "./esm/common/model/function/index.js",
|
|
100
87
|
"default": "./lib/common/model/function/index.js"
|
|
101
88
|
},
|
|
102
89
|
"./lib/common/sse": {
|
|
103
|
-
"webpack": "./lib/common/sse/index.js",
|
|
104
90
|
"import": "./esm/common/sse/index.js",
|
|
105
91
|
"default": "./lib/common/sse/index.js"
|
|
106
92
|
},
|
|
107
93
|
"./lib/common/utils": {
|
|
108
|
-
"webpack": "./lib/common/utils/index.js",
|
|
109
94
|
"import": "./esm/common/utils/index.js",
|
|
110
95
|
"default": "./lib/common/utils/index.js"
|
|
111
96
|
},
|
|
112
97
|
"./lib/*.js": {
|
|
113
|
-
"webpack": "./lib/*.js",
|
|
114
98
|
"import": "./esm/*.js",
|
|
115
99
|
"default": "./lib/*.js"
|
|
116
100
|
},
|
|
117
101
|
"./lib/*": {
|
|
118
|
-
"webpack": "./lib/*.js",
|
|
119
102
|
"import": "./esm/*.js",
|
|
120
103
|
"default": "./lib/*.js"
|
|
121
104
|
},
|
|
122
105
|
"./*": "./*"
|
|
106
|
+
},
|
|
107
|
+
"cell": {
|
|
108
|
+
"modules": {
|
|
109
|
+
"frontend": [
|
|
110
|
+
"lib/common/module"
|
|
111
|
+
],
|
|
112
|
+
"backend": [
|
|
113
|
+
"lib/common/module"
|
|
114
|
+
]
|
|
115
|
+
}
|
|
123
116
|
}
|
|
124
117
|
}
|