@keo-ai/axiom 0.1.4 → 0.1.5
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 +25 -27
- package/dist/function_call_loop/index.d.ts +3 -3
- package/dist/function_call_loop/index.js +3 -3
- package/dist/function_call_loop/loop.d.ts +0 -21
- package/dist/function_call_loop/loop.js +22 -56
- package/dist/function_call_loop/provider.d.ts +2 -2
- package/dist/function_call_loop/provider.js +36 -54
- package/dist/function_call_loop/types.d.ts +4 -6
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -3
- package/dist/llm_provider/bailian.d.ts +28 -0
- package/dist/llm_provider/bailian.js +174 -0
- package/dist/llm_provider/index.d.ts +73 -0
- package/dist/llm_provider/index.js +62 -0
- package/dist/llm_provider/llm.d.ts +32 -0
- package/dist/llm_provider/llm.js +2 -0
- package/dist/llm_provider/models.d.ts +14 -0
- package/dist/llm_provider/models.js +17 -0
- package/dist/llm_provider/types.d.ts +51 -0
- package/dist/llm_provider/types.js +2 -0
- package/dist/predict/config.d.ts +2 -2
- package/dist/predict/index.d.ts +6 -5
- package/dist/predict/index.js +2 -2
- package/dist/predict/llm.d.ts +3 -33
- package/dist/predict/llm.js +1 -1
- package/dist/predict/models.d.ts +2 -14
- package/dist/predict/models.js +2 -14
- package/dist/predict/predict.d.ts +1 -1
- package/dist/predict/predict.js +1 -1
- package/dist/predict/types.d.ts +1 -51
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -213,9 +213,7 @@ Function Call Loop 是 Axiom 的底层 Function Call 引擎。它负责把 **LLM
|
|
|
213
213
|
|
|
214
214
|
### 快速开始
|
|
215
215
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
像 predict 模块一样,设置环境变量后一行调用:
|
|
216
|
+
设置环境变量后一行调用:
|
|
219
217
|
|
|
220
218
|
```bash
|
|
221
219
|
export BAILIAN_API_KEY="your-api-key"
|
|
@@ -224,12 +222,12 @@ export BAILIAN_API_KEY="your-api-key"
|
|
|
224
222
|
```ts
|
|
225
223
|
import { FunctionCallLoop } from '@keo-ai/axiom';
|
|
226
224
|
|
|
227
|
-
const result = await FunctionCallLoop.
|
|
225
|
+
const result = await FunctionCallLoop.runLoop({
|
|
228
226
|
messages: [
|
|
229
227
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
230
228
|
{ role: 'user', content: 'What is the weather in Beijing?' },
|
|
231
229
|
],
|
|
232
|
-
model: '
|
|
230
|
+
model: 'qwen3.7-max',
|
|
233
231
|
temperature: 0.7,
|
|
234
232
|
maxTokens: 2048,
|
|
235
233
|
tools: [
|
|
@@ -243,7 +241,7 @@ const result = await FunctionCallLoop.runLoopWithModel({
|
|
|
243
241
|
},
|
|
244
242
|
required: ['city'],
|
|
245
243
|
},
|
|
246
|
-
execute: async (args) => {
|
|
244
|
+
execute: async (args, context) => {
|
|
247
245
|
return { temperature: 25, condition: 'Sunny' };
|
|
248
246
|
},
|
|
249
247
|
},
|
|
@@ -266,7 +264,7 @@ console.log('Harness:', result.harness);
|
|
|
266
264
|
Loop 每处理一个 tool call,按顺序抛出两个事件:
|
|
267
265
|
|
|
268
266
|
```ts
|
|
269
|
-
const result = await FunctionCallLoop.
|
|
267
|
+
const result = await FunctionCallLoop.runLoop({
|
|
270
268
|
// ...
|
|
271
269
|
onEvent: (event) => {
|
|
272
270
|
if (event.type === 'execution:start') {
|
|
@@ -315,7 +313,7 @@ console.log(result.harness);
|
|
|
315
313
|
对于敏感操作(转账、删除数据等),可以给 tool 配置 `approval`,Loop 会在执行前暂停并返回 `pendingApproval`:
|
|
316
314
|
|
|
317
315
|
```ts
|
|
318
|
-
const result = await FunctionCallLoop.
|
|
316
|
+
const result = await FunctionCallLoop.runLoop({
|
|
319
317
|
messages: [
|
|
320
318
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
321
319
|
{ role: 'user', content: 'Transfer 1000 to Alice' },
|
|
@@ -333,7 +331,7 @@ const result = await FunctionCallLoop.runLoopWithModel({
|
|
|
333
331
|
return { ticketId: ticket.id };
|
|
334
332
|
},
|
|
335
333
|
},
|
|
336
|
-
execute: async (args) => {
|
|
334
|
+
execute: async (args, context) => {
|
|
337
335
|
// 审批通过后才会执行到这里
|
|
338
336
|
return await doTransfer(args);
|
|
339
337
|
},
|
|
@@ -357,7 +355,7 @@ if (result.pendingApproval) {
|
|
|
357
355
|
```ts
|
|
358
356
|
const record = await db.findByTicketId(ticketId);
|
|
359
357
|
|
|
360
|
-
const result = await FunctionCallLoop.
|
|
358
|
+
const result = await FunctionCallLoop.runLoop({
|
|
361
359
|
messages: record.messages,
|
|
362
360
|
metadata: { userId: 'u-123', orgId: 'o-456' },
|
|
363
361
|
tools: [
|
|
@@ -375,7 +373,7 @@ const result = await FunctionCallLoop.runLoopWithModel({
|
|
|
375
373
|
return { ticketId: ticket.id };
|
|
376
374
|
},
|
|
377
375
|
},
|
|
378
|
-
execute: async (args) => await doTransfer(args),
|
|
376
|
+
execute: async (args, context) => await doTransfer(args),
|
|
379
377
|
},
|
|
380
378
|
],
|
|
381
379
|
});
|
|
@@ -406,7 +404,7 @@ const tool = {
|
|
|
406
404
|
visible: isAdmin,
|
|
407
405
|
};
|
|
408
406
|
},
|
|
409
|
-
execute: async (args) => { /* ... */ },
|
|
407
|
+
execute: async (args, context) => { /* ... */ },
|
|
410
408
|
};
|
|
411
409
|
```
|
|
412
410
|
|
|
@@ -419,17 +417,17 @@ const tool = {
|
|
|
419
417
|
|
|
420
418
|
### Turn Policy(全局轮次策略)
|
|
421
419
|
|
|
422
|
-
每轮开始时,Loop 先执行 Turn Policy
|
|
420
|
+
每轮开始时,Loop 先执行 Turn Policy,可注入一条 system 消息干预本轮:
|
|
423
421
|
|
|
424
422
|
```ts
|
|
425
|
-
const result = await FunctionCallLoop.
|
|
423
|
+
const result = await FunctionCallLoop.runLoop({
|
|
426
424
|
// ...
|
|
427
425
|
turnPolicy: async (harness, turn, metadata) => {
|
|
428
|
-
//
|
|
426
|
+
// 连续失败时注入提醒
|
|
429
427
|
const failCount = harness.filter((r) => r.status === 'error').length;
|
|
430
428
|
if (failCount >= 3) {
|
|
431
429
|
return {
|
|
432
|
-
|
|
430
|
+
injectMessage: 'Previous calls failed, please try a different approach.',
|
|
433
431
|
};
|
|
434
432
|
}
|
|
435
433
|
return {};
|
|
@@ -443,21 +441,20 @@ const result = await FunctionCallLoop.runLoopWithModel({
|
|
|
443
441
|
|---|---|
|
|
444
442
|
| `turn < maxTurns - 1` | 继续执行 |
|
|
445
443
|
| `turn === maxTurns - 1` | 注入 `warningMessage`,继续执行 |
|
|
446
|
-
| `turn >= maxTurns` |
|
|
444
|
+
| `turn >= maxTurns` | 交由硬兜底处理 |
|
|
447
445
|
|
|
448
446
|
| 边界情况 | 行为 |
|
|
449
447
|
|---|---|
|
|
450
|
-
| `turnPolicy` 抛异常 |
|
|
451
|
-
| 返回 `terminate` | 向 Messages 塞入 `fallbackMessage`,结束 Loop,不再调 LLM |
|
|
448
|
+
| `turnPolicy` 抛异常 | 视为无注入,继续执行 |
|
|
452
449
|
|
|
453
|
-
###
|
|
450
|
+
### 上下文 compact
|
|
454
451
|
|
|
455
452
|
控制给 LLM 的历史消息长度,只动 Messages,不动 Harness:
|
|
456
453
|
|
|
457
454
|
```ts
|
|
458
|
-
const result = await FunctionCallLoop.
|
|
455
|
+
const result = await FunctionCallLoop.runLoop({
|
|
459
456
|
// ...
|
|
460
|
-
|
|
457
|
+
compact: {
|
|
461
458
|
keepRounds: 3, // 最近 3 轮保持完整
|
|
462
459
|
compress: async (oldRounds) => {
|
|
463
460
|
// oldRounds: 需要压缩的轮次数组,每轮是一个 Message 数组
|
|
@@ -475,7 +472,7 @@ const result = await FunctionCallLoop.runLoopWithModel({
|
|
|
475
472
|
|
|
476
473
|
| 规则 | 说明 |
|
|
477
474
|
|---|---|
|
|
478
|
-
|
|
|
475
|
+
| compact 只影响 Messages | Harness 始终完整保留 |
|
|
479
476
|
| 系统消息和初始输入不参与压缩 | 始终保留 |
|
|
480
477
|
| `compress` 抛异常 | 视为不压缩,使用原始 Messages 继续执行 |
|
|
481
478
|
|
|
@@ -486,7 +483,7 @@ const result = await FunctionCallLoop.runLoopWithModel({
|
|
|
486
483
|
```ts
|
|
487
484
|
const controller = new AbortController();
|
|
488
485
|
|
|
489
|
-
const promise = FunctionCallLoop.
|
|
486
|
+
const promise = FunctionCallLoop.runLoop({
|
|
490
487
|
// ...
|
|
491
488
|
signal: controller.signal,
|
|
492
489
|
});
|
|
@@ -499,7 +496,7 @@ const result = await promise;
|
|
|
499
496
|
|
|
500
497
|
### 配置项
|
|
501
498
|
|
|
502
|
-
#### `
|
|
499
|
+
#### `runLoop` 配置
|
|
503
500
|
|
|
504
501
|
**对话入口**
|
|
505
502
|
|
|
@@ -512,9 +509,10 @@ const result = await promise;
|
|
|
512
509
|
| 配置项 | 类型 | 必填 | 说明 |
|
|
513
510
|
|---|---|---|---|
|
|
514
511
|
| `tools` | `Tool[]` | ✅ | 注册的 tool 列表 |
|
|
512
|
+
| `llmCaller` | `LLMCaller` | — | 自定义 LLM 调用器。未设置时从环境变量自动创建 |
|
|
515
513
|
| `maxTurns` | `number` | — | 最大轮次,默认无限制 |
|
|
516
514
|
| `turnPolicy` | `TurnPolicy` | — | 自定义轮次策略 |
|
|
517
|
-
| `
|
|
515
|
+
| `compact` | `CompactConfig` | — | 上下文 compact 配置 |
|
|
518
516
|
| `metadata` | `unknown` | — | 传递给 Tool 和 Turn Policy 的元数据 |
|
|
519
517
|
| `warningMessage` | `string` | — | 默认策略在 `maxTurns - 1` 时注入的告警文本 |
|
|
520
518
|
| `terminateMessage` | `string` | — | 默认策略在 `maxTurns` 时注入的终止文本 |
|
|
@@ -525,7 +523,7 @@ const result = await promise;
|
|
|
525
523
|
|
|
526
524
|
| 配置项 | 类型 | 必填 | 说明 |
|
|
527
525
|
|---|---|---|---|
|
|
528
|
-
| `model` | `
|
|
526
|
+
| `model` | `Model` | — | 模型枚举值。默认从 `BAILIAN_DEFAULT_MODEL` 环境变量读取,否则 `qwen-max` |
|
|
529
527
|
| `maxTokens` | `number` | — | 单次 LLM 调用的最大输出 token 数 |
|
|
530
528
|
| `temperature` | `number` | — | 采样温度,范围 0~2 |
|
|
531
529
|
| `topP` | `number` | — | 核采样概率阈值,范围 0~1 |
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* - 每次 tool 执行向上层回传执行前、执行后两个事件
|
|
6
6
|
* - 维护跨轮次的执行历史(Harness),tool 之间能互相看见
|
|
7
7
|
* - 每次调 LLM 之前动态决定暴露哪些 tool
|
|
8
|
-
* -
|
|
8
|
+
* - 支持自定义上下文 compact
|
|
9
9
|
* - 每轮全局策略判断,决定继续执行还是终止降级
|
|
10
10
|
* - 单轮支持多个 tool 并行执行(Plan and Execute)
|
|
11
11
|
* - 系统 Prompt 完全由外部注入
|
|
12
12
|
*/
|
|
13
|
-
export {
|
|
14
|
-
export type { Message, ToolCall, ToolDefinition, HarnessRecord, HarnessRecordStatus, ToolExecutionStartEvent, ToolExecutionEndEvent, LoopEvent, ToolDiscoverResult, ToolExecuteContext, Tool, ApprovalConfig, TurnPolicyResult, TurnPolicy,
|
|
13
|
+
export { runLoop } from './loop';
|
|
14
|
+
export type { Message, ToolCall, ToolDefinition, HarnessRecord, HarnessRecordStatus, ToolExecutionStartEvent, ToolExecutionEndEvent, LoopEvent, ToolDiscoverResult, ToolExecuteContext, Tool, ApprovalConfig, TurnPolicyResult, TurnPolicy, CompactConfig, LLMCaller, LLMCallOptions, LoopConfig, LoopResult, PendingApprovalInfo, } from './types';
|
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
* - 每次 tool 执行向上层回传执行前、执行后两个事件
|
|
7
7
|
* - 维护跨轮次的执行历史(Harness),tool 之间能互相看见
|
|
8
8
|
* - 每次调 LLM 之前动态决定暴露哪些 tool
|
|
9
|
-
* -
|
|
9
|
+
* - 支持自定义上下文 compact
|
|
10
10
|
* - 每轮全局策略判断,决定继续执行还是终止降级
|
|
11
11
|
* - 单轮支持多个 tool 并行执行(Plan and Execute)
|
|
12
12
|
* - 系统 Prompt 完全由外部注入
|
|
13
13
|
*/
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
15
|
+
exports.runLoop = void 0;
|
|
16
16
|
var loop_1 = require("./loop");
|
|
17
|
-
Object.defineProperty(exports, "
|
|
17
|
+
Object.defineProperty(exports, "runLoop", { enumerable: true, get: function () { return loop_1.runLoop; } });
|
|
@@ -1,23 +1,2 @@
|
|
|
1
1
|
import type { LoopConfig, LoopResult } from './types';
|
|
2
2
|
export declare function runLoop(config: LoopConfig): Promise<LoopResult>;
|
|
3
|
-
/**
|
|
4
|
-
* 便捷入口:直接指定模型运行 Function Call Loop。
|
|
5
|
-
* 内部自动创建 LLM Caller,无需手动实现 llmCaller.call。
|
|
6
|
-
*
|
|
7
|
-
* 环境变量配置:
|
|
8
|
-
* - `BAILIAN_API_KEY` — 必填
|
|
9
|
-
* - `BAILIAN_BASE_URL` — 可选,默认百炼兼容地址
|
|
10
|
-
* - `BAILIAN_DEFAULT_MODEL` — 可选,默认 `qwen-max`
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const result = await runLoopWithModel({
|
|
15
|
-
* messages: [
|
|
16
|
-
* { role: 'system', content: 'You are helpful' },
|
|
17
|
-
* { role: 'user', content: 'What is the weather?' },
|
|
18
|
-
* ],
|
|
19
|
-
* tools: [...],
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export declare function runLoopWithModel(config: Omit<LoopConfig, 'llmCaller'>): Promise<LoopResult>;
|
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.runLoop = runLoop;
|
|
13
|
-
exports.runLoopWithModel = runLoopWithModel;
|
|
14
13
|
const harness_1 = require("./harness");
|
|
15
14
|
const provider_1 = require("./provider");
|
|
16
15
|
/**
|
|
@@ -24,9 +23,19 @@ const provider_1 = require("./provider");
|
|
|
24
23
|
function buildInitialMessages(config) {
|
|
25
24
|
return [...config.messages];
|
|
26
25
|
}
|
|
26
|
+
const DEFAULT_BAILIAN_BASE_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
|
27
|
+
const DEFAULT_BAILIAN_MODEL = 'qwen-max';
|
|
27
28
|
function runLoop(config) {
|
|
28
29
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
var _a, _b, _c;
|
|
30
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
31
|
+
const llmCaller = (_a = config.llmCaller) !== null && _a !== void 0 ? _a : (0, provider_1.createLLMCaller)({
|
|
32
|
+
apiKey: (_b = process.env.BAILIAN_API_KEY) !== null && _b !== void 0 ? _b : '',
|
|
33
|
+
baseUrl: (_c = process.env.BAILIAN_BASE_URL) !== null && _c !== void 0 ? _c : DEFAULT_BAILIAN_BASE_URL,
|
|
34
|
+
defaultModel: (_d = process.env.BAILIAN_DEFAULT_MODEL) !== null && _d !== void 0 ? _d : DEFAULT_BAILIAN_MODEL,
|
|
35
|
+
});
|
|
36
|
+
if (!config.llmCaller && !process.env.BAILIAN_API_KEY) {
|
|
37
|
+
throw new Error('BAILIAN_API_KEY environment variable is not set.');
|
|
38
|
+
}
|
|
30
39
|
const harness = new harness_1.Harness();
|
|
31
40
|
const messages = buildInitialMessages(config);
|
|
32
41
|
let turn = 0;
|
|
@@ -40,10 +49,6 @@ function runLoop(config) {
|
|
|
40
49
|
turn++;
|
|
41
50
|
// 第二步:全局轮次策略
|
|
42
51
|
const decision = yield runTurnPolicy(config, harness, turn);
|
|
43
|
-
if (decision.message) {
|
|
44
|
-
messages.push({ role: 'assistant', content: decision.message });
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
52
|
if (decision.injectMessage) {
|
|
48
53
|
messages.push({ role: 'system', content: decision.injectMessage });
|
|
49
54
|
}
|
|
@@ -51,7 +56,7 @@ function runLoop(config) {
|
|
|
51
56
|
if (config.maxTurns !== undefined && turn >= config.maxTurns) {
|
|
52
57
|
messages.push({
|
|
53
58
|
role: 'assistant',
|
|
54
|
-
content: (
|
|
59
|
+
content: (_e = config.terminateMessage) !== null && _e !== void 0 ? _e : 'Max turns reached.',
|
|
55
60
|
});
|
|
56
61
|
break;
|
|
57
62
|
}
|
|
@@ -70,7 +75,7 @@ function runLoop(config) {
|
|
|
70
75
|
// 第四步:上下文压缩
|
|
71
76
|
const messagesForLLM = yield compressMessages(config, messages, turn);
|
|
72
77
|
// 第五步:调用 LLM
|
|
73
|
-
const llmResponse = yield
|
|
78
|
+
const llmResponse = yield llmCaller.call(messagesForLLM, toolDefinitions, {
|
|
74
79
|
model: config.model,
|
|
75
80
|
temperature: config.temperature,
|
|
76
81
|
maxTokens: config.maxTokens,
|
|
@@ -81,7 +86,7 @@ function runLoop(config) {
|
|
|
81
86
|
if (!llmResponse.tool_calls || llmResponse.tool_calls.length === 0) {
|
|
82
87
|
messages.push({
|
|
83
88
|
role: 'assistant',
|
|
84
|
-
content: (
|
|
89
|
+
content: (_f = llmResponse.content) !== null && _f !== void 0 ? _f : '',
|
|
85
90
|
});
|
|
86
91
|
return {
|
|
87
92
|
messages,
|
|
@@ -93,7 +98,7 @@ function runLoop(config) {
|
|
|
93
98
|
// 有 tool_calls,将 assistant message 加入 Messages
|
|
94
99
|
messages.push({
|
|
95
100
|
role: 'assistant',
|
|
96
|
-
content: (
|
|
101
|
+
content: (_g = llmResponse.content) !== null && _g !== void 0 ? _g : '',
|
|
97
102
|
tool_calls: llmResponse.tool_calls,
|
|
98
103
|
});
|
|
99
104
|
// 第七步:并行执行 tool
|
|
@@ -140,12 +145,12 @@ function runLoop(config) {
|
|
|
140
145
|
*/
|
|
141
146
|
function runTurnPolicy(config, harness, turn) {
|
|
142
147
|
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
var _a
|
|
148
|
+
var _a;
|
|
144
149
|
if (config.turnPolicy) {
|
|
145
150
|
try {
|
|
146
151
|
return yield config.turnPolicy(harness.getAll(), turn, config.metadata);
|
|
147
152
|
}
|
|
148
|
-
catch (
|
|
153
|
+
catch (_b) {
|
|
149
154
|
return {};
|
|
150
155
|
}
|
|
151
156
|
}
|
|
@@ -159,9 +164,7 @@ function runTurnPolicy(config, harness, turn) {
|
|
|
159
164
|
injectMessage: config.warningMessage,
|
|
160
165
|
};
|
|
161
166
|
}
|
|
162
|
-
return {
|
|
163
|
-
message: (_b = config.terminateMessage) !== null && _b !== void 0 ? _b : 'Max turns reached.',
|
|
164
|
-
};
|
|
167
|
+
return {};
|
|
165
168
|
});
|
|
166
169
|
}
|
|
167
170
|
/**
|
|
@@ -227,7 +230,7 @@ function groupMessagesByRound(messages) {
|
|
|
227
230
|
*/
|
|
228
231
|
function compressMessages(config, messages, _turn) {
|
|
229
232
|
return __awaiter(this, void 0, void 0, function* () {
|
|
230
|
-
if (!config.
|
|
233
|
+
if (!config.compact) {
|
|
231
234
|
return messages;
|
|
232
235
|
}
|
|
233
236
|
const firstAssistantIndex = messages.findIndex((m) => m.role === 'assistant');
|
|
@@ -237,14 +240,14 @@ function compressMessages(config, messages, _turn) {
|
|
|
237
240
|
const initialMessages = messages.slice(0, firstAssistantIndex);
|
|
238
241
|
const restMessages = messages.slice(firstAssistantIndex);
|
|
239
242
|
const rounds = groupMessagesByRound(restMessages);
|
|
240
|
-
if (rounds.length <= config.
|
|
243
|
+
if (rounds.length <= config.compact.keepRounds) {
|
|
241
244
|
return messages;
|
|
242
245
|
}
|
|
243
|
-
const keepCount = config.
|
|
246
|
+
const keepCount = config.compact.keepRounds;
|
|
244
247
|
const oldRounds = rounds.slice(0, rounds.length - keepCount);
|
|
245
248
|
const recentRounds = rounds.slice(rounds.length - keepCount);
|
|
246
249
|
try {
|
|
247
|
-
const compressed = yield config.
|
|
250
|
+
const compressed = yield config.compact.compress(oldRounds);
|
|
248
251
|
return [...initialMessages, ...compressed, ...recentRounds.flat()];
|
|
249
252
|
}
|
|
250
253
|
catch (_a) {
|
|
@@ -433,40 +436,3 @@ function executeToolsInParallel(config, harness, toolCalls, visibleToolNames, ha
|
|
|
433
436
|
return results;
|
|
434
437
|
});
|
|
435
438
|
}
|
|
436
|
-
const DEFAULT_BAILIAN_BASE_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
|
437
|
-
const DEFAULT_BAILIAN_MODEL = 'qwen-max';
|
|
438
|
-
/**
|
|
439
|
-
* 便捷入口:直接指定模型运行 Function Call Loop。
|
|
440
|
-
* 内部自动创建 LLM Caller,无需手动实现 llmCaller.call。
|
|
441
|
-
*
|
|
442
|
-
* 环境变量配置:
|
|
443
|
-
* - `BAILIAN_API_KEY` — 必填
|
|
444
|
-
* - `BAILIAN_BASE_URL` — 可选,默认百炼兼容地址
|
|
445
|
-
* - `BAILIAN_DEFAULT_MODEL` — 可选,默认 `qwen-max`
|
|
446
|
-
*
|
|
447
|
-
* @example
|
|
448
|
-
* ```ts
|
|
449
|
-
* const result = await runLoopWithModel({
|
|
450
|
-
* messages: [
|
|
451
|
-
* { role: 'system', content: 'You are helpful' },
|
|
452
|
-
* { role: 'user', content: 'What is the weather?' },
|
|
453
|
-
* ],
|
|
454
|
-
* tools: [...],
|
|
455
|
-
* });
|
|
456
|
-
* ```
|
|
457
|
-
*/
|
|
458
|
-
function runLoopWithModel(config) {
|
|
459
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
460
|
-
var _a, _b;
|
|
461
|
-
const apiKey = process.env.BAILIAN_API_KEY;
|
|
462
|
-
if (!apiKey) {
|
|
463
|
-
throw new Error('BAILIAN_API_KEY environment variable is not set.');
|
|
464
|
-
}
|
|
465
|
-
const llmCaller = (0, provider_1.createLLMCaller)({
|
|
466
|
-
apiKey,
|
|
467
|
-
baseUrl: (_a = process.env.BAILIAN_BASE_URL) !== null && _a !== void 0 ? _a : DEFAULT_BAILIAN_BASE_URL,
|
|
468
|
-
defaultModel: (_b = process.env.BAILIAN_DEFAULT_MODEL) !== null && _b !== void 0 ? _b : DEFAULT_BAILIAN_MODEL,
|
|
469
|
-
});
|
|
470
|
-
return runLoop(Object.assign(Object.assign({}, config), { llmCaller }));
|
|
471
|
-
});
|
|
472
|
-
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Function Call Loop
|
|
3
|
-
*
|
|
2
|
+
* Function Call Loop 的 LLM Provider 适配器。
|
|
3
|
+
* 基于 predict 模块的 `callChatCompletions` 构建,共用底层 OpenAI 协议 HTTP 层。
|
|
4
4
|
*/
|
|
5
5
|
import type { Message, ToolCall, ToolDefinition, LLMCallOptions } from './types';
|
|
6
6
|
export interface ProviderOptions {
|
|
@@ -10,6 +10,35 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.createLLMCaller = createLLMCaller;
|
|
13
|
+
const llm_provider_1 = require("../llm_provider");
|
|
14
|
+
function toOpenAIMessages(messages) {
|
|
15
|
+
return messages.map((m) => {
|
|
16
|
+
var _a;
|
|
17
|
+
return ({
|
|
18
|
+
role: m.role,
|
|
19
|
+
content: m.content,
|
|
20
|
+
tool_calls: (_a = m.tool_calls) === null || _a === void 0 ? void 0 : _a.map((tc) => ({
|
|
21
|
+
id: tc.id,
|
|
22
|
+
type: tc.type,
|
|
23
|
+
function: {
|
|
24
|
+
name: tc.function.name,
|
|
25
|
+
arguments: tc.function.arguments,
|
|
26
|
+
},
|
|
27
|
+
})),
|
|
28
|
+
tool_call_id: m.tool_call_id,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function toOpenAITools(tools) {
|
|
33
|
+
return tools.map((t) => ({
|
|
34
|
+
type: t.type,
|
|
35
|
+
function: {
|
|
36
|
+
name: t.function.name,
|
|
37
|
+
description: t.function.description,
|
|
38
|
+
parameters: t.function.parameters,
|
|
39
|
+
},
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
13
42
|
/**
|
|
14
43
|
* 基于 OpenAI 协议的 LLM 调用器。
|
|
15
44
|
*/
|
|
@@ -17,66 +46,19 @@ function createLLMCaller(options) {
|
|
|
17
46
|
return {
|
|
18
47
|
call(messages, tools, callOptions) {
|
|
19
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
var _a, _b
|
|
21
|
-
const
|
|
22
|
-
const body = {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
const data = yield (0, llm_provider_1.callChatCompletions)(options.baseUrl, options.apiKey, {
|
|
23
51
|
model: (_a = callOptions === null || callOptions === void 0 ? void 0 : callOptions.model) !== null && _a !== void 0 ? _a : options.defaultModel,
|
|
24
|
-
messages: messages
|
|
25
|
-
var _a;
|
|
26
|
-
return ({
|
|
27
|
-
role: m.role,
|
|
28
|
-
content: m.content,
|
|
29
|
-
tool_calls: (_a = m.tool_calls) === null || _a === void 0 ? void 0 : _a.map((tc) => ({
|
|
30
|
-
id: tc.id,
|
|
31
|
-
type: tc.type,
|
|
32
|
-
function: {
|
|
33
|
-
name: tc.function.name,
|
|
34
|
-
arguments: tc.function.arguments,
|
|
35
|
-
},
|
|
36
|
-
})),
|
|
37
|
-
tool_call_id: m.tool_call_id,
|
|
38
|
-
});
|
|
39
|
-
}),
|
|
52
|
+
messages: toOpenAIMessages(messages),
|
|
40
53
|
temperature: callOptions === null || callOptions === void 0 ? void 0 : callOptions.temperature,
|
|
41
54
|
max_tokens: callOptions === null || callOptions === void 0 ? void 0 : callOptions.maxTokens,
|
|
42
55
|
top_p: callOptions === null || callOptions === void 0 ? void 0 : callOptions.topP,
|
|
43
|
-
tools: tools.length > 0
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
function: {
|
|
47
|
-
name: t.function.name,
|
|
48
|
-
description: t.function.description,
|
|
49
|
-
parameters: t.function.parameters,
|
|
50
|
-
},
|
|
51
|
-
}))
|
|
52
|
-
: undefined,
|
|
53
|
-
};
|
|
54
|
-
let response;
|
|
55
|
-
try {
|
|
56
|
-
response = yield fetch(url, {
|
|
57
|
-
method: 'POST',
|
|
58
|
-
headers: {
|
|
59
|
-
'Content-Type': 'application/json',
|
|
60
|
-
Authorization: `Bearer ${options.apiKey}`,
|
|
61
|
-
},
|
|
62
|
-
body: JSON.stringify(body),
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
catch (cause) {
|
|
66
|
-
throw new Error(`[llm] ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
67
|
-
}
|
|
68
|
-
if (!response.ok) {
|
|
69
|
-
const text = yield response.text();
|
|
70
|
-
throw new Error(`[llm] HTTP ${response.status}: ${text}`);
|
|
71
|
-
}
|
|
72
|
-
const data = (yield response.json());
|
|
73
|
-
const choice = (_b = data.choices) === null || _b === void 0 ? void 0 : _b[0];
|
|
74
|
-
if (!choice) {
|
|
75
|
-
throw new Error('[llm] No choice in response');
|
|
76
|
-
}
|
|
56
|
+
tools: tools.length > 0 ? toOpenAITools(tools) : undefined,
|
|
57
|
+
}, 'llm');
|
|
58
|
+
const choice = data.choices[0];
|
|
77
59
|
return {
|
|
78
60
|
content: choice.message.content,
|
|
79
|
-
tool_calls: (
|
|
61
|
+
tool_calls: (_b = choice.message.tool_calls) === null || _b === void 0 ? void 0 : _b.map((tc) => ({
|
|
80
62
|
id: tc.id,
|
|
81
63
|
type: tc.type,
|
|
82
64
|
function: {
|
|
@@ -121,8 +121,6 @@ export interface Tool {
|
|
|
121
121
|
* Turn Policy 决策结果。
|
|
122
122
|
*/
|
|
123
123
|
export interface TurnPolicyResult {
|
|
124
|
-
/** 终止 Loop,此消息作为 assistant 回复写入历史 */
|
|
125
|
-
readonly message?: string;
|
|
126
124
|
/** 继续执行前作为 system 消息注入 */
|
|
127
125
|
readonly injectMessage?: string;
|
|
128
126
|
}
|
|
@@ -133,7 +131,7 @@ export type TurnPolicy = (harness: ReadonlyArray<HarnessRecord>, turn: number, m
|
|
|
133
131
|
/**
|
|
134
132
|
* 上下文压缩配置。
|
|
135
133
|
*/
|
|
136
|
-
export interface
|
|
134
|
+
export interface CompactConfig {
|
|
137
135
|
readonly keepRounds: number;
|
|
138
136
|
readonly compress: (messages: ReadonlyArray<ReadonlyArray<Message>>) => Message[] | Promise<Message[]>;
|
|
139
137
|
}
|
|
@@ -163,10 +161,10 @@ export interface LoopConfig {
|
|
|
163
161
|
/** 初始消息列表,直接作为对话起点 */
|
|
164
162
|
readonly messages: ReadonlyArray<Message>;
|
|
165
163
|
readonly tools: ReadonlyArray<Tool>;
|
|
166
|
-
readonly llmCaller
|
|
164
|
+
readonly llmCaller?: LLMCaller;
|
|
167
165
|
readonly maxTurns?: number;
|
|
168
166
|
readonly turnPolicy?: TurnPolicy;
|
|
169
|
-
readonly
|
|
167
|
+
readonly compact?: CompactConfig;
|
|
170
168
|
readonly metadata?: unknown;
|
|
171
169
|
/** 告警文本:当 turnPolicy 未自定义且当前轮次等于 maxTurns - 1 时塞入 Messages */
|
|
172
170
|
readonly warningMessage?: string;
|
|
@@ -177,7 +175,7 @@ export interface LoopConfig {
|
|
|
177
175
|
/** 取消信号 */
|
|
178
176
|
readonly signal?: AbortSignal;
|
|
179
177
|
/** 模型名称。未设置时从环境变量或 Provider 默认值读取 */
|
|
180
|
-
readonly model?:
|
|
178
|
+
readonly model?: import('../llm_provider/models').Model;
|
|
181
179
|
/** 采样温度,范围 0~2 */
|
|
182
180
|
readonly temperature?: number;
|
|
183
181
|
/** 单次 LLM 调用的最大输出 token 数 */
|
package/dist/index.d.ts
CHANGED
|
@@ -26,9 +26,10 @@ export type { LLMProvider, PredictorOptions } from './predict';
|
|
|
26
26
|
export { Predictor } from './predict';
|
|
27
27
|
/** 百炼 Provider 实现 */
|
|
28
28
|
export { BailianProvider } from './predict';
|
|
29
|
-
/**
|
|
30
|
-
export { MODEL_REGISTRY } from './
|
|
31
|
-
export type { Model
|
|
29
|
+
/** 模型注册表与枚举,定义模型与 Provider 的映射关系 */
|
|
30
|
+
export { MODEL_REGISTRY } from './llm_provider';
|
|
31
|
+
export type { Model } from './llm_provider';
|
|
32
|
+
export type { PredictConfig, PredictWithMessagesConfig } from './predict';
|
|
32
33
|
/** 向量检索(Embedding + pgvector + Rerank) */
|
|
33
34
|
export { EmbeddingSearch, embed } from './embedding_search';
|
|
34
35
|
export type { EmbeddingSearchConfig, SearchResult } from './embedding_search';
|
package/dist/index.js
CHANGED
|
@@ -63,9 +63,9 @@ Object.defineProperty(exports, "Predictor", { enumerable: true, get: function ()
|
|
|
63
63
|
/** 百炼 Provider 实现 */
|
|
64
64
|
var predict_3 = require("./predict");
|
|
65
65
|
Object.defineProperty(exports, "BailianProvider", { enumerable: true, get: function () { return predict_3.BailianProvider; } });
|
|
66
|
-
/**
|
|
67
|
-
var
|
|
68
|
-
Object.defineProperty(exports, "MODEL_REGISTRY", { enumerable: true, get: function () { return
|
|
66
|
+
/** 模型注册表与枚举,定义模型与 Provider 的映射关系 */
|
|
67
|
+
var llm_provider_1 = require("./llm_provider");
|
|
68
|
+
Object.defineProperty(exports, "MODEL_REGISTRY", { enumerable: true, get: function () { return llm_provider_1.MODEL_REGISTRY; } });
|
|
69
69
|
/** 向量检索(Embedding + pgvector + Rerank) */
|
|
70
70
|
var embedding_search_1 = require("./embedding_search");
|
|
71
71
|
Object.defineProperty(exports, "EmbeddingSearch", { enumerable: true, get: function () { return embedding_search_1.EmbeddingSearch; } });
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { LLMProvider } from './llm';
|
|
2
|
+
import type { LLMRequest, LLMResponse, ProviderConfig, StreamChunk } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* 阿里云百炼(兼容 OpenAI 协议)Provider 实现。
|
|
5
|
+
* 支持非流式调用和流式 SSE 输出。
|
|
6
|
+
*/
|
|
7
|
+
export declare class BailianProvider implements LLMProvider {
|
|
8
|
+
readonly config: ProviderConfig;
|
|
9
|
+
readonly name = "bailian";
|
|
10
|
+
private static readonly SUPPORTED_MODELS;
|
|
11
|
+
constructor(config: ProviderConfig);
|
|
12
|
+
supports(model: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 发送非流式 chat completion 请求到百炼服务。
|
|
15
|
+
* @param request - 标准化 LLM 请求
|
|
16
|
+
* @returns 解析后的模型响应(含 content、usage、model)
|
|
17
|
+
* @throws Error - 网络异常时抛 `[bailian] ...`;HTTP 非 2xx 时抛 `[bailian] HTTP {status}: ...`
|
|
18
|
+
*/
|
|
19
|
+
generate(request: LLMRequest): Promise<LLMResponse>;
|
|
20
|
+
/**
|
|
21
|
+
* 发送流式 chat completion 请求,解析 SSE 响应逐块返回。
|
|
22
|
+
* @param request - 标准化 LLM 请求
|
|
23
|
+
* @yields 内容片段(`content`)或结束标记(`finish`)
|
|
24
|
+
* @throws Error - 网络异常或 HTTP 错误时抛出
|
|
25
|
+
*/
|
|
26
|
+
stream(request: LLMRequest): AsyncGenerator<StreamChunk, void, unknown>;
|
|
27
|
+
private buildRequestBody;
|
|
28
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
12
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
13
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
14
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
15
|
+
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
16
|
+
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
17
|
+
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
18
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
19
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
20
|
+
function fulfill(value) { resume("next", value); }
|
|
21
|
+
function reject(value) { resume("throw", value); }
|
|
22
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.BailianProvider = void 0;
|
|
26
|
+
const index_1 = require("./index");
|
|
27
|
+
/**
|
|
28
|
+
* 阿里云百炼(兼容 OpenAI 协议)Provider 实现。
|
|
29
|
+
* 支持非流式调用和流式 SSE 输出。
|
|
30
|
+
*/
|
|
31
|
+
class BailianProvider {
|
|
32
|
+
constructor(config) {
|
|
33
|
+
this.config = config;
|
|
34
|
+
this.name = 'bailian';
|
|
35
|
+
}
|
|
36
|
+
supports(model) {
|
|
37
|
+
return BailianProvider.SUPPORTED_MODELS.has(model);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* 发送非流式 chat completion 请求到百炼服务。
|
|
41
|
+
* @param request - 标准化 LLM 请求
|
|
42
|
+
* @returns 解析后的模型响应(含 content、usage、model)
|
|
43
|
+
* @throws Error - 网络异常时抛 `[bailian] ...`;HTTP 非 2xx 时抛 `[bailian] HTTP {status}: ...`
|
|
44
|
+
*/
|
|
45
|
+
generate(request) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
var _a;
|
|
48
|
+
const body = this.buildRequestBody(request);
|
|
49
|
+
const data = yield (0, index_1.callChatCompletions)(this.config.baseUrl, this.config.apiKey, Object.assign(Object.assign({}, body), { stream: false }), this.name);
|
|
50
|
+
const choice = data.choices[0];
|
|
51
|
+
return {
|
|
52
|
+
content: choice.message.content,
|
|
53
|
+
usage: data.usage
|
|
54
|
+
? {
|
|
55
|
+
promptTokens: data.usage.prompt_tokens,
|
|
56
|
+
completionTokens: data.usage.completion_tokens,
|
|
57
|
+
totalTokens: data.usage.total_tokens,
|
|
58
|
+
}
|
|
59
|
+
: undefined,
|
|
60
|
+
model: (_a = data.model) !== null && _a !== void 0 ? _a : body.model,
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 发送流式 chat completion 请求,解析 SSE 响应逐块返回。
|
|
66
|
+
* @param request - 标准化 LLM 请求
|
|
67
|
+
* @yields 内容片段(`content`)或结束标记(`finish`)
|
|
68
|
+
* @throws Error - 网络异常或 HTTP 错误时抛出
|
|
69
|
+
*/
|
|
70
|
+
stream(request) {
|
|
71
|
+
return __asyncGenerator(this, arguments, function* stream_1() {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
const url = `${this.config.baseUrl}/chat/completions`;
|
|
74
|
+
const body = this.buildRequestBody(request);
|
|
75
|
+
let response;
|
|
76
|
+
try {
|
|
77
|
+
response = yield __await(fetch(url, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
'Content-Type': 'application/json',
|
|
81
|
+
Authorization: `Bearer ${this.config.apiKey}`,
|
|
82
|
+
},
|
|
83
|
+
body: JSON.stringify(Object.assign(Object.assign({}, body), { stream: true })),
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
catch (cause) {
|
|
87
|
+
throw new Error(`[${this.name}] ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
88
|
+
}
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const text = yield __await(response.text());
|
|
91
|
+
throw new Error(`[${this.name}] HTTP ${response.status}: ${text}`);
|
|
92
|
+
}
|
|
93
|
+
if (!response.body) {
|
|
94
|
+
throw new Error(`[${this.name}] Response body is null`);
|
|
95
|
+
}
|
|
96
|
+
const reader = response.body.getReader();
|
|
97
|
+
const decoder = new TextDecoder();
|
|
98
|
+
let buffer = '';
|
|
99
|
+
try {
|
|
100
|
+
while (true) {
|
|
101
|
+
const { done, value } = yield __await(reader.read());
|
|
102
|
+
if (done)
|
|
103
|
+
break;
|
|
104
|
+
buffer += decoder.decode(value, { stream: true });
|
|
105
|
+
const lines = buffer.split('\n');
|
|
106
|
+
buffer = (_a = lines.pop()) !== null && _a !== void 0 ? _a : '';
|
|
107
|
+
for (const line of lines) {
|
|
108
|
+
const trimmed = line.trim();
|
|
109
|
+
if (!trimmed || !trimmed.startsWith('data: '))
|
|
110
|
+
continue;
|
|
111
|
+
const data = trimmed.slice(6);
|
|
112
|
+
if (data === '[DONE]')
|
|
113
|
+
continue;
|
|
114
|
+
let parsed;
|
|
115
|
+
try {
|
|
116
|
+
parsed = JSON.parse(data);
|
|
117
|
+
}
|
|
118
|
+
catch (_c) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const choice = (_b = parsed.choices) === null || _b === void 0 ? void 0 : _b[0];
|
|
122
|
+
if (!choice)
|
|
123
|
+
continue;
|
|
124
|
+
const delta = choice.delta;
|
|
125
|
+
if (delta.content) {
|
|
126
|
+
yield yield __await({ type: 'content', delta: delta.content });
|
|
127
|
+
}
|
|
128
|
+
if (choice.finish_reason && parsed.usage) {
|
|
129
|
+
yield yield __await({
|
|
130
|
+
type: 'finish',
|
|
131
|
+
usage: {
|
|
132
|
+
promptTokens: parsed.usage.prompt_tokens,
|
|
133
|
+
completionTokens: parsed.usage.completion_tokens,
|
|
134
|
+
totalTokens: parsed.usage.total_tokens,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
yield yield __await({ type: 'finish' });
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
reader.releaseLock();
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
buildRequestBody(request) {
|
|
148
|
+
var _a;
|
|
149
|
+
return {
|
|
150
|
+
model: (_a = request.model) !== null && _a !== void 0 ? _a : this.config.defaultModel,
|
|
151
|
+
messages: request.messages.map((m) => ({
|
|
152
|
+
role: m.role,
|
|
153
|
+
content: m.content,
|
|
154
|
+
})),
|
|
155
|
+
temperature: request.temperature,
|
|
156
|
+
max_tokens: request.maxTokens,
|
|
157
|
+
top_p: request.topP,
|
|
158
|
+
response_format: request.responseFormat
|
|
159
|
+
? { type: request.responseFormat === 'json' ? 'json_object' : 'text' }
|
|
160
|
+
: undefined,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.BailianProvider = BailianProvider;
|
|
165
|
+
BailianProvider.SUPPORTED_MODELS = new Set([
|
|
166
|
+
'qwen3.7-max',
|
|
167
|
+
'qwen-plus',
|
|
168
|
+
'qwen-turbo',
|
|
169
|
+
'qwq-plus',
|
|
170
|
+
'deepseek-v4-pro',
|
|
171
|
+
'deepseek-v4-flash',
|
|
172
|
+
'kimi-k2.6',
|
|
173
|
+
'qwen-vl-plus',
|
|
174
|
+
]);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM Provider 底层 HTTP 调用层。
|
|
3
|
+
* Predict 模块和 Function Call Loop 模块共用,只负责纯粹的请求/响应/错误处理。
|
|
4
|
+
*/
|
|
5
|
+
export interface OpenAIChatMessage {
|
|
6
|
+
role: string;
|
|
7
|
+
content: string | null;
|
|
8
|
+
tool_calls?: Array<{
|
|
9
|
+
id: string;
|
|
10
|
+
type: 'function';
|
|
11
|
+
function: {
|
|
12
|
+
name: string;
|
|
13
|
+
arguments: string;
|
|
14
|
+
};
|
|
15
|
+
}>;
|
|
16
|
+
tool_call_id?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface OpenAIChatTool {
|
|
19
|
+
type: 'function';
|
|
20
|
+
function: {
|
|
21
|
+
name: string;
|
|
22
|
+
description: string;
|
|
23
|
+
parameters: unknown;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface OpenAIChatRequest {
|
|
27
|
+
model: string;
|
|
28
|
+
messages: OpenAIChatMessage[];
|
|
29
|
+
temperature?: number;
|
|
30
|
+
max_tokens?: number;
|
|
31
|
+
top_p?: number;
|
|
32
|
+
tools?: OpenAIChatTool[];
|
|
33
|
+
response_format?: {
|
|
34
|
+
type: 'text' | 'json_object';
|
|
35
|
+
};
|
|
36
|
+
stream?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface OpenAIChatResponse {
|
|
39
|
+
choices: Array<{
|
|
40
|
+
message: {
|
|
41
|
+
content: string | null;
|
|
42
|
+
tool_calls?: Array<{
|
|
43
|
+
id: string;
|
|
44
|
+
type: 'function';
|
|
45
|
+
function: {
|
|
46
|
+
name: string;
|
|
47
|
+
arguments: string;
|
|
48
|
+
};
|
|
49
|
+
}>;
|
|
50
|
+
};
|
|
51
|
+
finish_reason: string | null;
|
|
52
|
+
}>;
|
|
53
|
+
usage?: {
|
|
54
|
+
prompt_tokens: number;
|
|
55
|
+
completion_tokens: number;
|
|
56
|
+
total_tokens: number;
|
|
57
|
+
};
|
|
58
|
+
model?: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 发送 OpenAI 兼容的 chat completions 请求。
|
|
62
|
+
* 只做 HTTP 层:构造请求、fetch、错误处理、基础解析。
|
|
63
|
+
* 业务层(请求体组装、结果转换)由调用方负责。
|
|
64
|
+
*/
|
|
65
|
+
export declare function callChatCompletions(baseUrl: string, apiKey: string, body: OpenAIChatRequest, errorPrefix?: string): Promise<OpenAIChatResponse>;
|
|
66
|
+
/** 模型枚举与注册表 */
|
|
67
|
+
export type { Model, ModelConfig } from './models';
|
|
68
|
+
export { MODEL_REGISTRY } from './models';
|
|
69
|
+
/** 标准化类型 */
|
|
70
|
+
export type { Message, LLMRequest, LLMResponse, ProviderConfig, StreamChunk } from './types';
|
|
71
|
+
/** Provider 接口与实现 */
|
|
72
|
+
export type { LLMProvider } from './llm';
|
|
73
|
+
export { BailianProvider } from './bailian';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LLM Provider 底层 HTTP 调用层。
|
|
4
|
+
* Predict 模块和 Function Call Loop 模块共用,只负责纯粹的请求/响应/错误处理。
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.BailianProvider = exports.MODEL_REGISTRY = void 0;
|
|
17
|
+
exports.callChatCompletions = callChatCompletions;
|
|
18
|
+
/**
|
|
19
|
+
* 发送 OpenAI 兼容的 chat completions 请求。
|
|
20
|
+
* 只做 HTTP 层:构造请求、fetch、错误处理、基础解析。
|
|
21
|
+
* 业务层(请求体组装、结果转换)由调用方负责。
|
|
22
|
+
*/
|
|
23
|
+
function callChatCompletions(baseUrl_1, apiKey_1, body_1) {
|
|
24
|
+
return __awaiter(this, arguments, void 0, function* (baseUrl, apiKey, body, errorPrefix = 'llm') {
|
|
25
|
+
var _a;
|
|
26
|
+
const url = `${baseUrl}/chat/completions`;
|
|
27
|
+
let response;
|
|
28
|
+
try {
|
|
29
|
+
response = yield fetch(url, {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
Authorization: `Bearer ${apiKey}`,
|
|
34
|
+
},
|
|
35
|
+
body: JSON.stringify(body),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch (cause) {
|
|
39
|
+
throw new Error(`[${errorPrefix}] ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
40
|
+
}
|
|
41
|
+
if (!response.ok) {
|
|
42
|
+
const text = yield response.text();
|
|
43
|
+
throw new Error(`[${errorPrefix}] HTTP ${response.status}: ${text}`);
|
|
44
|
+
}
|
|
45
|
+
let data;
|
|
46
|
+
try {
|
|
47
|
+
data = (yield response.json());
|
|
48
|
+
}
|
|
49
|
+
catch (cause) {
|
|
50
|
+
throw new Error(`[${errorPrefix}] Failed to parse response: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
51
|
+
}
|
|
52
|
+
const choice = (_a = data.choices) === null || _a === void 0 ? void 0 : _a[0];
|
|
53
|
+
if (!choice) {
|
|
54
|
+
throw new Error(`[${errorPrefix}] No choice in response`);
|
|
55
|
+
}
|
|
56
|
+
return data;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
var models_1 = require("./models");
|
|
60
|
+
Object.defineProperty(exports, "MODEL_REGISTRY", { enumerable: true, get: function () { return models_1.MODEL_REGISTRY; } });
|
|
61
|
+
var bailian_1 = require("./bailian");
|
|
62
|
+
Object.defineProperty(exports, "BailianProvider", { enumerable: true, get: function () { return bailian_1.BailianProvider; } });
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { LLMRequest, LLMResponse, ProviderConfig, StreamChunk } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* LLM Provider 抽象接口。每个 Provider 实现负责对接具体的模型服务
|
|
4
|
+
*(如百炼、OpenAI、Anthropic 等),处理 HTTP 请求、流式解析和错误转换。
|
|
5
|
+
*/
|
|
6
|
+
export interface LLMProvider {
|
|
7
|
+
/** Provider 标识名,用于路由和故障转移日志 */
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** Provider 配置(API Key、Base URL、默认模型等) */
|
|
10
|
+
readonly config: ProviderConfig;
|
|
11
|
+
/**
|
|
12
|
+
* 校验当前 Provider 是否支持指定模型。
|
|
13
|
+
* 框架层在 MODEL_REGISTRY 候选过滤后,再调用此方法做二次确认。
|
|
14
|
+
* @param model - 模型标识名
|
|
15
|
+
* @returns true 表示支持,false 表示不支持(将自动轮询下一个候选)
|
|
16
|
+
*/
|
|
17
|
+
supports(model: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* 发送非流式请求,返回完整的模型响应。
|
|
20
|
+
* @param request - LLM 请求参数
|
|
21
|
+
* @returns 模型生成的完整响应
|
|
22
|
+
* @throws 网络异常、HTTP 错误、解析失败等均抛 {@link Error}
|
|
23
|
+
*/
|
|
24
|
+
generate(request: LLMRequest): Promise<LLMResponse>;
|
|
25
|
+
/**
|
|
26
|
+
* 发送流式请求,逐块返回模型输出。
|
|
27
|
+
* @param request - LLM 请求参数
|
|
28
|
+
* @yields 内容片段或结束标记
|
|
29
|
+
* @throws 网络异常、HTTP 错误等均抛 {@link Error}
|
|
30
|
+
*/
|
|
31
|
+
stream(request: LLMRequest): AsyncGenerator<StreamChunk, void, unknown>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 支持的模型枚举。项目层通过此枚举选择模型,Axiom 内部路由到对应 Provider。
|
|
3
|
+
*/
|
|
4
|
+
export type Model = 'qwen3.7-max' | 'qwen-plus' | 'qwen-turbo' | 'qwq-plus' | 'deepseek-v4-pro' | 'deepseek-v4-flash' | 'kimi-k2.6' | 'qwen-vl-plus';
|
|
5
|
+
/** 模型到 Provider 的映射配置 */
|
|
6
|
+
export interface ModelConfig {
|
|
7
|
+
readonly model: string;
|
|
8
|
+
readonly provider: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 模型注册表。每个模型对应一个或多个 Provider 候选,按优先级排序。
|
|
12
|
+
* 当首选 Provider 失败时,Predictor 按此表顺序尝试下一个。
|
|
13
|
+
*/
|
|
14
|
+
export declare const MODEL_REGISTRY: Readonly<Record<Model, ReadonlyArray<ModelConfig>>>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MODEL_REGISTRY = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 模型注册表。每个模型对应一个或多个 Provider 候选,按优先级排序。
|
|
6
|
+
* 当首选 Provider 失败时,Predictor 按此表顺序尝试下一个。
|
|
7
|
+
*/
|
|
8
|
+
exports.MODEL_REGISTRY = {
|
|
9
|
+
'qwen3.7-max': [{ model: 'qwen3.7-max', provider: 'bailian' }],
|
|
10
|
+
'qwen-plus': [{ model: 'qwen-plus', provider: 'bailian' }],
|
|
11
|
+
'qwen-turbo': [{ model: 'qwen-turbo', provider: 'bailian' }],
|
|
12
|
+
'qwq-plus': [{ model: 'qwq-plus', provider: 'bailian' }],
|
|
13
|
+
'deepseek-v4-pro': [{ model: 'deepseek-v4-pro', provider: 'bailian' }],
|
|
14
|
+
'deepseek-v4-flash': [{ model: 'deepseek-v4-flash', provider: 'bailian' }],
|
|
15
|
+
'kimi-k2.6': [{ model: 'kimi-k2.6', provider: 'bailian' }],
|
|
16
|
+
'qwen-vl-plus': [{ model: 'qwen-vl-plus', provider: 'bailian' }],
|
|
17
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM 对话消息。支持 system、user、assistant 三种角色。
|
|
3
|
+
*/
|
|
4
|
+
export interface Message {
|
|
5
|
+
readonly role: 'system' | 'user' | 'assistant';
|
|
6
|
+
readonly content: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 标准化 LLM 请求参数。各 Provider 据此构建自身的协议请求。
|
|
10
|
+
*/
|
|
11
|
+
export interface LLMRequest {
|
|
12
|
+
readonly messages: ReadonlyArray<Message>;
|
|
13
|
+
readonly temperature?: number;
|
|
14
|
+
readonly maxTokens?: number;
|
|
15
|
+
readonly topP?: number;
|
|
16
|
+
readonly stream?: boolean;
|
|
17
|
+
readonly model?: string;
|
|
18
|
+
readonly responseFormat?: 'text' | 'json';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 标准化 LLM 响应。各 Provider 将原始响应解析为此格式后返回。
|
|
22
|
+
*/
|
|
23
|
+
export interface LLMResponse {
|
|
24
|
+
readonly content: string | null;
|
|
25
|
+
readonly usage?: {
|
|
26
|
+
readonly promptTokens: number;
|
|
27
|
+
readonly completionTokens: number;
|
|
28
|
+
readonly totalTokens: number;
|
|
29
|
+
};
|
|
30
|
+
readonly model: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 流式输出片段。迭代器每次 yield 一个 chunk。
|
|
34
|
+
*/
|
|
35
|
+
export type StreamChunk = {
|
|
36
|
+
readonly type: 'content';
|
|
37
|
+
readonly delta: string;
|
|
38
|
+
} | {
|
|
39
|
+
readonly type: 'finish';
|
|
40
|
+
usage?: LLMResponse['usage'];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Provider 配置。每个 Provider 实例需要一组连接参数。
|
|
44
|
+
*/
|
|
45
|
+
export interface ProviderConfig {
|
|
46
|
+
readonly name: string;
|
|
47
|
+
readonly apiKey: string;
|
|
48
|
+
readonly baseUrl: string;
|
|
49
|
+
readonly defaultModel: string;
|
|
50
|
+
readonly timeoutMs?: number;
|
|
51
|
+
}
|
package/dist/predict/config.d.ts
CHANGED
package/dist/predict/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export type { LLMRequest, LLMResponse, Message, ProviderConfig, StreamChunk, } from '
|
|
2
|
-
export type { LLMProvider
|
|
1
|
+
export type { LLMRequest, LLMResponse, Message, ProviderConfig, StreamChunk, } from '../llm_provider/types';
|
|
2
|
+
export type { LLMProvider } from '../llm_provider/llm';
|
|
3
|
+
export type { PredictorOptions } from './llm';
|
|
3
4
|
export { Predictor } from './llm';
|
|
4
|
-
export { BailianProvider } from '
|
|
5
|
-
export type { Model } from '
|
|
6
|
-
export { MODEL_REGISTRY } from '
|
|
5
|
+
export { BailianProvider } from '../llm_provider/bailian';
|
|
6
|
+
export type { Model } from '../llm_provider/models';
|
|
7
|
+
export { MODEL_REGISTRY } from '../llm_provider/models';
|
|
7
8
|
export type { PredictConfig, PredictWithMessagesConfig } from './config';
|
|
8
9
|
export { LLM } from './predict';
|
package/dist/predict/index.js
CHANGED
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.LLM = exports.MODEL_REGISTRY = exports.BailianProvider = exports.Predictor = void 0;
|
|
4
4
|
var llm_1 = require("./llm");
|
|
5
5
|
Object.defineProperty(exports, "Predictor", { enumerable: true, get: function () { return llm_1.Predictor; } });
|
|
6
|
-
var bailian_1 = require("
|
|
6
|
+
var bailian_1 = require("../llm_provider/bailian");
|
|
7
7
|
Object.defineProperty(exports, "BailianProvider", { enumerable: true, get: function () { return bailian_1.BailianProvider; } });
|
|
8
|
-
var models_1 = require("
|
|
8
|
+
var models_1 = require("../llm_provider/models");
|
|
9
9
|
Object.defineProperty(exports, "MODEL_REGISTRY", { enumerable: true, get: function () { return models_1.MODEL_REGISTRY; } });
|
|
10
10
|
var predict_1 = require("./predict");
|
|
11
11
|
Object.defineProperty(exports, "LLM", { enumerable: true, get: function () { return predict_1.LLM; } });
|
package/dist/predict/llm.d.ts
CHANGED
|
@@ -1,36 +1,6 @@
|
|
|
1
|
-
import type { LLMRequest, LLMResponse,
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
* LLM Provider 抽象接口。每个 Provider 实现负责对接具体的模型服务
|
|
5
|
-
*(如百炼、OpenAI、Anthropic 等),处理 HTTP 请求、流式解析和错误转换。
|
|
6
|
-
*/
|
|
7
|
-
export interface LLMProvider {
|
|
8
|
-
/** Provider 标识名,用于路由和故障转移日志 */
|
|
9
|
-
readonly name: string;
|
|
10
|
-
/** Provider 配置(API Key、Base URL、默认模型等) */
|
|
11
|
-
readonly config: ProviderConfig;
|
|
12
|
-
/**
|
|
13
|
-
* 校验当前 Provider 是否支持指定模型。
|
|
14
|
-
* 框架层在 MODEL_REGISTRY 候选过滤后,再调用此方法做二次确认。
|
|
15
|
-
* @param model - 模型标识名
|
|
16
|
-
* @returns true 表示支持,false 表示不支持(将自动轮询下一个候选)
|
|
17
|
-
*/
|
|
18
|
-
supports(model: string): boolean;
|
|
19
|
-
/**
|
|
20
|
-
* 发送非流式请求,返回完整的模型响应。
|
|
21
|
-
* @param request - LLM 请求参数
|
|
22
|
-
* @returns 模型生成的完整响应
|
|
23
|
-
* @throws 网络异常、HTTP 错误、解析失败等均抛 {@link Error}
|
|
24
|
-
*/
|
|
25
|
-
generate(request: LLMRequest): Promise<LLMResponse>;
|
|
26
|
-
/**
|
|
27
|
-
* 发送流式请求,逐块返回模型输出。
|
|
28
|
-
* @param request - LLM 请求参数
|
|
29
|
-
* @yields 内容片段或结束标记
|
|
30
|
-
* @throws 网络异常、HTTP 错误等均抛 {@link Error}
|
|
31
|
-
*/
|
|
32
|
-
stream(request: LLMRequest): AsyncGenerator<StreamChunk, void, unknown>;
|
|
33
|
-
}
|
|
1
|
+
import type { LLMRequest, LLMResponse, StreamChunk } from '../llm_provider/types';
|
|
2
|
+
import type { LLMProvider } from '../llm_provider/llm';
|
|
3
|
+
import type { Model } from '../llm_provider/models';
|
|
34
4
|
/** Predictor 构造选项 */
|
|
35
5
|
export interface PredictorOptions {
|
|
36
6
|
/** Provider 列表,按优先级排序 */
|
package/dist/predict/llm.js
CHANGED
|
@@ -30,7 +30,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
|
|
|
30
30
|
};
|
|
31
31
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
32
|
exports.Predictor = void 0;
|
|
33
|
-
const models_1 = require("
|
|
33
|
+
const models_1 = require("../llm_provider/models");
|
|
34
34
|
/**
|
|
35
35
|
* 预测器核心类。负责按模型路由到对应 Provider,执行故障转移和重试。
|
|
36
36
|
* 对外透明:调用方只需指定模型,无需关心底层是哪个 Provider。
|
package/dist/predict/models.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
export type Model = 'qwen3.7-max' | 'qwen-plus' | 'qwen-turbo' | 'qwq-plus' | 'deepseek-v4-pro' | 'deepseek-v4-flash' | 'kimi-k2.6' | 'qwen-vl-plus';
|
|
5
|
-
/** 模型到 Provider 的映射配置 */
|
|
6
|
-
export interface ModelConfig {
|
|
7
|
-
readonly model: string;
|
|
8
|
-
readonly provider: string;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 模型注册表。每个模型对应一个或多个 Provider 候选,按优先级排序。
|
|
12
|
-
* 当首选 Provider 失败时,Predictor 按此表顺序尝试下一个。
|
|
13
|
-
*/
|
|
14
|
-
export declare const MODEL_REGISTRY: Readonly<Record<Model, ReadonlyArray<ModelConfig>>>;
|
|
1
|
+
export type { Model, ModelConfig } from '../llm_provider/models';
|
|
2
|
+
export { MODEL_REGISTRY } from '../llm_provider/models';
|
package/dist/predict/models.js
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MODEL_REGISTRY = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* 当首选 Provider 失败时,Predictor 按此表顺序尝试下一个。
|
|
7
|
-
*/
|
|
8
|
-
exports.MODEL_REGISTRY = {
|
|
9
|
-
'qwen3.7-max': [{ model: 'qwen3.7-max', provider: 'bailian' }],
|
|
10
|
-
'qwen-plus': [{ model: 'qwen-plus', provider: 'bailian' }],
|
|
11
|
-
'qwen-turbo': [{ model: 'qwen-turbo', provider: 'bailian' }],
|
|
12
|
-
'qwq-plus': [{ model: 'qwq-plus', provider: 'bailian' }],
|
|
13
|
-
'deepseek-v4-pro': [{ model: 'deepseek-v4-pro', provider: 'bailian' }],
|
|
14
|
-
'deepseek-v4-flash': [{ model: 'deepseek-v4-flash', provider: 'bailian' }],
|
|
15
|
-
'kimi-k2.6': [{ model: 'kimi-k2.6', provider: 'bailian' }],
|
|
16
|
-
'qwen-vl-plus': [{ model: 'qwen-vl-plus', provider: 'bailian' }],
|
|
17
|
-
};
|
|
4
|
+
var models_1 = require("../llm_provider/models");
|
|
5
|
+
Object.defineProperty(exports, "MODEL_REGISTRY", { enumerable: true, get: function () { return models_1.MODEL_REGISTRY; } });
|
package/dist/predict/predict.js
CHANGED
|
@@ -37,7 +37,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
37
37
|
exports.LLM = void 0;
|
|
38
38
|
const llm_1 = require("./llm");
|
|
39
39
|
const config_1 = require("./config");
|
|
40
|
-
const bailian_1 = require("
|
|
40
|
+
const bailian_1 = require("../llm_provider/bailian");
|
|
41
41
|
const DEFAULT_BAILIAN_BASE_URL = 'https://dashscope.aliyuncs.com/compatible-mode/v1';
|
|
42
42
|
const DEFAULT_BAILIAN_MODEL = 'qwen-max';
|
|
43
43
|
function createBailianProvider() {
|
package/dist/predict/types.d.ts
CHANGED
|
@@ -1,51 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* LLM 对话消息。支持 system、user、assistant 三种角色。
|
|
3
|
-
*/
|
|
4
|
-
export interface Message {
|
|
5
|
-
readonly role: 'system' | 'user' | 'assistant';
|
|
6
|
-
readonly content: string;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* 标准化 LLM 请求参数。各 Provider 据此构建自身的协议请求。
|
|
10
|
-
*/
|
|
11
|
-
export interface LLMRequest {
|
|
12
|
-
readonly messages: ReadonlyArray<Message>;
|
|
13
|
-
readonly temperature?: number;
|
|
14
|
-
readonly maxTokens?: number;
|
|
15
|
-
readonly topP?: number;
|
|
16
|
-
readonly stream?: boolean;
|
|
17
|
-
readonly model?: string;
|
|
18
|
-
readonly responseFormat?: 'text' | 'json';
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 标准化 LLM 响应。各 Provider 将原始响应解析为此格式后返回。
|
|
22
|
-
*/
|
|
23
|
-
export interface LLMResponse {
|
|
24
|
-
readonly content: string | null;
|
|
25
|
-
readonly usage?: {
|
|
26
|
-
readonly promptTokens: number;
|
|
27
|
-
readonly completionTokens: number;
|
|
28
|
-
readonly totalTokens: number;
|
|
29
|
-
};
|
|
30
|
-
readonly model: string;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* 流式输出片段。迭代器每次 yield 一个 chunk。
|
|
34
|
-
*/
|
|
35
|
-
export type StreamChunk = {
|
|
36
|
-
readonly type: 'content';
|
|
37
|
-
readonly delta: string;
|
|
38
|
-
} | {
|
|
39
|
-
readonly type: 'finish';
|
|
40
|
-
readonly usage?: LLMResponse['usage'];
|
|
41
|
-
};
|
|
42
|
-
/**
|
|
43
|
-
* Provider 配置。每个 Provider 实例需要一组连接参数。
|
|
44
|
-
*/
|
|
45
|
-
export interface ProviderConfig {
|
|
46
|
-
readonly name: string;
|
|
47
|
-
readonly apiKey: string;
|
|
48
|
-
readonly baseUrl: string;
|
|
49
|
-
readonly defaultModel: string;
|
|
50
|
-
readonly timeoutMs?: number;
|
|
51
|
-
}
|
|
1
|
+
export type { Message, LLMRequest, LLMResponse, ProviderConfig, StreamChunk, } from '../llm_provider/types';
|