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
|
import asyncio
import functools
import json
import os
import threading
import time
import uuid
from contextlib import ExitStack
from typing import Any, AsyncGenerator, Generator, Optional, Union
import fire
import httpx
import nest_asyncio
import requests
from .requests import (
R2RAnalyticsRequest,
R2RDeleteRequest,
R2RDocumentChunksRequest,
R2RDocumentsOverviewRequest,
R2RIngestFilesRequest,
R2RLogsRequest,
R2RPrintRelationshipsRequest,
R2RRAGRequest,
R2RSearchRequest,
R2RUpdateFilesRequest,
R2RUpdatePromptRequest,
R2RUsersOverviewRequest,
)
nest_asyncio.apply()
class R2RHTTPError(Exception):
def __init__(self, status_code, error_type, message):
self.status_code = status_code
self.error_type = error_type
self.message = message
super().__init__(f"[{status_code}] {error_type}: {message}")
def handle_request_error(response):
if response.status_code >= 400:
try:
error_content = response.json()
if isinstance(error_content, dict) and "detail" in error_content:
detail = error_content["detail"]
if isinstance(detail, dict):
message = detail.get("message", str(response.text))
error_type = detail.get("error_type", "UnknownError")
else:
message = str(detail)
error_type = "HTTPException"
else:
message = str(error_content)
error_type = "UnknownError"
except json.JSONDecodeError:
message = response.text
error_type = "UnknownError"
raise R2RHTTPError(
status_code=response.status_code,
error_type=error_type,
message=message,
)
def monitor_request(func):
@functools.wraps(func)
def wrapper(*args, monitor=False, **kwargs):
if not monitor:
return func(*args, **kwargs)
result = None
exception = None
def run_func():
nonlocal result, exception
try:
result = func(*args, **kwargs)
except Exception as e:
exception = e
thread = threading.Thread(target=run_func)
thread.start()
dots = [".", "..", "..."]
i = 0
while thread.is_alive():
print(f"\rRequesting{dots[i % 3]}", end="", flush=True)
i += 1
time.sleep(0.5)
thread.join()
print("\r", end="", flush=True)
if exception:
raise exception
return result
return wrapper
class R2RClient:
def __init__(self, base_url: str, prefix: str = "/v1"):
self.base_url = base_url
self.prefix = prefix
def _make_request(self, method, endpoint, **kwargs):
url = f"{self.base_url}{self.prefix}/{endpoint}"
response = requests.request(method, url, **kwargs)
handle_request_error(response)
return response.json()
def health(self) -> dict:
return self._make_request("GET", "health")
def update_prompt(
self,
name: str = "default_system",
template: Optional[str] = None,
input_types: Optional[dict] = None,
) -> dict:
request = R2RUpdatePromptRequest(
name=name, template=template, input_types=input_types
)
return self._make_request(
"POST", "update_prompt", json=json.loads(request.json())
)
@monitor_request
def ingest_files(
self,
file_paths: list[str],
metadatas: Optional[list[dict]] = None,
document_ids: Optional[list[Union[uuid.UUID, str]]] = None,
versions: Optional[list[str]] = None,
) -> dict:
all_file_paths = []
for path in file_paths:
if os.path.isdir(path):
for root, _, files in os.walk(path):
all_file_paths.extend(
os.path.join(root, file) for file in files
)
else:
all_file_paths.append(path)
files_to_upload = [
(
"files",
(
os.path.basename(file),
open(file, "rb"),
"application/octet-stream",
),
)
for file in all_file_paths
]
request = R2RIngestFilesRequest(
metadatas=metadatas,
document_ids=(
[str(ele) for ele in document_ids] if document_ids else None
),
versions=versions,
)
try:
return self._make_request(
"POST",
"ingest_files",
data={
k: json.dumps(v)
for k, v in json.loads(request.json()).items()
},
files=files_to_upload,
)
finally:
for _, file_tuple in files_to_upload:
file_tuple[1].close()
@monitor_request
def update_files(
self,
file_paths: list[str],
document_ids: list[str],
metadatas: Optional[list[dict]] = None,
) -> dict:
request = R2RUpdateFilesRequest(
metadatas=metadatas,
document_ids=document_ids,
)
with ExitStack() as stack:
return self._make_request(
"POST",
"update_files",
data={
k: json.dumps(v)
for k, v in json.loads(request.json()).items()
},
files=[
(
"files",
(
path.split("/")[-1],
stack.enter_context(open(path, "rb")),
"application/octet-stream",
),
)
for path in file_paths
],
)
def search(
self,
query: str,
use_vector_search: bool = True,
search_filters: Optional[dict[str, Any]] = {},
search_limit: int = 10,
do_hybrid_search: bool = False,
use_kg_search: bool = False,
kg_agent_generation_config: Optional[dict] = None,
) -> dict:
request = R2RSearchRequest(
query=query,
vector_search_settings={
"use_vector_search": use_vector_search,
"search_filters": search_filters or {},
"search_limit": search_limit,
"do_hybrid_search": do_hybrid_search,
},
kg_search_settings={
"use_kg_search": use_kg_search,
"agent_generation_config": kg_agent_generation_config,
},
)
return self._make_request(
"POST", "search", json=json.loads(request.json())
)
def rag(
self,
query: str,
use_vector_search: bool = True,
search_filters: Optional[dict[str, Any]] = {},
search_limit: int = 10,
do_hybrid_search: bool = False,
use_kg_search: bool = False,
kg_agent_generation_config: Optional[dict] = None,
rag_generation_config: Optional[dict] = None,
) -> dict:
request = R2RRAGRequest(
query=query,
vector_search_settings={
"use_vector_search": use_vector_search,
"search_filters": search_filters or {},
"search_limit": search_limit,
"do_hybrid_search": do_hybrid_search,
},
kg_search_settings={
"use_kg_search": use_kg_search,
"agent_generation_config": kg_agent_generation_config,
},
rag_generation_config=rag_generation_config,
)
if rag_generation_config and rag_generation_config.get(
"stream", False
):
return self._stream_rag_sync(request)
else:
return self._make_request(
"POST", "rag", json=json.loads(request.json())
)
async def _stream_rag(
self, rag_request: R2RRAGRequest
) -> AsyncGenerator[str, None]:
url = f"{self.base_url}{self.prefix}/rag"
async with httpx.AsyncClient() as client:
async with client.stream(
"POST", url, json=json.loads(rag_request.json())
) as response:
handle_request_error(response)
async for chunk in response.aiter_text():
yield chunk
def _stream_rag_sync(
self, rag_request: R2RRAGRequest
) -> Generator[str, None, None]:
async def run_async_generator():
async for chunk in self._stream_rag(rag_request):
yield chunk
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
async_gen = run_async_generator()
try:
while True:
chunk = loop.run_until_complete(async_gen.__anext__())
yield chunk
except StopAsyncIteration:
pass
finally:
loop.close()
def delete(
self, keys: list[str], values: list[Union[bool, int, str]]
) -> dict:
request = R2RDeleteRequest(keys=keys, values=values)
return self._make_request(
"DELETE", "delete", json=json.loads(request.json())
)
def logs(self, log_type_filter: Optional[str] = None) -> dict:
request = R2RLogsRequest(log_type_filter=log_type_filter)
return self._make_request(
"GET", "logs", json=json.loads(request.json())
)
def app_settings(self) -> dict:
return self._make_request("GET", "app_settings")
def analytics(self, filter_criteria: dict, analysis_types: dict) -> dict:
request = R2RAnalyticsRequest(
filter_criteria=filter_criteria, analysis_types=analysis_types
)
return self._make_request(
"GET", "analytics", json=json.loads(request.json())
)
def users_overview(
self, user_ids: Optional[list[uuid.UUID]] = None
) -> dict:
request = R2RUsersOverviewRequest(user_ids=user_ids)
return self._make_request(
"GET", "users_overview", json=json.loads(request.json())
)
def documents_overview(
self,
document_ids: Optional[list[str]] = None,
user_ids: Optional[list[str]] = None,
) -> dict:
request = R2RDocumentsOverviewRequest(
document_ids=(
[uuid.UUID(did) for did in document_ids]
if document_ids
else None
),
user_ids=(
[uuid.UUID(uid) for uid in user_ids] if user_ids else None
),
)
return self._make_request(
"GET", "documents_overview", json=json.loads(request.json())
)
def document_chunks(self, document_id: str) -> dict:
request = R2RDocumentChunksRequest(document_id=document_id)
return self._make_request(
"GET", "document_chunks", json=json.loads(request.json())
)
def inspect_knowledge_graph(self, limit: int = 100) -> str:
request = R2RPrintRelationshipsRequest(limit=limit)
return self._make_request(
"POST", "inspect_knowledge_graph", json=json.loads(request.json())
)
if __name__ == "__main__":
client = R2RClient(base_url="http://localhost:8000")
fire.Fire(client)
|