ctod 0.1.5 → 0.3.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 +65 -105
- package/dist/index.js +1 -1
- package/examples/chat-demo.ts +70 -0
- package/examples/{applications/cosplay.ts → plugin-demo.ts} +20 -13
- package/lib/broker/chat.ts +277 -0
- package/lib/core/plugin.ts +9 -60
- package/lib/index.ts +13 -25
- package/lib/plugins/index.ts +6 -1
- package/lib/plugins/limiter.ts +4 -4
- package/lib/plugins/print-log.ts +32 -73
- package/lib/plugins/retry.ts +23 -30
- package/lib/plugins/role.ts +26 -33
- package/lib/service/{chatgpt35.ts → openai/chat.ts} +17 -32
- package/lib/service/{chatgpt3.ts → openai/completion.ts} +12 -27
- package/lib/service/{images-generations.ts → openai/images-generation.ts} +7 -22
- package/lib/service/openai/index.ts +43 -0
- package/package.json +2 -2
- package/types/lib/broker/{35.d.ts → chat.d.ts} +42 -17
- package/types/lib/core/plugin.d.ts +9 -43
- package/types/lib/index.d.ts +12 -25
- package/types/lib/plugins/index.d.ts +17 -19
- package/types/lib/plugins/limiter.d.ts +4 -4
- package/types/lib/plugins/print-log.d.ts +4 -15
- package/types/lib/plugins/retry.d.ts +5 -11
- package/types/lib/plugins/role.d.ts +4 -10
- package/types/lib/service/{chatgpt35.d.ts → openai/chat.d.ts} +14 -24
- package/types/lib/service/{chatgpt3.d.ts → openai/completion.d.ts} +9 -19
- package/types/lib/service/{images-generations.d.ts → openai/images-generation.d.ts} +4 -14
- package/types/lib/service/openai/index.d.ts +22 -0
- package/README-TW.md +0 -239
- package/TODO +0 -6
- package/examples/applications/bbc-news-reader.ts +0 -224
- package/examples/applications/story-generations.ts +0 -131
- package/examples/applications/talk-generations.ts +0 -257
- package/examples/chatgpt3.5-broker.ts +0 -79
- package/examples/chatgpt3.5.ts +0 -42
- package/examples/utils/index.ts +0 -56
- package/lib/broker/3.ts +0 -160
- package/lib/broker/35.ts +0 -207
- package/lib/broker/index.ts +0 -83
- package/types/lib/broker/3.d.ts +0 -71
- package/types/lib/broker/index.d.ts +0 -30
package/lib/plugins/role.ts
CHANGED
|
@@ -1,35 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatBrokerPlugin } from '../core/plugin'
|
|
2
2
|
|
|
3
|
-
export default {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
ver35: new Broker35Plugin({
|
|
10
|
-
name: 'role',
|
|
11
|
-
params: yup => {
|
|
12
|
-
return {
|
|
13
|
-
role: yup.string().required()
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
receiveData: () => {
|
|
17
|
-
return {}
|
|
18
|
-
},
|
|
19
|
-
onInstall({ attach, params }) {
|
|
20
|
-
attach('talkFirst', async({ messages, changeMessages }) => {
|
|
21
|
-
changeMessages([
|
|
22
|
-
{
|
|
23
|
-
role: 'user',
|
|
24
|
-
content: `你現在是${params.role}。`
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
role: 'assistant',
|
|
28
|
-
content: `沒問題,我現在是${params.role},有什麼可以幫你的嗎?`
|
|
29
|
-
},
|
|
30
|
-
...messages
|
|
31
|
-
])
|
|
32
|
-
})
|
|
3
|
+
export default new ChatBrokerPlugin({
|
|
4
|
+
name: 'role',
|
|
5
|
+
params: yup => {
|
|
6
|
+
return {
|
|
7
|
+
role: yup.string().required()
|
|
33
8
|
}
|
|
34
|
-
}
|
|
35
|
-
|
|
9
|
+
},
|
|
10
|
+
receiveData: () => {
|
|
11
|
+
return {}
|
|
12
|
+
},
|
|
13
|
+
onInstall({ attach, params }) {
|
|
14
|
+
attach('start', async({ messages, changeMessages }) => {
|
|
15
|
+
changeMessages([
|
|
16
|
+
{
|
|
17
|
+
role: 'user',
|
|
18
|
+
content: `你現在是${params.role}。`
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
role: 'assistant',
|
|
22
|
+
content: `沒問題,我現在是${params.role},有什麼可以幫你的嗎?`
|
|
23
|
+
},
|
|
24
|
+
...messages
|
|
25
|
+
])
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
})
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from 'axios'
|
|
2
1
|
import { json } from 'power-helper'
|
|
3
|
-
import {
|
|
2
|
+
import { OpenAI } from './index'
|
|
3
|
+
import { PromiseResponseType } from '../../types'
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type ChatGPTMessage = {
|
|
6
6
|
role: 'system' | 'user' | 'assistant'
|
|
7
7
|
name?: string
|
|
8
8
|
content: string
|
|
@@ -35,10 +35,10 @@ type Config = {
|
|
|
35
35
|
*/
|
|
36
36
|
n: number
|
|
37
37
|
/**
|
|
38
|
-
* @zh 選擇運行的模型,16k意味著能處理長度為 16,384
|
|
38
|
+
* @zh 選擇運行的模型,16k意味著能處理長度為 16,384 的文本,32k意味著能處理長度為 32768 的文本。
|
|
39
39
|
* @en How many chat completion choices to generate for each input message.
|
|
40
40
|
*/
|
|
41
|
-
model: 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k'
|
|
41
|
+
model: 'gpt-4' | 'gpt-4-32k' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k'
|
|
42
42
|
/**
|
|
43
43
|
* @zh 冒險指數,數值由 0 ~ 2 之間。
|
|
44
44
|
* @en What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
|
|
@@ -46,31 +46,16 @@ type Config = {
|
|
|
46
46
|
temperature: number
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
export class
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
private config: Config = {
|
|
49
|
+
export class OpenAIChat {
|
|
50
|
+
openai: OpenAI
|
|
51
|
+
config: Config = {
|
|
53
52
|
n: 1,
|
|
54
53
|
model: 'gpt-3.5-turbo',
|
|
55
54
|
temperature: 1
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* @en If you need to set axios, use this method
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
setAxios(axios: AxiosInstance) {
|
|
64
|
-
this.axios = axios
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @zh 設定 api key
|
|
69
|
-
* @en Set api key
|
|
70
|
-
*/
|
|
71
|
-
|
|
72
|
-
setConfiguration(apiKey: string) {
|
|
73
|
-
this.apiKey = apiKey
|
|
57
|
+
constructor(openai: OpenAI) {
|
|
58
|
+
this.openai = openai
|
|
74
59
|
}
|
|
75
60
|
|
|
76
61
|
/**
|
|
@@ -87,9 +72,9 @@ export class ChatGPT35 {
|
|
|
87
72
|
* @en Talk to the AI
|
|
88
73
|
*/
|
|
89
74
|
|
|
90
|
-
async talk(messages:
|
|
75
|
+
async talk(messages: ChatGPTMessage[] = []) {
|
|
91
76
|
const newMessages = json.jpjs(messages)
|
|
92
|
-
const result = await this.
|
|
77
|
+
const result = await this.openai._axios.post<ApiResponse>('https://api.openai.com/v1/chat/completions', {
|
|
93
78
|
model: this.config.model,
|
|
94
79
|
n: this.config.n,
|
|
95
80
|
messages: newMessages,
|
|
@@ -97,7 +82,7 @@ export class ChatGPT35 {
|
|
|
97
82
|
}, {
|
|
98
83
|
headers: {
|
|
99
84
|
'Content-Type': 'application/json',
|
|
100
|
-
'Authorization': `Bearer ${this.
|
|
85
|
+
'Authorization': `Bearer ${this.openai._apiKey}`
|
|
101
86
|
}
|
|
102
87
|
})
|
|
103
88
|
const choices = result.data.choices || []
|
|
@@ -109,8 +94,8 @@ export class ChatGPT35 {
|
|
|
109
94
|
return {
|
|
110
95
|
id: result?.data.id as string,
|
|
111
96
|
text: message.content as string,
|
|
112
|
-
isDone: choices[0]?.finish_reason === 'stop',
|
|
113
97
|
newMessages,
|
|
98
|
+
isDone: choices[0]?.finish_reason === 'stop',
|
|
114
99
|
apiReseponse: result.data
|
|
115
100
|
}
|
|
116
101
|
}
|
|
@@ -119,7 +104,7 @@ export class ChatGPT35 {
|
|
|
119
104
|
* @zh 開啟持續性對話
|
|
120
105
|
*/
|
|
121
106
|
|
|
122
|
-
async
|
|
107
|
+
async keepTalk(prompt: string | string[], oldMessages: ChatGPTMessage[] = []) {
|
|
123
108
|
const result = await this.talk([
|
|
124
109
|
...oldMessages,
|
|
125
110
|
{
|
|
@@ -129,9 +114,9 @@ export class ChatGPT35 {
|
|
|
129
114
|
])
|
|
130
115
|
return {
|
|
131
116
|
result,
|
|
132
|
-
nextTalk: (prompt: string | string[]) => this.
|
|
117
|
+
nextTalk: (prompt: string | string[]) => this.keepTalk(prompt, result.newMessages)
|
|
133
118
|
}
|
|
134
119
|
}
|
|
135
120
|
}
|
|
136
121
|
|
|
137
|
-
export type
|
|
122
|
+
export type OpenAIChatTalkResponse = PromiseResponseType<OpenAIChat['talk']>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { PromiseResponseType } from '
|
|
1
|
+
import { OpenAI } from './index'
|
|
2
|
+
import { PromiseResponseType } from '../../types'
|
|
3
3
|
|
|
4
4
|
type Config = {
|
|
5
5
|
/**
|
|
@@ -38,31 +38,16 @@ type ApiResponse = {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
export class
|
|
42
|
-
private
|
|
43
|
-
private apiKey = ''
|
|
41
|
+
export class OpenAICompletion {
|
|
42
|
+
private openai: OpenAI
|
|
44
43
|
private config: Config = {
|
|
45
44
|
n: 1,
|
|
46
45
|
maxTokens: 2048,
|
|
47
46
|
temperature: 1
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
* @en If you need to set axios, please use this method.
|
|
53
|
-
*/
|
|
54
|
-
|
|
55
|
-
setAxios(axios: AxiosInstance) {
|
|
56
|
-
this.axios = axios
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @zh 設定 api key。
|
|
61
|
-
* @en Set api key.
|
|
62
|
-
*/
|
|
63
|
-
|
|
64
|
-
setConfiguration(apiKey: string) {
|
|
65
|
-
this.apiKey = apiKey
|
|
49
|
+
constructor(openai: OpenAI) {
|
|
50
|
+
this.openai = openai
|
|
66
51
|
}
|
|
67
52
|
|
|
68
53
|
/**
|
|
@@ -75,12 +60,12 @@ export class ChatGPT3 {
|
|
|
75
60
|
}
|
|
76
61
|
|
|
77
62
|
/**
|
|
78
|
-
* @zh
|
|
79
|
-
* @en
|
|
63
|
+
* @zh 進行補文。
|
|
64
|
+
* @en Do completion.
|
|
80
65
|
*/
|
|
81
66
|
|
|
82
|
-
async
|
|
83
|
-
const result = await this.
|
|
67
|
+
async run(prompt: string | string[]) {
|
|
68
|
+
const result = await this.openai._axios.post<ApiResponse>('https://api.openai.com/v1/completions', {
|
|
84
69
|
model: 'text-davinci-003',
|
|
85
70
|
n: this.config.n,
|
|
86
71
|
prompt: Array.isArray(prompt) ? prompt.join('\n') : prompt,
|
|
@@ -89,7 +74,7 @@ export class ChatGPT3 {
|
|
|
89
74
|
}, {
|
|
90
75
|
headers: {
|
|
91
76
|
'Content-Type': 'application/json',
|
|
92
|
-
'Authorization': `Bearer ${this.
|
|
77
|
+
'Authorization': `Bearer ${this.openai._apiKey}`
|
|
93
78
|
}
|
|
94
79
|
})
|
|
95
80
|
const choices = result.data.choices || []
|
|
@@ -102,4 +87,4 @@ export class ChatGPT3 {
|
|
|
102
87
|
}
|
|
103
88
|
}
|
|
104
89
|
|
|
105
|
-
export type
|
|
90
|
+
export type OpenAiOpenAICompletionResponse = PromiseResponseType<OpenAICompletion['run']>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { OpenAI } from './index'
|
|
2
2
|
|
|
3
3
|
type ApiResponse = {
|
|
4
4
|
created: string
|
|
@@ -20,30 +20,15 @@ type Config = {
|
|
|
20
20
|
size: `${number}x${number}`
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
export class
|
|
24
|
-
private
|
|
25
|
-
private apiKey = ''
|
|
23
|
+
export class OpenAIImagesGeneration {
|
|
24
|
+
private openai: OpenAI
|
|
26
25
|
private config: Config = {
|
|
27
26
|
n: 1,
|
|
28
27
|
size: '1024x1024'
|
|
29
28
|
}
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* @en If you need to set axios, use this method
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
setAxios(axios: AxiosInstance) {
|
|
37
|
-
this.axios = axios
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @zh 設定 api key
|
|
42
|
-
* @en Set api key
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
setConfiguration(apiKey: string) {
|
|
46
|
-
this.apiKey = apiKey
|
|
30
|
+
constructor(openai: OpenAI) {
|
|
31
|
+
this.openai = openai
|
|
47
32
|
}
|
|
48
33
|
|
|
49
34
|
/**
|
|
@@ -61,7 +46,7 @@ export class ImagesGenerations {
|
|
|
61
46
|
*/
|
|
62
47
|
|
|
63
48
|
async create(prompt: string) {
|
|
64
|
-
const result = await this.
|
|
49
|
+
const result = await this.openai._axios.post<ApiResponse>('https://api.openai.com/v1/images/generations', {
|
|
65
50
|
prompt,
|
|
66
51
|
n: this.config.n,
|
|
67
52
|
size: this.config.size,
|
|
@@ -70,7 +55,7 @@ export class ImagesGenerations {
|
|
|
70
55
|
timeout: 1000 * 60 * 5,
|
|
71
56
|
headers: {
|
|
72
57
|
'Content-Type': 'application/json',
|
|
73
|
-
'Authorization': `Bearer ${this.
|
|
58
|
+
'Authorization': `Bearer ${this.openai._apiKey}`
|
|
74
59
|
}
|
|
75
60
|
})
|
|
76
61
|
return result.data
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { OpenAIChat } from './chat'
|
|
2
|
+
import { OpenAICompletion } from './completion'
|
|
3
|
+
import { OpenAIImagesGeneration } from './images-generation'
|
|
4
|
+
import axios, { AxiosInstance } from 'axios'
|
|
5
|
+
|
|
6
|
+
export class OpenAI {
|
|
7
|
+
_axios = axios.create()
|
|
8
|
+
_apiKey = ''
|
|
9
|
+
|
|
10
|
+
constructor(apiKey = '') {
|
|
11
|
+
this._apiKey = apiKey
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @zh 如果你有需要特別設定 axios,請使用這方法。
|
|
16
|
+
* @en If you need to set axios, please use this method.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
setAxios(axios: AxiosInstance) {
|
|
20
|
+
this._axios = axios
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @zh 設定 api key。
|
|
25
|
+
* @en Set api key.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
setConfiguration(apiKey: string) {
|
|
29
|
+
this._apiKey = apiKey
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
createChat() {
|
|
33
|
+
return new OpenAIChat(this)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
createCompletion() {
|
|
37
|
+
return new OpenAICompletion(this)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
createImagesGeneration() {
|
|
41
|
+
return new OpenAIImagesGeneration(this)
|
|
42
|
+
}
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ctod",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "CtoD Is Chat To Data
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "CtoD Is Chat To Data Utils.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"build": "cross-env webpack --progress",
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { ChatBrokerPlugin } from '../core/plugin';
|
|
2
|
+
import { Hook, Log } from 'power-helper';
|
|
3
|
+
import { Translator, TranslatorParams } from '../core/translator';
|
|
4
4
|
import { ValidateCallback, ValidateCallbackOutputs } from '../utils/validate';
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
declare type Message = {
|
|
6
|
+
role: 'system' | 'user' | 'assistant';
|
|
7
|
+
name?: string;
|
|
8
|
+
content: string;
|
|
9
|
+
};
|
|
10
|
+
export declare type ChatBrokerHooks<S extends ValidateCallback<any>, O extends ValidateCallback<any>, P extends ChatBrokerPlugin<any, any>, PS extends Record<string, ReturnType<P['use']>>> = {
|
|
7
11
|
/**
|
|
8
12
|
* @zh 第一次聊天的時候觸發
|
|
9
13
|
* @en Triggered when chatting for the first time
|
|
10
14
|
*/
|
|
11
|
-
|
|
15
|
+
start: {
|
|
12
16
|
id: string;
|
|
13
17
|
data: ValidateCallbackOutputs<S>;
|
|
14
18
|
plugins: {
|
|
@@ -16,9 +20,9 @@ export declare class ChatGPT35Broker<S extends ValidateCallback<any>, O extends
|
|
|
16
20
|
send: (data: PS[K]['__receiveData']) => void;
|
|
17
21
|
};
|
|
18
22
|
};
|
|
19
|
-
messages:
|
|
20
|
-
setPreMessages: (messages:
|
|
21
|
-
changeMessages: (messages:
|
|
23
|
+
messages: Message[];
|
|
24
|
+
setPreMessages: (messages: Message[]) => void;
|
|
25
|
+
changeMessages: (messages: Message[]) => void;
|
|
22
26
|
};
|
|
23
27
|
/**
|
|
24
28
|
* @zh 發送聊天訊息給機器人前觸發
|
|
@@ -27,7 +31,7 @@ export declare class ChatGPT35Broker<S extends ValidateCallback<any>, O extends
|
|
|
27
31
|
talkBefore: {
|
|
28
32
|
id: string;
|
|
29
33
|
data: ValidateCallbackOutputs<S>;
|
|
30
|
-
messages:
|
|
34
|
+
messages: Message[];
|
|
31
35
|
lastUserMessage: string;
|
|
32
36
|
};
|
|
33
37
|
/**
|
|
@@ -37,8 +41,8 @@ export declare class ChatGPT35Broker<S extends ValidateCallback<any>, O extends
|
|
|
37
41
|
talkAfter: {
|
|
38
42
|
id: string;
|
|
39
43
|
data: ValidateCallbackOutputs<S>;
|
|
40
|
-
response:
|
|
41
|
-
messages:
|
|
44
|
+
response: any;
|
|
45
|
+
messages: Message[];
|
|
42
46
|
parseText: string;
|
|
43
47
|
lastUserMessage: string;
|
|
44
48
|
changeParseText: (text: string) => void;
|
|
@@ -60,14 +64,14 @@ export declare class ChatGPT35Broker<S extends ValidateCallback<any>, O extends
|
|
|
60
64
|
error: any;
|
|
61
65
|
retry: () => void;
|
|
62
66
|
count: number;
|
|
63
|
-
response:
|
|
67
|
+
response: any;
|
|
64
68
|
parserFails: {
|
|
65
69
|
name: string;
|
|
66
70
|
error: any;
|
|
67
71
|
}[];
|
|
68
|
-
messages:
|
|
72
|
+
messages: Message[];
|
|
69
73
|
lastUserMessage: string;
|
|
70
|
-
changeMessages: (messages:
|
|
74
|
+
changeMessages: (messages: Message[]) => void;
|
|
71
75
|
};
|
|
72
76
|
/**
|
|
73
77
|
* @zh 不論成功失敗,執行結束的時候會執行。
|
|
@@ -76,11 +80,32 @@ export declare class ChatGPT35Broker<S extends ValidateCallback<any>, O extends
|
|
|
76
80
|
done: {
|
|
77
81
|
id: string;
|
|
78
82
|
};
|
|
79
|
-
}
|
|
80
|
-
|
|
83
|
+
};
|
|
84
|
+
export declare type Params<S extends ValidateCallback<any>, O extends ValidateCallback<any>, C extends Record<string, any>, P extends ChatBrokerPlugin<any, any>, PS extends Record<string, ReturnType<P['use']>>> = Omit<TranslatorParams<S, O>, 'parsers'> & {
|
|
85
|
+
name?: string;
|
|
86
|
+
plugins?: PS | (() => PS);
|
|
87
|
+
request: (messages: Message[]) => Promise<string>;
|
|
88
|
+
install: (context: {
|
|
89
|
+
log: Log;
|
|
90
|
+
attach: Hook<C>['attach'];
|
|
91
|
+
attachAfter: Hook<C>['attachAfter'];
|
|
92
|
+
translator: Translator<S, O>;
|
|
93
|
+
}) => void;
|
|
94
|
+
};
|
|
95
|
+
export declare class ChatBroker<S extends ValidateCallback<any>, O extends ValidateCallback<any>, P extends ChatBrokerPlugin<any, any>, PS extends Record<string, ReturnType<P['use']>>, C extends ChatBrokerHooks<S, O, P, PS> = ChatBrokerHooks<S, O, P, PS>> {
|
|
96
|
+
protected __hookType: C;
|
|
97
|
+
protected log: Log;
|
|
98
|
+
protected hook: import("power-helper/dist/modules/hook").Hook<C>;
|
|
99
|
+
protected params: Params<S, O, C, P, PS>;
|
|
100
|
+
protected plugins: PS;
|
|
101
|
+
protected installed: boolean;
|
|
102
|
+
protected translator: Translator<S, O>;
|
|
103
|
+
constructor(params: Params<S, O, C, P, PS>);
|
|
104
|
+
protected _install(): any;
|
|
81
105
|
/**
|
|
82
106
|
* @zh 將請求發出至聊天機器人。
|
|
83
107
|
* @en Send request to chatbot.
|
|
84
108
|
*/
|
|
85
109
|
request<T extends Translator<S, O>>(data: T['__schemeType']): Promise<T['__outputType']>;
|
|
86
110
|
}
|
|
111
|
+
export {};
|
|
@@ -1,51 +1,17 @@
|
|
|
1
|
-
import { Log, Hook } from 'power-helper';
|
|
2
|
-
import { ChatGPT3 } from '../service/chatgpt3';
|
|
3
|
-
import { ChatGPT35 } from '../service/chatgpt35';
|
|
4
|
-
import { ChatGPT3Broker } from '../broker/3';
|
|
5
|
-
import { ChatGPT35Broker } from '../broker/35';
|
|
6
1
|
import { Translator } from './translator';
|
|
2
|
+
import { ChatBrokerHooks } from '../broker/chat';
|
|
3
|
+
import { Log, Hook } from 'power-helper';
|
|
7
4
|
import { ValidateCallback, ValidateCallbackOutputs } from '../utils/validate';
|
|
8
|
-
declare type
|
|
9
|
-
declare type
|
|
10
|
-
name: string;
|
|
11
|
-
params: T;
|
|
12
|
-
receiveData: R;
|
|
13
|
-
onInstall: (context: {
|
|
14
|
-
bot: ChatGPT3;
|
|
15
|
-
log: Log;
|
|
16
|
-
params: ValidateCallbackOutputs<T>;
|
|
17
|
-
attach: Hook<Broker3Hooks>['attach'];
|
|
18
|
-
attachAfter: Hook<Broker3Hooks>['attachAfter'];
|
|
19
|
-
translator: Translator<any, any>;
|
|
20
|
-
receive: (callback: (params: {
|
|
21
|
-
id: string;
|
|
22
|
-
data: ValidateCallbackOutputs<R>;
|
|
23
|
-
}) => void) => void;
|
|
24
|
-
}) => void;
|
|
25
|
-
};
|
|
26
|
-
export declare class Broker3Plugin<T extends ValidateCallback<any>, R extends ValidateCallback<any>> {
|
|
27
|
-
_event: import("power-helper/dist/modules/event").Event<Record<string, Record<string, any>>>;
|
|
28
|
-
_params: Broker3PluginParams<T, R>;
|
|
29
|
-
constructor(params: Broker3PluginParams<T, R>);
|
|
30
|
-
use(params: ValidateCallbackOutputs<T>): {
|
|
31
|
-
instance: any;
|
|
32
|
-
params: ValidateCallbackOutputs<T, ReturnType<T>>;
|
|
33
|
-
send: (data: ValidateCallbackOutputs<R>) => void;
|
|
34
|
-
receive: (callback: any) => void;
|
|
35
|
-
__receiveData: ValidateCallbackOutputs<R, ReturnType<R>>;
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
declare type Broker35Hooks = ChatGPT35Broker<any, any, any, any>['__hookType'];
|
|
39
|
-
declare type Broker35PluginParams<T extends ValidateCallback<any>, R extends ValidateCallback<any>> = {
|
|
5
|
+
declare type BrokerHooks = ChatBrokerHooks<any, any, any, any>;
|
|
6
|
+
declare type BrokerPluginParams<T extends ValidateCallback<any>, R extends ValidateCallback<any>> = {
|
|
40
7
|
name: string;
|
|
41
8
|
params: T;
|
|
42
9
|
receiveData: R;
|
|
43
10
|
onInstall: (context: {
|
|
44
|
-
bot: ChatGPT35;
|
|
45
11
|
log: Log;
|
|
46
12
|
params: ValidateCallbackOutputs<T>;
|
|
47
|
-
attach: Hook<
|
|
48
|
-
attachAfter: Hook<
|
|
13
|
+
attach: Hook<BrokerHooks>['attach'];
|
|
14
|
+
attachAfter: Hook<BrokerHooks>['attachAfter'];
|
|
49
15
|
translator: Translator<any, any>;
|
|
50
16
|
receive: (callback: (params: {
|
|
51
17
|
id: string;
|
|
@@ -53,10 +19,10 @@ declare type Broker35PluginParams<T extends ValidateCallback<any>, R extends Val
|
|
|
53
19
|
}) => void) => void;
|
|
54
20
|
}) => void;
|
|
55
21
|
};
|
|
56
|
-
export declare class
|
|
22
|
+
export declare class ChatBrokerPlugin<T extends ValidateCallback<any>, R extends ValidateCallback<any>> {
|
|
57
23
|
_event: import("power-helper/dist/modules/event").Event<Record<string, Record<string, any>>>;
|
|
58
|
-
_params:
|
|
59
|
-
constructor(params:
|
|
24
|
+
_params: BrokerPluginParams<T, R>;
|
|
25
|
+
constructor(params: BrokerPluginParams<T, R>);
|
|
60
26
|
use(params: ValidateCallbackOutputs<T>): {
|
|
61
27
|
instance: any;
|
|
62
28
|
params: ValidateCallbackOutputs<T, ReturnType<T>>;
|
package/types/lib/index.d.ts
CHANGED
|
@@ -1,44 +1,31 @@
|
|
|
1
|
-
import * as _Plugins from './core/plugin';
|
|
2
1
|
import * as _plugins from './plugins';
|
|
3
2
|
import * as _templates from './templates';
|
|
4
3
|
import * as _Translator from './core/translator';
|
|
5
4
|
import { ValidateCallback } from './utils/validate';
|
|
5
|
+
import { OpenAI as _OpenAI } from './service/openai';
|
|
6
6
|
import { TextParser as _TextParser } from './core/parser';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
import { ChatGPT35Broker as _ChatGPT35Broker } from './broker/35';
|
|
11
|
-
import { ImagesGenerations as _ImagesGenerations } from './service/images-generations';
|
|
7
|
+
import { ChatBroker as _ChatBroker } from './broker/chat';
|
|
8
|
+
import { ChatBrokerPlugin as _ChatBrokerPlugin } from './core/plugin';
|
|
9
|
+
export declare type OpenAI = _OpenAI;
|
|
12
10
|
export declare type TextParser = _TextParser;
|
|
13
11
|
export declare type Translator<S extends ValidateCallback<any>, O extends ValidateCallback<any>> = _Translator.Translator<S, O>;
|
|
14
12
|
export declare type TranslatorParams<S extends ValidateCallback<any>, O extends ValidateCallback<any>> = _Translator.TranslatorParams<S, O>;
|
|
15
|
-
export declare type
|
|
16
|
-
export declare type
|
|
17
|
-
export declare
|
|
18
|
-
export declare type Broker35Plugin<T extends ValidateCallback<any>, R extends ValidateCallback<any>> = _Plugins.Broker35Plugin<T, R>;
|
|
19
|
-
export declare type ImagesGenerations = _ImagesGenerations;
|
|
13
|
+
export declare type ChatBroker<S extends ValidateCallback<any>, O extends ValidateCallback<any>> = _ChatBroker<S, O, any, any>;
|
|
14
|
+
export declare type ChatBrokerPlugin<T extends ValidateCallback<any>, R extends ValidateCallback<any>> = _ChatBrokerPlugin<T, R>;
|
|
15
|
+
export declare const OpenAI: typeof _OpenAI;
|
|
20
16
|
export declare const TextParser: typeof _TextParser;
|
|
21
17
|
export declare const Translator: typeof _Translator.Translator;
|
|
22
|
-
export declare const
|
|
23
|
-
export declare const
|
|
24
|
-
export declare const ChatGPT3Broker: typeof _ChatGPT3Broker;
|
|
25
|
-
export declare const ChatGPT35Broker: typeof _ChatGPT35Broker;
|
|
26
|
-
export declare const Broker3Plugin: typeof _Plugins.Broker3Plugin;
|
|
27
|
-
export declare const Broker35Plugin: typeof _Plugins.Broker35Plugin;
|
|
28
|
-
export declare const ImagesGenerations: typeof _ImagesGenerations;
|
|
18
|
+
export declare const ChatBroker: typeof _ChatBroker;
|
|
19
|
+
export declare const ChatBrokerPlugin: typeof _ChatBrokerPlugin;
|
|
29
20
|
export declare const plugins: typeof _plugins;
|
|
30
21
|
export declare const templates: typeof _templates;
|
|
31
22
|
export declare const ctod: {
|
|
23
|
+
OpenAI: typeof _OpenAI;
|
|
32
24
|
plugins: typeof _plugins;
|
|
33
25
|
templates: typeof _templates;
|
|
34
|
-
|
|
35
|
-
ChatGPT35: typeof _ChatGPT35;
|
|
26
|
+
ChatBroker: typeof _ChatBroker;
|
|
36
27
|
Translator: typeof _Translator.Translator;
|
|
37
28
|
TextParser: typeof _TextParser;
|
|
38
|
-
|
|
39
|
-
Broker35Plugin: typeof _Plugins.Broker35Plugin;
|
|
40
|
-
ChatGPT3Broker: typeof _ChatGPT3Broker;
|
|
41
|
-
ChatGPT35Broker: typeof _ChatGPT35Broker;
|
|
42
|
-
ImagesGenerations: typeof _ImagesGenerations;
|
|
29
|
+
ChatBrokerPlugin: typeof _ChatBrokerPlugin;
|
|
43
30
|
};
|
|
44
31
|
export default ctod;
|
|
@@ -2,27 +2,27 @@
|
|
|
2
2
|
* @zh 一個基於印出 log 的 plugin。
|
|
3
3
|
* @en A plugin based on printing log.
|
|
4
4
|
*/
|
|
5
|
-
export declare const PrintLogPlugin: {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
detail: import("yup").BooleanSchema<boolean, import("yup").AnyObject, false, "d">;
|
|
9
|
-
}, () => {}>;
|
|
10
|
-
};
|
|
5
|
+
export declare const PrintLogPlugin: import("../core/plugin").ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
6
|
+
detail: import("yup").BooleanSchema<boolean, import("yup").AnyObject, false, "d">;
|
|
7
|
+
}, () => {}>;
|
|
11
8
|
/**
|
|
12
9
|
* @zh 當解析失敗時,會自動重試的對話。
|
|
13
10
|
* @en A conversation that will automatically retry when parsing fails.
|
|
14
11
|
*/
|
|
15
|
-
export declare const RetryPlugin: {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}, () => {}>;
|
|
20
|
-
};
|
|
12
|
+
export declare const RetryPlugin: import("../core/plugin").ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
13
|
+
retry: import("yup").NumberSchema<number, import("yup").AnyObject, 1, "d">;
|
|
14
|
+
printWarn: import("yup").BooleanSchema<boolean, import("yup").AnyObject, true, "d">;
|
|
15
|
+
}, () => {}>;
|
|
21
16
|
/**
|
|
22
17
|
* @zh 限制使用流量,這個 plugin 可以有效讓所有對話不會再限制內同時發送,可用於在開發過程中遭遇伺服器因頻率過高而阻擋請求。
|
|
23
18
|
* @en Limit the use of traffic. This plugin can effectively prevent all conversations from being sent at the same time within the limit, and can be used when the server blocks requests due to high frequency during development.
|
|
24
19
|
*/
|
|
25
|
-
export declare const LimiterPlugin: {
|
|
20
|
+
export declare const LimiterPlugin: import("../core/plugin").ChatBrokerPlugin<() => {}, () => {}>;
|
|
21
|
+
/**
|
|
22
|
+
* @zh 排程系統將全域託管,有什麼必要設定可以來更動它的狀態,例如:關閉排程。
|
|
23
|
+
* @en The scheduling system will be globally hosted. What is necessary to set can come to change its status, for example: close the schedule.
|
|
24
|
+
*/
|
|
25
|
+
export declare const LimiterPluginGlobState: {
|
|
26
26
|
event: import("power-helper/dist/modules/event").Event<{
|
|
27
27
|
run: {
|
|
28
28
|
id: string;
|
|
@@ -36,14 +36,12 @@ export declare const LimiterPlugin: {
|
|
|
36
36
|
interval: number;
|
|
37
37
|
};
|
|
38
38
|
closeSchedule: () => void;
|
|
39
|
-
|
|
39
|
+
plugin: import("../core/plugin").ChatBrokerPlugin<() => {}, () => {}>;
|
|
40
40
|
};
|
|
41
41
|
/**
|
|
42
42
|
* @zh 設定角色扮演。
|
|
43
43
|
* @en Set role play.
|
|
44
44
|
*/
|
|
45
|
-
export declare const RolePlugin: {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}, () => {}>;
|
|
49
|
-
};
|
|
45
|
+
export declare const RolePlugin: import("../core/plugin").ChatBrokerPlugin<(yup: typeof import("yup")) => {
|
|
46
|
+
role: import("yup").StringSchema<string, import("yup").AnyObject, undefined, "">;
|
|
47
|
+
}, () => {}>;
|