doomiaichat 5.1.0 → 6.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/dist/azureai.d.ts +22 -8
- package/dist/azureai.js +121 -17
- package/dist/declare.d.ts +27 -0
- package/dist/gptprovider.d.ts +1 -0
- package/dist/gptprovider.js +5 -1
- package/dist/openai.d.ts +7 -19
- package/dist/openai.js +87 -74
- package/dist/openaibase.d.ts +18 -0
- package/dist/openaibase.js +20 -0
- package/dist/openaiproxy.d.ts +21 -0
- package/dist/openaiproxy.js +102 -0
- package/dist/stabilityai.d.ts +1 -20
- package/dist/stabilityplusai.d.ts +6 -2
- package/dist/stabilityplusai.js +17 -17
- package/package.json +4 -3
- package/src/azureai.ts +98 -19
- package/src/declare.ts +35 -4
- package/src/gptprovider.ts +5 -1
- package/src/openai.ts +81 -617
- package/src/openaibase.ts +30 -0
- package/src/openaiproxy.ts +89 -0
- package/src/stabilityai.ts +1 -22
- package/src/stabilityplusai.ts +23 -21
package/src/openai.ts
CHANGED
|
@@ -1,39 +1,17 @@
|
|
|
1
|
-
import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "azure-openai"
|
|
2
|
-
|
|
1
|
+
// import { Configuration, OpenAIApi, ChatCompletionRequestMessage } from "azure-openai"
|
|
2
|
+
/**
|
|
3
|
+
* OpenAI
|
|
4
|
+
*/
|
|
5
|
+
import OpenAIBase from "./openaibase"
|
|
3
6
|
import { OpenAIApiParameters, ChatReponse, EmbeddingResult } from './declare'
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
protected aiApi: OpenAIApi | undefined;
|
|
8
|
-
protected readonly chatModel: string;
|
|
9
|
-
protected readonly maxtoken: number;
|
|
10
|
-
protected readonly top_p: number;
|
|
11
|
-
protected readonly presence_penalty:number;
|
|
12
|
-
protected readonly frequency_penalty: number;
|
|
13
|
-
protected readonly temperature: number;
|
|
14
|
-
protected readonly embeddingmodel: string;
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
17
|
-
* @param apiKey 调用OpenAI 的key
|
|
18
|
-
* @param apiOption
|
|
19
|
-
*/
|
|
20
|
-
constructor(apiKey: string, apiOption: OpenAIApiParameters = {}) {
|
|
21
|
-
super();
|
|
22
|
-
|
|
23
|
-
this.apiKey = apiKey;
|
|
24
|
-
this.chatModel = apiOption.model || 'gpt-3.5-turbo';
|
|
25
|
-
this.maxtoken = apiOption.maxtoken || 2048;
|
|
26
|
-
this.top_p = apiOption.top_p || 0.95;
|
|
27
|
-
this.temperature = apiOption.temperature || 0.9;
|
|
28
|
-
this.presence_penalty = apiOption.presence_penalty || 0;
|
|
29
|
-
this.frequency_penalty = apiOption.frequency_penalty || 0;
|
|
30
|
-
this.embeddingmodel = apiOption.embedding || 'text-embedding-ada-002';
|
|
31
|
-
}
|
|
7
|
+
import OpenAI from "openai";
|
|
8
|
+
import { ChatCompletionToolChoiceOption } from "openai/resources";
|
|
9
|
+
export default class OpenAIGpt extends OpenAIBase<OpenAI> {
|
|
32
10
|
/**
|
|
33
11
|
* 初始化OpenAI 的聊天对象Api
|
|
34
12
|
*/
|
|
35
|
-
createOpenAI(apiKey: string):
|
|
36
|
-
return new
|
|
13
|
+
createOpenAI(apiKey: string): OpenAI {
|
|
14
|
+
return new OpenAI({ apiKey })
|
|
37
15
|
}
|
|
38
16
|
|
|
39
17
|
/**
|
|
@@ -46,7 +24,8 @@ export default class OpenAIGpt extends GptBase {
|
|
|
46
24
|
this.aiApi = this.createOpenAI(this.apiKey);
|
|
47
25
|
}
|
|
48
26
|
try {
|
|
49
|
-
const response: any = await this.aiApi.createEmbedding({
|
|
27
|
+
//const response: any = await this.aiApi.createEmbedding({
|
|
28
|
+
const response: any = await this.aiApi.embeddings.create({
|
|
50
29
|
model: this.embeddingmodel,
|
|
51
30
|
input: text,
|
|
52
31
|
}, axiosOption);
|
|
@@ -62,31 +41,25 @@ export default class OpenAIGpt extends GptBase {
|
|
|
62
41
|
*/
|
|
63
42
|
public async chatRequest(chatText: string | Array<any>, callChatOption: OpenAIApiParameters, axiosOption: any = {}): Promise<ChatReponse> {
|
|
64
43
|
if (!chatText) return { successed: false, error: { errcode: 2, errmsg: '缺失聊天的内容' } };
|
|
65
|
-
if (!this.aiApi)
|
|
66
|
-
this.aiApi = this.createOpenAI(this.apiKey);
|
|
67
|
-
//return { successed: false, error: { errcode: 1, errmsg: '聊天机器人无效' } };
|
|
68
|
-
}
|
|
44
|
+
if (!this.aiApi) this.aiApi = this.createOpenAI(this.apiKey);
|
|
69
45
|
|
|
70
|
-
let message: Array<
|
|
46
|
+
let message: Array<any> = typeof (chatText) == 'string' ?
|
|
71
47
|
[{ role: 'user', content: chatText }] : chatText;
|
|
72
|
-
// console.log('message', message)
|
|
73
48
|
try {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
49
|
+
// const response: any = await this.aiApi.createChatCompletion({
|
|
50
|
+
const response: any = await this.aiApi.chat.completions.create(
|
|
51
|
+
{
|
|
52
|
+
model:callChatOption?.model || this.chatModel,
|
|
53
|
+
messages:message,
|
|
77
54
|
temperature: Number(callChatOption?.temperature || this.temperature),
|
|
78
55
|
max_tokens: Number(callChatOption?.maxtoken || this.maxtoken),
|
|
79
56
|
top_p: Number(callChatOption?.top_p || this.top_p),
|
|
80
57
|
presence_penalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
|
|
81
58
|
frequency_penalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
|
|
82
|
-
n: Number(callChatOption?.replyCounts || 1) || 1
|
|
59
|
+
n: Number(callChatOption?.replyCounts || 1) || 1,
|
|
60
|
+
tools: callChatOption.tools,
|
|
61
|
+
tool_choice: (callChatOption.tool_choice || 'none') as ChatCompletionToolChoiceOption,
|
|
83
62
|
}, axiosOption);
|
|
84
|
-
// console.log('finish_reason==>', response.data.choices)
|
|
85
|
-
////输出的内容不合规
|
|
86
|
-
if (response.data.choices[0].finish_reason === 'content_filter') {
|
|
87
|
-
console.log('content_filter')
|
|
88
|
-
return { successed: false, error: 'content_filter' }
|
|
89
|
-
}
|
|
90
63
|
return { successed: true, message: response.data.choices, usage: response.data.usage };
|
|
91
64
|
} catch (error) {
|
|
92
65
|
console.log('result is error ', error)
|
|
@@ -106,584 +79,75 @@ export default class OpenAIGpt extends GptBase {
|
|
|
106
79
|
this.aiApi = this.createOpenAI(this.apiKey);
|
|
107
80
|
}
|
|
108
81
|
// const DATA_END_TAG = `"usage":null}`
|
|
109
|
-
let message: Array<
|
|
82
|
+
let message: Array<any> = typeof (chatText) == 'string' ?
|
|
110
83
|
[{ role: 'user', content: chatText }] : chatText;
|
|
111
|
-
axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }, { responseType: 'stream' })
|
|
84
|
+
//axiosOption = Object.assign({}, axiosOption || { timeout: 60000 }, { responseType: 'stream' })
|
|
85
|
+
axiosOption = Object.assign({}, axiosOption || { timeout: 60000 })
|
|
112
86
|
let requestid = Math.ceil(Math.random() * (new Date().getTime() * Math.random()) / 1000);
|
|
113
87
|
try {
|
|
114
|
-
let finishreason: any = null, usage: any = null,errtxt = '';
|
|
88
|
+
// let finishreason: any = null, usage: any = null,errtxt = '';
|
|
115
89
|
///便于知道返回的requestid
|
|
116
90
|
// console.log('model', callChatOption?.model || this.chatModel,)
|
|
117
|
-
const response: any = await this.aiApi.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
91
|
+
//const response: any = await this.aiApi.chat.completions.create({
|
|
92
|
+
const response: any = await this.aiApi.chat.completions.create(
|
|
93
|
+
{
|
|
94
|
+
model: callChatOption?.model || this.chatModel,
|
|
95
|
+
messages: message,
|
|
96
|
+
temperature: Number(callChatOption?.temperature || this.temperature),
|
|
97
|
+
max_tokens: Number(callChatOption?.maxtoken || this.maxtoken),
|
|
98
|
+
top_p: Number(callChatOption?.top_p || this.top_p),
|
|
99
|
+
presence_penalty: Number(callChatOption?.presence_penalty || this.presence_penalty),
|
|
100
|
+
frequency_penalty: Number(callChatOption?.frequency_penalty || this.frequency_penalty),
|
|
101
|
+
n: Number(callChatOption?.replyCounts || 1) || 1,
|
|
102
|
+
stream:true
|
|
103
|
+
}, axiosOption);
|
|
127
104
|
let replytext: string[] = [];
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
})
|
|
105
|
+
for await (const chunk of response) {
|
|
106
|
+
const [choice] = chunk.choices, { content, finishreason, index, usage } = choice.delta;
|
|
107
|
+
if (!content) continue;
|
|
108
|
+
replytext.push(content);
|
|
109
|
+
let output = { successed: true, requestid, segment: content, text: replytext.join(''), finish_reason: finishreason, index, usage };
|
|
110
|
+
if (attach) output = Object.assign({}, output, attach);
|
|
111
|
+
this.emit(finishreason ? 'chatdone' : 'chattext', output)
|
|
112
|
+
}
|
|
113
|
+
// response.data.on('data', (data: any) => {
|
|
114
|
+
// const lines = data.toString().split('\n').filter((line: string) => line.trim() !== '');
|
|
115
|
+
// ///已经返回了结束原因
|
|
116
|
+
// if (finishreason) return;
|
|
117
|
+
// let alltext = (errtxt +lines.join('')).split('data:');
|
|
118
|
+
// errtxt = '';
|
|
119
|
+
// for (const line of alltext) {
|
|
120
|
+
// let txt = line.trim();
|
|
121
|
+
// if (!txt) continue;
|
|
122
|
+
// if (txt === '[DONE]') {
|
|
123
|
+
// let output = { successed: true, requestid, text: replytext.join(''), finish_reason: 'stop', usage };
|
|
124
|
+
// if (attach) output = Object.assign({}, output, attach);
|
|
125
|
+
// this.emit('chatdone', output)
|
|
126
|
+
// return; // Stream finished
|
|
127
|
+
// }
|
|
128
|
+
// try {
|
|
129
|
+
// ///{ delta: { content: '$\\' }, index: 0, finish_reason: null }
|
|
130
|
+
// ///发送出去
|
|
131
|
+
// const parsed = JSON.parse(txt);
|
|
132
|
+
// ///已经返回一个正确的了,可以重置这个变量了
|
|
133
|
+
// finishreason = parsed.choices[0].finish_reason;
|
|
134
|
+
// usage = parsed.usage;
|
|
135
|
+
// let streamtext = parsed.choices[0].delta.content;
|
|
136
|
+
// replytext.push(streamtext);
|
|
137
|
+
// let output = { successed: true, requestid, segment: streamtext, text: replytext.join(''), finish_reason: finishreason, index: parsed.choices[0].index, usage };
|
|
138
|
+
// if (attach) output = Object.assign({}, output, attach);
|
|
139
|
+
// this.emit(finishreason ? 'chatdone' : 'chattext', output)
|
|
140
|
+
// if (finishreason) return;
|
|
141
|
+
// } catch (error) {
|
|
142
|
+
// errtxt+=txt; ///这一段json没有结束,作为下一次的流过来时使用
|
|
143
|
+
// this.emit('chaterror', { successed: false, requestid, error: 'JSON parse stream message', errtxt });
|
|
144
|
+
// }
|
|
145
|
+
// }
|
|
146
|
+
// });
|
|
165
147
|
return { successed: true, requestid }
|
|
166
148
|
} catch (error) {
|
|
167
149
|
this.emit('requesterror', { successed: false, requestid, error: 'call axios faied ' + error });
|
|
168
150
|
return { successed: false, requestid }
|
|
169
151
|
}
|
|
170
152
|
}
|
|
171
|
-
|
|
172
|
-
// /**
|
|
173
|
-
// * 点评问题回答的评价
|
|
174
|
-
// * @param question
|
|
175
|
-
// * @param answer
|
|
176
|
-
// * @param axiosOption
|
|
177
|
-
// */
|
|
178
|
-
// override async commentQuestionAnswer(question: string, answer: string, axiosOption: any = { timeout: 30000 }): Promise<CommentResult> {
|
|
179
|
-
// if (!question || !answer) return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } }
|
|
180
|
-
// let message = [
|
|
181
|
-
// { role: 'system', content: '你是一名专业的知识点评师。' },
|
|
182
|
-
// { role: 'user', content: `问题题干:“${question}”` },
|
|
183
|
-
// { role: 'user', content: `回答内容:“${answer}”` },
|
|
184
|
-
// { role: 'user', content: `请根据以上的回答内容进行点评,给出一段不超过200字的评语,并给出0-100的评分。` },
|
|
185
|
-
// { role: 'user', content: `结果完整按照{"comment":"点评内容","score":"评分"}的JSON结构输出` }
|
|
186
|
-
// ]
|
|
187
|
-
// const result = await this.chatRequest(message, {}, axiosOption);
|
|
188
|
-
// if (result.successed && result.message) {
|
|
189
|
-
// let value = result.message[0].message.content.trim();
|
|
190
|
-
// let replyJson = this.fixedJsonString(value);
|
|
191
|
-
// ///能够提取到内容
|
|
192
|
-
// if (replyJson.length) return { successed: true, ...replyJson[0] }
|
|
193
|
-
// ///回答的内容非JSON格式,自己来提取算了
|
|
194
|
-
// console.log('自己组装')
|
|
195
|
-
// let matched = value.match(/\d+分/g), score = 0;
|
|
196
|
-
// if (matched && matched.length) {
|
|
197
|
-
// score = Number(matched[0].replace('分', ''));
|
|
198
|
-
// }
|
|
199
|
-
// return { successed: true, comment: value, score }
|
|
200
|
-
// }
|
|
201
|
-
// return { successed: false };
|
|
202
|
-
// }
|
|
203
|
-
// /**
|
|
204
|
-
// * 判断一句话的表达情绪
|
|
205
|
-
// * @param {*} s1
|
|
206
|
-
// * @param {*} axiosOption
|
|
207
|
-
// */
|
|
208
|
-
// override async getScentenceEmotional(s1: string, axiosOption: any = { timeout: 30000 }): Promise<EmotionResult> {
|
|
209
|
-
// if (!s1) return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } }
|
|
210
|
-
// const emotion = ['愤怒', '威胁', '讽刺', '愧疚', '兴奋', '友好', '消极', '生气', '正常'];
|
|
211
|
-
// const messages = [
|
|
212
|
-
// { role: 'system', content: `你是一名专业的语言大师` },
|
|
213
|
-
// { role: 'user', content: s1 },
|
|
214
|
-
// { role: 'user', content: `请分析上述内容的语言情绪,请从"${emotion.join(',')}"这些情绪中对应一个输出` },
|
|
215
|
-
// ]
|
|
216
|
-
// const result = await this.chatRequest(messages, {}, axiosOption);
|
|
217
|
-
// if (result.successed && result.message) {
|
|
218
|
-
// let value = result.message[0].message.content.trim();
|
|
219
|
-
// for (const word of emotion) {
|
|
220
|
-
// if (value.indexOf(word) >= 0) return { successed: true, emotion: word };
|
|
221
|
-
// }
|
|
222
|
-
// }
|
|
223
|
-
// return { successed: true, emotion: '未知' };
|
|
224
|
-
// }
|
|
225
|
-
|
|
226
|
-
// /**
|
|
227
|
-
// * 获取两句话的相似度取值
|
|
228
|
-
// * @param {*} s1
|
|
229
|
-
// * @param {*} s2
|
|
230
|
-
// */
|
|
231
|
-
// override async getScentenseSimilarity(s1: string, s2: string, axiosOption: any = { timeout: 30000 }): Promise<SimilarityResult> {
|
|
232
|
-
// if (!s1 || !s2) return { successed: false, error: { errcode: 2, errmsg: '缺失参数' } }
|
|
233
|
-
// const messages = [
|
|
234
|
-
// { role: 'system', content: '你是一名专业的语言分析大师' },
|
|
235
|
-
// { role: 'user', content: s1 },
|
|
236
|
-
// { role: 'user', content: s2 },
|
|
237
|
-
// { role: 'user', content: '请从语义上对比以上两句话的相似度,请仅输出0至100之间的整数对比结果即可' },
|
|
238
|
-
// ]
|
|
239
|
-
// const result = await this.chatRequest(messages, { maxtoken: 32 }, axiosOption);
|
|
240
|
-
// if (result.successed && result.message) {
|
|
241
|
-
// let value = result.message[0].message.content.replace(/[^\d]/g, "")
|
|
242
|
-
// if (value > 100) value = Math.floor(value / 10);
|
|
243
|
-
// return { successed: true, value: Number(value) };
|
|
244
|
-
// }
|
|
245
|
-
// return { successed: false, error: result.error, value: 0 };
|
|
246
|
-
|
|
247
|
-
// }
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
// /**
|
|
251
|
-
// * 获得一种内容的相似说法
|
|
252
|
-
// * 比如:
|
|
253
|
-
// * 你今年多大?
|
|
254
|
-
// * 相似问法:您是哪一年出生的
|
|
255
|
-
// * 您今年贵庚?
|
|
256
|
-
// * @param {*} content
|
|
257
|
-
// * @param {需要出来的数量} count
|
|
258
|
-
// */
|
|
259
|
-
// override async getSimilarityContent(content: string, count: number = 1, axiosOption: any = {}): Promise<ChatReponse> {
|
|
260
|
-
// let chnReg: boolean = /([\u4e00-\u9fa5]|[\ufe30-\uffa0])/.test(content) ///检查源话是否含有中文内容
|
|
261
|
-
// let engReg: boolean = /[a-zA-Z]/.test(content) ///检查源话是否含有英文内容
|
|
262
|
-
// ///如果源话是全中文,那么结果中不应该出来英文的相似说法,如果源话是全英文,则结果不能出现全中文的说法
|
|
263
|
-
// let prefix = (!chnReg && engReg) ? '请用完整的英文表达,' : ((chnReg && !engReg) ? '请用完整的中文表达,' : '')
|
|
264
|
-
// const text = `${prefix}生成与下面句子意思相同的内容"${content}"`
|
|
265
|
-
// let result = await this.chatRequest(text, { replyCounts: count }, axiosOption);
|
|
266
|
-
// if (!result.successed || !result.message) return result;
|
|
267
|
-
// let replys = result.message.map(item => { return item.message.content.trim(); })
|
|
268
|
-
// return { successed: true, message: replys }
|
|
269
|
-
// }
|
|
270
|
-
// /**
|
|
271
|
-
// * 提取内容的中心思想摘要
|
|
272
|
-
// * @param content
|
|
273
|
-
// * @param axiosOption
|
|
274
|
-
// */
|
|
275
|
-
// override async getSummaryOfContent(content: string | Array<any>, axiosOption: any = {}): Promise<SummaryReponse> {
|
|
276
|
-
// let arrContent: any[] = [];
|
|
277
|
-
// if (typeof (content) == 'string') {
|
|
278
|
-
// let splittext = this.splitLongText(content);
|
|
279
|
-
// for (const string of splittext) {
|
|
280
|
-
// arrContent.push({ role: 'user', content: string })
|
|
281
|
-
// }
|
|
282
|
-
// } else {
|
|
283
|
-
// arrContent = content;
|
|
284
|
-
// }
|
|
285
|
-
// let summary: Array<OutlineSummaryItem> = [];
|
|
286
|
-
// while (arrContent.length > 0) {
|
|
287
|
-
// let subarray = arrContent.slice(0, MESSAGE_LENGTH);
|
|
288
|
-
// subarray.push({ role: 'user', content: '根据上述内容精简,提炼内容提纲及摘要内容,每项摘要内容尽量精简数据化不超过100字,结果严格按照[{outline:"提纲标题","summary":["摘要内容1","摘要内容2","摘要内容3"]}]的JSON结构输出' })
|
|
289
|
-
// let result = await this.chatRequest(subarray, {}, axiosOption);
|
|
290
|
-
// if (result.successed && result.message) {
|
|
291
|
-
// try {
|
|
292
|
-
// // console.log('result.message[0].content', result.message[0].message.content)
|
|
293
|
-
// let jsonObjItems = JSON.parse(result.message[0].message.content)
|
|
294
|
-
// if (Array.isArray(jsonObjItems)) summary = summary.concat(jsonObjItems)
|
|
295
|
-
// } catch (error) {
|
|
296
|
-
// console.log('result.message[0].content', error)
|
|
297
|
-
// }
|
|
298
|
-
// }
|
|
299
|
-
// ////删除已经处理的文本
|
|
300
|
-
// arrContent.splice(0, MESSAGE_LENGTH);
|
|
301
|
-
// }
|
|
302
|
-
// return { successed: true, article: summary }
|
|
303
|
-
// }
|
|
304
|
-
// /**
|
|
305
|
-
// * 从指定的文本内容中生成相关的问答
|
|
306
|
-
// * @param {*} content
|
|
307
|
-
// * @param {*} count
|
|
308
|
-
// * @param {*} axiosOption
|
|
309
|
-
// * @returns
|
|
310
|
-
// *///并在答案末尾处必须给出答案内容中的关键词
|
|
311
|
-
// override async generateQuestionsFromContent(content: string, count: number = 1, everyContentLength: number = SECTION_LENGTH, axiosOption: any = {}): Promise<ChatReponse> {
|
|
312
|
-
// let arrContent = this.splitLongText(content, everyContentLength || SECTION_LENGTH);
|
|
313
|
-
// ///如果最后一段的文字内容过短,则把最后一段内容追加到前一段中,并删除最后一段
|
|
314
|
-
// let totalLen = arrContent.length;
|
|
315
|
-
// if (totalLen >= 2 && (arrContent[totalLen - 1]?.length || 0) < 100) {
|
|
316
|
-
// arrContent[totalLen - 2] += arrContent[totalLen - 1];
|
|
317
|
-
// arrContent.splice(totalLen - 1, 1);
|
|
318
|
-
// }
|
|
319
|
-
// ///没20句话分为一组,适应大文件内容多次请求组合结果
|
|
320
|
-
// ///每一句话需要产生的题目
|
|
321
|
-
// let questions4EverySentense: number = count / arrContent.length; //Math.ceil(arrContent.length / 20);
|
|
322
|
-
// let faqs: FaqItem[] = [], gotted: number = 0;
|
|
323
|
-
// while (arrContent.length > 0 && gotted < count) {
|
|
324
|
-
// questions4EverySentense = (count - gotted) / arrContent.length
|
|
325
|
-
// ////每次最多送MESSAGE_LENGTH句话给openai
|
|
326
|
-
// let itemCount = Math.min(Math.ceil(questions4EverySentense), count - gotted);
|
|
327
|
-
// let subarray = [
|
|
328
|
-
// { role: 'system', content: FAQ_ROLE_DEFINE },
|
|
329
|
-
// { role: 'user', content: `从以下内容中提取${itemCount}条提问及答案,并从答案内容提取出至少2个关键词,最终结果按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的JSON数组结构输出。如果内容不足以提取问题和答案,请直接输出JSON空数组 []。` },
|
|
330
|
-
// { role: 'user', content: arrContent.slice(0, 1)[0] }
|
|
331
|
-
// ]
|
|
332
|
-
// console.log('Faq Question Pick Prompt:', subarray)
|
|
333
|
-
// let result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
334
|
-
// ///如果请求发生了网络错误(不是内容合规问题),则再重试一次,如果任然有错则放弃
|
|
335
|
-
// if (!result.successed && result.error != 'content_filter') {
|
|
336
|
-
// console.log('network error,retry onemore time')
|
|
337
|
-
// result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
338
|
-
// }
|
|
339
|
-
// if (result.successed && result.message) {
|
|
340
|
-
// let msgs = await this.pickUpFaqContent(result.message);
|
|
341
|
-
// if (msgs.length) {
|
|
342
|
-
// ///对外发送检出问答题的信号
|
|
343
|
-
// this.emit('parseout', { type: 'qa', items: msgs })
|
|
344
|
-
// gotted += msgs.length; //result.message.length;
|
|
345
|
-
// faqs = faqs.concat(msgs);
|
|
346
|
-
// }
|
|
347
|
-
// }
|
|
348
|
-
// ////删除已经处理的文本
|
|
349
|
-
// arrContent.splice(0, 1);
|
|
350
|
-
// }
|
|
351
|
-
// arrContent = []; /// 释放内存
|
|
352
|
-
// ///发出信号,解析完毕
|
|
353
|
-
// this.emit('parseover', { type: 'qa', items: faqs })
|
|
354
|
-
// return { successed: true, message: faqs.slice(0, count) };
|
|
355
|
-
// }
|
|
356
|
-
|
|
357
|
-
// /**
|
|
358
|
-
// * 解析Faq返回的问题
|
|
359
|
-
// * @param {*} messages
|
|
360
|
-
// * @returns
|
|
361
|
-
// */
|
|
362
|
-
// protected async pickUpFaqContent(messages: Array<any>): Promise<Array<FaqItem>> {
|
|
363
|
-
// if (!messages[0]?.message?.content) return [];
|
|
364
|
-
// let answerString = messages[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
365
|
-
// if (answerString === '[]') return [];
|
|
366
|
-
// let jsonObj = this.fixedJsonString(answerString);
|
|
367
|
-
// if (!jsonObj.length) {
|
|
368
|
-
// let fixedAsk = [
|
|
369
|
-
// { role: 'system', content: '角色扮演:假设你是一位高级JSON数据分析师' },
|
|
370
|
-
// { role: 'user', content: `请分析以下内容,严格按照[{"question":"提问内容","answer":"答案内容","keywords":["关键词1","关键词2"]}]的标准JSON数组结构输出。如果内容不足以提取问题和答案,请直接输出JSON空数组,无需提供参考。` },
|
|
371
|
-
// { role: 'user', content: answerString },
|
|
372
|
-
// ]
|
|
373
|
-
// console.log('pickUpFaqContent fixedAsk', fixedAsk)
|
|
374
|
-
// let fixedJsonResult: any = await this.chatRequest(fixedAsk, { replyCounts: 1 }, {})
|
|
375
|
-
// if (fixedJsonResult.successed) {
|
|
376
|
-
// answerString = fixedJsonResult.message[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
377
|
-
// jsonObj = this.fixedJsonString(answerString);
|
|
378
|
-
// }
|
|
379
|
-
// if (!jsonObj.length) return []
|
|
380
|
-
// }
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
// try {
|
|
384
|
-
// //let jsonObj = JSON.parse(answerString);
|
|
385
|
-
// //let jsonObj = eval(answerString);
|
|
386
|
-
// jsonObj.map((item: FaqItem) => {
|
|
387
|
-
// let realKeyword: string[] = [];
|
|
388
|
-
// let keywords: string[] = (item.keywords + '').split(',');
|
|
389
|
-
// let answer = item.answer || '';
|
|
390
|
-
// for (const k of keywords) {
|
|
391
|
-
// if (k && answer.indexOf(k) >= 0) realKeyword.push(k)
|
|
392
|
-
// }
|
|
393
|
-
// item.keywords = realKeyword;
|
|
394
|
-
// return item;
|
|
395
|
-
// })
|
|
396
|
-
// return jsonObj;
|
|
397
|
-
// } catch (err) {
|
|
398
|
-
// console.log('JSON error', err)
|
|
399
|
-
// return [];
|
|
400
|
-
// }
|
|
401
|
-
// }
|
|
402
|
-
|
|
403
|
-
// /**
|
|
404
|
-
// * 从指定的文本内容中生成一张试卷
|
|
405
|
-
// * @param {*} content
|
|
406
|
-
// * @param {试卷的参数} paperOption
|
|
407
|
-
// * totalscore: 试卷总分,默认100
|
|
408
|
-
// * section: {type:[0,1,2,3]为单选、多选、判断、填空题型 count:生成多少道 score:本段分数}
|
|
409
|
-
// * @param {*} axiosOption
|
|
410
|
-
// * @returns
|
|
411
|
-
// *///并在答案末尾处必须给出答案内容中的关键词
|
|
412
|
-
// override async generateExaminationPaperFromContent(content: string, paperOption: any = {}, everyContentLength: number = SECTION_LENGTH, axiosOption: any = {}): Promise<ExaminationPaperResult> {
|
|
413
|
-
// let arrContent = this.splitLongText(content, everyContentLength || SECTION_LENGTH);
|
|
414
|
-
// ///如果最后一段的文字内容过短,则把最后一段内容追加到前一段中,并删除最后一段
|
|
415
|
-
// let totalLen = arrContent.length;
|
|
416
|
-
// if (totalLen >= 2 && (arrContent[totalLen - 1]?.length || 0) < 100) {
|
|
417
|
-
// arrContent[totalLen - 2] += arrContent[totalLen - 1];
|
|
418
|
-
// arrContent.splice(totalLen - 1, 1);
|
|
419
|
-
// }
|
|
420
|
-
// let sectionCount: any = {
|
|
421
|
-
// singlechoice: (paperOption.singlechoice?.count || 0) / arrContent.length,
|
|
422
|
-
// multiplechoice: (paperOption.multiplechoice?.count || 0) / arrContent.length,
|
|
423
|
-
// trueorfalse: (paperOption.trueorfalse?.count || 0) / arrContent.length,
|
|
424
|
-
// completion: (paperOption.completion?.count || 0) / arrContent.length
|
|
425
|
-
// };
|
|
426
|
-
// ///剩余待生成的题目数量
|
|
427
|
-
// let remainCount: any = {
|
|
428
|
-
// singlechoice: paperOption.singlechoice?.count || 0,
|
|
429
|
-
// multiplechoice: paperOption.multiplechoice?.count || 0,
|
|
430
|
-
// trueorfalse: paperOption.trueorfalse?.count || 0,
|
|
431
|
-
// completion: paperOption.completion?.count || 0
|
|
432
|
-
// };
|
|
433
|
-
// ///每种类型的题目的分数
|
|
434
|
-
// let ITEM_SCORE: any = {
|
|
435
|
-
// singlechoice: paperOption.singlechoice?.score || 0,
|
|
436
|
-
// multiplechoice: paperOption.multiplechoice?.score || 0,
|
|
437
|
-
// trueorfalse: paperOption.trueorfalse?.score || 0,
|
|
438
|
-
// completion: paperOption.completion?.score || 0
|
|
439
|
-
// };
|
|
440
|
-
// ///最后生成出来的结果
|
|
441
|
-
// let paperReturned: any = {
|
|
442
|
-
// singlechoice: [], multiplechoice: [], trueorfalse: [], completion: []
|
|
443
|
-
|
|
444
|
-
// }, noMoreQuestionRetrive: boolean = false, totalscore: number = 0;
|
|
445
|
-
|
|
446
|
-
// while (arrContent.length > 0 && !noMoreQuestionRetrive) {
|
|
447
|
-
// ////每次最多送MESSAGE_LENGTH句话给openai
|
|
448
|
-
// /**
|
|
449
|
-
// * 每种类型的题目进行遍历
|
|
450
|
-
// */
|
|
451
|
-
// noMoreQuestionRetrive = true;
|
|
452
|
-
// for (const key of QUESTION_TYPE) {
|
|
453
|
-
// ///还需要抓取题目
|
|
454
|
-
// if (remainCount[key] > 0) {
|
|
455
|
-
// noMoreQuestionRetrive = false;
|
|
456
|
-
|
|
457
|
-
// //let itemCount = Math.min(remainCount[key], Math.ceil(subarray.length * sectionCount[key]));
|
|
458
|
-
// let itemCount = Math.min(remainCount[key], Math.ceil(sectionCount[key]));
|
|
459
|
-
// let subarray: any[] = [
|
|
460
|
-
// { role: 'system', content: QUESTION_ROLE_DEFINE[key] },
|
|
461
|
-
// { role: 'user', content: QUESTION_PROMPT[key].replace('@ITEMCOUNT@', itemCount).replace('@CONTENT@', arrContent.slice(0, 1)[0]) },
|
|
462
|
-
// // { role: 'user', content:`出题材料:${arrContent.slice(0, 1)[0]}`}
|
|
463
|
-
// ]
|
|
464
|
-
// // subarray.unshift()
|
|
465
|
-
// console.log('subarray', subarray)
|
|
466
|
-
// let result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
467
|
-
// ///如果请求发生了网络错误(不是内容合规问题),则再重试一次,如果任然有错则放弃
|
|
468
|
-
// if (!result.successed && result.error != 'content_filter') {
|
|
469
|
-
// console.log('network error,retry onemore time')
|
|
470
|
-
// result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
471
|
-
// }
|
|
472
|
-
// console.log('subarray returned', result.successed)
|
|
473
|
-
// if (result.successed && result.message) {
|
|
474
|
-
// ///以下再次验证好像没有什么效果,暂时取消
|
|
475
|
-
// // subarray.push(
|
|
476
|
-
// // {role: 'assistant', content: result.message[0].message.content},
|
|
477
|
-
// // {role: 'user', content:`请将上述结果,结合出题材料再次进行验证,结果必须满足问题对应的正确答案必须在出题材料中完整且一字不差连续出现,如出现不满足的情况,请再次修正直到问题对应的正确答案必须在出题材料中完整且一字不差连续出现为止,最终按照结果要求输出后输出`
|
|
478
|
-
// // })
|
|
479
|
-
// // console.log('subarray reask', subarray)
|
|
480
|
-
// // result = await this.chatRequest(subarray, { replyCounts: 1 }, axiosOption);
|
|
481
|
-
// // if (result.successed && result.message){
|
|
482
|
-
// //console.log('paper result', key, result.message.length)
|
|
483
|
-
// let pickedQuestions = await this.pickUpQuestions(result.message, itemCount, key, ITEM_SCORE[key]);
|
|
484
|
-
// if (pickedQuestions.length) {
|
|
485
|
-
// ///对外发送检出题目的信号
|
|
486
|
-
// this.emit('parseout', { type: 'question', name: key, items: pickedQuestions })
|
|
487
|
-
// paperReturned[key] = paperReturned[key].concat(pickedQuestions);
|
|
488
|
-
// remainCount[key] = remainCount[key] - pickedQuestions.length;
|
|
489
|
-
// totalscore = totalscore + pickedQuestions.length * ITEM_SCORE[key];
|
|
490
|
-
|
|
491
|
-
// console.log('start sleep', new Date().getTime())
|
|
492
|
-
// await this.sleep(30);
|
|
493
|
-
// console.log('do check', new Date().getTime())
|
|
494
|
-
// let allItemInfo = pickedQuestions.map((q: QuestionItem, idx: number) => { return `\r\n(${idx + 1}).` + q.question + '\n选项\n' + q.choice?.map(c => { return `(${c.id}) ${c.content}`; }).join('\n'); }).join('\n');
|
|
495
|
-
// console.log('allItemInfo', allItemInfo)
|
|
496
|
-
// let prmopt = [{
|
|
497
|
-
// role: 'user',
|
|
498
|
-
// content: `请根据内容,完成以下题目作答,最终结果标准JSON数组并按照[{"seq":serialnumber,"answer":[正确答案]}]的结构输出。
|
|
499
|
-
// \n内容:"${arrContent.slice(0, 1)[0]}"
|
|
500
|
-
// \r\n${QUESTION_TYPE_TITLE[key]}\r\n${allItemInfo}`
|
|
501
|
-
// }];
|
|
502
|
-
// let checkResult = await this.chatRequest(prmopt, { replyCounts: 1 }, axiosOption);
|
|
503
|
-
// if (checkResult.successed && checkResult.message) {
|
|
504
|
-
// console.log("出题试卷答案:", pickedQuestions.map((oq, on) => {
|
|
505
|
-
// return `(${on + 1}).` + oq.question + ' 答案 : ' + oq.answer + ''
|
|
506
|
-
// }).join('\n'))
|
|
507
|
-
// console.log("复查后的答案:", checkResult.message[0].message.content.trim())
|
|
508
|
-
// }
|
|
509
|
-
// await this.sleep(30);
|
|
510
|
-
// }
|
|
511
|
-
// }
|
|
512
|
-
// //subarray.splice(0, 1); ///把第一个角色定位的问法删除
|
|
513
|
-
// // subarray.splice(subarray.length - 1, 1); ///把第一个角色定位的问法删除
|
|
514
|
-
// }
|
|
515
|
-
// }
|
|
516
|
-
|
|
517
|
-
// ////删除已经处理的文本
|
|
518
|
-
// arrContent.splice(0, MESSAGE_LENGTH);
|
|
519
|
-
// }
|
|
520
|
-
// console.log('parseover')
|
|
521
|
-
// ///发出信号,解析完毕
|
|
522
|
-
// this.emit('parseover', { type: 'question', items: paperReturned })
|
|
523
|
-
// return { successed: true, score: totalscore, paper: paperReturned }
|
|
524
|
-
// }
|
|
525
|
-
// /**
|
|
526
|
-
// * 从答复中得到题目
|
|
527
|
-
// * @param {*} result
|
|
528
|
-
// *
|
|
529
|
-
// */
|
|
530
|
-
// protected async pickUpQuestions(result: Array<any>, count: number, questiontype: string, score: number = 1): Promise<Array<QuestionItem>> {
|
|
531
|
-
// if (!result[0]?.message?.content) return [];
|
|
532
|
-
// let answerString = result[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
533
|
-
// if (answerString === '[]') return [];
|
|
534
|
-
// let jsonObj = this.fixedJsonString(answerString);
|
|
535
|
-
// ////修复的结果无法用程序修复,请求GPT来分析修复一下这个结果
|
|
536
|
-
// if (!jsonObj.length) {
|
|
537
|
-
// console.log('Json格式有误,再次进行GPT修复', answerString);
|
|
538
|
-
// let fixedAsk = [
|
|
539
|
-
// { role: 'system', content: '角色扮演:假设你是一位高级JSON数据分析师' },
|
|
540
|
-
// { role: 'user', content: `请分析以下内容,严格按照${QUESTION_PROMPT_FIXED[questiontype]}的标准JSON数组结构输出。如果内容不足以提取问题和答案,请直接输出JSON空数组,无需提供参考。` },
|
|
541
|
-
// { role: 'user', content: answerString },
|
|
542
|
-
// ]
|
|
543
|
-
// let fixedJsonResult: any = await this.chatRequest(fixedAsk, { replyCounts: 1 }, {})
|
|
544
|
-
// if (fixedJsonResult.successed) {
|
|
545
|
-
// answerString = fixedJsonResult.message[0].message.content.trim().replace(/\t|\n|\v|\r|\f/g, '');
|
|
546
|
-
// jsonObj = this.fixedJsonString(answerString);
|
|
547
|
-
// }
|
|
548
|
-
// console.log('Json GPT修复 结果', fixedJsonResult.successed, answerString)
|
|
549
|
-
// if (!jsonObj.length) return []
|
|
550
|
-
// }
|
|
551
|
-
// let returnItems: QuestionItem[] = [];
|
|
552
|
-
// try {
|
|
553
|
-
// // let jsonObj = JSON.parse(answerString);
|
|
554
|
-
// returnItems = jsonObj.map((questionitem: any) => {
|
|
555
|
-
// console.log('answer item from jsonObj', questionitem);
|
|
556
|
-
// if (questionitem.choice && Array.isArray(questionitem.choice) && questiontype != 'completion') {
|
|
557
|
-
// questionitem.fullanswer = (questionitem.answer + '').replace(/,|[^ABCDE]/g, '');
|
|
558
|
-
// questionitem.score = score;
|
|
559
|
-
// if (questionitem.choice) {
|
|
560
|
-
// questionitem.choice = questionitem.choice.map((item: string, index: number) => {
|
|
561
|
-
// let seqNo = 'ABCDEFG'[index]; //String.fromCharCode(65 + index);
|
|
562
|
-
// let correctReg = new RegExp(`${seqNo}.|${seqNo}`, 'ig')
|
|
563
|
-
// return {
|
|
564
|
-
// id: seqNo,
|
|
565
|
-
// content: (item + '').replace(correctReg, '').trim(),
|
|
566
|
-
// iscorrect: (questionitem.fullanswer || '').indexOf(seqNo) >= 0 ? 1 : 0
|
|
567
|
-
// }
|
|
568
|
-
// })
|
|
569
|
-
// }
|
|
570
|
-
// ///如果是非判断题,题目的选项数量小于2 ,则无效
|
|
571
|
-
// ///如果是判断题,题目的选项必须=2
|
|
572
|
-
// if (!questionitem.choice || (questiontype != 'trueorfalse' && questionitem.choice.length < 3) || (questiontype == 'trueorfalse' && questionitem.choice.length != 2)) {
|
|
573
|
-
// return null;
|
|
574
|
-
// }
|
|
575
|
-
|
|
576
|
-
// }
|
|
577
|
-
// switch (questiontype) {
|
|
578
|
-
// case 'singlechoice':
|
|
579
|
-
// questionitem.answer = (questionitem.answer + '').replace(/,|[^ABCDEFG]/g, '').split('').slice(0, 1);
|
|
580
|
-
// break;
|
|
581
|
-
// case 'multiplechoice':
|
|
582
|
-
// questionitem.answer = Array.from(new Set((questionitem.answer + '').replace(/,|[^ABCDEFG]/g, '').split(''))).sort();
|
|
583
|
-
// break;
|
|
584
|
-
// case 'trueorfalse':
|
|
585
|
-
// let rightItem = questionitem.choice.find((x: any) => { return x.iscorrect == 1 });
|
|
586
|
-
// questionitem.answer = [rightItem?.id || 'Z']; //[(questionitem.answer + '').indexOf('正确') >= 0 ? 'A' : 'B']
|
|
587
|
-
// break;
|
|
588
|
-
// }
|
|
589
|
-
// ///单选题验证
|
|
590
|
-
// if (questiontype == 'singlechoice') {
|
|
591
|
-
// let rightAnswer = questionitem.choice ? questionitem.choice.filter((item: { iscorrect: number; }) => { return item.iscorrect === 1 }) : [];
|
|
592
|
-
// ///单选题的正确选项大于了1个
|
|
593
|
-
// if (rightAnswer.length != 1 || !questionitem.answer || questionitem.answer.length !== 1) return null;
|
|
594
|
-
// ///正确选项和答案不一致
|
|
595
|
-
// if (rightAnswer[0].id.toUpperCase() != (questionitem.answer[0] || '').toUpperCase()) return null;
|
|
596
|
-
// }
|
|
597
|
-
// ///多选题验证
|
|
598
|
-
// if (questiontype == 'multiplechoice') {
|
|
599
|
-
// let rightAnswer = questionitem.choice ? questionitem.choice.filter((item: { iscorrect: number; }) => { return item.iscorrect === 1 }) : [];
|
|
600
|
-
// ///单选题的正确选项大于了1个
|
|
601
|
-
// if (rightAnswer.length === 0 || !questionitem.answer || questionitem.answer.length === 0) return null;
|
|
602
|
-
// }
|
|
603
|
-
// ///判断题验证:防止没有答案的
|
|
604
|
-
// if (questiontype == 'trueorfalse' && !questionitem.answer.length) return null;
|
|
605
|
-
|
|
606
|
-
// return questionitem;
|
|
607
|
-
// })
|
|
608
|
-
// } catch (err) {
|
|
609
|
-
// console.log('error happened:', err);
|
|
610
|
-
// }
|
|
611
|
-
// return returnItems.filter(i => { return i != null; }).slice(0, count);
|
|
612
|
-
// }
|
|
613
|
-
// /**
|
|
614
|
-
// * 验证JSON字符串是否是真正可转换为JSON的合法格式
|
|
615
|
-
// * 这里只能做一个最简单的处理,就是用两端的符号
|
|
616
|
-
// * @param jsonstr
|
|
617
|
-
// */
|
|
618
|
-
// protected fixedJsonString(jsonstr: string): any[] {
|
|
619
|
-
// console.log('original json string:', jsonstr)
|
|
620
|
-
// ///检查返回的是不是一个数组对象(我们需要的是数组对象)
|
|
621
|
-
// let firstBracketSymbol = jsonstr.indexOf("["); ////必须过滤出来数组
|
|
622
|
-
// let lastBracketSymbol = jsonstr.lastIndexOf("]");
|
|
623
|
-
// ///第一个花括号出现的位置,如果花括号出现的位置早于 [ ,则默认返回的对象不是一个数组,仅仅是一个对象,
|
|
624
|
-
// ///则需要我们用中括号包住
|
|
625
|
-
// let firstBraceSymbol = jsonstr.indexOf("{");
|
|
626
|
-
// let lastBraceSymbol = jsonstr.lastIndexOf("}");
|
|
627
|
-
// ///返回的不是一个数组结构的,只是一个{},我们帮他完成数组拼接
|
|
628
|
-
// if (firstBraceSymbol >= 0 &&
|
|
629
|
-
// firstBraceSymbol < (firstBracketSymbol >= 0 ? firstBracketSymbol : 1000) &&
|
|
630
|
-
// lastBraceSymbol > firstBraceSymbol &&
|
|
631
|
-
// lastBraceSymbol >= 0 && lastBraceSymbol > lastBracketSymbol) {
|
|
632
|
-
|
|
633
|
-
// jsonstr = '[' + jsonstr.substr(firstBraceSymbol, lastBraceSymbol - firstBraceSymbol + 1); +']';
|
|
634
|
-
// firstBracketSymbol = 0;
|
|
635
|
-
// lastBracketSymbol = jsonstr.length - 1;
|
|
636
|
-
// }
|
|
637
|
-
// else if (firstBracketSymbol < 0 || lastBracketSymbol < 0 || lastBracketSymbol <= firstBracketSymbol) {
|
|
638
|
-
// return [];
|
|
639
|
-
// }
|
|
640
|
-
// jsonstr = jsonstr.substr(firstBracketSymbol, lastBracketSymbol - firstBracketSymbol + 1);
|
|
641
|
-
// ///尽量处理一些能够一眼识别出来的JSON错误
|
|
642
|
-
// jsonstr = jsonstr.replace(/}{/g, '},{');
|
|
643
|
-
// let mutilitems = jsonstr.split('][');
|
|
644
|
-
// ///确实存在多个数组拼接在一起,中间没有逗号隔开的了
|
|
645
|
-
// let retObject: any[] = [];
|
|
646
|
-
// for (let str of mutilitems) {
|
|
647
|
-
// if (!str.startsWith('[')) str = '[' + str;
|
|
648
|
-
// if (!str.endsWith(']')) str = str + ']';
|
|
649
|
-
// // console.log('json str', str)
|
|
650
|
-
// try {
|
|
651
|
-
// let jsonObj = eval(str);
|
|
652
|
-
// retObject = retObject.concat(jsonObj);
|
|
653
|
-
// } catch (err) {
|
|
654
|
-
// console.log('json error', str)
|
|
655
|
-
// }
|
|
656
|
-
// }
|
|
657
|
-
// return retObject;
|
|
658
|
-
// }
|
|
659
|
-
|
|
660
|
-
// /**
|
|
661
|
-
// * 将一段很长的文本,按1024长度来划分到多个中
|
|
662
|
-
// * @param {*} content
|
|
663
|
-
// */
|
|
664
|
-
// protected splitLongText(content: string, len = SECTION_LENGTH): string[] { //Array<ChatCompletionRequestMessage> {
|
|
665
|
-
// let start = 0, message: string[] = [], length = content.length;
|
|
666
|
-
// while (start < length) {
|
|
667
|
-
|
|
668
|
-
// let realLength: number = len;
|
|
669
|
-
// ////以句号或引号进行分段,不要随意截取
|
|
670
|
-
// for (let i = start + len; i >= start; i--) {
|
|
671
|
-
// if (/[。”"??]/.test(content[i] + '')) {
|
|
672
|
-
// realLength = i - start + 1;
|
|
673
|
-
// break;
|
|
674
|
-
// }
|
|
675
|
-
// }
|
|
676
|
-
// const subtext = content.substr(start, realLength).replace(/\s+/g, "").replace(/\t|\n|\v|\r|\f/g, ' ')
|
|
677
|
-
// if (subtext) message.push(subtext); //message.push({ role: 'user', content: subtext })
|
|
678
|
-
// start += realLength || len;
|
|
679
|
-
// }
|
|
680
|
-
// return message;
|
|
681
|
-
// }
|
|
682
|
-
|
|
683
|
-
// sleep(second: number) {
|
|
684
|
-
// return new Promise(resolve => {
|
|
685
|
-
// setTimeout(() => { resolve(true) }, second * 1000)
|
|
686
|
-
// })
|
|
687
|
-
// }
|
|
688
|
-
|
|
689
153
|
}
|