@3-/aiapi 0.1.52 → 0.1.53

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
@@ -4,29 +4,12 @@ import fmtSeg from './fmtSeg.js';
4
4
  import partition from './partition.js';
5
5
 
6
6
  export default async(chat, txt) => {
7
- var gen, i, pli, result, sum, tmp;
7
+ var pli;
8
8
  if (!txt) {
9
9
  return [];
10
10
  }
11
11
  pli = (await partition(chat, txt));
12
- result = [];
13
- sum = 0;
14
- tmp = [];
15
- gen = () => {
16
- result.push(fmtSeg(chat, tmp.join('\n')));
17
- };
18
- for (i of pli) {
19
- tmp.push(i);
20
- sum += i.length;
21
- if (sum > 3000) {
22
- console.log(sum);
23
- gen();
24
- sum = 0;
25
- tmp = [];
26
- }
27
- }
28
- if (tmp.length) {
29
- gen();
30
- }
31
- return Promise.all(result);
12
+ return Promise.all(pli.map(async([title, li]) => {
13
+ return [title, (await fmtSeg(chat, li.join('\n')))];
14
+ }));
32
15
  };
package/fmtJsonMd.js CHANGED
@@ -7,15 +7,17 @@ txtFmt = (txt) => {
7
7
  return TxtLi(txt).join('\n\n');
8
8
  };
9
9
 
