@keo-ai/axiom 0.1.4 → 0.1.6
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 +29 -28
- 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 +33 -57
- package/dist/function_call_loop/provider.d.ts +2 -2
- package/dist/function_call_loop/provider.js +38 -53
- package/dist/function_call_loop/types.d.ts +9 -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 +175 -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 +18 -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
|
@@ -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,175 @@
|
|
|
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
|
+
'glm-5.1',
|
|
174
|
+
'qwen-vl-plus',
|
|
175
|
+
]);
|
|
@@ -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' | 'glm-5.1' | '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,18 @@
|
|
|
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
|
+
'glm-5.1': [{ model: 'glm-5.1', provider: 'bailian' }],
|
|
17
|
+
'qwen-vl-plus': [{ model: 'qwen-vl-plus', provider: 'bailian' }],
|
|
18
|
+
};
|
|
@@ -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() {
|