bjchy-udg-sdk 1.8.9 → 1.8.10
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 +20 -1
- package/package.json +1 -1
- package/src/ai.js +21 -10
package/README.md
CHANGED
|
@@ -592,13 +592,32 @@ curl -X POST https://udg.vule.cn/api/execute -H 'X-Admin-Key: <ADMIN_KEY>' -H 'C
|
|
|
592
592
|
|
|
593
593
|
`keys` 也可写成 `[{"name":"street","direction":1}]`;方向支持 `1/-1/2d/2dsphere/text/hashed`;不传 `name` 时按 MongoDB 惯例生成索引名;重复创建幂等。`dropCollection` 删非空集合必须显式带 `force:true`(否则报错并告知集合内文档数)。
|
|
594
594
|
|
|
595
|
+
## AI 模型选择
|
|
596
|
+
|
|
597
|
+
登录后调用 `client.ai.listModels()`,获取 `{ defaultModel, models }`。`models` 是网关开放的模型 ID 或 AIGATE 别名数组,包含默认模型,可直接作为选择器的值。租户须开启 `aiEnabled`。获取列表不占用 AI token 配额。
|
|
598
|
+
|
|
599
|
+
```js
|
|
600
|
+
const { defaultModel, models } = await client.ai.listModels()
|
|
601
|
+
// 用 models 填充选择器,用 defaultModel 标记默认项。
|
|
602
|
+
// selectedModel 是用户选中的模型 ID 或别名,未选择时传 undefined 或省略 model。
|
|
603
|
+
const { text, model } = await client.ai.complete({
|
|
604
|
+
purpose: 'summarize',
|
|
605
|
+
prompt: '把这段内容概括成一句话:本周完成了三项整改。',
|
|
606
|
+
model: selectedModel
|
|
607
|
+
})
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
`complete`、`analyzeDocument`、`nlQuery` 及 `ask(question, options)` 的选项均支持 `model`。省略时使用网关默认模型 DeepSeek V4 Flash(`deepseek/deepseek-v4-flash`,可由网关配置调整)。已知网关开放的模型 ID 或 AIGATE 别名(如 `chat-fast`)时,可直接填写 `model`,无需先调用 `listModels()`。传入清单外的值会收到 `INVALID_PARAM`。`complete`、`analyzeDocument` 和 `nlQuery` 返回 `model`。问数的计划、重试和解读会使用同一个模型选项,响应中的 `model` 为生成最终计划的模型。`ask` 按 `format` 返回数字、行数组或摘要字符串。
|
|
611
|
+
|
|
612
|
+
HTTP 使用 Bearer access token,向 `POST /api/ai/models` 发送 JSON `{ appid }`。`POST /api/ai/complete` 和 `/api/ai/nl-query` 的 JSON 请求及 `POST /api/ai/document` 的 multipart 表单均可传 `model`。
|
|
613
|
+
|
|
595
614
|
## 文档上传、转换和 AI 解析
|
|
596
615
|
|
|
597
616
|
复用已登录的 `client`。Web 传 `File`,Node 传 `Buffer`、字节数组或 `Blob` 加 `filename`,微信小程序传 `filePath` 与选中文件的 `filename`。小程序须将 UDG 域名同时加入请求和上传合法域名。
|
|
598
617
|
|
|
599
618
|
```js
|
|
600
619
|
// 默认只返回解析结果,文件留存由调用方选择
|
|
601
|
-
const { json } = await client.ai.analyzeDocument({
|
|
620
|
+
const { json, model } = await client.ai.analyzeDocument({
|
|
602
621
|
file: selectedFile,
|
|
603
622
|
prompt: '提取事项、责任单位、截止日期和原文出处,返回 JSON 对象。',
|
|
604
623
|
json: true
|
package/package.json
CHANGED
package/src/ai.js
CHANGED
|
@@ -9,10 +9,15 @@ class AiNamespace {
|
|
|
9
9
|
this._ctx = ctx
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/** @returns {Promise<{defaultModel: string, models: string[]}>} 网关允许当前租户选择的模型 ID 或别名。 */
|
|
13
|
+
listModels() {
|
|
14
|
+
return post(this._ctx, '/api/ai/models')
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
/**
|
|
13
18
|
* 模板化补全。
|
|
14
|
-
* @param {object} p { purpose: 'summarize'|'explain'|'classify', prompt?|messages?, json? }
|
|
15
|
-
* @returns {Promise<{text?, json?, usage}>}
|
|
19
|
+
* @param {object} p { purpose: 'summarize'|'explain'|'classify', prompt?|messages?, json?, model? }
|
|
20
|
+
* @returns {Promise<{text?, json?, usage, model}>}
|
|
16
21
|
*/
|
|
17
22
|
complete(p) {
|
|
18
23
|
const o = p || {}
|
|
@@ -20,38 +25,44 @@ class AiNamespace {
|
|
|
20
25
|
purpose: o.purpose,
|
|
21
26
|
prompt: o.prompt,
|
|
22
27
|
messages: o.messages,
|
|
23
|
-
json: o.json
|
|
28
|
+
json: o.json,
|
|
29
|
+
model: o.model
|
|
24
30
|
})
|
|
25
31
|
}
|
|
26
32
|
|
|
27
|
-
/** 上传并解析 DOCX/XLSX,填写 prompt,可选 sheet、json 和返回 syl URL 的选项。 */
|
|
28
|
-
analyzeDocument(options) {
|
|
33
|
+
/** 上传并解析 DOCX/XLSX,填写 prompt,可选 sheet、json、model 和返回 syl URL 的选项。 */
|
|
34
|
+
async analyzeDocument(options) {
|
|
35
|
+
const model = options?.model
|
|
36
|
+
if (model !== undefined && (typeof model !== 'string' || !model.trim())) {
|
|
37
|
+
throw new UdgRequestError({ errorCode: 'INVALID_PARAM', errorMessage: 'model 必须是非空的模型 ID 或别名' })
|
|
38
|
+
}
|
|
29
39
|
return postDocument(this._ctx, '/api/ai/document', options)
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
/**
|
|
33
43
|
* 自然语言问数(只读,自动执行)。
|
|
34
|
-
* @param {object} p { collection, question, db? }
|
|
35
|
-
* @returns {Promise<{plan, validation, data, summary, copyable}>}
|
|
44
|
+
* @param {object} p { collection, question, db?, model? }
|
|
45
|
+
* @returns {Promise<{plan, validation, data, summary, copyable, model}>}
|
|
36
46
|
*/
|
|
37
47
|
nlQuery(p) {
|
|
38
48
|
const o = p || {}
|
|
39
49
|
return post(this._ctx, '/api/ai/nl-query', {
|
|
40
50
|
db: o.db,
|
|
41
51
|
collection: o.collection,
|
|
42
|
-
question: o.question
|
|
52
|
+
question: o.question,
|
|
53
|
+
model: o.model
|
|
43
54
|
})
|
|
44
55
|
}
|
|
45
56
|
|
|
46
57
|
/**
|
|
47
58
|
* 一行问数:内部 nlQuery + 结果解析。
|
|
48
59
|
* @param {string} question 中文问题
|
|
49
|
-
* @param {object} opts { collection(必填), db?, format?: 'number'|'rows'|'summary'(默认 summary) }
|
|
60
|
+
* @param {object} opts { collection(必填), db?, model?, format?: 'number'|'rows'|'summary'(默认 summary) }
|
|
50
61
|
* @returns {Promise<number|Array|string>} format=number 时尽力解析出单个数字
|
|
51
62
|
*/
|
|
52
63
|
async ask(question, opts) {
|
|
53
64
|
const o = opts || {}
|
|
54
|
-
const r = await this.nlQuery({ collection: o.collection, db: o.db, question })
|
|
65
|
+
const r = await this.nlQuery({ collection: o.collection, db: o.db, question, model: o.model })
|
|
55
66
|
const data = r.data || {}
|
|
56
67
|
if (o.format === 'number') {
|
|
57
68
|
if (typeof data.total === 'number') return data.total
|