listpage-next-ai 0.0.256 → 0.0.259
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/cjs/index.d.ts +107 -24
- package/dist/cjs/index.js +418 -82
- package/package.json +18 -10
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,32 +1,115 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { AnyAnnotationRoot } from 'langchain';
|
|
2
|
+
import { BaseMessage } from 'langchain';
|
|
3
|
+
import { BaseMessageChunk } from 'langchain';
|
|
4
|
+
import { ChatOpenAICallOptions } from '@langchain/openai';
|
|
5
|
+
import { ChatOpenAICompletions } from '@langchain/openai';
|
|
6
|
+
import { LanguageModelLike } from '@langchain/core/language_models/base';
|
|
7
|
+
import { MessageStructure } from '@langchain/core/messages';
|
|
8
|
+
import { MessageType } from '@langchain/core/messages';
|
|
9
|
+
import { OpenAIClient } from '@langchain/openai';
|
|
10
|
+
import { ReactAgent } from 'langchain';
|
|
11
|
+
import { ResponseFormatUndefined } from 'langchain';
|
|
4
12
|
|
|
5
|
-
export declare
|
|
13
|
+
export declare interface AgentOptions {
|
|
14
|
+
features: {
|
|
15
|
+
database?: boolean;
|
|
16
|
+
knowledge?: boolean;
|
|
17
|
+
word?: boolean;
|
|
18
|
+
websearch?: boolean;
|
|
19
|
+
};
|
|
20
|
+
databaseOptions?: DatabaseAgentOptions;
|
|
21
|
+
knowledgeOptions?: KnowledgeAgentOptions;
|
|
22
|
+
wordOptions?: WordToolOptions;
|
|
23
|
+
websearchOptions?: {};
|
|
24
|
+
model: LanguageModelLike;
|
|
6
25
|
system_prompt: string;
|
|
26
|
+
tools?: any[];
|
|
27
|
+
middleware?: any[];
|
|
28
|
+
name?: string;
|
|
29
|
+
inject_current_time?: boolean;
|
|
30
|
+
max_iterations?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export declare class ChatVolcengine extends ChatOpenAICompletions<ChatOpenAICallOptions> {
|
|
34
|
+
static lc_name(): string;
|
|
35
|
+
_llmType(): string;
|
|
36
|
+
constructor(fields?: Partial<ChatVolcengineInput>);
|
|
37
|
+
protected _convertCompletionsDeltaToBaseMessageChunk(delta: Record<string, any>, rawResponse: OpenAIClient.ChatCompletionChunk, defaultRole?: 'function' | 'user' | 'system' | 'developer' | 'assistant' | 'tool'): BaseMessageChunk<MessageStructure, MessageType>;
|
|
38
|
+
protected _convertCompletionsMessageToBaseMessage(message: OpenAIClient.ChatCompletionMessage, rawResponse: OpenAIClient.ChatCompletion): BaseMessage<MessageStructure, MessageType>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
declare type ChatVolcengineInput = {
|
|
42
|
+
apiKey: string;
|
|
7
43
|
model: string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
44
|
+
temperature?: number;
|
|
45
|
+
streaming?: boolean;
|
|
46
|
+
configuration?: any;
|
|
47
|
+
timeout?: number;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
declare interface Column {
|
|
51
|
+
name: string;
|
|
52
|
+
description: string;
|
|
53
|
+
type: string;
|
|
54
|
+
enums?: {
|
|
55
|
+
key: string;
|
|
56
|
+
value: string;
|
|
57
|
+
}[] | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export declare function createReactAgent(options: AgentOptions): ReactAgent<ResponseFormatUndefined, undefined, AnyAnnotationRoot, any[]>;
|
|
61
|
+
|
|
62
|
+
declare interface DatabaseAgentOptions extends DatabaseToolOptions {
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
declare interface DatabaseToolOptions {
|
|
66
|
+
datasource: {
|
|
67
|
+
host: string;
|
|
68
|
+
port: number;
|
|
69
|
+
username: string;
|
|
70
|
+
password: string;
|
|
71
|
+
database: string;
|
|
72
|
+
};
|
|
73
|
+
datasets: {
|
|
74
|
+
name: string;
|
|
75
|
+
description: string;
|
|
76
|
+
columns: Column[];
|
|
77
|
+
}[];
|
|
78
|
+
onGenerateChart?: (options: {
|
|
79
|
+
sql: string;
|
|
80
|
+
title: string;
|
|
81
|
+
chartConfig: {
|
|
82
|
+
type: 'BAR_CHART' | 'LINE_CHART' | 'PIE_CHART';
|
|
83
|
+
dimension: string;
|
|
84
|
+
metric: {
|
|
85
|
+
name: string;
|
|
86
|
+
alias: string;
|
|
87
|
+
aggregate: string;
|
|
88
|
+
}[];
|
|
89
|
+
};
|
|
90
|
+
}) => Promise<string>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
declare interface KnowledgeAgentOptions extends KnowledgeToolOptions {
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
declare interface KnowledgeToolOptions {
|
|
97
|
+
knowledges: string[];
|
|
98
|
+
onRetrieveFragments: (index: string, query: string) => Promise<string>;
|
|
99
|
+
onRetrieveFullDocs: (index: string, title: string) => Promise<string>;
|
|
25
100
|
}
|
|
26
101
|
|
|
27
|
-
declare interface
|
|
28
|
-
|
|
29
|
-
|
|
102
|
+
declare interface WordToolOptions {
|
|
103
|
+
glossry: {
|
|
104
|
+
name: string;
|
|
105
|
+
description: string;
|
|
106
|
+
words: {
|
|
107
|
+
name: string;
|
|
108
|
+
description: string;
|
|
109
|
+
}[];
|
|
110
|
+
}[];
|
|
111
|
+
min_score?: number;
|
|
112
|
+
top_n?: number;
|
|
30
113
|
}
|
|
31
114
|
|
|
32
115
|
export { }
|
package/dist/cjs/index.js
CHANGED
|
@@ -24,10 +24,417 @@ var __webpack_require__ = {};
|
|
|
24
24
|
var __webpack_exports__ = {};
|
|
25
25
|
__webpack_require__.r(__webpack_exports__);
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
-
|
|
27
|
+
createReactAgent: ()=>createReactAgent,
|
|
28
|
+
ChatVolcengine: ()=>ChatVolcengine,
|
|
29
|
+
AgentOptions: ()=>types_namespaceObject.AgentOptions
|
|
28
30
|
});
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
var types_namespaceObject = {};
|
|
32
|
+
__webpack_require__.r(types_namespaceObject);
|
|
33
|
+
const external_langchain_namespaceObject = require("langchain");
|
|
34
|
+
const promise_namespaceObject = require("mysql2/promise");
|
|
35
|
+
const external_lodash_namespaceObject = require("lodash");
|
|
36
|
+
function getTableSchema(table) {
|
|
37
|
+
return `## Table: ${table.name} (${table.description})
|
|
38
|
+
${table.columns.map((c)=>`- ${c.name} (${c.type} : ${c.description})`).join('\n')}
|
|
39
|
+
|
|
40
|
+
## Column Value Mappings:
|
|
41
|
+
${table.columns.filter((c)=>!(0, external_lodash_namespaceObject.isEmpty)(c.enums)).map((c)=>`- Column: \`${c.name}\`
|
|
42
|
+
${c.enums.map(({ key, value })=>`- ${key}: ${value}`).join('\n')}`).join('\n\n')}
|
|
43
|
+
`;
|
|
44
|
+
}
|
|
45
|
+
function createDatabaseTools(options) {
|
|
46
|
+
const { datasource, datasets, onGenerateChart } = options;
|
|
47
|
+
return [
|
|
48
|
+
(0, external_langchain_namespaceObject.tool)(async ({ query })=>{
|
|
49
|
+
const tablesMd = datasets.map((t)=>`${t.name}: ${t.description}`).join('\n');
|
|
50
|
+
return `\n==========\n${tablesMd}\n==========\n`;
|
|
51
|
+
}, {
|
|
52
|
+
name: 'list_table',
|
|
53
|
+
description: '在需要访问的数据库信息的时候调用该工具,用于列出所有与 `query` 相关的数据表,如果列出全部数据表,返回的结果包含数据表的名称以及描述。',
|
|
54
|
+
schema: {
|
|
55
|
+
type: 'object',
|
|
56
|
+
properties: {
|
|
57
|
+
query: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
description: '根据 query 语义检索数据表'
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}),
|
|
64
|
+
(0, external_langchain_namespaceObject.tool)(async ({ name })=>{
|
|
65
|
+
const table = datasets.find((t)=>t.name === name);
|
|
66
|
+
if (!table) return `表名 ${name} 不存在`;
|
|
67
|
+
const data = {
|
|
68
|
+
name: table.name,
|
|
69
|
+
description: table.description,
|
|
70
|
+
columns: table.columns.map((c)=>({
|
|
71
|
+
name: c.name,
|
|
72
|
+
description: c.description,
|
|
73
|
+
type: c.type,
|
|
74
|
+
enums: c.enums
|
|
75
|
+
}))
|
|
76
|
+
};
|
|
77
|
+
return getTableSchema(data);
|
|
78
|
+
}, {
|
|
79
|
+
name: 'describe_table',
|
|
80
|
+
description: '在需要生成SQL时使用该工具,用于获取数据表的详细字段信息,并且包含字段的枚举值。',
|
|
81
|
+
schema: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
name: {
|
|
85
|
+
type: 'string',
|
|
86
|
+
description: '表名, 必须是 list_table 中返回的表名'
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}),
|
|
91
|
+
(0, external_langchain_namespaceObject.tool)(async ({ sql })=>{
|
|
92
|
+
let connection;
|
|
93
|
+
try {
|
|
94
|
+
connection = await promise_namespaceObject.createConnection({
|
|
95
|
+
host: datasource.host,
|
|
96
|
+
port: datasource.port,
|
|
97
|
+
user: datasource.username,
|
|
98
|
+
password: datasource.password,
|
|
99
|
+
database: datasource.database
|
|
100
|
+
});
|
|
101
|
+
console.log(sql);
|
|
102
|
+
await connection.connect();
|
|
103
|
+
const [rows] = await connection.query(sql);
|
|
104
|
+
return JSON.stringify(rows);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return error.message;
|
|
107
|
+
} finally{
|
|
108
|
+
await connection.end();
|
|
109
|
+
}
|
|
110
|
+
}, {
|
|
111
|
+
name: 'execute_sql',
|
|
112
|
+
description: '在需要执行SQL查询获取数据时调用该工具,用于执行 SQL 语句,仅支持查询语句,基于 MySQL 8.0的语法。',
|
|
113
|
+
schema: {
|
|
114
|
+
type: 'object',
|
|
115
|
+
properties: {
|
|
116
|
+
sql: {
|
|
117
|
+
type: 'string',
|
|
118
|
+
description: 'SQL 语句'
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}),
|
|
123
|
+
onGenerateChart && (0, external_langchain_namespaceObject.tool)(async ({ sql, chartConfig, title })=>{
|
|
124
|
+
try {
|
|
125
|
+
const id = await onGenerateChart({
|
|
126
|
+
sql,
|
|
127
|
+
title,
|
|
128
|
+
chartConfig
|
|
129
|
+
});
|
|
130
|
+
return `<chart-view id="${id}"></chart-view>`;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
return `工具目前不可用:${error.message}`;
|
|
133
|
+
}
|
|
134
|
+
}, {
|
|
135
|
+
name: 'generate_chart',
|
|
136
|
+
description: '在需要可视化展示数据或者生成图表时调用该工具,用于生成图表,工具返回的结果可以直接被嵌入到 Markdown 中。',
|
|
137
|
+
schema: {
|
|
138
|
+
type: 'object',
|
|
139
|
+
properties: {
|
|
140
|
+
title: {
|
|
141
|
+
type: 'string',
|
|
142
|
+
description: '图表标题'
|
|
143
|
+
},
|
|
144
|
+
sql: {
|
|
145
|
+
type: 'string',
|
|
146
|
+
description: 'SQL 查询语句'
|
|
147
|
+
},
|
|
148
|
+
chartConfig: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
description: '图表的配置,可为空,如果为空表示只展示二维表的查询结果',
|
|
151
|
+
properties: {
|
|
152
|
+
type: {
|
|
153
|
+
type: 'string',
|
|
154
|
+
description: '图表的类型,可选值有:BAR_CHART、PIE_CHART、LINE_CHART'
|
|
155
|
+
},
|
|
156
|
+
metric: {
|
|
157
|
+
type: 'array',
|
|
158
|
+
items: {
|
|
159
|
+
type: 'object',
|
|
160
|
+
properties: {
|
|
161
|
+
name: {
|
|
162
|
+
type: 'string',
|
|
163
|
+
description: '指标的名称,对应SQL执行出来的数据里面的字段名'
|
|
164
|
+
},
|
|
165
|
+
alias: {
|
|
166
|
+
type: 'string',
|
|
167
|
+
description: '指标的别名,例如“数量”、“金额”、“比例”等'
|
|
168
|
+
},
|
|
169
|
+
aggregate: {
|
|
170
|
+
type: 'string',
|
|
171
|
+
description: '指标的聚合函数,这里指的是对SQL执行的结果进行进一步聚合,如果不指定,则默认不聚合,可选值有 SUM、COUNT、AVG'
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
dimension: {
|
|
177
|
+
type: 'string',
|
|
178
|
+
description: '图表的维度,例如时间、地点、指标等,最多只能有一个维度'
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
function createKnowledgeTools(options) {
|
|
188
|
+
const { knowledges, onRetrieveFullDocs, onRetrieveFragments } = options;
|
|
189
|
+
return [
|
|
190
|
+
(0, external_langchain_namespaceObject.tool)(async ({ index, query })=>{
|
|
191
|
+
if (!knowledges.includes(index)) return `错误,你没有权限访问【${index}】知识库`;
|
|
192
|
+
return await onRetrieveFragments(index, query);
|
|
193
|
+
}, {
|
|
194
|
+
name: 'retrieve_fragments',
|
|
195
|
+
description: '在需要查询专业词库获取专业且详细的信息的时候调用该工具,根据 query 进行检索,返回与之相关的分片内容',
|
|
196
|
+
schema: {
|
|
197
|
+
type: 'object',
|
|
198
|
+
properties: {
|
|
199
|
+
index: {
|
|
200
|
+
type: 'string',
|
|
201
|
+
description: `知识库名称, 可以选择的库有\n ${knowledges.map((k)=>`- ${k}`).join('\n')}\n库名称必须为以上库中的一个`
|
|
202
|
+
},
|
|
203
|
+
query: {
|
|
204
|
+
type: 'string',
|
|
205
|
+
description: '根据 query 进行检索'
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}),
|
|
210
|
+
(0, external_langchain_namespaceObject.tool)(async ({ title, index })=>{
|
|
211
|
+
if (!knowledges.includes(index)) return `错误,你没有权限访问【${index}】知识库`;
|
|
212
|
+
return await onRetrieveFullDocs(index, title);
|
|
213
|
+
}, {
|
|
214
|
+
name: 'retrieve_full_docs',
|
|
215
|
+
description: '在需要从知识库中检索文档全文的时候调用该工具,根据 title 检索文档标题(全匹配)',
|
|
216
|
+
schema: {
|
|
217
|
+
type: 'object',
|
|
218
|
+
properties: {
|
|
219
|
+
index: {
|
|
220
|
+
type: 'string',
|
|
221
|
+
description: `知识库名称, 可以选择的库有\n ${knowledges.map((k)=>`- ${k}`).join('\n')}\n库名称必须为以上库中的一个`
|
|
222
|
+
},
|
|
223
|
+
title: {
|
|
224
|
+
type: 'string',
|
|
225
|
+
description: '根据 title 进行检索'
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
function currentTimeMiddleware() {
|
|
233
|
+
return (0, external_langchain_namespaceObject.createMiddleware)({
|
|
234
|
+
name: 'currentTimeMiddleware',
|
|
235
|
+
wrapModelCall: (request, handler)=>{
|
|
236
|
+
const now = new Date();
|
|
237
|
+
const timeString = new Intl.DateTimeFormat('zh-CN', {
|
|
238
|
+
year: 'numeric',
|
|
239
|
+
month: '2-digit',
|
|
240
|
+
day: '2-digit',
|
|
241
|
+
hour: '2-digit',
|
|
242
|
+
minute: '2-digit',
|
|
243
|
+
second: '2-digit',
|
|
244
|
+
weekday: 'long',
|
|
245
|
+
timeZone: 'Asia/Shanghai',
|
|
246
|
+
hour12: false
|
|
247
|
+
}).format(now);
|
|
248
|
+
return handler({
|
|
249
|
+
...request,
|
|
250
|
+
systemMessage: request.systemMessage.concat('\n\n当前时间: ' + timeString)
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
const langgraph_namespaceObject = require("@langchain/langgraph");
|
|
256
|
+
const external_zod_namespaceObject = require("zod");
|
|
257
|
+
const messages_namespaceObject = require("@langchain/core/messages");
|
|
258
|
+
const maxIterationsMiddleware = ({ maxIterations })=>(0, external_langchain_namespaceObject.createMiddleware)({
|
|
259
|
+
name: 'max_iterations',
|
|
260
|
+
stateSchema: external_zod_namespaceObject.z.object({
|
|
261
|
+
iterations: external_zod_namespaceObject.z.number().default(0)
|
|
262
|
+
}),
|
|
263
|
+
beforeAgent: async (state)=>{
|
|
264
|
+
const currentIterations = state.iterations || 0;
|
|
265
|
+
const newIterations = currentIterations + 1;
|
|
266
|
+
if (newIterations > maxIterations) return {
|
|
267
|
+
iterations: newIterations,
|
|
268
|
+
messages: [
|
|
269
|
+
new messages_namespaceObject.AIMessage({
|
|
270
|
+
content: '由于达到了最大迭代次数限制,且无法直接生成回答,我必须停止当前的思考过程。'
|
|
271
|
+
})
|
|
272
|
+
],
|
|
273
|
+
jumpTo: langgraph_namespaceObject.END
|
|
274
|
+
};
|
|
275
|
+
if (newIterations === maxIterations) return {
|
|
276
|
+
iterations: newIterations,
|
|
277
|
+
messages: [
|
|
278
|
+
new messages_namespaceObject.SystemMessage('system_directive: 你已经达到了最大迭代次数限制。请停止使用工具,并根据目前已有的信息,直接回答用户的问题。')
|
|
279
|
+
]
|
|
280
|
+
};
|
|
281
|
+
return {
|
|
282
|
+
iterations: newIterations
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
function createPatchToolCallsMiddleware() {
|
|
287
|
+
return (0, external_langchain_namespaceObject.createMiddleware)({
|
|
288
|
+
name: 'patchToolCallsMiddleware',
|
|
289
|
+
beforeAgent: async (state)=>{
|
|
290
|
+
const messages = state.messages;
|
|
291
|
+
if (!messages || 0 === messages.length) return;
|
|
292
|
+
const patchedMessages = [];
|
|
293
|
+
for(let i = 0; i < messages.length; i++){
|
|
294
|
+
const msg = messages[i];
|
|
295
|
+
patchedMessages.push(msg);
|
|
296
|
+
if (external_langchain_namespaceObject.AIMessage.isInstance(msg) && null != msg.tool_calls) for (const toolCall of msg.tool_calls){
|
|
297
|
+
const correspondingToolMsg = messages.slice(i).find((m)=>external_langchain_namespaceObject.ToolMessage.isInstance(m) && m.tool_call_id === toolCall.id);
|
|
298
|
+
if (!correspondingToolMsg) {
|
|
299
|
+
const toolMsg = `Tool call ${toolCall.name} with id ${toolCall.id} was cancelled - another message came in before it could be completed.`;
|
|
300
|
+
patchedMessages.push(new external_langchain_namespaceObject.ToolMessage({
|
|
301
|
+
content: toolMsg,
|
|
302
|
+
name: toolCall.name,
|
|
303
|
+
tool_call_id: toolCall.id
|
|
304
|
+
}));
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return {
|
|
309
|
+
messages: [
|
|
310
|
+
new messages_namespaceObject.RemoveMessage({
|
|
311
|
+
id: langgraph_namespaceObject.REMOVE_ALL_MESSAGES
|
|
312
|
+
}),
|
|
313
|
+
...patchedMessages
|
|
314
|
+
]
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
const jieba_namespaceObject = require("@node-rs/jieba");
|
|
320
|
+
const dict_namespaceObject = require("@node-rs/jieba/dict");
|
|
321
|
+
class BM25 {
|
|
322
|
+
constructor(documents, k1 = 1.5, b = 0.75){
|
|
323
|
+
this.documents = documents;
|
|
324
|
+
this.k1 = k1;
|
|
325
|
+
this.b = b;
|
|
326
|
+
this.docFreqs = new Map();
|
|
327
|
+
this.idf = new Map();
|
|
328
|
+
this.avgdl = 0;
|
|
329
|
+
this.initialize();
|
|
330
|
+
}
|
|
331
|
+
initialize() {
|
|
332
|
+
let totalLength = 0;
|
|
333
|
+
this.documents.forEach((doc)=>{
|
|
334
|
+
totalLength += doc.length;
|
|
335
|
+
const uniqueTerms = new Set(doc);
|
|
336
|
+
uniqueTerms.forEach((term)=>{
|
|
337
|
+
this.docFreqs.set(term, (this.docFreqs.get(term) || 0) + 1);
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
this.avgdl = totalLength / this.documents.length || 1;
|
|
341
|
+
const N = this.documents.length;
|
|
342
|
+
this.docFreqs.forEach((count, term)=>{
|
|
343
|
+
const idf = Math.log((N - count + 0.5) / (count + 0.5) + 1);
|
|
344
|
+
this.idf.set(term, Math.max(idf, 0));
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
getScore(query, index) {
|
|
348
|
+
let score = 0;
|
|
349
|
+
const doc = this.documents[index];
|
|
350
|
+
const docLen = doc.length;
|
|
351
|
+
query.forEach((term)=>{
|
|
352
|
+
if (!this.idf.has(term)) return;
|
|
353
|
+
const tf = doc.filter((t)=>t === term).length;
|
|
354
|
+
const idf = this.idf.get(term);
|
|
355
|
+
const numerator = tf * (this.k1 + 1);
|
|
356
|
+
const denominator = tf + this.k1 * (1 - this.b + this.b * (docLen / this.avgdl));
|
|
357
|
+
score += numerator / denominator * idf;
|
|
358
|
+
});
|
|
359
|
+
return score;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const jieba = jieba_namespaceObject.Jieba.withDict(dict_namespaceObject.dict);
|
|
363
|
+
function createWordTools(options) {
|
|
364
|
+
const { glossry, min_score = 0, top_n = 5 } = options;
|
|
365
|
+
const enableGlossry = (options.glossry ?? []).map((g)=>`- ${g.name}: ${g.description}`).join('\n');
|
|
366
|
+
return [
|
|
367
|
+
(0, external_langchain_namespaceObject.tool)(async (options)=>{
|
|
368
|
+
const { name, query } = options;
|
|
369
|
+
const { words } = glossry.find((g)=>g.name === name) ?? {};
|
|
370
|
+
if (!words) return `获取错误,你没有权限查看【${name}】词条库`;
|
|
371
|
+
const documents = words.map((f)=>{
|
|
372
|
+
const content = `${f.name} ${f.description}`;
|
|
373
|
+
return jieba.cut(content, false);
|
|
374
|
+
});
|
|
375
|
+
const bm25 = new BM25(documents);
|
|
376
|
+
const queryTokens = jieba.cut(query, false);
|
|
377
|
+
const scoredFiles = words.map((f, index)=>{
|
|
378
|
+
const score = bm25.getScore(queryTokens, index);
|
|
379
|
+
return {
|
|
380
|
+
file: f,
|
|
381
|
+
score
|
|
382
|
+
};
|
|
383
|
+
});
|
|
384
|
+
const result = scoredFiles.filter((item)=>item.score > min_score).sort((a, b)=>b.score - a.score).slice(0, top_n).map((item)=>`${item.file.name}: ${item.file.description}`).join('\n');
|
|
385
|
+
if (!result) return '没有找到该词条对应的数据';
|
|
386
|
+
}, {
|
|
387
|
+
name: 'query_professional_thesaurus',
|
|
388
|
+
description: '查询专业词库中特定术语的定义、用法及相关领域解释',
|
|
389
|
+
schema: {
|
|
390
|
+
type: 'object',
|
|
391
|
+
properties: {
|
|
392
|
+
name: {
|
|
393
|
+
type: 'string',
|
|
394
|
+
description: `词库名, 可以选择的库有\n ${enableGlossry}\n库名称必须为以上库中的一个`
|
|
395
|
+
},
|
|
396
|
+
query: {
|
|
397
|
+
type: 'string',
|
|
398
|
+
description: '根据 query 语义检索词条'
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
})
|
|
403
|
+
];
|
|
404
|
+
}
|
|
405
|
+
function createReactAgent(options) {
|
|
406
|
+
const { name, model, system_prompt, tools, middleware, features, databaseOptions, knowledgeOptions, wordOptions, inject_current_time, max_iterations = 0 } = options;
|
|
407
|
+
const agent = (0, external_langchain_namespaceObject.createAgent)({
|
|
408
|
+
name,
|
|
409
|
+
model,
|
|
410
|
+
systemPrompt: system_prompt,
|
|
411
|
+
tools: [
|
|
412
|
+
...features.database ? createDatabaseTools(databaseOptions) : [],
|
|
413
|
+
...features.knowledge ? createKnowledgeTools(knowledgeOptions) : [],
|
|
414
|
+
...features.word ? createWordTools(wordOptions) : [],
|
|
415
|
+
...tools
|
|
416
|
+
],
|
|
417
|
+
middleware: [
|
|
418
|
+
inject_current_time && currentTimeMiddleware(),
|
|
419
|
+
max_iterations > 0 && maxIterationsMiddleware({
|
|
420
|
+
maxIterations: max_iterations
|
|
421
|
+
}),
|
|
422
|
+
(0, external_langchain_namespaceObject.todoListMiddleware)(),
|
|
423
|
+
(0, external_langchain_namespaceObject.summarizationMiddleware)({
|
|
424
|
+
model: model,
|
|
425
|
+
trigger: {
|
|
426
|
+
tokens: 170000
|
|
427
|
+
},
|
|
428
|
+
keep: {
|
|
429
|
+
messages: 6
|
|
430
|
+
}
|
|
431
|
+
}),
|
|
432
|
+
createPatchToolCallsMiddleware(),
|
|
433
|
+
...middleware
|
|
434
|
+
].filter(Boolean)
|
|
435
|
+
});
|
|
436
|
+
return agent;
|
|
437
|
+
}
|
|
31
438
|
const openai_namespaceObject = require("@langchain/openai");
|
|
32
439
|
class ChatVolcengine extends openai_namespaceObject.ChatOpenAICompletions {
|
|
33
440
|
static lc_name() {
|
|
@@ -42,7 +449,8 @@ class ChatVolcengine extends openai_namespaceObject.ChatOpenAICompletions {
|
|
|
42
449
|
configuration: {
|
|
43
450
|
baseURL: 'https://ark.cn-beijing.volces.com/api/v3',
|
|
44
451
|
...fields?.configuration
|
|
45
|
-
}
|
|
452
|
+
},
|
|
453
|
+
timeout: fields?.timeout
|
|
46
454
|
});
|
|
47
455
|
}
|
|
48
456
|
_convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
|
|
@@ -56,85 +464,13 @@ class ChatVolcengine extends openai_namespaceObject.ChatOpenAICompletions {
|
|
|
56
464
|
return langChainMessage;
|
|
57
465
|
}
|
|
58
466
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
constructor(options){
|
|
63
|
-
this.system_prompt = options.system_prompt;
|
|
64
|
-
this.model = options.model;
|
|
65
|
-
this.tools = options.tools;
|
|
66
|
-
this.api_key = options.api_key;
|
|
67
|
-
this.model_config = options.model_config || {};
|
|
68
|
-
}
|
|
69
|
-
async stream(sessionId, input, chat_history) {
|
|
70
|
-
const runnable = await this.createRunnable(chat_history);
|
|
71
|
-
return runnable.streamEvents({
|
|
72
|
-
input
|
|
73
|
-
}, {
|
|
74
|
-
version: 'v2',
|
|
75
|
-
configurable: {
|
|
76
|
-
sessionId
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
async createRunnable(messages) {
|
|
81
|
-
const executor = this.createExecutor();
|
|
82
|
-
const runnable = new runnables_namespaceObject.RunnableWithMessageHistory({
|
|
83
|
-
getMessageHistory: async ()=>{
|
|
84
|
-
const msg = new chat_history_namespaceObject.InMemoryChatMessageHistory();
|
|
85
|
-
messages.forEach((m)=>{
|
|
86
|
-
if ('user' === m.role) msg.addUserMessage(m.content);
|
|
87
|
-
else if ('assistant' === m.role) msg.addAIMessage(m.content);
|
|
88
|
-
});
|
|
89
|
-
return msg;
|
|
90
|
-
},
|
|
91
|
-
runnable: executor,
|
|
92
|
-
inputMessagesKey: 'input',
|
|
93
|
-
historyMessagesKey: 'chat_history'
|
|
94
|
-
});
|
|
95
|
-
return runnable;
|
|
96
|
-
}
|
|
97
|
-
createExecutor() {
|
|
98
|
-
const llm = this.createModel();
|
|
99
|
-
const prompt = this.createPrompt();
|
|
100
|
-
const agent = (0, agents_namespaceObject.createToolCallingAgent)({
|
|
101
|
-
llm: llm,
|
|
102
|
-
tools: this.tools,
|
|
103
|
-
prompt,
|
|
104
|
-
streamRunnable: true
|
|
105
|
-
});
|
|
106
|
-
return new agents_namespaceObject.AgentExecutor({
|
|
107
|
-
agent,
|
|
108
|
-
tools: this.tools
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
createPrompt() {
|
|
112
|
-
return prompts_namespaceObject.ChatPromptTemplate.fromMessages([
|
|
113
|
-
[
|
|
114
|
-
'system',
|
|
115
|
-
this.system_prompt
|
|
116
|
-
],
|
|
117
|
-
new prompts_namespaceObject.MessagesPlaceholder('chat_history'),
|
|
118
|
-
[
|
|
119
|
-
'human',
|
|
120
|
-
'{input}'
|
|
121
|
-
],
|
|
122
|
-
new prompts_namespaceObject.MessagesPlaceholder('agent_scratchpad')
|
|
123
|
-
]);
|
|
124
|
-
}
|
|
125
|
-
createModel() {
|
|
126
|
-
return new ChatVolcengine({
|
|
127
|
-
apiKey: this.api_key,
|
|
128
|
-
model: this.model,
|
|
129
|
-
temperature: 0,
|
|
130
|
-
streaming: true,
|
|
131
|
-
...this.model_config
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
exports.Agent = __webpack_exports__.Agent;
|
|
467
|
+
exports.AgentOptions = __webpack_exports__.AgentOptions;
|
|
468
|
+
exports.ChatVolcengine = __webpack_exports__.ChatVolcengine;
|
|
469
|
+
exports.createReactAgent = __webpack_exports__.createReactAgent;
|
|
136
470
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
137
|
-
"
|
|
471
|
+
"AgentOptions",
|
|
472
|
+
"ChatVolcengine",
|
|
473
|
+
"createReactAgent"
|
|
138
474
|
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
139
475
|
Object.defineProperty(exports, '__esModule', {
|
|
140
476
|
value: true
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "listpage-next-ai",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.259",
|
|
4
4
|
"description": "A React component library for creating filter forms with Ant Design",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.js",
|
|
@@ -23,26 +23,34 @@
|
|
|
23
23
|
"build": "rslib build",
|
|
24
24
|
"format": "prettier --write .",
|
|
25
25
|
"prepublishOnly": "npm run build",
|
|
26
|
-
"test": "npx
|
|
26
|
+
"test": "npx tsx ./src/test.ts"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
+
"@microsoft/api-extractor": "~7.53.3",
|
|
29
30
|
"@rsbuild/core": "^1.5.7",
|
|
30
31
|
"@rsbuild/plugin-react": "^1.4.0",
|
|
31
32
|
"@rslib/core": "^0.13.3",
|
|
33
|
+
"@types/lodash": "~4.17.20",
|
|
32
34
|
"prettier": "^3.6.2",
|
|
33
|
-
"typescript": "^5.9.2",
|
|
34
35
|
"ts-node": "~10.9.2",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
36
|
+
"tsx": "^4.19.0",
|
|
37
|
+
"typescript": "^5.9.2"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"
|
|
40
|
-
"lodash": "~4.17.21",
|
|
41
|
-
"@langchain/core": "1.0.5",
|
|
40
|
+
"@langchain/classic": "~1.0.3",
|
|
42
41
|
"@langchain/community": "~1.0.3",
|
|
42
|
+
"@langchain/core": "^1.1.5",
|
|
43
|
+
"@langchain/langgraph": "^1.0.0",
|
|
43
44
|
"@langchain/openai": "~1.1.1",
|
|
44
|
-
"@langchain/
|
|
45
|
+
"@langchain/tavily": "~1.0.1",
|
|
46
|
+
"deepagents": "~1.3.1",
|
|
47
|
+
"fast-glob": "^3.3.3",
|
|
48
|
+
"langchain": "^1.2.0",
|
|
49
|
+
"lodash": "~4.17.21",
|
|
45
50
|
"rxjs": "~7.8.2",
|
|
46
|
-
"
|
|
51
|
+
"uuid": "9.0.1",
|
|
52
|
+
"zod": "^3.23.8",
|
|
53
|
+
"mysql2": "~3.15.3",
|
|
54
|
+
"@node-rs/jieba": "~2.0.1"
|
|
47
55
|
}
|
|
48
56
|
}
|