@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 CHANGED
@@ -1,4 +1,9 @@
1
1
  v0.1.0:
2
2
  date:
3
3
  changes:
4
- - 初始化版本
4
+ - 初始化版本
5
+ 1、@auipage/types 提供类型定义
6
+ 2、@auipage/core 提供核心功能
7
+ 3、@auipage/model 提供模型相关功能
8
+ 4、@auipage/agent 提供智能体相关功能
9
+ 5、@auipage/fetch 提供数据获取相关功能
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&amp;downloadRank=true&amp;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
- ### 2. 调用 completions
45
-
46
- `model.completions(params, logback, thinkback)` 会向 `${baseURL}/chat/completions` 发起请求,并以流式方式解析返回数据,最终返回一个 `Promise`,其值为 `{ content, tool_calls }`。
47
-
48
- 参数说明:
49
-
50
- - `params.messages`:对话消息数组,遵循 OpenAI Chat Completions 的 `messages` 格式。
51
- - `params.tools`:工具列表,框架会自动取每个工具的 `description` 字段作为 `tools` 参数提交。
52
- - `params.stream`:是否启用流式输出,`true` 时会逐步回调;`false` 时等待完整响应再统一处理。
53
- - `logback`(可选):流式输出时,每当接收到一段正文(`delta.content`),便会触发该回调,适合实时把内容渲染到界面上。
54
- - `thinkback`(可选):当模型返回了推理过程(`delta.reasoning` 或 `delta.reasoning_content`)时会触发该回调;如果内容中带有 `<think>...</think>` 标签,会自动剥离标签并将剩余正文合并到 `content` 中。
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
- ### 4. 非流式输出示例
51
+ - `thinkback` 接收 `delta.reasoning` / `delta.reasoning_content`;正文中的 `<think>...</think>` 标签会被剥除,剩余内容并入 `content`。
52
+ - `stream: false` 时两个回调仍会调用,只是在响应结束后各触发一次。
53
+ - 返回的 `tool_calls` 已按 `id` 切片、拼好 `function.arguments`。
85
54
 
86
- 将 `params.stream` 置为 `false` 时,`logback` 和 `thinkback` 仍会被调用一次:等到响应结束后,框架会把整段响应解析成一个 `delta` 一次性交给内部 `choices_handler`,所以 `logback` 会收到完整正文,`thinkback` 也只会回调一次(如果响应中包含推理内容的话):
55
+ ## 示例
87
56
 
88
57
  ```javascript
89
58
  model.completions({
90
59
  messages: [
91
- { role: "user", content: "你好" }
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: false
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.0",
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.0",
29
- "@auipage/types": "^0.1.0-alpha.0"
33
+ "@auipage/fetch": "^0.1.0-alpha.1",
34
+ "@auipage/types": "^0.1.0-alpha.1"
30
35
  }
31
36
  }
@@ -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 => {
@@ -1,17 +1,16 @@
1
1
  // 字符串转JSON
2
2
  // 适配LLM返回数据格式
3
3
  export default function (value) {
4
- try {
5
- value = value.trim().replace(/^data: /, "").replace(/data: \[DONE\]$/, "");
4
+ value = value.trim().replace(/^data: /, "").replace(/data: \[DONE\]$/, "");
6
5
 
7
- if (/\n\ndata: /.test(value)) {
8
- return JSON.parse("[" + (value.split(/\n\ndata: /)).join(",") + "]");
9
- } else if (value === "[DONE]") {
10
- return [];
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
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @auipage/model 类型入口
3
+ *
4
+ * 对 OpenAI 风格 chat/completions 的跨端请求封装,支持流式输出与工具调用。
5
+ */
6
+
7
+ export { Model } from "./model.js";
8
+
9
+ export type {
10
+ CompletionsParams,
11
+ CompletionsResult,
12
+ StreamCallback
13
+ } from "./completions.js";
@@ -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
+ }