1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
import json
from datetime import datetime
from io import BytesIO
from pathlib import Path
from typing import Any, Optional
from uuid import UUID
import aiofiles
from shared.api.models import (
WrappedBooleanResponse,
WrappedChunksResponse,
WrappedCollectionsResponse,
WrappedDocumentResponse,
WrappedDocumentSearchResponse,
WrappedDocumentsResponse,
WrappedEntitiesResponse,
WrappedGenericMessageResponse,
WrappedIngestionResponse,
WrappedRelationshipsResponse,
)
from ..models import IngestionMode, SearchMode, SearchSettings
class DocumentsSDK:
"""SDK for interacting with documents in the v3 API."""
def __init__(self, client):
self.client = client
async def create(
self,
file_path: Optional[str] = None,
raw_text: Optional[str] = None,
chunks: Optional[list[str]] = None,
id: Optional[str | UUID] = None,
ingestion_mode: Optional[str] = None,
collection_ids: Optional[list[str | UUID]] = None,
metadata: Optional[dict] = None,
ingestion_config: Optional[dict | IngestionMode] = None,
run_with_orchestration: Optional[bool] = True,
) -> WrappedIngestionResponse:
"""Create a new document from either a file or content.
Args:
file_path (Optional[str]): The file to upload, if any
content (Optional[str]): Optional text content to upload, if no file path is provided
id (Optional[str | UUID]): Optional ID to assign to the document
collection_ids (Optional[list[str | UUID]]): Collection IDs to associate with the document. If none are provided, the document will be assigned to the user's default collection.
metadata (Optional[dict]): Optional metadata to assign to the document
ingestion_config (Optional[dict]): Optional ingestion configuration to use
run_with_orchestration (Optional[bool]): Whether to run with orchestration
Returns:
WrappedIngestionResponse
"""
if not file_path and not raw_text and not chunks:
raise ValueError(
"Either `file_path`, `raw_text` or `chunks` must be provided"
)
if (
(file_path and raw_text)
or (file_path and chunks)
or (raw_text and chunks)
):
raise ValueError(
"Only one of `file_path`, `raw_text` or `chunks` may be provided"
)
data: dict[str, Any] = {}
files = None
if id:
data["id"] = str(id)
if metadata:
data["metadata"] = json.dumps(metadata)
if ingestion_config:
if isinstance(ingestion_config, IngestionMode):
ingestion_config = {"mode": ingestion_config.value}
app_config: dict[str, Any] = (
{}
if isinstance(ingestion_config, dict)
else ingestion_config["app"]
)
ingestion_config = dict(ingestion_config)
ingestion_config["app"] = app_config
data["ingestion_config"] = json.dumps(ingestion_config)
if collection_ids:
collection_ids = [
str(collection_id) for collection_id in collection_ids
] # type: ignore
data["collection_ids"] = json.dumps(collection_ids)
if run_with_orchestration is not None:
data["run_with_orchestration"] = str(run_with_orchestration)
if ingestion_mode is not None:
data["ingestion_mode"] = ingestion_mode
if file_path:
# Create a new file instance that will remain open during the request
file_instance = open(file_path, "rb")
files = [
(
"file",
(file_path, file_instance, "application/octet-stream"),
)
]
try:
response_dict = await self.client._make_request(
"POST",
"documents",
data=data,
files=files,
version="v3",
)
finally:
# Ensure we close the file after the request is complete
file_instance.close()
elif raw_text:
data["raw_text"] = raw_text # type: ignore
response_dict = await self.client._make_request(
"POST",
"documents",
data=data,
version="v3",
)
else:
data["chunks"] = json.dumps(chunks)
response_dict = await self.client._make_request(
"POST",
"documents",
data=data,
version="v3",
)
return WrappedIngestionResponse(**response_dict)
async def append_metadata(
self,
id: str | UUID,
metadata: list[dict],
) -> WrappedDocumentResponse:
"""Append metadata to a document.
Args:
id (str | UUID): ID of document to append metadata to
metadata (list[dict]): Metadata to append
Returns:
WrappedDocumentResponse
"""
data = json.dumps(metadata)
response_dict = await self.client._make_request(
"PATCH",
f"documents/{str(id)}/metadata",
data=data,
version="v3",
)
return WrappedDocumentResponse(**response_dict)
async def replace_metadata(
self,
id: str | UUID,
metadata: list[dict],
) -> WrappedDocumentResponse:
"""Replace metadata for a document.
Args:
id (str | UUID): ID of document to replace metadata for
metadata (list[dict]): The metadata that will replace the existing metadata
"""
data = json.dumps(metadata)
response_dict = await self.client._make_request(
"PUT",
f"documents/{str(id)}/metadata",
data=data,
version="v3",
)
return WrappedDocumentResponse(**response_dict)
async def retrieve(
self,
id: str | UUID,
) -> WrappedDocumentResponse:
"""Get a specific document by ID.
Args:
id (str | UUID): ID of document to retrieve
Returns:
WrappedDocumentResponse
"""
response_dict = await self.client._make_request(
"GET",
f"documents/{str(id)}",
version="v3",
)
return WrappedDocumentResponse(**response_dict)
async def download(
self,
id: str | UUID,
) -> BytesIO:
response = await self.client._make_request(
"GET",
f"documents/{str(id)}/download",
version="v3",
)
if not isinstance(response, BytesIO):
raise ValueError("Expected BytesIO response")
return response
async def download_zip(
self,
document_ids: Optional[list[str | UUID]] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
output_path: Optional[str | Path] = None,
) -> BytesIO | None:
"""Download multiple documents as a zip file."""
params: dict[str, Any] = {}
if document_ids:
params["document_ids"] = [str(doc_id) for doc_id in document_ids]
if start_date:
params["start_date"] = start_date.isoformat()
if end_date:
params["end_date"] = end_date.isoformat()
response = await self.client._make_request(
"GET",
"documents/download_zip",
params=params,
version="v3",
)
if not isinstance(response, BytesIO):
raise ValueError("Expected BytesIO response")
if output_path:
output_path = (
Path(output_path)
if isinstance(output_path, str)
else output_path
)
async with aiofiles.open(output_path, "wb") as f:
await f.write(response.getvalue())
return None
return response
async def export(
self,
output_path: str | Path,
columns: Optional[list[str]] = None,
filters: Optional[dict] = None,
include_header: bool = True,
) -> None:
"""Export documents to a CSV file, streaming the results directly to
disk.
Args:
output_path (str | Path): Local path where the CSV file should be saved
columns (Optional[list[str]]): Specific columns to export. If None, exports default columns
filters (Optional[dict]): Optional filters to apply when selecting documents
include_header (bool): Whether to include column headers in the CSV (default: True)
Returns:
None
"""
# Convert path to string if it's a Path object
output_path = (
str(output_path) if isinstance(output_path, Path) else output_path
)
data: dict[str, Any] = {"include_header": include_header}
if columns:
data["columns"] = columns
if filters:
data["filters"] = filters
# Stream response directly to file
async with aiofiles.open(output_path, "wb") as f:
async with self.client.session.post(
f"{self.client.base_url}/v3/documents/export",
json=data,
headers={
"Accept": "text/csv",
**self.client._get_auth_header(),
},
) as response:
if response.status != 200:
raise ValueError(
f"Export failed with status {response.status}",
response,
)
async for chunk in response.content.iter_chunks():
if chunk:
await f.write(chunk[0])
async def export_entities(
self,
id: str | UUID,
output_path: str | Path,
columns: Optional[list[str]] = None,
filters: Optional[dict] = None,
include_header: bool = True,
) -> None:
"""Export documents to a CSV file, streaming the results directly to
disk.
Args:
output_path (str | Path): Local path where the CSV file should be saved
columns (Optional[list[str]]): Specific columns to export. If None, exports default columns
filters (Optional[dict]): Optional filters to apply when selecting documents
include_header (bool): Whether to include column headers in the CSV (default: True)
Returns:
None
"""
# Convert path to string if it's a Path object
output_path = (
str(output_path) if isinstance(output_path, Path) else output_path
)
# Prepare request data
data: dict[str, Any] = {"include_header": include_header}
if columns:
data["columns"] = columns
if filters:
data["filters"] = filters
# Stream response directly to file
async with aiofiles.open(output_path, "wb") as f:
async with self.client.session.post(
f"{self.client.base_url}/v3/documents/{str(id)}/entities/export",
json=data,
headers={
"Accept": "text/csv",
**self.client._get_auth_header(),
},
) as response:
if response.status != 200:
raise ValueError(
f"Export failed with status {response.status}",
response,
)
async for chunk in response.content.iter_chunks():
if chunk:
await f.write(chunk[0])
async def export_relationships(
self,
id: str | UUID,
output_path: str | Path,
columns: Optional[list[str]] = None,
filters: Optional[dict] = None,
include_header: bool = True,
) -> None:
"""Export document relationships to a CSV file, streaming the results
directly to disk.
Args:
output_path (str | Path): Local path where the CSV file should be saved
columns (Optional[list[str]]): Specific columns to export. If None, exports default columns
filters (Optional[dict]): Optional filters to apply when selecting documents
include_header (bool): Whether to include column headers in the CSV (default: True)
Returns:
None
"""
# Convert path to string if it's a Path object
output_path = (
str(output_path) if isinstance(output_path, Path) else output_path
)
# Prepare request data
data: dict[str, Any] = {"include_header": include_header}
if columns:
data["columns"] = columns
if filters:
data["filters"] = filters
# Stream response directly to file
async with aiofiles.open(output_path, "wb") as f:
async with self.client.session.post(
f"{self.client.base_url}/v3/documents/{str(id)}/relationships/export",
json=data,
headers={
"Accept": "text/csv",
**self.client._get_auth_header(),
},
) as response:
if response.status != 200:
raise ValueError(
f"Export failed with status {response.status}",
response,
)
async for chunk in response.content.iter_chunks():
if chunk:
await f.write(chunk[0])
async def delete(
self,
id: str | UUID,
) -> WrappedBooleanResponse:
"""Delete a specific document.
Args:
id (str | UUID): ID of document to delete
Returns:
WrappedBooleanResponse
"""
response_dict = await self.client._make_request(
"DELETE",
f"documents/{str(id)}",
version="v3",
)
return WrappedBooleanResponse(**response_dict)
async def list_chunks(
self,
id: str | UUID,
include_vectors: Optional[bool] = False,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedChunksResponse:
"""Get chunks for a specific document.
Args:
id (str | UUID): ID of document to retrieve chunks for
include_vectors (Optional[bool]): Whether to include vector embeddings in the response
offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
Returns:
WrappedChunksResponse
"""
params = {
"offset": offset,
"limit": limit,
"include_vectors": include_vectors,
}
response_dict = await self.client._make_request(
"GET",
f"documents/{str(id)}/chunks",
params=params,
version="v3",
)
return WrappedChunksResponse(**response_dict)
async def list_collections(
self,
id: str | UUID,
include_vectors: Optional[bool] = False,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedCollectionsResponse:
"""List collections for a specific document.
Args:
id (str | UUID): ID of document to retrieve collections for
offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
Returns:
WrappedCollectionsResponse
"""
params = {
"offset": offset,
"limit": limit,
}
response_dict = await self.client._make_request(
"GET",
f"documents/{str(id)}/collections",
params=params,
version="v3",
)
return WrappedCollectionsResponse(**response_dict)
async def delete_by_filter(
self,
filters: dict,
) -> WrappedBooleanResponse:
"""Delete documents based on filters.
Args:
filters (dict): Filters to apply when selecting documents to delete
Returns:
WrappedBooleanResponse
"""
filters_json = json.dumps(filters)
response_dict = await self.client._make_request(
"DELETE",
"documents/by-filter",
data=filters_json,
version="v3",
)
return WrappedBooleanResponse(**response_dict)
async def extract(
self,
id: str | UUID,
settings: Optional[dict] = None,
run_with_orchestration: Optional[bool] = True,
) -> WrappedGenericMessageResponse:
"""Extract entities and relationships from a document.
Args:
id (str, UUID): ID of document to extract from
settings (Optional[dict]): Settings for extraction process
run_with_orchestration (Optional[bool]): Whether to run with orchestration
Returns:
WrappedGenericMessageResponse
"""
data: dict[str, Any] = {}
if settings:
data["settings"] = json.dumps(settings)
if run_with_orchestration is not None:
data["run_with_orchestration"] = str(run_with_orchestration)
response_dict = await self.client._make_request(
"POST",
f"documents/{str(id)}/extract",
params=data,
version="v3",
)
return WrappedGenericMessageResponse(**response_dict)
async def list_entities(
self,
id: str | UUID,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
include_embeddings: Optional[bool] = False,
) -> WrappedEntitiesResponse:
"""List entities extracted from a document.
Args:
id (str | UUID): ID of document to get entities from
offset (Optional[int]): Number of items to skip
limit (Optional[int]): Max number of items to return
include_embeddings (Optional[bool]): Whether to include embeddings
Returns:
WrappedEntitiesResponse
"""
params = {
"offset": offset,
"limit": limit,
"include_embeddings": include_embeddings,
}
response_dict = await self.client._make_request(
"GET",
f"documents/{str(id)}/entities",
params=params,
version="v3",
)
return WrappedEntitiesResponse(**response_dict)
async def list_relationships(
self,
id: str | UUID,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
entity_names: Optional[list[str]] = None,
relationship_types: Optional[list[str]] = None,
) -> WrappedRelationshipsResponse:
"""List relationships extracted from a document.
Args:
id (str | UUID): ID of document to get relationships from
offset (Optional[int]): Number of items to skip
limit (Optional[int]): Max number of items to return
entity_names (Optional[list[str]]): Filter by entity names
relationship_types (Optional[list[str]]): Filter by relationship types
Returns:
WrappedRelationshipsResponse
"""
params: dict[str, Any] = {
"offset": offset,
"limit": limit,
}
if entity_names:
params["entity_names"] = entity_names
if relationship_types:
params["relationship_types"] = relationship_types
response_dict = await self.client._make_request(
"GET",
f"documents/{str(id)}/relationships",
params=params,
version="v3",
)
return WrappedRelationshipsResponse(**response_dict)
async def list(
self,
ids: Optional[list[str | UUID]] = None,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedDocumentsResponse:
"""List documents with pagination.
Args:
ids (Optional[list[str | UUID]]): Optional list of document IDs to filter by
offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
Returns:
WrappedDocumentsResponse
"""
params = {
"offset": offset,
"limit": limit,
}
if ids:
params["ids"] = [str(doc_id) for doc_id in ids] # type: ignore
response_dict = await self.client._make_request(
"GET",
"documents",
params=params,
version="v3",
)
return WrappedDocumentsResponse(**response_dict)
async def search(
self,
query: str,
search_mode: Optional[str | SearchMode] = "custom",
search_settings: Optional[dict | SearchSettings] = None,
) -> WrappedDocumentSearchResponse:
"""Conduct a vector and/or graph search.
Args:
query (str): The query to search for.
search_settings (Optional[dict, SearchSettings]]): Vector search settings.
Returns:
WrappedDocumentSearchResponse
"""
if search_settings and not isinstance(search_settings, dict):
search_settings = search_settings.model_dump()
data: dict[str, Any] = {
"query": query,
"search_settings": search_settings,
}
if search_mode:
data["search_mode"] = search_mode
response_dict = await self.client._make_request(
"POST",
"documents/search",
json=data,
version="v3",
)
return WrappedDocumentSearchResponse(**response_dict)
async def deduplicate(
self,
id: str | UUID,
settings: Optional[dict] = None,
run_with_orchestration: Optional[bool] = True,
) -> WrappedGenericMessageResponse:
"""Deduplicate entities and relationships from a document.
Args:
id (str, UUID): ID of document to extract from
settings (Optional[dict]): Settings for extraction process
run_with_orchestration (Optional[bool]): Whether to run with orchestration
Returns:
WrappedGenericMessageResponse
"""
data: dict[str, Any] = {}
if settings:
data["settings"] = json.dumps(settings)
if run_with_orchestration is not None:
data["run_with_orchestration"] = str(run_with_orchestration)
response_dict = await self.client._make_request(
"POST",
f"documents/{str(id)}/deduplicate",
params=data,
version="v3",
)
return WrappedGenericMessageResponse(**response_dict)
|