@aliyun-rds/rag-agent-sdk 0.1.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/README.md ADDED
@@ -0,0 +1,253 @@
1
+ # RAG Agent SDK for TypeScript/JavaScript
2
+
3
+ A comprehensive TypeScript SDK for interacting with RAG Agent API services.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @rag-agent/sdk
9
+ # or
10
+ yarn add @rag-agent/sdk
11
+ # or
12
+ pnpm add @rag-agent/sdk
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```typescript
18
+ import { RagAgentClient } from '@rag-agent/sdk';
19
+
20
+ const client = new RagAgentClient({
21
+ baseUrl: 'http://localhost:9621',
22
+ apiKey: 'your-api-key',
23
+ });
24
+
25
+ // Create a dataset
26
+ const dataset = await client.datasets.create({
27
+ name: 'my-knowledge-base',
28
+ description: 'My first knowledge base',
29
+ });
30
+ console.log(`Created dataset: ${dataset.dataset_uuid}`);
31
+
32
+ // Upload a document
33
+ const file = new File(['document content'], 'doc.txt', { type: 'text/plain' });
34
+ const result = await client.documents.upload(dataset.dataset_uuid, file);
35
+ console.log(`Upload status: ${result.status}`);
36
+
37
+ // Query the dataset
38
+ const response = await client.query.query(
39
+ dataset.dataset_uuid,
40
+ 'What is this document about?',
41
+ { mode: 'mix' }
42
+ );
43
+ console.log(`Answer: ${response.response}`);
44
+ ```
45
+
46
+ ## Authentication
47
+
48
+ The SDK supports multiple authentication methods:
49
+
50
+ ### API Key Authentication
51
+
52
+ ```typescript
53
+ const client = new RagAgentClient({
54
+ baseUrl: 'http://localhost:9621',
55
+ apiKey: 'your-api-key',
56
+ });
57
+ ```
58
+
59
+ ### JWT Token Authentication
60
+
61
+ ```typescript
62
+ const client = new RagAgentClient({
63
+ baseUrl: 'http://localhost:9621',
64
+ jwtToken: 'your-jwt-token',
65
+ });
66
+ ```
67
+
68
+ ### Supabase Authentication
69
+
70
+ ```typescript
71
+ const client = new RagAgentClient({
72
+ baseUrl: 'http://localhost:9621',
73
+ supabaseUrl: 'https://xxx.supabase.co',
74
+ supabaseAnonKey: 'your-anon-key',
75
+ supabaseAccessToken: 'user-access-token',
76
+ });
77
+ ```
78
+
79
+ ### Username/Password Authentication
80
+
81
+ ```typescript
82
+ const client = new RagAgentClient({
83
+ baseUrl: 'http://localhost:9621',
84
+ username: 'admin',
85
+ password: 'password',
86
+ });
87
+ ```
88
+
89
+ ## Features
90
+
91
+ ### Dataset Management
92
+
93
+ ```typescript
94
+ // List datasets
95
+ const datasets = await client.datasets.list({ page: 1, pageSize: 20 });
96
+
97
+ // Get dataset by ID
98
+ const dataset = await client.datasets.get('dataset-uuid');
99
+
100
+ // Update dataset
101
+ const updated = await client.datasets.update('dataset-uuid', {
102
+ description: 'Updated description',
103
+ });
104
+
105
+ // Delete dataset
106
+ await client.datasets.delete('dataset-uuid');
107
+ ```
108
+
109
+ ### Document Processing
110
+
111
+ ```typescript
112
+ // Upload file
113
+ const result = await client.documents.upload(datasetId, file);
114
+
115
+ // Insert text
116
+ const result = await client.documents.insertText(
117
+ datasetId,
118
+ 'Your text content here',
119
+ 'manual-input'
120
+ );
121
+
122
+ // Batch insert
123
+ const result = await client.documents.insertBatch(
124
+ datasetId,
125
+ ['Text 1', 'Text 2', 'Text 3'],
126
+ ['source1', 'source2', 'source3']
127
+ );
128
+
129
+ // List documents
130
+ const docs = await client.documents.list(datasetId);
131
+
132
+ // Delete document
133
+ await client.documents.delete(datasetId, docId);
134
+ ```
135
+
136
+ ### RAG Queries
137
+
138
+ ```typescript
139
+ // Basic query
140
+ const response = await client.query.query(
141
+ datasetId,
142
+ 'What is the main topic?',
143
+ { mode: 'mix', topK: 10 }
144
+ );
145
+
146
+ // Streaming query
147
+ for await (const chunk of client.query.queryStream(
148
+ datasetId,
149
+ 'Explain the concept in detail'
150
+ )) {
151
+ process.stdout.write(chunk);
152
+ }
153
+
154
+ // Cross-dataset query
155
+ const response = await client.query.crossDatasetQuery(
156
+ 'Compare the topics',
157
+ ['uuid1', 'uuid2'],
158
+ { enableRerank: true }
159
+ );
160
+
161
+ // Get context only (without LLM response)
162
+ const context = await client.query.getContext(
163
+ datasetId,
164
+ 'Find relevant information'
165
+ );
166
+ console.log(`Entities: ${context.entities?.length}`);
167
+ console.log(`Chunks: ${context.chunks?.length}`);
168
+ ```
169
+
170
+ ### Knowledge Graph
171
+
172
+ ```typescript
173
+ // Get knowledge graph
174
+ const graph = await client.graph.getKnowledgeGraph(
175
+ datasetId,
176
+ 'Person',
177
+ { maxDepth: 2, maxNodes: 100 }
178
+ );
179
+
180
+ // Get all labels
181
+ const labels = await client.graph.getLabels(datasetId);
182
+
183
+ // Check entity exists
184
+ const exists = await client.graph.checkEntityExists(datasetId, 'John Doe');
185
+
186
+ // Edit entity
187
+ await client.graph.editEntity(
188
+ datasetId,
189
+ 'John Doe',
190
+ { description: 'Updated description' },
191
+ false // allowRename
192
+ );
193
+ ```
194
+
195
+ ## Error Handling
196
+
197
+ ```typescript
198
+ import {
199
+ RagAgentError,
200
+ AuthenticationError,
201
+ AuthorizationError,
202
+ DatasetNotFoundError,
203
+ ValidationError,
204
+ } from '@rag-agent/sdk';
205
+
206
+ try {
207
+ const dataset = await client.datasets.get('non-existent-id');
208
+ } catch (error) {
209
+ if (error instanceof DatasetNotFoundError) {
210
+ console.log(`Dataset not found: ${error.datasetId}`);
211
+ } else if (error instanceof AuthenticationError) {
212
+ console.log('Authentication failed - check your credentials');
213
+ } else if (error instanceof AuthorizationError) {
214
+ console.log('Permission denied');
215
+ } else if (error instanceof RagAgentError) {
216
+ console.log(`API error: ${error.message} (status: ${error.statusCode})`);
217
+ }
218
+ }
219
+ ```
220
+
221
+ ## Configuration Options
222
+
223
+ | Parameter | Type | Default | Description |
224
+ |-----------|------|---------|-------------|
225
+ | `baseUrl` | string | required | RAG Agent server URL |
226
+ | `apiKey` | string | - | API key for authentication |
227
+ | `jwtToken` | string | - | JWT token for authentication |
228
+ | `timeout` | number | 30000 | Request timeout in milliseconds |
229
+ | `maxRetries` | number | 3 | Maximum retry attempts |
230
+ | `retryDelay` | number | 1000 | Initial delay between retries (ms) |
231
+
232
+ ## TypeScript Support
233
+
234
+ This SDK is written in TypeScript and provides full type definitions out of the box.
235
+
236
+ ```typescript
237
+ import type {
238
+ Dataset,
239
+ Document,
240
+ QueryResponse,
241
+ QueryMode,
242
+ Entity,
243
+ Relation,
244
+ } from '@rag-agent/sdk';
245
+ ```
246
+
247
+ ## Browser Support
248
+
249
+ This SDK works in modern browsers and Node.js environments. It uses the native `fetch` API.
250
+
251
+ ## License
252
+
253
+ MIT License