@3-/aiapi 0.1.45 → 0.1.47

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/fmtJson.js CHANGED
@@ -6,15 +6,12 @@ import partition from './partition.js';
6
6
  import seg from './seg.js';
7
7
 
8
8
  export default async(chat, txt) => {
9
- var gen, i, pli, result, split_li, sum, tmp, txt_li;
9
+ var gen, i, pli, result, sum, tmp, txt_li;
10
10
  if (!txt) {
11
11
  return [];
12
12
  }
13
13
  txt_li = txt.split('\n');
14
- split_li = (await seg(chat, txt_li));
15
- pli = partition(txt_li, split_li.map((i) => {
16
- return [i.题, i.行];
17
- }));
14
+ pli = (await partition(chat, txt_li, (await seg(chat, txt_li))));
18
15
  result = [];
19
16
  sum = 0;
20
17
  tmp = [];
package/gemini.js CHANGED
@@ -21,7 +21,7 @@ export default (token_li, model = 'gemini-2.5-pro') => {
21
21
  return _NEXT_TOKEN.next().value;
22
22
  };
23
23
  return async(text, schema, system, option) => {
24
- var body, err, error, message, parts, r, retryed, status;
24
+ var body, err, error, json, message, parts, r, retryed, status;
25
25
  body = {
26
26
  contents: [
27
27
  {
@@ -52,6 +52,7 @@ export default (token_li, model = 'gemini-2.5-pro') => {
52
52
  retryed = token_li.length;
53
53
  while (true) {
54
54
  try {
55
+ // console.log 'https://generativelanguage.googleapis.com/v1beta/models/'+model+':generateContent'
55
56
  r = (await fetch('https://generativelanguage.googleapis.com/v1beta/models/' + model + ':generateContent', {
56
57
  headers: {
57
58
  'X-goog-api-key': nextToken(),
@@ -94,25 +95,26 @@ export default (token_li, model = 'gemini-2.5-pro') => {
94
95
  }
95
96
  throw new Error(text);
96
97
  }
97
- r = ((await r.json())).candidates[0];
98
+ json = (await r.json());
99
+ ({parts} = json.candidates[0].content);
100
+ if (!parts) {
101
+ console.warn('miss parts', JSON.stringify(json, null, 2));
102
+ continue;
103
+ }
104
+ text = parts[0].text;
98
105
  if (schema) {
99
106
  try {
100
- ({parts} = r.content);
101
- if (!parts) {
102
- console.warn('miss parts', r.content);
103
- continue;
104
- }
105
- r = JSON.parse(parts[0].text);
107
+ return JSON.parse(text);
106
108
  } catch (error1) {
107
109
  err = error1;
108
- console.error(body, r, err);
110
+ console.error(body, text, err);
109
111
  if (--retryed <= 0) {
110
112
  throw err;
111
113
  }
112
114
  continue;
113
115
  }
114
116
  }
115
- return r;
117
+ return text;
116
118
  }
117
119
  };
118
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@3-/aiapi",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://atomgit.com/i18n/lib.git"
package/partition.js CHANGED
@@ -1,27 +1,33 @@
1
- export default (lines, title_number) => {
2
- let title = ''
3
- // 对行号进行升序排序
4
- const order = title_number.sort((a, b) => a[1] - b[1]);
1
+ import seg from "./seg.js"
5
2
 
6
- // 存储最终结果的数组
7
- const result = [];
8
- // 追踪上一个分割点的索引
9
- let start = 0;
3
+ const partition = async (lines, title_number, remain_seg) => {
4
+ let title = ""
5
+ const order = title_number.sort((a, b) => a[1] - b[1])
6
+ const result = []
7
+ let start = 0
10
8
 
11
- // 遍历排序后的行号
12
- for (const [t, row] of order) {
13
- console.log(t)
14
- // 将行号转换为从0开始的数组索引
15
- const index = row - 1;
16
- result.push(title + lines.slice(start, index).join('\n'));
17
- title = '# ' + t + '\n'
18
- start = index;
19
- }
9
+ for (const [t, row] of order) {
10
+ console.log(t)
11
+ const index = row - 1
12
+ result.push(title + lines.slice(start, index).join("\n"))
13
+ title = "# " + t + "\n"
14
+ start = index
15
+ }
20
16
 
21
- // 添加从最后一个分割点到末尾的剩余文本
22
- if (start < lines.length) {
23
- result.push(title + lines.slice(start).join('\n'));
24
- }
17
+ if (start < lines.length) {
18
+ lines = lines.slice(start)
19
+ const remain = lines.join("\n")
20
+ if (remain.length > 6000) {
21
+ return result.concat(await remain_seg(lines))
22
+ } else {
23
+ result.push(title + remain)
24
+ }
25
+ }
25
26
 
26
- return result;
27
- };
27
+ return result
28
+ }
29
+
30
+ export default (chat, lines, title_number) =>
31
+ partition(lines, title_number, async (li) =>
32
+ partition(li, await seg(chat, li), (i) => i),
33
+ )
package/seg.js CHANGED
@@ -8,7 +8,7 @@ export default async(chat, txt_li) => {
8
8
  输出章节标题和每章首个问题的行号及原文,输出格式为JSON数组:\n` + txt_li.map((i, pos) => {
9
9
  return (pos + 1) + '\t' + i.trim();
10
10
  }).join('\n');
11
- return split_li = (await chat(提示词, {
11
+ split_li = (await chat(提示词, {
12
12
  type: TYPE.ARRAY,
13
13
  description: '章节和行号的列表',
14
14
  minItems: 1,
@@ -31,4 +31,7 @@ export default async(chat, txt_li) => {
31
31
  required: ['题', '行', '文']
32
32
  }
33
33
  }, '你是专业资深的秘书'));
34
+ return split_li.map((i) => {
35
+ return [i.题, i.行];
36
+ });
34
37
  };