graphor 0.4.2 → 0.5.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/CHANGELOG.md +16 -0
- package/README.md +306 -282
- package/client.d.mts +5 -5
- package/client.d.mts.map +1 -1
- package/client.d.ts +5 -5
- package/client.d.ts.map +1 -1
- package/client.js +8 -8
- package/client.js.map +1 -1
- package/client.mjs +8 -8
- package/client.mjs.map +1 -1
- package/internal/request-options.d.mts +1 -1
- package/internal/request-options.d.ts +1 -1
- package/package.json +1 -1
- package/src/client.ts +10 -10
- package/src/internal/request-options.ts +1 -1
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/README.md
CHANGED
|
@@ -1,403 +1,427 @@
|
|
|
1
|
-
# Graphor
|
|
1
|
+
# Graphor TypeScript SDK
|
|
2
2
|
|
|
3
|
-
[)](https://npmjs.org/package/graphor)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
The official TypeScript SDK for the [Graphor](https://graphorlm.com) API. Build intelligent document applications with ease.
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
**Features:**
|
|
9
|
+
- 📄 **Document Ingestion** — Upload files, web pages, GitHub repos, and YouTube videos
|
|
10
|
+
- 💬 **Document Chat** — Ask questions with conversational memory
|
|
11
|
+
- 📊 **Structured Extraction** — Extract data using JSON Schema
|
|
12
|
+
- 🔍 **Semantic Search** — Retrieve relevant chunks for custom RAG pipelines
|
|
13
|
+
- 🔒 **Type Safety** — Complete TypeScript definitions for all params and responses
|
|
14
|
+
- 🌐 **Multi-Runtime** — Works in Node.js, Deno, Bun, and browsers
|
|
10
15
|
|
|
11
16
|
## MCP Server
|
|
12
17
|
|
|
13
|
-
Use the Graphor
|
|
18
|
+
Use the Graphor MCP Server to enable AI assistants to interact with this API, allowing them to explore endpoints, make test requests, and use documentation to help integrate this SDK into your application.
|
|
14
19
|
|
|
15
|
-
[](https://cursor.com/en-US/install-mcp?name=graphor-mcp&config=
|
|
16
|
-
[](https://vscode.stainless.com/mcp/%7B%22name%22%3A%22graphor-mcp%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22graphor-mcp%22%5D%2C%22env%22%3A%7B%
|
|
20
|
+
[](https://cursor.com/en-US/install-mcp?name=graphor-mcp&config=eyJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsImdyYXBob3ItbWNwIl0sImVudiI6eyJHUkFQSE9SX0FQSV9LRVkiOiJNeSBBUEkgS2V5In19)
|
|
21
|
+
[](https://vscode.stainless.com/mcp/%7B%22name%22%3A%22graphor-mcp%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22graphor-mcp%22%5D%2C%22env%22%3A%7B%22GRAPHOR_API_KEY%22%3A%22My%20API%20Key%22%7D%7D)
|
|
17
22
|
|
|
18
23
|
> Note: You may need to set environment variables in your MCP client.
|
|
19
24
|
|
|
25
|
+
## Documentation
|
|
26
|
+
|
|
27
|
+
📚 **Full documentation**: [docs.graphorlm.com/sdk/overview](https://docs.graphorlm.com/sdk/overview)
|
|
28
|
+
|
|
20
29
|
## Installation
|
|
21
30
|
|
|
22
|
-
```
|
|
31
|
+
```bash
|
|
23
32
|
npm install graphor
|
|
24
33
|
```
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
Or with your preferred package manager:
|
|
27
36
|
|
|
28
|
-
|
|
37
|
+
```bash
|
|
38
|
+
yarn add graphor
|
|
39
|
+
pnpm add graphor
|
|
40
|
+
```
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
```js
|
|
32
|
-
import GraphorPrd from 'graphor';
|
|
42
|
+
## Quick Start
|
|
33
43
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
```typescript
|
|
45
|
+
import Graphor from 'graphor';
|
|
46
|
+
import fs from 'fs';
|
|
47
|
+
|
|
48
|
+
const client = new Graphor(); // Uses GRAPHOR_API_KEY env var
|
|
37
49
|
|
|
38
|
-
|
|
50
|
+
// Upload a document
|
|
51
|
+
const source = await client.sources.upload({ file: fs.createReadStream('document.pdf') });
|
|
52
|
+
console.log(`Uploaded: ${source.file_name}`);
|
|
39
53
|
|
|
40
|
-
|
|
54
|
+
// Ask questions about your documents
|
|
55
|
+
const response = await client.sources.ask({ question: 'What are the main topics?' });
|
|
56
|
+
console.log(`Answer: ${response.answer}`);
|
|
41
57
|
```
|
|
42
58
|
|
|
43
|
-
|
|
59
|
+
## Authentication
|
|
44
60
|
|
|
45
|
-
|
|
61
|
+
Set your API key as an environment variable (recommended):
|
|
46
62
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
63
|
+
```bash
|
|
64
|
+
export GRAPHOR_API_KEY="grlm_your_api_key_here"
|
|
65
|
+
```
|
|
50
66
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
67
|
+
```typescript
|
|
68
|
+
import Graphor from 'graphor';
|
|
54
69
|
|
|
55
|
-
const
|
|
56
|
-
const publicSource: GraphorPrd.PublicSource = await client.sources.upload(params);
|
|
70
|
+
const client = new Graphor(); // Automatically uses GRAPHOR_API_KEY
|
|
57
71
|
```
|
|
58
72
|
|
|
59
|
-
|
|
73
|
+
Or pass it directly:
|
|
60
74
|
|
|
61
|
-
|
|
75
|
+
```typescript
|
|
76
|
+
const client = new Graphor({ apiKey: 'grlm_your_api_key_here' });
|
|
77
|
+
```
|
|
62
78
|
|
|
63
|
-
|
|
79
|
+
## Core Features
|
|
80
|
+
|
|
81
|
+
### 📄 Upload Documents
|
|
64
82
|
|
|
65
|
-
|
|
66
|
-
- a `fetch` `Response` (or an object with the same structure)
|
|
67
|
-
- an `fs.ReadStream`
|
|
68
|
-
- the return value of our `toFile` helper
|
|
83
|
+
Upload files, web pages, GitHub repositories, or YouTube videos:
|
|
69
84
|
|
|
70
|
-
```
|
|
85
|
+
```typescript
|
|
86
|
+
import Graphor from 'graphor';
|
|
71
87
|
import fs from 'fs';
|
|
72
|
-
import GraphorPrd, { toFile } from 'graphor';
|
|
73
88
|
|
|
74
|
-
const client = new
|
|
89
|
+
const client = new Graphor();
|
|
75
90
|
|
|
76
|
-
//
|
|
77
|
-
await client.sources.upload({ file: fs.createReadStream('
|
|
91
|
+
// Upload a local file
|
|
92
|
+
const source = await client.sources.upload({ file: fs.createReadStream('report.pdf') });
|
|
78
93
|
|
|
79
|
-
//
|
|
80
|
-
await client.sources.
|
|
94
|
+
// Upload from URL
|
|
95
|
+
const urlSource = await client.sources.uploadUrl({ url: 'https://example.com/article' });
|
|
81
96
|
|
|
82
|
-
//
|
|
83
|
-
await client.sources.
|
|
84
|
-
|
|
85
|
-
// Finally, if none of the above are convenient, you can use our `toFile` helper:
|
|
86
|
-
await client.sources.upload({ file: await toFile(Buffer.from('my bytes'), 'file') });
|
|
87
|
-
await client.sources.upload({ file: await toFile(new Uint8Array([0, 1, 2]), 'file') });
|
|
88
|
-
```
|
|
97
|
+
// Upload from GitHub
|
|
98
|
+
const githubSource = await client.sources.uploadGithub({ url: 'https://github.com/org/repo' });
|
|
89
99
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
When the library is unable to connect to the API,
|
|
93
|
-
or if the API returns a non-success status code (i.e., 4xx or 5xx response),
|
|
94
|
-
a subclass of `APIError` will be thrown:
|
|
95
|
-
|
|
96
|
-
<!-- prettier-ignore -->
|
|
97
|
-
```ts
|
|
98
|
-
const publicSource = await client.sources
|
|
99
|
-
.upload({ file: fs.createReadStream('path/to/file') })
|
|
100
|
-
.catch(async (err) => {
|
|
101
|
-
if (err instanceof GraphorPrd.APIError) {
|
|
102
|
-
console.log(err.status); // 400
|
|
103
|
-
console.log(err.name); // BadRequestError
|
|
104
|
-
console.log(err.headers); // {server: 'nginx', ...}
|
|
105
|
-
} else {
|
|
106
|
-
throw err;
|
|
107
|
-
}
|
|
108
|
-
});
|
|
100
|
+
// Upload from YouTube
|
|
101
|
+
const youtubeSource = await client.sources.uploadYoutube({ url: 'https://youtube.com/watch?v=...' });
|
|
109
102
|
```
|
|
110
103
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
| Status Code | Error Type |
|
|
114
|
-
| ----------- | -------------------------- |
|
|
115
|
-
| 400 | `BadRequestError` |
|
|
116
|
-
| 401 | `AuthenticationError` |
|
|
117
|
-
| 403 | `PermissionDeniedError` |
|
|
118
|
-
| 404 | `NotFoundError` |
|
|
119
|
-
| 422 | `UnprocessableEntityError` |
|
|
120
|
-
| 429 | `RateLimitError` |
|
|
121
|
-
| >=500 | `InternalServerError` |
|
|
122
|
-
| N/A | `APIConnectionError` |
|
|
104
|
+
**Supported formats:** PDF, DOCX, TXT, MD, HTML, CSV, XLSX, PNG, JPG, MP3, MP4, and more.
|
|
123
105
|
|
|
124
|
-
|
|
106
|
+
📖 [Full upload documentation](https://docs.graphorlm.com/sdk/sources/upload)
|
|
125
107
|
|
|
126
|
-
|
|
127
|
-
Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict,
|
|
128
|
-
429 Rate Limit, and >=500 Internal errors will all be retried by default.
|
|
108
|
+
### ⚙️ Process Documents
|
|
129
109
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
<!-- prettier-ignore -->
|
|
133
|
-
```js
|
|
134
|
-
// Configure the default for all requests:
|
|
135
|
-
const client = new GraphorPrd({
|
|
136
|
-
maxRetries: 0, // default is 2
|
|
137
|
-
});
|
|
110
|
+
Reprocess documents with different OCR/parsing methods:
|
|
138
111
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
112
|
+
```typescript
|
|
113
|
+
// Reprocess with high-resolution parsing
|
|
114
|
+
const source = await client.sources.parse({
|
|
115
|
+
file_name: 'document.pdf',
|
|
116
|
+
partition_method: 'hi_res' // Options: basic, hi_res, hi_res_ft, mai, graphorlm
|
|
142
117
|
});
|
|
143
118
|
```
|
|
144
119
|
|
|
145
|
-
|
|
120
|
+
📖 [Full processing documentation](https://docs.graphorlm.com/sdk/sources/process)
|
|
121
|
+
|
|
122
|
+
### 💬 Chat with Documents
|
|
146
123
|
|
|
147
|
-
|
|
124
|
+
Ask questions about your documents with conversational memory:
|
|
148
125
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
timeout: 20 * 1000, // 20 seconds (default is 1 minute)
|
|
126
|
+
```typescript
|
|
127
|
+
// Ask a question
|
|
128
|
+
const response = await client.sources.ask({
|
|
129
|
+
question: 'What are the key findings?'
|
|
154
130
|
});
|
|
131
|
+
console.log(response.answer);
|
|
155
132
|
|
|
156
|
-
//
|
|
157
|
-
await client.sources.
|
|
158
|
-
|
|
133
|
+
// Follow-up question (maintains context)
|
|
134
|
+
const followUp = await client.sources.ask({
|
|
135
|
+
question: 'Can you elaborate on the first point?',
|
|
136
|
+
conversation_id: response.conversation_id
|
|
159
137
|
});
|
|
160
|
-
|
|
138
|
+
console.log(followUp.answer);
|
|
161
139
|
|
|
162
|
-
|
|
140
|
+
// Scope to specific documents
|
|
141
|
+
const scopedResponse = await client.sources.ask({
|
|
142
|
+
question: 'Compare these two reports',
|
|
143
|
+
file_names: ['report-2023.pdf', 'report-2024.pdf']
|
|
144
|
+
});
|
|
145
|
+
```
|
|
163
146
|
|
|
164
|
-
|
|
147
|
+
📖 [Full chat documentation](https://docs.graphorlm.com/sdk/chat)
|
|
148
|
+
|
|
149
|
+
### 📊 Extract Structured Data
|
|
150
|
+
|
|
151
|
+
Extract structured information using JSON Schema:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const result = await client.sources.extract({
|
|
155
|
+
file_names: ['invoice.pdf'],
|
|
156
|
+
user_instruction: 'Extract invoice details',
|
|
157
|
+
output_schema: {
|
|
158
|
+
type: 'object',
|
|
159
|
+
properties: {
|
|
160
|
+
invoice_number: { type: 'string' },
|
|
161
|
+
total_amount: { type: 'number' },
|
|
162
|
+
line_items: {
|
|
163
|
+
type: 'array',
|
|
164
|
+
items: {
|
|
165
|
+
type: 'object',
|
|
166
|
+
properties: {
|
|
167
|
+
description: { type: 'string' },
|
|
168
|
+
amount: { type: 'number' }
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
165
175
|
|
|
166
|
-
|
|
176
|
+
console.log(result.structured_output);
|
|
177
|
+
// { invoice_number: "INV-001", total_amount: 1500.00, line_items: [...] }
|
|
178
|
+
```
|
|
167
179
|
|
|
168
|
-
|
|
180
|
+
📖 [Full extraction documentation](https://docs.graphorlm.com/sdk/extract)
|
|
169
181
|
|
|
170
|
-
|
|
171
|
-
This method returns as soon as the headers for a successful response are received and does not consume the response body, so you are free to write custom parsing or streaming logic.
|
|
182
|
+
### 🔍 Retrieve Chunks (Prebuilt RAG)
|
|
172
183
|
|
|
173
|
-
|
|
174
|
-
Unlike `.asResponse()` this method consumes the body, returning once it is parsed.
|
|
184
|
+
Build custom RAG pipelines with semantic search:
|
|
175
185
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
186
|
+
```typescript
|
|
187
|
+
// Retrieve relevant chunks
|
|
188
|
+
const result = await client.sources.retrieveChunks({
|
|
189
|
+
query: 'What are the payment terms?'
|
|
190
|
+
});
|
|
179
191
|
|
|
180
|
-
const
|
|
181
|
-
.
|
|
182
|
-
.
|
|
183
|
-
console.log(
|
|
184
|
-
console.log(
|
|
192
|
+
for (const chunk of result.chunks) {
|
|
193
|
+
console.log(`[${chunk.file_name}, Page ${chunk.page_number}]`);
|
|
194
|
+
console.log(`Score: ${chunk.score.toFixed(2)}`);
|
|
195
|
+
console.log(chunk.text);
|
|
196
|
+
console.log('---');
|
|
197
|
+
}
|
|
185
198
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
console.log(raw.headers.get('X-My-Header'));
|
|
190
|
-
console.log(publicSource.project_id);
|
|
199
|
+
// Use with your preferred LLM
|
|
200
|
+
const context = result.chunks.map(c => c.text).join('\n');
|
|
201
|
+
// Pass context to OpenAI, Anthropic, etc.
|
|
191
202
|
```
|
|
192
203
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
> [!IMPORTANT]
|
|
196
|
-
> All log messages are intended for debugging only. The format and content of log messages
|
|
197
|
-
> may change between releases.
|
|
204
|
+
📖 [Full RAG documentation](https://docs.graphorlm.com/sdk/prebuilt-rag)
|
|
198
205
|
|
|
199
|
-
|
|
206
|
+
### 📋 Manage Sources
|
|
200
207
|
|
|
201
|
-
|
|
208
|
+
List, inspect, and delete documents:
|
|
202
209
|
|
|
203
|
-
|
|
204
|
-
|
|
210
|
+
```typescript
|
|
211
|
+
// List all sources
|
|
212
|
+
const sources = await client.sources.list();
|
|
213
|
+
for (const source of sources) {
|
|
214
|
+
console.log(`${source.file_name}: ${source.status}`);
|
|
215
|
+
}
|
|
205
216
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
217
|
+
// Get document elements
|
|
218
|
+
const elements = await client.sources.loadElements({
|
|
219
|
+
file_name: 'document.pdf',
|
|
220
|
+
page: 1,
|
|
221
|
+
page_size: 50
|
|
211
222
|
});
|
|
212
|
-
```
|
|
213
223
|
|
|
214
|
-
|
|
224
|
+
// Delete a source
|
|
225
|
+
const result = await client.sources.delete({ file_name: 'document.pdf' });
|
|
226
|
+
```
|
|
215
227
|
|
|
216
|
-
|
|
217
|
-
- `'info'` - Show info messages, warnings, and errors
|
|
218
|
-
- `'warn'` - Show warnings and errors (default)
|
|
219
|
-
- `'error'` - Show only errors
|
|
220
|
-
- `'off'` - Disable all logging
|
|
228
|
+
## File Uploads
|
|
221
229
|
|
|
222
|
-
|
|
223
|
-
Some authentication-related headers are redacted, but sensitive data in request and response bodies
|
|
224
|
-
may still be visible.
|
|
230
|
+
Request parameters that correspond to file uploads can be passed in many different forms:
|
|
225
231
|
|
|
226
|
-
|
|
232
|
+
```typescript
|
|
233
|
+
import fs from 'fs';
|
|
234
|
+
import Graphor, { toFile } from 'graphor';
|
|
227
235
|
|
|
228
|
-
|
|
229
|
-
Most logging libraries are supported, including [pino](https://www.npmjs.com/package/pino), [winston](https://www.npmjs.com/package/winston), [bunyan](https://www.npmjs.com/package/bunyan), [consola](https://www.npmjs.com/package/consola), [signale](https://www.npmjs.com/package/signale), and [@std/log](https://jsr.io/@std/log). If your logger doesn't work, please open an issue.
|
|
236
|
+
const client = new Graphor();
|
|
230
237
|
|
|
231
|
-
|
|
232
|
-
|
|
238
|
+
// Using fs.createReadStream (Node.js)
|
|
239
|
+
await client.sources.upload({ file: fs.createReadStream('/path/to/file') });
|
|
233
240
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
import pino from 'pino';
|
|
241
|
+
// Using File API (browsers)
|
|
242
|
+
await client.sources.upload({ file: new File(['my bytes'], 'file') });
|
|
237
243
|
|
|
238
|
-
|
|
244
|
+
// Using fetch Response
|
|
245
|
+
await client.sources.upload({ file: await fetch('https://somesite/file') });
|
|
239
246
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
logLevel: 'debug', // Send all messages to pino, allowing it to filter
|
|
243
|
-
});
|
|
247
|
+
// Using toFile helper
|
|
248
|
+
await client.sources.upload({ file: await toFile(Buffer.from('my bytes'), 'file') });
|
|
244
249
|
```
|
|
245
250
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
})
|
|
251
|
+
## Error Handling
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
import Graphor from 'graphor';
|
|
255
|
+
|
|
256
|
+
const client = new Graphor();
|
|
257
|
+
|
|
258
|
+
try {
|
|
259
|
+
const response = await client.sources.ask({ question: 'What is this about?' });
|
|
260
|
+
} catch (err) {
|
|
261
|
+
if (err instanceof Graphor.BadRequestError) {
|
|
262
|
+
console.log('Bad request');
|
|
263
|
+
} else if (err instanceof Graphor.AuthenticationError) {
|
|
264
|
+
console.log('Invalid API key');
|
|
265
|
+
} else if (err instanceof Graphor.NotFoundError) {
|
|
266
|
+
console.log('Resource not found');
|
|
267
|
+
} else if (err instanceof Graphor.RateLimitError) {
|
|
268
|
+
console.log('Rate limited - back off and retry');
|
|
269
|
+
} else if (err instanceof Graphor.APIConnectionError) {
|
|
270
|
+
console.log('Network error');
|
|
271
|
+
} else if (err instanceof Graphor.APIError) {
|
|
272
|
+
console.log(`API error: ${err.status}`);
|
|
273
|
+
} else {
|
|
274
|
+
throw err;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
261
277
|
```
|
|
262
278
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
baz: 'undocumented option',
|
|
274
|
-
});
|
|
275
|
-
```
|
|
279
|
+
| Status Code | Error Type |
|
|
280
|
+
|-------------|------------|
|
|
281
|
+
| 400 | `BadRequestError` |
|
|
282
|
+
| 401 | `AuthenticationError` |
|
|
283
|
+
| 403 | `PermissionDeniedError` |
|
|
284
|
+
| 404 | `NotFoundError` |
|
|
285
|
+
| 422 | `UnprocessableEntityError` |
|
|
286
|
+
| 429 | `RateLimitError` |
|
|
287
|
+
| ≥500 | `InternalServerError` |
|
|
288
|
+
| N/A | `APIConnectionError` |
|
|
276
289
|
|
|
277
|
-
|
|
278
|
-
extra param in the body.
|
|
290
|
+
## Configuration
|
|
279
291
|
|
|
280
|
-
|
|
281
|
-
options.
|
|
292
|
+
### Retries
|
|
282
293
|
|
|
283
|
-
|
|
294
|
+
Requests are automatically retried twice with exponential backoff:
|
|
284
295
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
296
|
+
```typescript
|
|
297
|
+
// Configure default retries
|
|
298
|
+
const client = new Graphor({ maxRetries: 5 });
|
|
288
299
|
|
|
289
|
-
|
|
300
|
+
// Or per-request
|
|
301
|
+
await client.sources.ask({ question: '...' }, { maxRetries: 3 });
|
|
302
|
+
```
|
|
290
303
|
|
|
291
|
-
|
|
304
|
+
### Timeouts
|
|
292
305
|
|
|
293
|
-
|
|
306
|
+
Default timeout is 60 seconds:
|
|
294
307
|
|
|
295
|
-
```
|
|
296
|
-
|
|
308
|
+
```typescript
|
|
309
|
+
// Configure default timeout (in milliseconds)
|
|
310
|
+
const client = new Graphor({ timeout: 120 * 1000 });
|
|
297
311
|
|
|
298
|
-
|
|
312
|
+
// Or per-request
|
|
313
|
+
await client.sources.parse(
|
|
314
|
+
{ file_name: 'large-document.pdf', partition_method: 'graphorlm' },
|
|
315
|
+
{ timeout: 300 * 1000 }
|
|
316
|
+
);
|
|
299
317
|
```
|
|
300
318
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
```ts
|
|
304
|
-
import GraphorPrd from 'graphor';
|
|
305
|
-
import fetch from 'my-fetch';
|
|
319
|
+
## Complete Example
|
|
306
320
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
### Fetch options
|
|
321
|
+
```typescript
|
|
322
|
+
import Graphor from 'graphor';
|
|
323
|
+
import fs from 'fs';
|
|
311
324
|
|
|
312
|
-
|
|
325
|
+
const client = new Graphor();
|
|
313
326
|
|
|
314
|
-
|
|
315
|
-
|
|
327
|
+
// 1. Upload a document
|
|
328
|
+
const source = await client.sources.upload({ file: fs.createReadStream('contract.pdf') });
|
|
329
|
+
console.log(`✅ Uploaded: ${source.file_name}`);
|
|
316
330
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
331
|
+
// 2. Process with advanced parsing
|
|
332
|
+
const processed = await client.sources.parse({
|
|
333
|
+
file_name: source.file_name,
|
|
334
|
+
partition_method: 'hi_res'
|
|
321
335
|
});
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
#### Configuring proxies
|
|
336
|
+
console.log(`✅ Processed: ${processed.status}`);
|
|
325
337
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
338
|
+
// 3. Ask questions
|
|
339
|
+
const response = await client.sources.ask({
|
|
340
|
+
question: 'What are the key terms of this contract?',
|
|
341
|
+
file_names: [source.file_name]
|
|
342
|
+
});
|
|
343
|
+
console.log(`📝 Answer: ${response.answer}`);
|
|
344
|
+
|
|
345
|
+
// 4. Extract structured data
|
|
346
|
+
const extracted = await client.sources.extract({
|
|
347
|
+
file_names: [source.file_name],
|
|
348
|
+
user_instruction: 'Extract contract details',
|
|
349
|
+
output_schema: {
|
|
350
|
+
type: 'object',
|
|
351
|
+
properties: {
|
|
352
|
+
parties: { type: 'array', items: { type: 'string' } },
|
|
353
|
+
effective_date: { type: 'string' },
|
|
354
|
+
termination_date: { type: 'string' },
|
|
355
|
+
total_value: { type: 'number' }
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
console.log(`📊 Extracted: ${JSON.stringify(extracted.structured_output)}`);
|
|
334
360
|
|
|
335
|
-
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
},
|
|
361
|
+
// 5. Build custom RAG
|
|
362
|
+
const chunks = await client.sources.retrieveChunks({
|
|
363
|
+
query: 'payment obligations',
|
|
364
|
+
file_names: [source.file_name]
|
|
340
365
|
});
|
|
366
|
+
console.log(`🔍 Found ${chunks.total} relevant chunks`);
|
|
341
367
|
```
|
|
342
368
|
|
|
343
|
-
|
|
369
|
+
## API Reference
|
|
344
370
|
|
|
345
|
-
|
|
346
|
-
import GraphorPrd from 'graphor';
|
|
371
|
+
### Sources
|
|
347
372
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
373
|
+
| Method | Description | Docs |
|
|
374
|
+
|--------|-------------|------|
|
|
375
|
+
| `sources.upload()` | Upload a local file | [📖](https://docs.graphorlm.com/sdk/sources/upload#upload-a-file) |
|
|
376
|
+
| `sources.uploadUrl()` | Upload from web URL | [📖](https://docs.graphorlm.com/sdk/sources/upload#upload-from-url) |
|
|
377
|
+
| `sources.uploadGithub()` | Upload from GitHub | [📖](https://docs.graphorlm.com/sdk/sources/upload#upload-from-github) |
|
|
378
|
+
| `sources.uploadYoutube()` | Upload from YouTube | [📖](https://docs.graphorlm.com/sdk/sources/upload#upload-from-youtube) |
|
|
379
|
+
| `sources.parse()` | Reprocess with different method | [📖](https://docs.graphorlm.com/sdk/sources/process) |
|
|
380
|
+
| `sources.list()` | List all sources | [📖](https://docs.graphorlm.com/sdk/sources/list) |
|
|
381
|
+
| `sources.delete()` | Delete a source | [📖](https://docs.graphorlm.com/sdk/sources/delete) |
|
|
382
|
+
| `sources.loadElements()` | Get parsed elements | [📖](https://docs.graphorlm.com/sdk/sources/list-elements) |
|
|
356
383
|
|
|
357
|
-
|
|
358
|
-
import GraphorPrd from 'npm:graphor';
|
|
384
|
+
### Chat & AI
|
|
359
385
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
});
|
|
366
|
-
```
|
|
386
|
+
| Method | Description | Docs |
|
|
387
|
+
|--------|-------------|------|
|
|
388
|
+
| `sources.ask()` | Ask questions about documents | [📖](https://docs.graphorlm.com/sdk/chat) |
|
|
389
|
+
| `sources.extract()` | Extract structured data | [📖](https://docs.graphorlm.com/sdk/extract) |
|
|
390
|
+
| `sources.retrieveChunks()` | Retrieve chunks for RAG | [📖](https://docs.graphorlm.com/sdk/prebuilt-rag) |
|
|
367
391
|
|
|
368
|
-
##
|
|
392
|
+
## TypeScript Support
|
|
369
393
|
|
|
370
|
-
|
|
394
|
+
This library includes complete TypeScript definitions for all request params and response fields:
|
|
371
395
|
|
|
372
|
-
|
|
396
|
+
```typescript
|
|
397
|
+
import Graphor from 'graphor';
|
|
373
398
|
|
|
374
|
-
|
|
375
|
-
2. Changes to library internals which are technically public but not intended or documented for external use. _(Please open a GitHub issue to let us know if you are relying on such internals.)_
|
|
376
|
-
3. Changes that we do not expect to impact the vast majority of users in practice.
|
|
399
|
+
const client = new Graphor();
|
|
377
400
|
|
|
378
|
-
|
|
401
|
+
// Type-safe params and responses
|
|
402
|
+
const params: Graphor.SourceUploadParams = { file: fs.createReadStream('path/to/file') };
|
|
403
|
+
const source: Graphor.PublicSource = await client.sources.upload(params);
|
|
404
|
+
```
|
|
379
405
|
|
|
380
|
-
|
|
406
|
+
Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
|
|
381
407
|
|
|
382
408
|
## Requirements
|
|
383
409
|
|
|
384
|
-
TypeScript >= 4.9
|
|
410
|
+
- TypeScript >= 4.9
|
|
411
|
+
- Node.js 20+ (LTS)
|
|
412
|
+
- Also works in: Deno v1.28+, Bun 1.0+, Cloudflare Workers, Vercel Edge Runtime, and modern browsers
|
|
385
413
|
|
|
386
|
-
|
|
414
|
+
## Contributing
|
|
387
415
|
|
|
388
|
-
|
|
389
|
-
- Node.js 20 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
|
|
390
|
-
- Deno v1.28.0 or higher.
|
|
391
|
-
- Bun 1.0 or later.
|
|
392
|
-
- Cloudflare Workers.
|
|
393
|
-
- Vercel Edge Runtime.
|
|
394
|
-
- Jest 28 or greater with the `"node"` environment (`"jsdom"` is not supported at this time).
|
|
395
|
-
- Nitro v2.6 or greater.
|
|
416
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.
|
|
396
417
|
|
|
397
|
-
|
|
418
|
+
## License
|
|
398
419
|
|
|
399
|
-
|
|
420
|
+
MIT License - see [LICENSE](./LICENSE) for details.
|
|
400
421
|
|
|
401
|
-
##
|
|
422
|
+
## Links
|
|
402
423
|
|
|
403
|
-
|
|
424
|
+
- 📚 [Documentation](https://docs.graphorlm.com/sdk/overview)
|
|
425
|
+
- 🐛 [Issue Tracker](https://github.com/synapseops/graphor-typescript-sdk/issues)
|
|
426
|
+
- 📦 [NPM](https://www.npmjs.com/package/graphor)
|
|
427
|
+
- 🏠 [Graphor](https://graphorlm.com)
|