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
|
from builtins import list as _list
from typing import Any, Optional
from uuid import UUID
from shared.api.models import (
WrappedBooleanResponse,
WrappedCommunitiesResponse,
WrappedCommunityResponse,
WrappedEntitiesResponse,
WrappedEntityResponse,
WrappedGenericMessageResponse,
WrappedGraphResponse,
WrappedGraphsResponse,
WrappedRelationshipResponse,
WrappedRelationshipsResponse,
)
class GraphsSDK:
"""SDK for interacting with knowledge graphs in the v3 API."""
def __init__(self, client):
self.client = client
async def list(
self,
collection_ids: Optional[list[str | UUID]] = None,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedGraphsResponse:
"""List graphs with pagination and filtering options.
Args:
ids (Optional[list[str | UUID]]): Filter graphs by ids
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:
WrappedGraphsResponse
"""
params: dict = {
"offset": offset,
"limit": limit,
}
if collection_ids:
params["collection_ids"] = collection_ids
response_dict = await self.client._make_request(
"GET", "graphs", params=params, version="v3"
)
return WrappedGraphsResponse(**response_dict)
async def retrieve(
self,
collection_id: str | UUID,
) -> WrappedGraphResponse:
"""Get detailed information about a specific graph.
Args:
collection_id (str | UUID): Graph ID to retrieve
Returns:
WrappedGraphResponse
"""
response_dict = await self.client._make_request(
"GET", f"graphs/{str(collection_id)}", version="v3"
)
return WrappedGraphResponse(**response_dict)
async def reset(
self,
collection_id: str | UUID,
) -> WrappedBooleanResponse:
"""Deletes a graph and all its associated data.
This endpoint permanently removes the specified graph along with all
entities and relationships that belong to only this graph.
Entities and relationships extracted from documents are not deleted.
Args:
collection_id (str | UUID): Graph ID to reset
Returns:
WrappedBooleanResponse
"""
response_dict = await self.client._make_request(
"POST", f"graphs/{str(collection_id)}/reset", version="v3"
)
return WrappedBooleanResponse(**response_dict)
async def update(
self,
collection_id: str | UUID,
name: Optional[str] = None,
description: Optional[str] = None,
) -> WrappedGraphResponse:
"""Update graph information.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
name (Optional[str]): Optional new name for the graph
description (Optional[str]): Optional new description for the graph
Returns:
WrappedGraphResponse
"""
data: dict[str, Any] = {}
if name is not None:
data["name"] = name
if description is not None:
data["description"] = description
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}",
json=data,
version="v3",
)
return WrappedGraphResponse(**response_dict)
async def list_entities(
self,
collection_id: str | UUID,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedEntitiesResponse:
"""List entities in a graph.
Args:
collection_id (str | UUID): Graph ID to list entities from
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:
WrappedEntitiesResponse
"""
params: dict = {
"offset": offset,
"limit": limit,
}
response_dict = await self.client._make_request(
"GET",
f"graphs/{str(collection_id)}/entities",
params=params,
version="v3",
)
return WrappedEntitiesResponse(**response_dict)
async def get_entity(
self,
collection_id: str | UUID,
entity_id: str | UUID,
) -> WrappedEntityResponse:
"""Get entity information in a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
entity_id (str | UUID): Entity ID to get from the graph
Returns:
WrappedEntityResponse
"""
response_dict = await self.client._make_request(
"GET",
f"graphs/{str(collection_id)}/entities/{str(entity_id)}",
version="v3",
)
return WrappedEntityResponse(**response_dict)
async def remove_entity(
self,
collection_id: str | UUID,
entity_id: str | UUID,
) -> WrappedBooleanResponse:
"""Remove an entity from a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
entity_id (str | UUID): Entity ID to remove from the graph
Returns:
WrappedBooleanResponse
"""
return await self.client._make_request(
"DELETE",
f"graphs/{str(collection_id)}/entities/{str(entity_id)}",
version="v3",
)
async def list_relationships(
self,
collection_id: str | UUID,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedRelationshipsResponse:
"""List relationships in a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
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:
WrappedRelationshipsResponse
"""
params: dict = {
"offset": offset,
"limit": limit,
}
response_dict = await self.client._make_request(
"GET",
f"graphs/{str(collection_id)}/relationships",
params=params,
version="v3",
)
return WrappedRelationshipsResponse(**response_dict)
async def get_relationship(
self,
collection_id: str | UUID,
relationship_id: str | UUID,
) -> WrappedRelationshipResponse:
"""Get relationship information in a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
relationship_id (str | UUID): Relationship ID to get from the graph
Returns:
WrappedRelationshipResponse
"""
response_dict = await self.client._make_request(
"GET",
f"graphs/{str(collection_id)}/relationships/{str(relationship_id)}",
version="v3",
)
return WrappedRelationshipResponse(**response_dict)
async def remove_relationship(
self,
collection_id: str | UUID,
relationship_id: str | UUID,
) -> WrappedBooleanResponse:
"""Remove a relationship from a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
relationship_id (str | UUID): Relationship ID to remove from the graph
Returns:
WrappedBooleanResponse
"""
response_dict = await self.client._make_request(
"DELETE",
f"graphs/{str(collection_id)}/relationships/{str(relationship_id)}",
version="v3",
)
return WrappedBooleanResponse(**response_dict)
async def build(
self,
collection_id: str | UUID,
settings: Optional[dict] = None,
run_with_orchestration: bool = True,
) -> WrappedGenericMessageResponse:
"""Build a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
settings (dict): Settings for the build
run_with_orchestration (bool, optional): Whether to run with orchestration. Defaults to True.
Returns:
WrappedGenericMessageResponse
"""
data: dict[str, Any] = {
"run_with_orchestration": run_with_orchestration,
}
if settings:
data["settings"] = settings
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}/communities/build",
json=data,
version="v3",
)
return WrappedGenericMessageResponse(**response_dict)
async def list_communities(
self,
collection_id: str | UUID,
offset: Optional[int] = 0,
limit: Optional[int] = 100,
) -> WrappedCommunitiesResponse:
"""List communities in a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
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:
WrappedCommunitiesResponse
"""
params: dict = {
"offset": offset,
"limit": limit,
}
response_dict = await self.client._make_request(
"GET",
f"graphs/{str(collection_id)}/communities",
params=params,
version="v3",
)
return WrappedCommunitiesResponse(**response_dict)
async def get_community(
self,
collection_id: str | UUID,
community_id: str | UUID,
) -> WrappedCommunityResponse:
"""Get community information in a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
community_id (str | UUID): Community ID to get from the graph
Returns:
WrappedCommunityResponse
"""
response_dict = await self.client._make_request(
"GET",
f"graphs/{str(collection_id)}/communities/{str(community_id)}",
version="v3",
)
return WrappedCommunityResponse(**response_dict)
async def update_community(
self,
collection_id: str | UUID,
community_id: str | UUID,
name: Optional[str] = None,
summary: Optional[str] = None,
findings: Optional[_list[str]] = None,
rating: Optional[int] = None,
rating_explanation: Optional[str] = None,
level: Optional[int] = None,
attributes: Optional[dict] = None,
) -> WrappedCommunityResponse:
"""Update community information.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
community_id (str | UUID): Community ID to update
name (Optional[str]): Optional new name for the community
summary (Optional[str]): Optional new summary for the community
findings (Optional[list[str]]): Optional new findings for the community
rating (Optional[int]): Optional new rating for the community
rating_explanation (Optional[str]): Optional new rating explanation for the community
level (Optional[int]): Optional new level for the community
attributes (Optional[dict]): Optional new attributes for the community
Returns:
WrappedCommunityResponse
"""
data: dict[str, Any] = {}
if name is not None:
data["name"] = name
if summary is not None:
data["summary"] = summary
if findings is not None:
data["findings"] = findings
if rating is not None:
data["rating"] = str(rating)
if rating_explanation is not None:
data["rating_explanation"] = rating_explanation
if level is not None:
data["level"] = level
if attributes is not None:
data["attributes"] = attributes
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}/communities/{str(community_id)}",
json=data,
version="v3",
)
return WrappedCommunityResponse(**response_dict)
async def delete_community(
self,
collection_id: str | UUID,
community_id: str | UUID,
) -> WrappedBooleanResponse:
"""Remove a community from a graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
community_id (str | UUID): Community ID to remove from the graph
Returns:
WrappedBooleanResponse
"""
response_dict = await self.client._make_request(
"DELETE",
f"graphs/{str(collection_id)}/communities/{str(community_id)}",
version="v3",
)
return WrappedBooleanResponse(**response_dict)
async def pull(
self,
collection_id: str | UUID,
) -> WrappedBooleanResponse:
"""Adds documents to a graph by copying their entities and
relationships.
This endpoint:
1. Copies document entities to the graphs_entities table
2. Copies document relationships to the graphs_relationships table
3. Associates the documents with the graph
When a document is added:
- Its entities and relationships are copied to graph-specific tables
- Existing entities/relationships are updated by merging their properties
- The document ID is recorded in the graph's document_ids array
Documents added to a graph will contribute their knowledge to:
- Graph analysis and querying
- Community detection
- Knowledge graph enrichment
Returns:
WrappedBooleanResponse
"""
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}/pull",
version="v3",
)
return WrappedBooleanResponse(**response_dict)
async def remove_document(
self,
collection_id: str | UUID,
document_id: str | UUID,
) -> WrappedBooleanResponse:
"""Removes a document from a graph and removes any associated entities.
This endpoint:
1. Removes the document ID from the graph's document_ids array
2. Optionally deletes the document's copied entities and relationships
The user must have access to both the graph and the document being removed.
Returns:
WrappedBooleanResponse
"""
response_dict = await self.client._make_request(
"DELETE",
f"graphs/{str(collection_id)}/documents/{str(document_id)}",
version="v3",
)
return WrappedBooleanResponse(**response_dict)
async def create_entity(
self,
collection_id: str | UUID,
name: str,
description: str,
category: Optional[str] = None,
metadata: Optional[dict] = None,
) -> WrappedEntityResponse:
"""Creates a new entity in the graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
name (str): The name of the entity to create
description (Optional[str]): The description of the entity
category (Optional[str]): The category of the entity
metadata (Optional[dict]): Additional metadata for the entity
Returns:
WrappedEntityResponse
"""
data: dict[str, Any] = {
"name": name,
"description": description,
}
if category is not None:
data["category"] = category
if metadata is not None:
data["metadata"] = metadata
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}/entities",
json=data,
version="v3",
)
return WrappedEntityResponse(**response_dict)
async def create_relationship(
self,
collection_id: str | UUID,
subject: str,
subject_id: str | UUID,
predicate: str,
object: str,
object_id: str | UUID,
description: str,
weight: Optional[float] = None,
metadata: Optional[dict] = None,
) -> WrappedRelationshipResponse:
"""Creates a new relationship in the graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
subject (str): The subject of the relationship
subject_id (str | UUID): The ID of the subject entity
predicate (str): The predicate/type of the relationship
object (str): The object of the relationship
object_id (str | UUID): The ID of the object entity
description (Optional[str]): Description of the relationship
weight (Optional[float]): Weight/strength of the relationship
metadata (Optional[dict]): Additional metadata for the relationship
Returns:
WrappedRelationshipResponse
"""
data: dict[str, Any] = {
"subject": subject,
"subject_id": str(subject_id),
"predicate": predicate,
"object": object,
"object_id": str(object_id),
"description": description,
}
if weight is not None:
data["weight"] = weight
if metadata is not None:
data["metadata"] = metadata
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}/relationships",
json=data,
version="v3",
)
return WrappedRelationshipResponse(**response_dict)
async def create_community(
self,
collection_id: str | UUID,
name: str,
summary: str,
findings: Optional[_list[str]] = None,
rating: Optional[float] = None,
rating_explanation: Optional[str] = None,
) -> WrappedCommunityResponse:
"""Creates a new community in the graph.
Args:
collection_id (str | UUID): The collection ID corresponding to the graph
name (str): The name of the community
summary (str): A summary description of the community
findings (Optional[list[str]]): List of findings about the community
rating (Optional[float]): Rating between 1 and 10
rating_explanation (Optional[str]): Explanation for the rating
Returns:
WrappedCommunityResponse
"""
data: dict[str, Any] = {
"name": name,
"summary": summary,
}
if findings is not None:
data["findings"] = findings
if rating is not None:
data["rating"] = rating
if rating_explanation is not None:
data["rating_explanation"] = rating_explanation
response_dict = await self.client._make_request(
"POST",
f"graphs/{str(collection_id)}/communities",
json=data,
version="v3",
)
return WrappedCommunityResponse(**response_dict)
|