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.
@@ -0,0 +1,203 @@
1
+ interface RagClientConfig {
2
+ /** rag-server 地址,默认 http://localhost:7100 */
3
+ ragServerUrl?: string;
4
+ /** 鉴权用的 app_key */
5
+ appKey: string;
6
+ }
7
+ interface OssTokenResult {
8
+ code: number;
9
+ data: {
10
+ token: string;
11
+ };
12
+ message: string;
13
+ }
14
+ interface QiniuUploadOptions {
15
+ /** 要上传的文件 */
16
+ file: File | Blob;
17
+ /** 七牛存储的 key(路径) */
18
+ key: string;
19
+ /** 直接传入七牛上传 token */
20
+ token?: string;
21
+ /** 异步获取 token 的函数(与 token 二选一) */
22
+ getToken?: () => Promise<string>;
23
+ /** 七牛区域,默认 z2 */
24
+ region?: string;
25
+ /** 上传进度回调 */
26
+ onProgress?: (percent: number) => void;
27
+ /** 上传成功回调 */
28
+ onSuccess?: (res: any) => void;
29
+ /** 上传失败回调 */
30
+ onError?: (err: Error) => void;
31
+ }
32
+ interface QiniuBatchUploadOptions {
33
+ /** 要上传的文件列表 */
34
+ files: Array<{
35
+ file: File | Blob;
36
+ key: string;
37
+ }>;
38
+ /** 直接传入七牛上传 token */
39
+ token?: string;
40
+ /** 异步获取 token 的函数 */
41
+ getToken?: () => Promise<string>;
42
+ /** 最大并发数,默认 3 */
43
+ maxConcurrency?: number;
44
+ /** 七牛区域,默认 z2 */
45
+ region?: string;
46
+ /** 单文件进度回调 */
47
+ onItemProgress?: (key: string, percent: number) => void;
48
+ /** 单文件成功回调 */
49
+ onItemSuccess?: (key: string, res: any) => void;
50
+ /** 单文件失败回调 */
51
+ onItemError?: (key: string, err: Error) => void;
52
+ /** 全部完成回调 */
53
+ onAllComplete?: () => void;
54
+ }
55
+ interface UploadItem {
56
+ id: string;
57
+ file: File | Blob;
58
+ name: string;
59
+ percent: number;
60
+ status: "pending" | "uploading" | "error" | "success";
61
+ key: string;
62
+ error?: string;
63
+ }
64
+ interface ProcessUrlParams {
65
+ /** 用户 ID */
66
+ userID: string;
67
+ /** 远程文件地址 */
68
+ fileUrl: string;
69
+ /** 业务文件 ID(可选,用于回调) */
70
+ fileID?: string;
71
+ }
72
+ interface ProcessUrlResult {
73
+ message: string;
74
+ filename: string;
75
+ total_chunks: number;
76
+ weaviate_index: string;
77
+ }
78
+ interface UploadDocumentParams {
79
+ /** 用户 ID */
80
+ userID: string;
81
+ /** 要上传的文件 */
82
+ file: File;
83
+ }
84
+ interface UploadDocumentResult {
85
+ message: string;
86
+ filename: string;
87
+ total_chunks: number;
88
+ weaviate_index: string;
89
+ }
90
+ interface DeleteDocumentParams {
91
+ /** 用户 ID */
92
+ userID: string;
93
+ /** 业务文件 ID */
94
+ fileID?: string;
95
+ /** 文件名 */
96
+ filename?: string;
97
+ }
98
+ interface AskParams {
99
+ /** 用户 ID */
100
+ userId: string;
101
+ /** 问题内容 */
102
+ query: string;
103
+ /** 检索 top K,默认 3 */
104
+ topK?: number;
105
+ }
106
+ interface AskStreamOptions extends AskParams {
107
+ /** 每次收到 SSE 数据块时的回调 */
108
+ onChunk: (chunk: string) => void;
109
+ /** 流结束后的回调 */
110
+ onDone?: (fullAnswer: string) => void;
111
+ /** 错误回调 */
112
+ onError?: (err: Error) => void;
113
+ }
114
+ interface AskResult {
115
+ answer: string;
116
+ retrieved_context: string[];
117
+ }
118
+ interface TtsStreamParams {
119
+ /** 需要合成语音的文本 */
120
+ text: string;
121
+ /** 可选的 AbortSignal,用于取消请求 */
122
+ signal?: AbortSignal;
123
+ }
124
+ interface TranscribeResult {
125
+ code: number;
126
+ data: {
127
+ text: string;
128
+ };
129
+ }
130
+ interface GenerateKeyResult {
131
+ app_name: string;
132
+ app_key: string;
133
+ message: string;
134
+ }
135
+
136
+ /**
137
+ * 创建 RAG 客户端实例
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * import { createRagClient } from 'yanmengs-rag-package';
142
+ *
143
+ * const rag = createRagClient({
144
+ * ragServerUrl: 'http://localhost:7100',
145
+ * appKey: 'your_app_key',
146
+ * });
147
+ *
148
+ * // 获取七牛上传 Token
149
+ * const tokenRes = await rag.oss.getPutPolicy();
150
+ *
151
+ * // 七牛上传
152
+ * rag.qiniu.upload({ file, key: 'xxx.pdf', token: tokenRes.data.token, onSuccess: (res) => {} });
153
+ *
154
+ * // 知识库
155
+ * await rag.knowledge.processUrl({ userID: 'u1', fileUrl: 'https://...' });
156
+ *
157
+ * // AI 问答(SSE 流式)
158
+ * rag.chat.askStream({ userId: 'u1', query: '...', onChunk: (c) => console.log(c) });
159
+ *
160
+ * // TTS
161
+ * const audioRes = await rag.tts.stream({ text: '...' });
162
+ *
163
+ * const result = await rag.asr.transcribe(audioFile);
164
+ * ```
165
+ */
166
+ declare function createRagClient(config?: RagClientConfig): {
167
+ /**七牛上传依赖的凭证接口模块 */
168
+ oss: {
169
+ getUploadCode: () => Promise<OssTokenResult>;
170
+ getPutPolicy: () => Promise<OssTokenResult>;
171
+ };
172
+ /** 七牛上传模块 */
173
+ qiniu: {
174
+ upload: (options: QiniuUploadOptions) => void;
175
+ batchUpload: (options: QiniuBatchUploadOptions) => void;
176
+ getUploadList: () => UploadItem[];
177
+ };
178
+ /** 知识库模块 */
179
+ knowledge: {
180
+ processUrl: (params: ProcessUrlParams) => Promise<ProcessUrlResult>;
181
+ uploadDocument: (params: UploadDocumentParams) => Promise<UploadDocumentResult>;
182
+ deleteDocument: (params: DeleteDocumentParams) => Promise<void>;
183
+ };
184
+ /** AI 问答模块(支持 SSE 流式) */
185
+ chat: {
186
+ askStream: (options: AskStreamOptions) => Promise<void>;
187
+ ask: (params: AskParams) => Promise<AskResult>;
188
+ };
189
+ /** TTS 语音合成模块 */
190
+ tts: {
191
+ stream: (params: TtsStreamParams) => Promise<Response>;
192
+ };
193
+ /** ASR 语音转写模块 */
194
+ asr: {
195
+ transcribe: (file: File | Blob) => Promise<TranscribeResult>;
196
+ };
197
+ /** 应用管理模块 */
198
+ app: {
199
+ generateKey: (appName: string) => Promise<GenerateKeyResult>;
200
+ };
201
+ };
202
+
203
+ export { type AskParams, type AskResult, type AskStreamOptions, type DeleteDocumentParams, type GenerateKeyResult, type OssTokenResult, type ProcessUrlParams, type ProcessUrlResult, type QiniuBatchUploadOptions, type QiniuUploadOptions, type RagClientConfig, type TranscribeResult, type TtsStreamParams, type UploadDocumentParams, type UploadDocumentResult, type UploadItem, createRagClient };
@@ -0,0 +1,203 @@
1
+ interface RagClientConfig {
2
+ /** rag-server 地址,默认 http://localhost:7100 */
3
+ ragServerUrl?: string;
4
+ /** 鉴权用的 app_key */
5
+ appKey: string;
6
+ }
7
+ interface OssTokenResult {
8
+ code: number;
9
+ data: {
10
+ token: string;
11
+ };
12
+ message: string;
13
+ }
14
+ interface QiniuUploadOptions {
15
+ /** 要上传的文件 */
16
+ file: File | Blob;
17
+ /** 七牛存储的 key(路径) */
18
+ key: string;
19
+ /** 直接传入七牛上传 token */
20
+ token?: string;
21
+ /** 异步获取 token 的函数(与 token 二选一) */
22
+ getToken?: () => Promise<string>;
23
+ /** 七牛区域,默认 z2 */
24
+ region?: string;
25
+ /** 上传进度回调 */
26
+ onProgress?: (percent: number) => void;
27
+ /** 上传成功回调 */
28
+ onSuccess?: (res: any) => void;
29
+ /** 上传失败回调 */
30
+ onError?: (err: Error) => void;
31
+ }
32
+ interface QiniuBatchUploadOptions {
33
+ /** 要上传的文件列表 */
34
+ files: Array<{
35
+ file: File | Blob;
36
+ key: string;
37
+ }>;
38
+ /** 直接传入七牛上传 token */
39
+ token?: string;
40
+ /** 异步获取 token 的函数 */
41
+ getToken?: () => Promise<string>;
42
+ /** 最大并发数,默认 3 */
43
+ maxConcurrency?: number;
44
+ /** 七牛区域,默认 z2 */
45
+ region?: string;
46
+ /** 单文件进度回调 */
47
+ onItemProgress?: (key: string, percent: number) => void;
48
+ /** 单文件成功回调 */
49
+ onItemSuccess?: (key: string, res: any) => void;
50
+ /** 单文件失败回调 */
51
+ onItemError?: (key: string, err: Error) => void;
52
+ /** 全部完成回调 */
53
+ onAllComplete?: () => void;
54
+ }
55
+ interface UploadItem {
56
+ id: string;
57
+ file: File | Blob;
58
+ name: string;
59
+ percent: number;
60
+ status: "pending" | "uploading" | "error" | "success";
61
+ key: string;
62
+ error?: string;
63
+ }
64
+ interface ProcessUrlParams {
65
+ /** 用户 ID */
66
+ userID: string;
67
+ /** 远程文件地址 */
68
+ fileUrl: string;
69
+ /** 业务文件 ID(可选,用于回调) */
70
+ fileID?: string;
71
+ }
72
+ interface ProcessUrlResult {
73
+ message: string;
74
+ filename: string;
75
+ total_chunks: number;
76
+ weaviate_index: string;
77
+ }
78
+ interface UploadDocumentParams {
79
+ /** 用户 ID */
80
+ userID: string;
81
+ /** 要上传的文件 */
82
+ file: File;
83
+ }
84
+ interface UploadDocumentResult {
85
+ message: string;
86
+ filename: string;
87
+ total_chunks: number;
88
+ weaviate_index: string;
89
+ }
90
+ interface DeleteDocumentParams {
91
+ /** 用户 ID */
92
+ userID: string;
93
+ /** 业务文件 ID */
94
+ fileID?: string;
95
+ /** 文件名 */
96
+ filename?: string;
97
+ }
98
+ interface AskParams {
99
+ /** 用户 ID */
100
+ userId: string;
101
+ /** 问题内容 */
102
+ query: string;
103
+ /** 检索 top K,默认 3 */
104
+ topK?: number;
105
+ }
106
+ interface AskStreamOptions extends AskParams {
107
+ /** 每次收到 SSE 数据块时的回调 */
108
+ onChunk: (chunk: string) => void;
109
+ /** 流结束后的回调 */
110
+ onDone?: (fullAnswer: string) => void;
111
+ /** 错误回调 */
112
+ onError?: (err: Error) => void;
113
+ }
114
+ interface AskResult {
115
+ answer: string;
116
+ retrieved_context: string[];
117
+ }
118
+ interface TtsStreamParams {
119
+ /** 需要合成语音的文本 */
120
+ text: string;
121
+ /** 可选的 AbortSignal,用于取消请求 */
122
+ signal?: AbortSignal;
123
+ }
124
+ interface TranscribeResult {
125
+ code: number;
126
+ data: {
127
+ text: string;
128
+ };
129
+ }
130
+ interface GenerateKeyResult {
131
+ app_name: string;
132
+ app_key: string;
133
+ message: string;
134
+ }
135
+
136
+ /**
137
+ * 创建 RAG 客户端实例
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * import { createRagClient } from 'yanmengs-rag-package';
142
+ *
143
+ * const rag = createRagClient({
144
+ * ragServerUrl: 'http://localhost:7100',
145
+ * appKey: 'your_app_key',
146
+ * });
147
+ *
148
+ * // 获取七牛上传 Token
149
+ * const tokenRes = await rag.oss.getPutPolicy();
150
+ *
151
+ * // 七牛上传
152
+ * rag.qiniu.upload({ file, key: 'xxx.pdf', token: tokenRes.data.token, onSuccess: (res) => {} });
153
+ *
154
+ * // 知识库
155
+ * await rag.knowledge.processUrl({ userID: 'u1', fileUrl: 'https://...' });
156
+ *
157
+ * // AI 问答(SSE 流式)
158
+ * rag.chat.askStream({ userId: 'u1', query: '...', onChunk: (c) => console.log(c) });
159
+ *
160
+ * // TTS
161
+ * const audioRes = await rag.tts.stream({ text: '...' });
162
+ *
163
+ * const result = await rag.asr.transcribe(audioFile);
164
+ * ```
165
+ */
166
+ declare function createRagClient(config?: RagClientConfig): {
167
+ /**七牛上传依赖的凭证接口模块 */
168
+ oss: {
169
+ getUploadCode: () => Promise<OssTokenResult>;
170
+ getPutPolicy: () => Promise<OssTokenResult>;
171
+ };
172
+ /** 七牛上传模块 */
173
+ qiniu: {
174
+ upload: (options: QiniuUploadOptions) => void;
175
+ batchUpload: (options: QiniuBatchUploadOptions) => void;
176
+ getUploadList: () => UploadItem[];
177
+ };
178
+ /** 知识库模块 */
179
+ knowledge: {
180
+ processUrl: (params: ProcessUrlParams) => Promise<ProcessUrlResult>;
181
+ uploadDocument: (params: UploadDocumentParams) => Promise<UploadDocumentResult>;
182
+ deleteDocument: (params: DeleteDocumentParams) => Promise<void>;
183
+ };
184
+ /** AI 问答模块(支持 SSE 流式) */
185
+ chat: {
186
+ askStream: (options: AskStreamOptions) => Promise<void>;
187
+ ask: (params: AskParams) => Promise<AskResult>;
188
+ };
189
+ /** TTS 语音合成模块 */
190
+ tts: {
191
+ stream: (params: TtsStreamParams) => Promise<Response>;
192
+ };
193
+ /** ASR 语音转写模块 */
194
+ asr: {
195
+ transcribe: (file: File | Blob) => Promise<TranscribeResult>;
196
+ };
197
+ /** 应用管理模块 */
198
+ app: {
199
+ generateKey: (appName: string) => Promise<GenerateKeyResult>;
200
+ };
201
+ };
202
+
203
+ export { type AskParams, type AskResult, type AskStreamOptions, type DeleteDocumentParams, type GenerateKeyResult, type OssTokenResult, type ProcessUrlParams, type ProcessUrlResult, type QiniuBatchUploadOptions, type QiniuUploadOptions, type RagClientConfig, type TranscribeResult, type TtsStreamParams, type UploadDocumentParams, type UploadDocumentResult, type UploadItem, createRagClient };