@auipage/model 0.1.0-alpha.0 → 0.1.0-alpha.1
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/CHANGELOG +6 -1
- package/README.md +31 -77
- package/package.json +8 -3
- package/src/completions.js +1 -1
- package/src/parseString.js +10 -11
- package/types/completions.d.ts +31 -0
- package/types/index.d.ts +13 -0
- package/types/model.d.ts +33 -0
package/CHANGELOG
CHANGED
package/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# [@auipage/model](https://github.com/handless-ui/auipage/packages/model/README.md)
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
OpenAI 风格 `chat/completions` 的流式封装:负责请求拼装、SSE 增量解析、工具调用切片拼接,兼容同协议的其他模型服务(DeepSeek 等)。
|
|
3
4
|
|
|
4
5
|
<p>
|
|
5
6
|
<a href="https://zxl20070701.github.io/toolbox/#/npm-download?packages=@auipage/model&interval=7">
|
|
@@ -15,101 +16,50 @@
|
|
|
15
16
|
|
|
16
17
|
<img src="https://nodei.co/npm/@auipage/model.png?downloads=true&downloadRank=true&stars=true" alt="NPM">
|
|
17
18
|
|
|
18
|
-
##
|
|
19
|
+
## 安装
|
|
19
20
|
|
|
20
21
|
```
|
|
21
22
|
npm install --save @auipage/model
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
### 1. 实例化 Model
|
|
27
|
-
|
|
28
|
-
通过 `new Model(fields)` 创建一个模型实例,其中 `fields` 包含三个必填字段:
|
|
29
|
-
|
|
30
|
-
- `model`:模型名称(例如 `gpt-4o`、`deepseek-chat` 等)
|
|
31
|
-
- `apiKey`:调用接口所需的密钥
|
|
32
|
-
- `baseURL`:接口服务的基础地址(请求时会拼接 `/chat/completions`)
|
|
25
|
+
## 创建实例
|
|
33
26
|
|
|
34
27
|
```javascript
|
|
35
28
|
import { Model } from "@auipage/model";
|
|
36
29
|
|
|
37
30
|
const model = new Model({
|
|
38
|
-
model: "deepseek-chat",
|
|
31
|
+
model: "deepseek-chat", // 模型名
|
|
39
32
|
apiKey: "your-api-key",
|
|
40
|
-
baseURL: "https://api.deepseek.com/v1"
|
|
33
|
+
baseURL: "https://api.deepseek.com/v1" // 请求时拼接 /chat/completions
|
|
41
34
|
});
|
|
42
35
|
```
|
|
43
36
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
返回值:
|
|
57
|
-
|
|
58
|
-
- `content`:累积的全部正文内容。
|
|
59
|
-
- `tool_calls`:解析得到的工具调用数组,会按 `id` 切片并拼接 `function.arguments`。
|
|
60
|
-
|
|
61
|
-
### 3. 流式输出示例
|
|
62
|
-
|
|
63
|
-
```javascript
|
|
64
|
-
model.completions({
|
|
65
|
-
messages: [
|
|
66
|
-
{ role: "system", content: "你是一个助手。" },
|
|
67
|
-
{ role: "user", content: "用一句话介绍 JavaScript。" }
|
|
68
|
-
],
|
|
69
|
-
tools: [],
|
|
70
|
-
stream: true
|
|
71
|
-
}, (chunk) => {
|
|
72
|
-
// 实时打印每一段正文
|
|
73
|
-
process.stdout.write(chunk);
|
|
74
|
-
}, (reasoning) => {
|
|
75
|
-
// 实时打印模型的思考过程
|
|
76
|
-
console.log("\n[think]", reasoning);
|
|
77
|
-
}).then(({ content, tool_calls }) => {
|
|
78
|
-
console.log("\n完整结果:", { content, tool_calls });
|
|
79
|
-
}).catch(err => {
|
|
80
|
-
console.error(err);
|
|
81
|
-
});
|
|
37
|
+
## completions
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
model.completions(
|
|
41
|
+
{
|
|
42
|
+
messages: ChatMessage[], // OpenAI 格式消息
|
|
43
|
+
tools?: Tool[], // 可选,默认 [];自动取每项的 description 作为 tools 提交
|
|
44
|
+
stream?: boolean // 默认 false
|
|
45
|
+
},
|
|
46
|
+
logback?, // (delta: string) => void,正文增量回调
|
|
47
|
+
thinkback? // (reasoning: string) => void,推理过程回调
|
|
48
|
+
): Promise<{ content: string, tool_calls: ModelToolCall[] }>
|
|
82
49
|
```
|
|
83
50
|
|
|
84
|
-
|
|
51
|
+
- `thinkback` 接收 `delta.reasoning` / `delta.reasoning_content`;正文中的 `<think>...</think>` 标签会被剥除,剩余内容并入 `content`。
|
|
52
|
+
- `stream: false` 时两个回调仍会调用,只是在响应结束后各触发一次。
|
|
53
|
+
- 返回的 `tool_calls` 已按 `id` 切片、拼好 `function.arguments`。
|
|
85
54
|
|
|
86
|
-
|
|
55
|
+
## 示例
|
|
87
56
|
|
|
88
57
|
```javascript
|
|
89
58
|
model.completions({
|
|
90
59
|
messages: [
|
|
91
|
-
{ role: "
|
|
60
|
+
{ role: "system", content: "你是一个助手。" },
|
|
61
|
+
{ role: "user", content: "查一下南京今天的天气" }
|
|
92
62
|
],
|
|
93
|
-
tools: [],
|
|
94
|
-
stream: false
|
|
95
|
-
}, (chunk) => {
|
|
96
|
-
// 非流式时这里会一次性收到完整正文
|
|
97
|
-
console.log(chunk);
|
|
98
|
-
}, (reasoning) => {
|
|
99
|
-
// 如果模型返回了 reasoning,这里会回调一次
|
|
100
|
-
console.log("[think]", reasoning);
|
|
101
|
-
}).then(({ content, tool_calls }) => {
|
|
102
|
-
console.log(content, tool_calls);
|
|
103
|
-
});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### 5. 配合工具调用(tool_calls)
|
|
107
|
-
|
|
108
|
-
只要在 `params.tools` 中传入工具描述数组,框架就会在请求体中附带 `tools` 字段,并把模型返回的工具调用解析到 `tool_calls` 中:
|
|
109
|
-
|
|
110
|
-
```javascript
|
|
111
|
-
model.completions({
|
|
112
|
-
messages: [{ role: "user", content: "查一下南京今天的天气" }],
|
|
113
63
|
tools: [
|
|
114
64
|
{
|
|
115
65
|
description: {
|
|
@@ -119,15 +69,19 @@ model.completions({
|
|
|
119
69
|
description: "查询指定城市的天气",
|
|
120
70
|
parameters: {
|
|
121
71
|
type: "object",
|
|
122
|
-
properties: { city: { type: "string" } }
|
|
72
|
+
properties: { city: { type: "string" } },
|
|
73
|
+
required: ["city"]
|
|
123
74
|
}
|
|
124
75
|
}
|
|
125
76
|
}
|
|
126
77
|
}
|
|
127
78
|
],
|
|
128
|
-
stream:
|
|
79
|
+
stream: true
|
|
80
|
+
}, (chunk) => {
|
|
81
|
+
process.stdout.write(chunk); // 正文增量
|
|
82
|
+
}, (reasoning) => {
|
|
83
|
+
console.log("[think]", reasoning); // 推理增量
|
|
129
84
|
}).then(({ content, tool_calls }) => {
|
|
130
|
-
// tool_calls 为模型决定要调用的工具及参数
|
|
131
85
|
console.log(content, tool_calls);
|
|
132
86
|
});
|
|
133
87
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auipage/model",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
4
|
"description": "@auipage/model",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -15,6 +15,11 @@
|
|
|
15
15
|
"url": "git+https://github.com/handless-ui/auipage.git",
|
|
16
16
|
"directory": "packages/model"
|
|
17
17
|
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"ui",
|
|
20
|
+
"page",
|
|
21
|
+
"agent"
|
|
22
|
+
],
|
|
18
23
|
"author": {
|
|
19
24
|
"name": "zxl20070701",
|
|
20
25
|
"url": "https://zxl20070701.github.io/notebook/home.html"
|
|
@@ -25,7 +30,7 @@
|
|
|
25
30
|
},
|
|
26
31
|
"homepage": "https://github.com/handless-ui/auipage#readme",
|
|
27
32
|
"dependencies": {
|
|
28
|
-
"@auipage/fetch": "^0.1.0-alpha.
|
|
29
|
-
"@auipage/types": "^0.1.0-alpha.
|
|
33
|
+
"@auipage/fetch": "^0.1.0-alpha.1",
|
|
34
|
+
"@auipage/types": "^0.1.0-alpha.1"
|
|
30
35
|
}
|
|
31
36
|
}
|
package/src/completions.js
CHANGED
|
@@ -20,7 +20,7 @@ export default function completions(params, logback, thinkback) {
|
|
|
20
20
|
body: JSON.stringify({
|
|
21
21
|
model: this.model,
|
|
22
22
|
messages: params.messages,
|
|
23
|
-
tools: params.tools.map(tool => tool.description),
|
|
23
|
+
tools: params.tools ? params.tools.map(tool => tool.description) : [],
|
|
24
24
|
stream: params.stream,
|
|
25
25
|
})
|
|
26
26
|
}).then(res => {
|
package/src/parseString.js
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
// 字符串转JSON
|
|
2
2
|
// 适配LLM返回数据格式
|
|
3
3
|
export default function (value) {
|
|
4
|
-
|
|
5
|
-
value = value.trim().replace(/^data: /, "").replace(/data: \[DONE\]$/, "");
|
|
4
|
+
value = value.trim().replace(/^data: /, "").replace(/data: \[DONE\]$/, "");
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return JSON.parse(value);
|
|
14
|
-
} catch (e) {
|
|
15
|
-
throw new Error("[转json失败]" + value);
|
|
6
|
+
if (/\r{0,1}\n\r{0,1}\ndata: /.test(value)) {
|
|
7
|
+
return JSON.parse("[" + (value.split(/\r{0,1}\n\r{0,1}\ndata: /)).join(",") + "]");
|
|
8
|
+
} else if (value === "[DONE]") {
|
|
9
|
+
return [];
|
|
16
10
|
}
|
|
11
|
+
|
|
12
|
+
let result = JSON.parse(value);
|
|
13
|
+
if (result.error) throw result.error;
|
|
14
|
+
|
|
15
|
+
return result;
|
|
17
16
|
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* chat/completions 请求与响应类型
|
|
3
|
+
*/
|
|
4
|
+
import type {
|
|
5
|
+
ChatMessage,
|
|
6
|
+
ModelToolCall,
|
|
7
|
+
ToolDescription
|
|
8
|
+
} from "@auipage/types";
|
|
9
|
+
|
|
10
|
+
/** completions 入参(对应底层请求体中本包负责拼装的部分) */
|
|
11
|
+
export interface CompletionsParams {
|
|
12
|
+
messages: ChatMessage[];
|
|
13
|
+
/** 工具列表,每项携带一个标准 description,发请求时自动取 .description */
|
|
14
|
+
tools?: Array<{ description: ToolDescription }>;
|
|
15
|
+
/** 是否流式请求,默认 false */
|
|
16
|
+
stream?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 流式回调:每收到一个增量分片调用一次,参数是本次增量文本
|
|
21
|
+
* (不是累计文本,拼接由调用方完成)。
|
|
22
|
+
*/
|
|
23
|
+
export type StreamCallback = (delta: string) => void;
|
|
24
|
+
|
|
25
|
+
/** completions 最终结果 */
|
|
26
|
+
export interface CompletionsResult {
|
|
27
|
+
/** 完整的正文内容 */
|
|
28
|
+
content: string;
|
|
29
|
+
/** 模型产生的工具调用(流式分片已自动拼装) */
|
|
30
|
+
tool_calls: ModelToolCall[];
|
|
31
|
+
}
|
package/types/index.d.ts
CHANGED
package/types/model.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model 构造器:对 OpenAI 风格 /chat/completions 接口的薄封装
|
|
3
|
+
*/
|
|
4
|
+
import type { ModelConfig } from "@auipage/types";
|
|
5
|
+
import type {
|
|
6
|
+
CompletionsParams,
|
|
7
|
+
CompletionsResult,
|
|
8
|
+
StreamCallback
|
|
9
|
+
} from "./completions.js";
|
|
10
|
+
|
|
11
|
+
export declare class Model {
|
|
12
|
+
constructor(fields: ModelConfig);
|
|
13
|
+
|
|
14
|
+
/** 模型名称 */
|
|
15
|
+
model: string;
|
|
16
|
+
/** 模型服务 API Key */
|
|
17
|
+
apiKey: string;
|
|
18
|
+
/** 模型服务 baseURL */
|
|
19
|
+
baseURL: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 发起一次 chat/completions 请求。
|
|
23
|
+
*
|
|
24
|
+
* @param params 消息、工具、是否流式
|
|
25
|
+
* @param logback 流式正文增量回调
|
|
26
|
+
* @param thinkback 流式思维链增量回调
|
|
27
|
+
*/
|
|
28
|
+
completions(
|
|
29
|
+
params: CompletionsParams,
|
|
30
|
+
logback?: StreamCallback,
|
|
31
|
+
thinkback?: StreamCallback
|
|
32
|
+
): Promise<CompletionsResult>;
|
|
33
|
+
}
|