yanmengs-rag-package 0.1.0

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 ADDED
@@ -0,0 +1,233 @@
1
+ # yanmengs-rag-package
2
+
3
+ 集团统一 AI 基建客户端 SDK,封装 RAG 知识库、AI 问答(SSE 流式)、七牛上传、TTS、ASR 等能力,供各业务前端直接调用。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ # 本地链接(开发阶段)
9
+ pnpm link ../yanmengs-ragPackage
10
+
11
+ # 或发布后通过 npm 安装
12
+ pnpm add yanmengs-rag-package
13
+ ```
14
+
15
+ ## 快速开始
16
+
17
+ ```ts
18
+ import { createRagClient } from "yanmengs-rag-package";
19
+
20
+ const rag = createRagClient({
21
+ ragServerUrl: "http://your-rag-server:7100", // rag-server 地址
22
+ appKey: "your_app_key", // 通过 /application/generate_key 获取
23
+ });
24
+ ```
25
+
26
+ > 所有模块均通过 `rag.xxx` 调用,下面逐一说明。
27
+
28
+ ---
29
+
30
+ ## 模块说明
31
+
32
+ ### 1. 七牛上传 `rag.qiniu`
33
+
34
+ Token 由各应用自行管理,本包只负责上传。
35
+
36
+ #### 单文件上传
37
+
38
+ ```ts
39
+ rag.qiniu.upload({
40
+ file: fileObject, // File 或 Blob
41
+ key: "uploads/2024/doc.pdf", // 七牛存储路径
42
+ token: "your_qiniu_upload_token", // 方式一:直接传 token
43
+ // getToken: async () => { ... }, // 方式二:传异步函数获取 token
44
+ region: "z2", // 可选,默认 z2
45
+ onProgress: (percent) => {
46
+ console.log(`上传进度: ${percent.toFixed(1)}%`);
47
+ },
48
+ onSuccess: (res) => {
49
+ console.log("上传成功", res);
50
+ },
51
+ onError: (err) => {
52
+ console.error("上传失败", err.message);
53
+ },
54
+ });
55
+ ```
56
+
57
+ #### 批量并发上传
58
+
59
+ ```ts
60
+ rag.qiniu.batchUpload({
61
+ files: [
62
+ { file: file1, key: "a.pdf" },
63
+ { file: file2, key: "b.docx" },
64
+ { file: file3, key: "c.png" },
65
+ ],
66
+ token: "your_qiniu_token",
67
+ maxConcurrency: 3, // 可选,默认 3
68
+ onItemProgress: (key, percent) => console.log(`${key}: ${percent}%`),
69
+ onItemSuccess: (key, res) => console.log(`${key} 上传完成`),
70
+ onItemError: (key, err) => console.error(`${key} 失败`, err),
71
+ onAllComplete: () => console.log("全部上传完成"),
72
+ });
73
+ ```
74
+
75
+ #### 查看上传队列
76
+
77
+ ```ts
78
+ const list = rag.qiniu.getUploadList();
79
+ // [{ id, name, key, percent, status, error? }, ...]
80
+ ```
81
+
82
+ ---
83
+
84
+ ### 2. 知识库 `rag.knowledge`
85
+
86
+ #### 通过 URL 注入文档
87
+
88
+ 将远程文件自动下载、解析、切片存入向量库。
89
+
90
+ ```ts
91
+ const result = await rag.knowledge.processUrl({
92
+ userID: "user_001",
93
+ fileUrl: "https://cdn.example.com/report.pdf",
94
+ fileID: "biz-file-id", // 可选,用于状态回调
95
+ });
96
+ // result => { message, filename, total_chunks, weaviate_index }
97
+ ```
98
+
99
+ #### 直传文件
100
+
101
+ ```ts
102
+ const result = await rag.knowledge.uploadDocument({
103
+ userID: "user_001",
104
+ file: fileObject, // File 对象
105
+ });
106
+ ```
107
+
108
+ #### 删除文档
109
+
110
+ ```ts
111
+ await rag.knowledge.deleteDocument({
112
+ userID: "user_001",
113
+ fileID: "xxx", // fileID 和 filename 二选一
114
+ filename: "可选文件名",
115
+ });
116
+ ```
117
+
118
+ ---
119
+
120
+ ### 3. AI 问答 `rag.chat`
121
+
122
+ #### SSE 流式问答(推荐)
123
+
124
+ ```ts
125
+ rag.chat.askStream({
126
+ userId: "user_001",
127
+ query: "项目有哪些技术难点?",
128
+ topK: 3, // 可选,默认 3
129
+ onChunk: (chunk) => {
130
+ // 每收到一段文字就回调,可用于逐字渲染
131
+ process.stdout.write(chunk);
132
+ },
133
+ onDone: (fullAnswer) => {
134
+ console.log("\n完整回答:", fullAnswer);
135
+ },
136
+ onError: (err) => {
137
+ console.error("问答出错:", err.message);
138
+ },
139
+ });
140
+ ```
141
+
142
+ #### 非流式问答
143
+
144
+ ```ts
145
+ const result = await rag.chat.ask({
146
+ userId: "user_001",
147
+ query: "项目有哪些技术难点?",
148
+ topK: 3,
149
+ });
150
+ // result => { answer: string, retrieved_context: string[] }
151
+ ```
152
+
153
+ ---
154
+
155
+ ### 4. TTS 语音合成 `rag.tts`
156
+
157
+ 流式返回音频数据,可直接喂给 `<audio>` 播放。
158
+
159
+ ```ts
160
+ const response = await rag.tts.stream({ text: "你好,这是一段测试文本" });
161
+
162
+ // 方式一:直接用 Blob URL 播放
163
+ const blob = await response.blob();
164
+ const audio = new Audio(URL.createObjectURL(blob));
165
+ audio.play();
166
+
167
+ // 方式二:流式播放(边下载边播放)
168
+ const reader = response.body.getReader();
169
+ // ... 自行处理 ReadableStream
170
+ ```
171
+
172
+ ---
173
+
174
+ ### 5. ASR 语音转写 `rag.asr`
175
+
176
+ ```ts
177
+ const result = await rag.asr.transcribe(audioFile);
178
+ // result => { code: 200, data: { text: '识别出的文字内容' } }
179
+ ```
180
+
181
+ ---
182
+
183
+ ### 6. 应用管理 `rag.app`
184
+
185
+ 生成 App Key(此接口不需要 app-key 鉴权)。
186
+
187
+ ```ts
188
+ const keyInfo = await rag.app.generateKey("my_app_name");
189
+ // keyInfo => { app_name: 'my_app_name', app_key: 'xxxx', message: 'Key 生成成功' }
190
+ ```
191
+
192
+ ---
193
+
194
+ ## 完整类型导出
195
+
196
+ 所有类型均可直接导入使用:
197
+
198
+ ```ts
199
+ import type {
200
+ RagClientConfig,
201
+ QiniuUploadOptions,
202
+ QiniuBatchUploadOptions,
203
+ UploadItem,
204
+ ProcessUrlParams,
205
+ ProcessUrlResult,
206
+ UploadDocumentParams,
207
+ UploadDocumentResult,
208
+ DeleteDocumentParams,
209
+ AskParams,
210
+ AskStreamOptions,
211
+ AskResult,
212
+ TtsStreamParams,
213
+ TranscribeResult,
214
+ GenerateKeyResult,
215
+ } from "yanmengs-rag-package";
216
+ ```
217
+
218
+ ## 开发
219
+
220
+ ```bash
221
+ # 安装依赖
222
+ pnpm install
223
+
224
+ # 开发模式(watch)
225
+ pnpm dev
226
+
227
+ # 构建
228
+ pnpm build
229
+ ```
230
+
231
+ ## 支持的文件类型(知识库上传)
232
+
233
+ PDF、Word、TXT、Excel、PPTX、图片(OCR)