@blueking/ai-blueking 0.1.0 → 0.1.1-beta.2

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 CHANGED
@@ -1,14 +1,100 @@
1
- ## AI 小鲸业务组件
1
+ # AI 小鲸使用指南
2
2
 
3
- #### 安装
3
+ ## 安装
4
4
 
5
5
  ```bash
6
6
  npm i @blueking/ai-blueking
7
7
  ```
8
8
 
9
- #### 使用
9
+ ## stream 模式
10
10
 
11
- - vue3框架下使用
11
+ 不同框架,组件引入方式不同,具体可参考下面的的例子。这里使用 vue3 项目为例,来展示 stream 模式交互:
12
+
13
+ ```vue
14
+ <template>
15
+ <AIBlueking
16
+ :loading="loading"
17
+ :messages="messages"
18
+ @clear="handleClear"
19
+ @send="handleSend"
20
+ />
21
+ </template>
22
+
23
+ <script lang="ts" setup>
24
+ import { ref } from 'vue';
25
+ import AIBlueking from '@blueking/ai-blueking';
26
+ import { ChatHelper } from '@blueking/ai-blueking/dist/chat-helper';
27
+ import '@blueking/ai-blueking/dist/vue3/style.css';
28
+
29
+ const loading = ref(false);
30
+ const messages = ref([]);
31
+
32
+ // 聊天开始
33
+ const handleStart = (id: number | string) => {
34
+ loading.value = true;
35
+ messages.value.push({
36
+ role: 'assistant',
37
+ content: '内容正在生成中...',
38
+ status: 'loading',
39
+ });
40
+ };
41
+
42
+ // 接收消息
43
+ const handleReceiveMessage = (message: string, id: number | string) => {
44
+ const currentMessage = messages.value.at(-1);
45
+ if (currentMessage.status === 'loading') {
46
+ // 如果是loading状态,直接覆盖
47
+ currentMessage.content = message;
48
+ currentMessage.status = 'success';
49
+ } else if (currentMessage.status === 'success') {
50
+ // 如果是后续消息,就追加消息
51
+ currentMessage.content += message;
52
+ }
53
+ };
54
+
55
+ // 聊天结束
56
+ const handleEnd = (id: number | string) => {
57
+ loading.value = false;
58
+ const currentMessage = messages.value.at(-1);
59
+ // loading 情况下终止
60
+ if (currentMessage.status === 'loading') {
61
+ currentMessage.content = '聊天内容已中断';
62
+ }
63
+ };
64
+
65
+ // 错误处理
66
+ const handleError = (message: string, code: string, id: number | string) => {
67
+ const currentMessage = messages.value.at(-1);
68
+ currentMessage.status = 'fail';
69
+ currentMessage.content = message;
70
+ loading.value = false;
71
+ };
72
+
73
+ const chatHelper = new ChatHelper('<接口地址>', handleStart, handleReceiveMessage, handleEnd, handleError);
74
+
75
+ // 清空消息
76
+ const handleClear = () => {
77
+ messages.value = [];
78
+ };
79
+
80
+ // 发送消息
81
+ const handleSend = (message: string) => {
82
+ // 添加一条消息
83
+ messages.value.push({
84
+ role: 'user',
85
+ content: message,
86
+ });
87
+ // ai 消息,id是唯一标识当前流,调用 chatHelper.stop 的时候需要传入
88
+ chatHelper.stream({
89
+ id: 1,
90
+ input: message,
91
+ chatHistory: messages.value,
92
+ });
93
+ };
94
+ </script>
95
+ ```
96
+
97
+ ## vue3 项目内使用(非 stream 模式)
12
98
 
13
99
  ```vue
14
100
  <template>
@@ -40,33 +126,33 @@ npm i @blueking/ai-blueking
40
126
  const messages = [
41
127
  {
42
128
  content: '你好呀',
43
- type: 'ai',
129
+ role: 'assistant',
44
130
  },
45
131
  {
46
132
  content: '1+1=?',
47
- type: 'user',
133
+ role: 'user',
48
134
  },
49
135
  {
50
136
  content: '1+1=3',
51
- type: 'ai',
137
+ role: 'assistant',
52
138
  status: 'error',
53
139
  },
54
140
  {
55
141
  content: '不对',
56
- type: 'user',
142
+ role: 'user',
57
143
  },
58
144
  {
59
145
  content: '1+1=2',
60
- type: 'ai',
146
+ role: 'assistant',
61
147
  status: 'loading',
62
148
  },
63
149
  {
64
150
  content: '对了',
65
- type: 'user',
151
+ role: 'user',
66
152
  },
67
153
  {
68
154
  content: '好的,任务已完成',
69
- type: 'ai',
155
+ role: 'assistant',
70
156
  },
71
157
  ];
72
158
  // 内置 prompt
@@ -129,9 +215,9 @@ npm i @blueking/ai-blueking
129
215
  </script>
130
216
  ```
131
217
 
132
- - vue2框架下使用
218
+ ## vue2 项目内使用(非 stream 模式)
133
219
 
134
- vue2 下,需要安装npm包,里面是vue3资源
220
+ vue2 下,需要安装 npm 包,里面是 vue3 资源
135
221
 
136
222
  ```bash
137
223
  npm i @blueking/bkui-library
@@ -157,45 +243,48 @@ npm i @blueking/bkui-library
157
243
  />
158
244
  </div>
159
245
  </template>
160
- <script setup lang="ts">
246
+ <script lang="ts">
161
247
  import { ref } from 'vue';
162
248
 
163
249
  import AIBlueking from '@blueking/ai-blueking/vue2';
164
250
  import '@blueking/ai-blueking/dist/vue2/style.css';
165
251
 
166
252
  export default {
253
+ components: {
254
+ AIBlueking,
255
+ },
167
256
  data() {
168
257
  return {
169
258
  messages: [
170
259
  {
171
260
  content: '你好呀',
172
- type: 'ai',
261
+ role: 'assistant',
173
262
  },
174
263
  {
175
264
  content: '1+1=?',
176
- type: 'user',
265
+ role: 'user',
177
266
  },
178
267
  {
179
268
  content: '1+1=3',
180
- type: 'ai',
269
+ role: 'assistant',
181
270
  status: 'error',
182
271
  },
183
272
  {
184
273
  content: '不对',
185
- type: 'user',
274
+ role: 'user',
186
275
  },
187
276
  {
188
277
  content: '1+1=2',
189
- type: 'ai',
278
+ role: 'assistant',
190
279
  status: 'loading',
191
280
  },
192
281
  {
193
282
  content: '对了',
194
- type: 'user',
283
+ role: 'user',
195
284
  },
196
285
  {
197
286
  content: '好的,任务已完成',
198
- type: 'ai',
287
+ role: 'assistant',
199
288
  },
200
289
  ],
201
290
  prompts: [
@@ -229,7 +318,7 @@ npm i @blueking/bkui-library
229
318
  },
230
319
  };
231
320
  },
232
- method: {
321
+ methods: {
233
322
  handleClear() {
234
323
  console.log('trigger clear');
235
324
  },