10
- export default (json_li) => {
11
- var li, md_li, x, y, 答, 话题, 问, 问答, 题;
10
+ export default (title_json_li) => {
11
+ var li, md_li, title, x, y, z, 答, 话题, 问, 问答, 题;
12
12
  md_li = [];
13
- for (li of json_li) {
14
- for (x of li) {
15
- ({话题, 问答} = x);
13
+ for (x of title_json_li) {
14
+ [title, li] = x;
15
+ md_li.push('# ' + title);
16
+ for (y of li) {
17
+ ({话题, 问答} = y);
16
18
  md_li.push('## ' + 话题);
17
- for (y of 问答) {
18
- ({题, 问, 答} = y);
19
+ for (z of 问答) {
20
+ ({题, 问, 答} = z);
19
21
  md_li.push('### ' + 题 + '\n问: ' + txtFmt(问) + '\n答: ' + txtFmt(答));
20
22
  }
21
23
  }
package/fmtSeg.js CHANGED
File without changes
package/gemini.js CHANGED
@@ -3,7 +3,7 @@ import sleep from '@3-/sleep';
3
3
 
4
4
  import merge from 'lodash-es/merge.js';
5
5
 
6
- export default (token_li, model = 'gemini-2.5-pro') => {
6
+ export default (token_li, model = 'gemini-3-pro-preview') => {
7
7
  var _NEXT_TOKEN, _nextToken, nextToken;
8
8
  token_li.sort(() => {
9
9
  return Math.random() - 0.5;
@@ -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, json, message, parts, r, retryed, status;
24
+ var body, err, error, json, message, parts, r, retryed, status, token, warn;
25
25
  body = {
26
26
  contents: [
27
27
  {
@@ -51,11 +51,15 @@ export default (token_li, model = 'gemini-2.5-pro') => {
51
51
  body = JSON.stringify(body);
52
52
  retryed = token_li.length;
53
53
  while (true) {
54
+ token = nextToken();
55
+ warn = (...args) => {
56
+ return console.log('\n→ ' + token + '\n', ...args, '\n');
57
+ };
54
58
  try {
55
59
  // console.log 'https://generativelanguage.googleapis.com/v1beta/models/'+model+':generateContent'
56
60
  r = (await fetch('https://generativelanguage.googleapis.com/v1beta/models/' + model + ':generateContent', {
57
61
  headers: {
58
- 'X-goog-api-key': nextToken(),
62
+ 'X-goog-api-key': token,
59
63
  'Content-Type': 'application/json'
60
64
  },
61
65
  method: 'POST',
@@ -64,7 +68,7 @@ export default (token_li, model = 'gemini-2.5-pro') => {
64
68
  ({status} = r);
65
69
  } catch (error1) {
66
70
  err = error1;
67
- console.error(err);
71
+ warn(err);
68
72
  await sleep(1000);
69
73
  continue;
70
74
  }
@@ -73,9 +77,9 @@ export default (token_li, model = 'gemini-2.5-pro') => {
73
77
  if (status === 429) {
74
78
  try {
75
79
  ({error} = JSON.parse(text));
76
- console.warn(error.status, error.message);
80
+ warn(error.status, error.message);
77
81
  } catch (error1) {
78
- console.warn(text);
82
+ warn(text);
79
83
  }
80
84
  continue;
81
85
  }
@@ -84,9 +88,9 @@ export default (token_li, model = 'gemini-2.5-pro') => {
84
88
  ({
85
89
  error: {message}
86
90
  } = JSON.parse(text));
87
- console.warn(status, message);
91
+ warn(status, message);
88
92
  } catch (error1) {
89
- console.warn(text);
93
+ warn(text);
90
94
  }
91
95
  if (status !== 500) {
92
96
  await sleep(1000);
@@ -98,7 +102,7 @@ export default (token_li, model = 'gemini-2.5-pro') => {
98
102
  json = (await r.json());
99
103
  ({parts} = json.candidates[0].content);
100
104
  if (!parts) {
101
- console.warn('miss parts', JSON.stringify(json, null, 2));
105
+ warn('miss parts', JSON.stringify(json, null, 2));
102
106
  continue;
103
107
  }
104
108
  text = parts[0].text;
@@ -107,7 +111,7 @@ export default (token_li, model = 'gemini-2.5-pro') => {
107
111
  return JSON.parse(text);
108
112
  } catch (error1) {
109
113
  err = error1;
110
- console.error(body, text, err);
114
+ warn(body, text, err);
111
115
  if (--retryed <= 0) {
112
116
  throw err;
113
117
  }
package/package.json CHANGED
@@ -1,30 +1 @@
1
- {
2
- "name": "@3-/aiapi",
3
- "version": "0.1.52",
4
- "repository": {
5
- "type": "git",
6
- "url": "git+https://atomgit.com/i18n/lib.git"
7
- },
8
- "homepage": "https://atomgit.com/i18n/lib/tree/dev/aiapi",
9
- "author": "i18n.site@gmail.com",
10
- "license": "MulanPSL-2.0",
11
- "exports": {
12
- ".": "./lib.js",
13
- "./*": "./*"
14
- },
15
- "files": [
16
- "./*"
17
- ],
18
- "devDependencies": {
19
- "@3-/read": "^0.1.4"
20
- },
21
- "type": "module",
22
- "dependencies": {
23
- "@3-/retry": "^0.0.2",
24
- "@3-/sleep": "^0.0.4",
25
- "@3-/txt_li": "^0.1.5",
26
- "@3-/utf8": "^0.0.4",
27
- "lodash-es": "^4.17.21"
28
- },
29
- "scripts": {}
30
- }
1
+ {"name":"@3-/aiapi","version":"0.1.53","repository":{"type":"git","url":"git+https://atomgit.com/i18n/lib.git"},"homepage":"https://atomgit.com/i18n/lib/tree/dev/aiapi","author":"i18n.site@gmail.com","license":"MulanPSL-2.0","exports":{".":"./lib.js","./*":"./*"},"files":["./*"],"devDependencies":{"@3-/read":"^0.1.4"},"scripts":{},"type":"module","dependencies":{"@3-/retry":"^0.0.2","@3-/sleep":"^0.0.4","@3-/txt_li":"^0.1.5","@3-/utf8":"^0.0.4","lodash-es":"^4.17.21"}}
package/partition.js CHANGED
@@ -1,46 +1,46 @@
1
- import seg from "./seg.js"
2
- import txtLi from "@3-/txt_li"
1
+ import seg from "./seg.js";
2
+ import txtLi from "@3-/txt_li";
3
3
 
4
4
  const partition = async (li, title_number, remain_seg) => {
5
- let title = ""
6
- const order = title_number.sort((a, b) => a[1] - b[1])
7
- const result = []
8
- let start = 0
5
+ const order = title_number.sort((a, b) => a[1] - b[1]);
6
+ const result = [];
9
7
 
10
- for (const [t, row] of order) {
11
- console.log(t)
12
- const index = row - 1
13
- result.push(title + li.slice(start, index).join("\n"))
14
- title = "# " + t + "\n"
15
- start = index
16
- }
8
+ let title,
9
+ row,
10
+ pre = 0;
11
+ for ([title, row] of order) {
12
+ console.log(title);
13
+ result.push([title, li.slice(pre, row)]);
14
+ pre = row;
15
+ }
17
16
 
18
- if (start < li.length) {
19
- li = li.slice(start)
20
- const remain = li.join("\n")
21
- if (remain.length > 6000) {
22
- return result.concat(await remain_seg(li))
23
- } else {
24
- result.push(title + remain)
25
- }
26
- }
17
+ if (pre < li.length) {
18
+ li = li.slice(pre);
19
+ if (li.join("\n").length > 3000) {
20
+ return result.concat(await remain_seg(li));
21
+ } else {
22
+ result.at(-1)[1].push(...li);
23
+ }
24
+ }
27
25
 
28
- return result
29
- }
30
-
31
- const repartition = (chat, len) => async (li) => {
32
- if (li.length == len) {
33
- return li
34
- }
35
- if (li.length == 1) {
36
- li = li[0].split("。")
37
- }
38
- return partition(li, await seg(chat, li), (i) =>
39
- repartition(chat, li.length)(i),
40
- )
41
- }
26
+ return result;
27
+ },
28
+ repartition = (chat, len) => async (li) => {
29
+ if (
30
+ // 这表示不可再分
31
+ li.length == len
32
+ ) {
33
+ return li;
34
+ }
35
+ if (li.length == 1) {
36
+ li = li[0].split("。");
37
+ }
38
+ return partition(li, await seg(chat, li), (i) =>
39
+ repartition(chat, li.length)(i),
40
+ );
41
+ };
42
42
 
43
43
  export default async (chat, txt) => {
44
- const li = txtLi(txt)
45
- return partition(li, await seg(chat, li), repartition(chat, li.length))
46
- }
44
+ const li = txtLi(txt);
45
+ return partition(li, await seg(chat, li), repartition(chat, li.length));
46
+ };
package/seg.js CHANGED
@@ -20,15 +20,11 @@ export default async(chat, txt_li) => {
20
20
  type: TYPE.STRING
21
21
  },
22
22
  行: {
23
- description: '该章首行行号',
23
+ description: '该章最后一行的行号',
24
24
  type: TYPE.INTEGER
25
- },
26
- 文: {
27
- description: '该章首行提问的原文',
28
- type: TYPE.STRING
29
25
  }
30
26
  },
31
- required: ['题', '行', '文']
27
+ required: ['题', '行']
32
28
  }
33
29
  }, '你是专业资深的秘书'));
34
30
  return split_li.map((i) => {