ctod 0.9.4 → 1.0.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/.nvmrc +1 -0
- package/README.md +66 -71
- package/README_ZH.md +246 -0
- package/dist/index.js +16 -16
- package/dist/index.js.map +4 -4
- package/eslint.config.mjs +1 -1
- package/examples/anthropic.ts +17 -29
- package/examples/aws-bedrock.ts +19 -33
- package/examples/google.ts +22 -32
- package/examples/llama.cpp.ts +9 -9
- package/examples/openai.ts +16 -28
- package/examples/plugin.ts +19 -24
- package/examples/x.ts +16 -28
- package/lib/broker/chat.ts +5 -5
- package/lib/core/plugin.ts +3 -3
- package/lib/core/translator.ts +3 -3
- package/lib/ctod.ts +29 -30
- package/lib/index.ts +19 -19
- package/lib/plugins/index.ts +4 -4
- package/lib/plugins/limiter.ts +1 -1
- package/lib/plugins/print-log.ts +3 -3
- package/lib/plugins/retry.ts +4 -4
- package/lib/plugins/role.ts +3 -3
- package/lib/service/anthropic/chat.ts +2 -3
- package/lib/service/anthropic/index.ts +2 -2
- package/lib/service/google/chat.ts +26 -33
- package/lib/service/google/index.ts +17 -23
- package/lib/service/{llama3.cpp → llama-cpp}/completion.ts +7 -7
- package/lib/service/{llama3.cpp → llama-cpp}/index.ts +6 -6
- package/lib/service/openai/chat.ts +2 -3
- package/lib/service/openai/images-generation.ts +1 -1
- package/lib/service/openai/index.ts +4 -4
- package/lib/service/openai/vision.ts +2 -3
- package/lib/service/x/chat.ts +1 -1
- package/lib/service/x/images-generation.ts +1 -1
- package/lib/service/x/index.ts +3 -3
- package/lib/utils/chinese-conv.ts +12 -10
- package/lib/utils/validate.ts +9 -42
- package/package.json +31 -30
- package/tsconfig.json +3 -3
- package/types/lib/broker/chat.d.ts +3 -3
- package/types/lib/core/plugin.d.ts +4 -4
- package/types/lib/core/translator.d.ts +3 -5
- package/types/lib/ctod.d.ts +7 -8
- package/types/lib/index.d.ts +19 -19
- package/types/lib/plugins/index.d.ts +9 -9
- package/types/lib/plugins/limiter.d.ts +1 -1
- package/types/lib/plugins/print-log.d.ts +3 -3
- package/types/lib/plugins/retry.d.ts +4 -4
- package/types/lib/plugins/role.d.ts +3 -3
- package/types/lib/service/anthropic/chat.d.ts +2 -3
- package/types/lib/service/anthropic/index.d.ts +1 -1
- package/types/lib/service/google/chat.d.ts +4 -5
- package/types/lib/service/google/index.d.ts +5 -5
- package/types/lib/service/{llama3.cpp → llama-cpp}/completion.d.ts +4 -4
- package/types/lib/service/{llama3.cpp → llama-cpp}/index.d.ts +3 -3
- package/types/lib/service/openai/chat.d.ts +3 -4
- package/types/lib/service/openai/images-generation.d.ts +1 -1
- package/types/lib/service/openai/index.d.ts +3 -3
- package/types/lib/service/openai/vision.d.ts +2 -3
- package/types/lib/service/x/chat.d.ts +2 -2
- package/types/lib/service/x/images-generation.d.ts +1 -1
- package/types/lib/service/x/index.d.ts +2 -2
- package/types/lib/utils/chinese-conv.d.ts +1 -1
- package/types/lib/utils/validate.d.ts +5 -11
- package/lib/types.ts +0 -4
- package/lib/utils/chinese-conv.json +0 -4115
- package/types/examples/basic.d.ts +0 -1
- package/types/examples/chat-demo.d.ts +0 -2
- package/types/examples/chat-for-llama.cpp-demo.d.ts +0 -2
- package/types/examples/chat-with-json-schema-demo.d.ts +0 -2
- package/types/examples/plugin-demo.d.ts +0 -2
- package/types/examples/stream-for-llama.cpp-demo.d.ts +0 -2
- package/types/examples/vision-demo.d.ts +0 -2
- package/types/lib/service/openai/completion.d.ts +0 -59
- package/types/lib/types.d.ts +0 -1
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
24
|
package/README.md
CHANGED
|
@@ -13,17 +13,22 @@
|
|
|
13
13
|
<br>
|
|
14
14
|
</h6>
|
|
15
15
|
|
|
16
|
+
<p align="center">
|
|
17
|
+
English |
|
|
18
|
+
<a href="./README_ZH.md">繁體中文</a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
16
21
|
<br>
|
|
17
22
|
|
|
18
|
-
##
|
|
23
|
+
## Summary
|
|
19
24
|
|
|
20
|
-
|
|
25
|
+
Nowadays, we frequently need to assign tasks to LLMs in a conversational manner and request JSON responses. CtoD allows you to structure this pattern in an organized way.
|
|
21
26
|
|
|
22
|
-
|
|
27
|
+
During the conversation, CtoD uses [zod](https://zod.dev/basics) to validate whether the request and response data meet expectations, ensuring consistency. By maintaining this interaction pattern, you can utilize it for API integration or automation systems.
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
We also support mainstream LLM services such as `OpenAI`, `Google`, `X`, `anthropic`, `llama.cpp`, both cloud-based and local.
|
|
25
30
|
|
|
26
|
-
##
|
|
31
|
+
## Installation
|
|
27
32
|
|
|
28
33
|
npm:
|
|
29
34
|
|
|
@@ -37,9 +42,9 @@ yarn:
|
|
|
37
42
|
yarn add ctod
|
|
38
43
|
```
|
|
39
44
|
|
|
40
|
-
##
|
|
45
|
+
## Quick Start
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
This example demonstrates how to pass a medication index and customer requirements to a chatbot and return the most suitable results. Developers can use the index results to search the database for the most appropriate medication for consumers:
|
|
43
48
|
|
|
44
49
|
```ts
|
|
45
50
|
import { CtoD, OpenAICtodService } from 'ctod'
|
|
@@ -62,60 +67,48 @@ const brokerBuilder = ctod.createBrokerBuilder<{
|
|
|
62
67
|
setPreMessages([
|
|
63
68
|
{
|
|
64
69
|
role: 'system',
|
|
65
|
-
content: '
|
|
70
|
+
content: 'You are a pharmacist skilled at categorizing indexes'
|
|
66
71
|
}
|
|
67
72
|
])
|
|
68
73
|
})
|
|
69
74
|
}
|
|
70
75
|
})
|
|
71
76
|
|
|
72
|
-
const broker = brokerBuilder.create(async({
|
|
77
|
+
const broker = brokerBuilder.create(async({ zod, data, setMessages }) => {
|
|
73
78
|
const { indexes, question } = data
|
|
74
79
|
setMessages([
|
|
75
80
|
{
|
|
76
81
|
role: 'user',
|
|
77
82
|
content: [
|
|
78
|
-
'
|
|
83
|
+
'I have the following indexes',
|
|
79
84
|
`${JSON.stringify(indexes)}`,
|
|
80
|
-
|
|
81
|
-
'
|
|
85
|
+
`Please help me analyze which index "${question}" might belong to`,
|
|
86
|
+
'Rank by relevance from high to low and give scores, scores range from 0 to 1'
|
|
82
87
|
]
|
|
83
88
|
}
|
|
84
89
|
])
|
|
85
|
-
const item =
|
|
86
|
-
name:
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}),
|
|
91
|
-
score: yup.number().required().meta({
|
|
92
|
-
jsonSchema: {
|
|
93
|
-
description: '評比分數'
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
}).required()
|
|
90
|
+
const item = zod.object({
|
|
91
|
+
name: zod.string().describe('Index name'),
|
|
92
|
+
score: zod.number().describe('Score')
|
|
93
|
+
})
|
|
97
94
|
return {
|
|
98
|
-
indexes:
|
|
99
|
-
jsonSchema: {
|
|
100
|
-
description: '由高到低排序的索引'
|
|
101
|
-
}
|
|
102
|
-
})
|
|
95
|
+
indexes: zod.array(item).describe('Indexes sorted from high to low')
|
|
103
96
|
}
|
|
104
97
|
})
|
|
105
98
|
|
|
106
99
|
broker.request({
|
|
107
|
-
indexes: ['
|
|
108
|
-
question: '
|
|
100
|
+
indexes: ['Stomach pain', 'Back pain', 'Headache', 'Sore throat', 'Limb pain'],
|
|
101
|
+
question: 'Drinking coffee, eating sweets, acid reflux'
|
|
109
102
|
}).then(e => {
|
|
110
|
-
console.log('
|
|
103
|
+
console.log('Output:', e.indexes)
|
|
111
104
|
/*
|
|
112
105
|
[
|
|
113
106
|
{
|
|
114
|
-
name: '
|
|
107
|
+
name: 'Stomach pain',
|
|
115
108
|
score: 1
|
|
116
109
|
},
|
|
117
110
|
{
|
|
118
|
-
name: '
|
|
111
|
+
name: 'Sore throat',
|
|
119
112
|
score: 0.7
|
|
120
113
|
},
|
|
121
114
|
...
|
|
@@ -124,53 +117,52 @@ broker.request({
|
|
|
124
117
|
}).catch(error => {
|
|
125
118
|
console.error('Error:', error)
|
|
126
119
|
})
|
|
127
|
-
|
|
128
120
|
```
|
|
129
121
|
|
|
130
|
-
##
|
|
122
|
+
## Plugin
|
|
131
123
|
|
|
132
|
-
|
|
124
|
+
While Broker itself can handle most tasks, Plugins can help improve complex workflows and assist in project engineering.
|
|
133
125
|
|
|
134
|
-
|
|
126
|
+
Each time a request is sent, Broker triggers a series of lifecycles. You can understand the parameters and behaviors of each lifecycle from the [source code](./lib/broker/chat.ts) and modify their behaviors.
|
|
135
127
|
|
|
136
|
-
|
|
128
|
+
Now, let's say we want to design a plugin that backs up messages to the server after each conversation ends:
|
|
137
129
|
|
|
138
130
|
```ts
|
|
139
131
|
import axios from 'axios'
|
|
140
132
|
import { CtoDPlugin } from 'ctod'
|
|
141
133
|
const backupPlugin = new CtoDPlugin({
|
|
142
134
|
name: 'backup-plugin',
|
|
143
|
-
//
|
|
144
|
-
params:
|
|
135
|
+
// Define parameter as sendUrl
|
|
136
|
+
params: zod => {
|
|
145
137
|
return {
|
|
146
|
-
sendUrl:
|
|
138
|
+
sendUrl: zod.string()
|
|
147
139
|
}
|
|
148
140
|
},
|
|
149
|
-
//
|
|
150
|
-
receiveData:
|
|
141
|
+
// You can receive information during execution, and the information structure is defined here.
|
|
142
|
+
receiveData: zod => {
|
|
151
143
|
return {
|
|
152
|
-
character:
|
|
144
|
+
character: zod.string()
|
|
153
145
|
}
|
|
154
146
|
},
|
|
155
147
|
onInstall({ params, attach, receive }) {
|
|
156
148
|
const store = new Map()
|
|
157
|
-
//
|
|
158
|
-
//
|
|
149
|
+
// If we have more custom information that needs to be passed in, it can be passed through plugins[key].send({ ... }) during the start phase
|
|
150
|
+
// See the "Ask the bot to roleplay" case in the Applications category
|
|
159
151
|
receive(({ id, context }) => {
|
|
160
152
|
store.get(id).context = context
|
|
161
153
|
})
|
|
162
|
-
//
|
|
154
|
+
// Initialize data on first conversation
|
|
163
155
|
attach('start', async({ id }) => {
|
|
164
156
|
store.set(id, {
|
|
165
157
|
messages: [],
|
|
166
158
|
context: null
|
|
167
159
|
})
|
|
168
160
|
})
|
|
169
|
-
//
|
|
161
|
+
// Save the conversation to state after each conversation
|
|
170
162
|
attach('talkAfter', async({ id, lastUserMessage }) => {
|
|
171
163
|
store.get(id).messages.push(lastUserMessage)
|
|
172
164
|
})
|
|
173
|
-
//
|
|
165
|
+
// Backup data after conversation ends
|
|
174
166
|
attach('done', async({ id }) => {
|
|
175
167
|
await axios.post(params.sendUrl, store.get(id))
|
|
176
168
|
store.delete(id)
|
|
@@ -192,60 +184,63 @@ const ctod = new CtoD({
|
|
|
192
184
|
|
|
193
185
|
## Examples
|
|
194
186
|
|
|
195
|
-
[
|
|
187
|
+
[Basic Usage - Medication Query Function](./examples/openai.ts)
|
|
196
188
|
|
|
197
|
-
[
|
|
189
|
+
[Advanced Usage - Ask AI to COSPLAY](./examples/plugin.ts)
|
|
198
190
|
|
|
199
|
-
##
|
|
191
|
+
## Version History
|
|
200
192
|
|
|
201
|
-
|
|
193
|
+
### 1.0.x
|
|
202
194
|
|
|
203
|
-
|
|
195
|
+
1. Support zod as schema definition tool and remove yup support.
|
|
196
|
+
2. Use nodenext as compilation target.
|
|
197
|
+
3. Change Google Service to use @google/genai package.
|
|
198
|
+
4. Rename Llama3Cpp to LlamaCpp.
|
|
204
199
|
|
|
205
200
|
### 0.9.x
|
|
206
201
|
|
|
207
|
-
1. OpenAI
|
|
208
|
-
2.
|
|
202
|
+
1. OpenAI API update, now you can set baseUrl through `setBaseUrl` method.
|
|
203
|
+
2. Add support for X Ai Service
|
|
209
204
|
|
|
210
205
|
### 0.8.x
|
|
211
206
|
|
|
212
|
-
1.
|
|
213
|
-
2.
|
|
207
|
+
1. Standardize Service naming
|
|
208
|
+
2. Add support for Google Service
|
|
214
209
|
|
|
215
210
|
### 0.7.x
|
|
216
211
|
|
|
217
|
-
|
|
212
|
+
Thanks to today's model support for json schema, we no longer need tedious declarations, so we added a simplified process through CtoD registration.
|
|
218
213
|
|
|
219
214
|
### 0.6.x
|
|
220
215
|
|
|
221
|
-
`bindYupToJsonSchemaToYup`
|
|
216
|
+
`bindYupToJsonSchemaToYup` had some dependency issues and has been removed, replaced with the following solution:
|
|
222
217
|
|
|
223
218
|
```ts
|
|
224
219
|
yup.array(item).required().meta({
|
|
225
220
|
jsonSchema: {
|
|
226
|
-
description: '
|
|
221
|
+
description: 'Indexes sorted from high to low'
|
|
227
222
|
}
|
|
228
223
|
})
|
|
229
224
|
```
|
|
230
225
|
|
|
231
|
-
1.
|
|
226
|
+
1. Added defineYupSchema to make creating complex Outputs easier.
|
|
232
227
|
|
|
233
228
|
### 0.5.x
|
|
234
229
|
|
|
235
|
-
|
|
230
|
+
Removed JSON Schema Info support and instead generate data formats through [yup-to-json-schema](https://github.com/sodaru/yup-to-json-schema).
|
|
236
231
|
|
|
237
|
-
|
|
232
|
+
Since the `yup-to-json-schema` extension requires global registration to use the `yup.string().description()` method, we provide the `bindYupToJsonSchemaToYup` method here, allowing users to decide whether to register.
|
|
238
233
|
|
|
239
|
-
1.
|
|
240
|
-
2.
|
|
234
|
+
1. Can respond with array in question, which will be merged through join.
|
|
235
|
+
2. Install parameter can now be omitted.
|
|
241
236
|
|
|
242
237
|
### 0.4.x
|
|
243
238
|
|
|
244
|
-
|
|
239
|
+
Mainly adjustments to support llama3.cpp or other self-hosted service workflows.
|
|
245
240
|
|
|
246
|
-
1.
|
|
247
|
-
2.
|
|
241
|
+
1. Support llama3.cpp server service
|
|
242
|
+
2. Add yup to json schema.
|
|
248
243
|
|
|
249
244
|
### 0.3.x
|
|
250
245
|
|
|
251
|
-
|
|
246
|
+
To support more platforms and self-hosted services, we abandoned the fully customized interface for ChatGPT, which also maintains the consistency of Broker and Plugin.
|
package/README_ZH.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
<br>
|
|
2
|
+
<p align="center"><img style="max-width: 300px" src="./logo.png"></p>
|
|
3
|
+
<h1 align="center">CtoD</h1>
|
|
4
|
+
<h3 align="center">Chat To Data</h3>
|
|
5
|
+
|
|
6
|
+
<h6 align="center">
|
|
7
|
+
<a href="https://www.npmjs.com/package/ctod">
|
|
8
|
+
<img src="https://img.shields.io/npm/v/ctod.svg">
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/KHC-ZhiHao/ctod">
|
|
11
|
+
<img src="https://img.shields.io/github/stars/KHC-ZhiHao/ctod.svg?style=social">
|
|
12
|
+
</a>
|
|
13
|
+
<br>
|
|
14
|
+
</h6>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<a href="./README.md">English</a> |
|
|
18
|
+
繁體中文
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<br>
|
|
22
|
+
|
|
23
|
+
## 摘要
|
|
24
|
+
|
|
25
|
+
現在我們頻繁的有透過口語化的方式交付任務 LLM 處理並要求回應 JSON 的需求,透過 CtoD 你可以讓這個模式有架構化的進行。
|
|
26
|
+
|
|
27
|
+
在對話過程中,CtoD 採用 [zod](https://zod.dev/basics) 來驗證請求與回復資料是否符合預期,以確保一致性,只要保持這個互動模式,就可以利用在 API 串接或是自動化系統上。
|
|
28
|
+
|
|
29
|
+
我們還附帶支援 `OpenAI`, `Google`, `X`, `anthropic`, `llama.cpp` 等主流 LLM 網路或本地服務。
|
|
30
|
+
|
|
31
|
+
## 安裝
|
|
32
|
+
|
|
33
|
+
npm:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install ctod
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
yarn:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
yarn add ctod
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 快速開始
|
|
46
|
+
|
|
47
|
+
這個例子示範如何將藥物索引與客戶需求傳遞給聊天機器人,並返回最適合的結果,開發人員可以利用索引結果去資料庫搜尋最適合的藥物給消費者:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { CtoD, OpenAICtodService } from 'ctod'
|
|
51
|
+
|
|
52
|
+
const ctod = new CtoD({
|
|
53
|
+
request: OpenAICtodService.createChatRequestWithJsonSchema({
|
|
54
|
+
apiKey: 'YOUR_API_KEY',
|
|
55
|
+
config: {
|
|
56
|
+
model: 'gpt-4o'
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
const brokerBuilder = ctod.createBrokerBuilder<{
|
|
62
|
+
indexes: string[]
|
|
63
|
+
question: string
|
|
64
|
+
}>({
|
|
65
|
+
install: ({ attach }) => {
|
|
66
|
+
attach('start', async({ setPreMessages }) => {
|
|
67
|
+
setPreMessages([
|
|
68
|
+
{
|
|
69
|
+
role: 'system',
|
|
70
|
+
content: '你現在是一位擅長分類索引的藥師'
|
|
71
|
+
}
|
|
72
|
+
])
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
const broker = brokerBuilder.create(async({ zod, data, setMessages }) => {
|
|
78
|
+
const { indexes, question } = data
|
|
79
|
+
setMessages([
|
|
80
|
+
{
|
|
81
|
+
role: 'user',
|
|
82
|
+
content: [
|
|
83
|
+
'我有以下索引',
|
|
84
|
+
`${JSON.stringify(indexes)}`,
|
|
85
|
+
`請幫我解析"${question}"可能是哪個索引`,
|
|
86
|
+
'且相關性由高到低排序並給予分數,分數由 0 ~ 1'
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
])
|
|
90
|
+
const item = zod.object({
|
|
91
|
+
name: zod.string().describe('索引名稱'),
|
|
92
|
+
score: zod.number().describe('評比分數')
|
|
93
|
+
})
|
|
94
|
+
return {
|
|
95
|
+
indexes: zod.array(item).describe('由高到低排序的索引')
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
broker.request({
|
|
100
|
+
indexes: ['胃痛', '腰痛', '頭痛', '喉嚨痛', '四肢疼痛'],
|
|
101
|
+
question: '喝咖啡,吃甜食,胃食道逆流'
|
|
102
|
+
}).then(e => {
|
|
103
|
+
console.log('輸出結果:', e.indexes)
|
|
104
|
+
/*
|
|
105
|
+
[
|
|
106
|
+
{
|
|
107
|
+
name: '胃痛',
|
|
108
|
+
score: 1
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: '喉嚨痛',
|
|
112
|
+
score: 0.7
|
|
113
|
+
},
|
|
114
|
+
...
|
|
115
|
+
]
|
|
116
|
+
*/
|
|
117
|
+
}).catch(error => {
|
|
118
|
+
console.error('Error:', error)
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Plugin
|
|
123
|
+
|
|
124
|
+
雖然 Broker 本身已經能夠處理大部分的事務,但透過 Plugin 可以協助改善複雜的流程,幫助專案工程化。
|
|
125
|
+
|
|
126
|
+
每次發送請求時,Broker 會觸發一系列的生命週期,你可以從[原始碼](./lib/broker/chat.ts)中了解每個生命週期的參數與行為,並對其行為進行加工。
|
|
127
|
+
|
|
128
|
+
現在,假設我們想要設計一個插件,它會在每次對話結束時將訊息備份到伺服器上:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
import axios from 'axios'
|
|
132
|
+
import { CtoDPlugin } from 'ctod'
|
|
133
|
+
const backupPlugin = new CtoDPlugin({
|
|
134
|
+
name: 'backup-plugin',
|
|
135
|
+
// 定義參數為 sendUrl
|
|
136
|
+
params: zod => {
|
|
137
|
+
return {
|
|
138
|
+
sendUrl: zod.string()
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
// 現階段你可以在執行過程中接收到資訊,資訊結構由這裡定義。
|
|
142
|
+
receiveData: zod => {
|
|
143
|
+
return {
|
|
144
|
+
character: zod.string()
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
onInstall({ params, attach, receive }) {
|
|
148
|
+
const store = new Map()
|
|
149
|
+
// 假設我們有更多的自訂義資訊需要被傳遞進來,可以在 start 階段透過 plugins[key].send({ ... }) 傳遞
|
|
150
|
+
// 可以從 Applications 分類中的 請機器人角色扮演 觀看案例
|
|
151
|
+
receive(({ id, context }) => {
|
|
152
|
+
store.get(id).context = context
|
|
153
|
+
})
|
|
154
|
+
// 第一次對話的時候初始化資料
|
|
155
|
+
attach('start', async({ id }) => {
|
|
156
|
+
store.set(id, {
|
|
157
|
+
messages: [],
|
|
158
|
+
context: null
|
|
159
|
+
})
|
|
160
|
+
})
|
|
161
|
+
// 每次對話完畢後把對話存入狀態
|
|
162
|
+
attach('talkAfter', async({ id, lastUserMessage }) => {
|
|
163
|
+
store.get(id).messages.push(lastUserMessage)
|
|
164
|
+
})
|
|
165
|
+
// 結束對話後備份資料
|
|
166
|
+
attach('done', async({ id }) => {
|
|
167
|
+
await axios.post(params.sendUrl, store.get(id))
|
|
168
|
+
store.delete(id)
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const ctod = new CtoD({
|
|
174
|
+
// ...
|
|
175
|
+
plugins: () => {
|
|
176
|
+
return {
|
|
177
|
+
backup: backupPlugin.use({
|
|
178
|
+
sendUrl: 'https://api/backup'
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Examples
|
|
186
|
+
|
|
187
|
+
[基礎用法 - 藥物查詢功能](./examples/openai.ts)
|
|
188
|
+
|
|
189
|
+
[進階用法 - 請 AI COSPLAY](./examples/plugin.ts)
|
|
190
|
+
|
|
191
|
+
## Version History
|
|
192
|
+
|
|
193
|
+
### 1.0.x
|
|
194
|
+
|
|
195
|
+
1. 支援 zod 作為 schema 定義工具,並移除 yup 的支援。
|
|
196
|
+
2. 改由 nodenext 作為編譯目標。
|
|
197
|
+
3. 將 Google Service 改使用 @google/genai 套件。
|
|
198
|
+
4. 將 Llama3Cpp 改名為 LlamaCpp。
|
|
199
|
+
|
|
200
|
+
### 0.9.x
|
|
201
|
+
|
|
202
|
+
1. OpenAI 的 API 更新,現在可以透過 `setBaseUrl` 方法來設定 baseUrl。
|
|
203
|
+
2. 新增 X Ai Service 的支援
|
|
204
|
+
|
|
205
|
+
### 0.8.x
|
|
206
|
+
|
|
207
|
+
1. 正規化 Service 的命名
|
|
208
|
+
2. 新增 Google Service 的支援
|
|
209
|
+
|
|
210
|
+
### 0.7.x
|
|
211
|
+
|
|
212
|
+
感謝當今的模型對於json schema的支援,我們不再需要繁瑣的宣告,因此新增了透過註冊 CtoD 的方式來簡化流程。
|
|
213
|
+
|
|
214
|
+
### 0.6.x
|
|
215
|
+
|
|
216
|
+
`bindYupToJsonSchemaToYup` 有一些依賴問題已經被移除,改用以下方案取代:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
yup.array(item).required().meta({
|
|
220
|
+
jsonSchema: {
|
|
221
|
+
description: '由高到低排序的索引'
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
1. 新增了 defineYupSchema 讓建立複雜的 Output 更加容易。
|
|
227
|
+
|
|
228
|
+
### 0.5.x
|
|
229
|
+
|
|
230
|
+
移除了 JSON Schema Info 的支援,而是透過 [yup-to-json-schema](https://github.com/sodaru/yup-to-json-schema) 進行生成資料格式。
|
|
231
|
+
|
|
232
|
+
由於 `yup-to-json-schema` 的延伸套件要使用 `yup.string().description()` 方法需要進行全域註冊,在此我們提供了 `bindYupToJsonSchemaToYup` 這個方法,讓使用者可以自行決定是否要進行註冊。
|
|
233
|
+
|
|
234
|
+
1. 可以在 question 中回應 array,會透過 join 進行合併。
|
|
235
|
+
2. 可以省略 install 參數了。
|
|
236
|
+
|
|
237
|
+
### 0.4.x
|
|
238
|
+
|
|
239
|
+
主要是支援 llama3.cpp 或者是其他自建服務上流程的調整。
|
|
240
|
+
|
|
241
|
+
1. 支援 llama3.cpp server service
|
|
242
|
+
2. 新增 yup to json scheme。
|
|
243
|
+
|
|
244
|
+
### 0.3.x
|
|
245
|
+
|
|
246
|
+
為了支援更多平台與自建服務,我們捨棄了完全為了 ChatGPT 客制化的接口,這樣也能完整保持 Broker 與 Plugin 的一致性。
|