-
Notifications
You must be signed in to change notification settings - Fork 460
Expand file tree
/
Copy pathrag_retrieval.py
More file actions
535 lines (471 loc) · 19.5 KB
/
Copy pathrag_retrieval.py
File metadata and controls
535 lines (471 loc) · 19.5 KB
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
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Retrieval query to get relevant contexts."""
import re
from typing import List, Optional
from google.cloud import aiplatform_v1beta1
from google.cloud.aiplatform import initializer
from agentplatform.preview.rag.utils import _gapic_utils
from agentplatform.preview.rag.utils import resources
from google.protobuf import any_pb2
def retrieval_query(
text: str,
rag_resources: Optional[List[resources.RagResource]] = None,
rag_retrieval_config: Optional[resources.RagRetrievalConfig] = None,
) -> aiplatform_v1beta1.RetrieveContextsResponse:
"""Retrieve top k relevant docs/chunks.
Example usage:
```
import agentplatform
agentplatform.init(project="my-project")
# Using RagRetrievalConfig.
config = agentplatform.preview.rag.RagRetrievalConfig(
top_k=2,
filter=agentplatform.preview.rag.Filter(
vector_distance_threshold=0.5
),
hybrid_search=agentplatform.preview.rag.rag_retrieval_config.hybrid_search(
alpha=0.5
),
ranking=vertex.preview.rag.Ranking(
llm_ranker=agentplatform.preview.rag.LlmRanker(
model_name="gemini-1.5-flash-002"
)
)
)
results = agentplatform.preview.rag.retrieval_query(
text="Why is the sky blue?",
rag_resources=[agentplatform.preview.rag.RagResource(
rag_corpus="projects/my-project/locations/us-central1/ragCorpora/rag-corpus-1",
rag_file_ids=["rag-file-1", "rag-file-2", ...],
)],
rag_retrieval_config=config,
)
```
Args:
text: The query in text format to get relevant contexts.
rag_resources: A list of RagResource. It can be used to specify corpus
only or ragfiles. Currently only support one corpus or multiple files
from one corpus. In the future we may open up multiple corpora support.
rag_retrieval_config: Optional. The config containing the retrieval
parameters, including top_k, vector_distance_threshold, and alpha.
Returns:
RetrieveContextsResonse.
"""
parent = initializer.global_config.common_location_path()
client = _gapic_utils.create_rag_service_client()
if rag_resources:
if len(rag_resources) > 1:
raise ValueError("Currently only support 1 RagResource.")
name = rag_resources[0].rag_corpus
else:
raise ValueError("rag_resources must be specified.")
data_client = _gapic_utils.create_rag_data_service_client()
if data_client.parse_rag_corpus_path(name):
rag_corpus_name = name
elif re.match(
"^{}$".format(
_gapic_utils._VALID_RESOURCE_NAME_REGEX # pylint: disable=protected-access
),
name,
):
rag_corpus_name = parent + "/ragCorpora/" + name
else:
raise ValueError(
f"Invalid RagCorpus name: {name}. Proper format should be:"
" projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}"
)
gapic_rag_resource = (
aiplatform_v1beta1.RetrieveContextsRequest.VertexRagStore.RagResource(
rag_corpus=rag_corpus_name,
rag_file_ids=rag_resources[0].rag_file_ids,
)
)
vertex_rag_store = aiplatform_v1beta1.RetrieveContextsRequest.VertexRagStore(
rag_resources=[gapic_rag_resource],
)
if not rag_retrieval_config:
api_retrival_config = aiplatform_v1beta1.RagRetrievalConfig()
else:
api_retrival_config = aiplatform_v1beta1.RagRetrievalConfig()
if rag_retrieval_config.top_k:
api_retrival_config.top_k = rag_retrieval_config.top_k
if (
rag_retrieval_config.hybrid_search
and rag_retrieval_config.hybrid_search.alpha
):
api_retrival_config.hybrid_search.alpha = (
rag_retrieval_config.hybrid_search.alpha
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_distance_threshold
and rag_retrieval_config.filter.vector_similarity_threshold
):
raise ValueError(
"Only one of vector_distance_threshold or"
" vector_similarity_threshold can be specified at a time"
" in rag_retrieval_config."
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_distance_threshold
):
api_retrival_config.filter.vector_distance_threshold = (
rag_retrieval_config.filter.vector_distance_threshold
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_similarity_threshold
):
api_retrival_config.filter.vector_similarity_threshold = (
rag_retrieval_config.filter.vector_similarity_threshold
)
if rag_retrieval_config.filter and rag_retrieval_config.filter.metadata_filter:
api_retrival_config.filter.metadata_filter = (
rag_retrieval_config.filter.metadata_filter
)
if (
rag_retrieval_config.ranking
and rag_retrieval_config.ranking.rank_service
and rag_retrieval_config.ranking.llm_ranker
):
raise ValueError("Only one of rank_service and llm_ranker can be set.")
if rag_retrieval_config.ranking and rag_retrieval_config.ranking.rank_service:
api_retrival_config.ranking.rank_service.model_name = (
rag_retrieval_config.ranking.rank_service.model_name
)
elif rag_retrieval_config.ranking and rag_retrieval_config.ranking.llm_ranker:
api_retrival_config.ranking.llm_ranker.model_name = (
rag_retrieval_config.ranking.llm_ranker.model_name
)
query = aiplatform_v1beta1.RagQuery(
text=text,
rag_retrieval_config=api_retrival_config,
)
request = aiplatform_v1beta1.RetrieveContextsRequest(
vertex_rag_store=vertex_rag_store,
parent=parent,
query=query,
)
try:
response = client.retrieve_contexts(request=request)
except Exception as e:
raise RuntimeError("Failed in retrieving contexts due to: ", e) from e
return response
async def async_retrieve_contexts(
text: str,
rag_resources: Optional[List[resources.RagResource]] = None,
rag_retrieval_config: Optional[resources.RagRetrievalConfig] = None,
timeout: int = 600,
) -> aiplatform_v1beta1.RetrieveContextsResponse:
"""Retrieve top k relevant docs/chunks asynchronously.
Example usage:
```
import agentplatform
agentplatform.init(project="my-project")
config = agentplatform.preview.rag.RagRetrievalConfig(
top_k=2,
)
results = await agentplatform.preview.rag.async_retrieve_contexts(
text="Why is the sky blue?",
rag_resources=[agentplatform.preview.rag.RagResource(
rag_corpus="projects/my-project/locations/us-central1/ragCorpora/rag-corpus-1",
rag_file_ids=["rag-file-1", "rag-file-2", ...],
)],
rag_retrieval_config=config,
)
```
Args:
text: Required. The query in text format to get relevant contexts.
rag_resources: Optional. A list of RagResource. It can be used to specify
corpus only or ragfiles. Currently only support one corpus or multiple
files from one corpus. In the future we may open up multiple corpora
support.
rag_retrieval_config: Optional. The config containing the retrieval
parameters, including top_k, vector_distance_threshold, and alpha.
timeout: Optional. The timeout for the request in seconds. Default is 600.
Returns:
RetrieveContextsResponse.
"""
parent = initializer.global_config.common_location_path()
client = _gapic_utils.create_rag_service_async_client()
if not rag_resources:
raise ValueError("rag_resources must be specified.")
data_client = _gapic_utils.create_rag_data_service_client()
gapic_rag_resources = []
if rag_resources:
for rag_resource in rag_resources:
name = rag_resource.rag_corpus
if data_client.parse_rag_corpus_path(name):
rag_corpus_name = name
elif re.match(
"^{}$".format(
_gapic_utils._VALID_RESOURCE_NAME_REGEX # pylint: disable=protected-access
),
name,
):
rag_corpus_name = parent + "/ragCorpora/" + name
else:
raise ValueError(
f"Invalid RagCorpus name: {name}. Proper format should be:"
" projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}"
)
gapic_rag_resources.append(
aiplatform_v1beta1.VertexRagStore.RagResource(
rag_corpus=rag_corpus_name,
rag_file_ids=rag_resource.rag_file_ids,
)
)
vertex_rag_store = aiplatform_v1beta1.VertexRagStore(
rag_resources=gapic_rag_resources,
)
if not rag_retrieval_config:
api_retrival_config = aiplatform_v1beta1.RagRetrievalConfig()
else:
api_retrival_config = aiplatform_v1beta1.RagRetrievalConfig()
if rag_retrieval_config.top_k:
api_retrival_config.top_k = rag_retrieval_config.top_k
if (
rag_retrieval_config.hybrid_search
and rag_retrieval_config.hybrid_search.alpha
):
api_retrival_config.hybrid_search.alpha = (
rag_retrieval_config.hybrid_search.alpha
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_distance_threshold
and rag_retrieval_config.filter.vector_similarity_threshold
):
raise ValueError(
"Only one of vector_distance_threshold or"
" vector_similarity_threshold can be specified at a time"
" in rag_retrieval_config."
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_distance_threshold
):
api_retrival_config.filter.vector_distance_threshold = (
rag_retrieval_config.filter.vector_distance_threshold
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_similarity_threshold
):
api_retrival_config.filter.vector_similarity_threshold = (
rag_retrieval_config.filter.vector_similarity_threshold
)
if (
rag_retrieval_config.ranking
and rag_retrieval_config.ranking.rank_service
and rag_retrieval_config.ranking.llm_ranker
):
raise ValueError("Only one of rank_service and llm_ranker can be set.")
if rag_retrieval_config.ranking and rag_retrieval_config.ranking.rank_service:
api_retrival_config.ranking.rank_service.model_name = (
rag_retrieval_config.ranking.rank_service.model_name
)
elif rag_retrieval_config.ranking and rag_retrieval_config.ranking.llm_ranker:
api_retrival_config.ranking.llm_ranker.model_name = (
rag_retrieval_config.ranking.llm_ranker.model_name
)
if rag_retrieval_config.filter and rag_retrieval_config.filter.metadata_filter:
api_retrival_config.filter.metadata_filter = (
rag_retrieval_config.filter.metadata_filter
)
query = aiplatform_v1beta1.RagQuery(
text=text,
rag_retrieval_config=api_retrival_config,
)
vertex_rag_store.rag_retrieval_config = api_retrival_config
tool = aiplatform_v1beta1.Tool(
retrieval=aiplatform_v1beta1.Retrieval(
vertex_rag_store=vertex_rag_store,
)
)
request = aiplatform_v1beta1.AsyncRetrieveContextsRequest(
parent=parent,
query=query,
tools=[tool],
)
try:
response_lro = await client.async_retrieve_contexts(
request=request, timeout=timeout
)
try:
response = await response_lro.result(timeout=timeout)
except Exception as e:
if response_lro.done():
raw_op = response_lro.operation
if raw_op.WhichOneof("result") == "response":
any_response = raw_op.response
inner_any = any_pb2.Any()
if any_response.Unpack(inner_any):
inner_any.type_url = "type.googleapis.com/google.cloud.aiplatform.v1beta1.RagContexts"
rag_contexts = aiplatform_v1beta1.RagContexts()
if inner_any.Unpack(rag_contexts._pb):
return aiplatform_v1beta1.AsyncRetrieveContextsResponse(
contexts=rag_contexts
)
raise e
except Exception as e:
raise RuntimeError(
"Failed in retrieving contexts asynchronously due to: ", e
) from e
return response
def ask_contexts(
text: str,
rag_resources: Optional[List[resources.RagResource]] = None,
rag_retrieval_config: Optional[resources.RagRetrievalConfig] = None,
timeout: int = 600,
) -> aiplatform_v1beta1.AskContextsResponse:
"""Ask questions on top k relevant docs/chunks.
Example usage:
```
import agentplatform
agentplatform.init(project="my-project")
config = agentplatform.preview.rag.RagRetrievalConfig(
top_k=2,
)
results = agentplatform.preview.rag.ask_contexts(
text="Why is the sky blue?",
rag_resources=[agentplatform.preview.rag.RagResource(
rag_corpus="projects/my-project/locations/us-central1/ragCorpora/rag-corpus-1",
rag_file_ids=["rag-file-1", "rag-file-2", ...],
)],
rag_retrieval_config=config,
)
```
Args:
text: Required. The query in text format to get relevant contexts.
rag_resources: Optional. A list of RagResource. It can be used to specify
corpus only or ragfiles. Currently only support one corpus or multiple
files from one corpus. In the future we may open up multiple corpora
support.
rag_retrieval_config: Optional. The config containing the retrieval
parameters, including top_k, vector_distance_threshold, and alpha.
timeout: Optional. The timeout for the request in seconds. Default is 600.
Returns:
AskContextsResponse.
"""
parent = initializer.global_config.common_location_path()
client = _gapic_utils.create_rag_service_client()
if not rag_resources:
raise ValueError("rag_resources must be specified.")
data_client = _gapic_utils.create_rag_data_service_client()
gapic_rag_resources = []
if rag_resources:
for rag_resource in rag_resources:
name = rag_resource.rag_corpus
if data_client.parse_rag_corpus_path(name):
rag_corpus_name = name
elif re.match(
"^{}$".format(
_gapic_utils._VALID_RESOURCE_NAME_REGEX # pylint: disable=protected-access
),
name,
):
rag_corpus_name = parent + "/ragCorpora/" + name
else:
raise ValueError(
f"Invalid RagCorpus name: {name}. Proper format should be:"
" projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}"
)
gapic_rag_resources.append(
aiplatform_v1beta1.VertexRagStore.RagResource(
rag_corpus=rag_corpus_name,
rag_file_ids=rag_resource.rag_file_ids,
)
)
vertex_rag_store = aiplatform_v1beta1.VertexRagStore(
rag_resources=gapic_rag_resources,
)
if not rag_retrieval_config:
api_retrival_config = aiplatform_v1beta1.RagRetrievalConfig()
else:
api_retrival_config = aiplatform_v1beta1.RagRetrievalConfig()
if rag_retrieval_config.top_k:
api_retrival_config.top_k = rag_retrieval_config.top_k
if (
rag_retrieval_config.hybrid_search
and rag_retrieval_config.hybrid_search.alpha
):
api_retrival_config.hybrid_search.alpha = (
rag_retrieval_config.hybrid_search.alpha
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_distance_threshold
and rag_retrieval_config.filter.vector_similarity_threshold
):
raise ValueError(
"Only one of vector_distance_threshold or"
" vector_similarity_threshold can be specified at a time"
" in rag_retrieval_config."
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_distance_threshold
):
api_retrival_config.filter.vector_distance_threshold = (
rag_retrieval_config.filter.vector_distance_threshold
)
if (
rag_retrieval_config.filter
and rag_retrieval_config.filter.vector_similarity_threshold
):
api_retrival_config.filter.vector_similarity_threshold = (
rag_retrieval_config.filter.vector_similarity_threshold
)
if (
rag_retrieval_config.ranking
and rag_retrieval_config.ranking.rank_service
and rag_retrieval_config.ranking.llm_ranker
):
raise ValueError("Only one of rank_service and llm_ranker can be set.")
if rag_retrieval_config.ranking and rag_retrieval_config.ranking.rank_service:
api_retrival_config.ranking.rank_service.model_name = (
rag_retrieval_config.ranking.rank_service.model_name
)
elif rag_retrieval_config.ranking and rag_retrieval_config.ranking.llm_ranker:
api_retrival_config.ranking.llm_ranker.model_name = (
rag_retrieval_config.ranking.llm_ranker.model_name
)
if rag_retrieval_config.filter and rag_retrieval_config.filter.metadata_filter:
api_retrival_config.filter.metadata_filter = (
rag_retrieval_config.filter.metadata_filter
)
query = aiplatform_v1beta1.RagQuery(
text=text,
rag_retrieval_config=api_retrival_config,
)
vertex_rag_store.rag_retrieval_config = api_retrival_config
tool = aiplatform_v1beta1.Tool(
retrieval=aiplatform_v1beta1.Retrieval(
vertex_rag_store=vertex_rag_store,
)
)
request = aiplatform_v1beta1.AskContextsRequest(
parent=parent,
query=query,
tools=[tool],
)
try:
response = client.ask_contexts(request=request, timeout=timeout)
except Exception as e:
raise RuntimeError("Failed in asking contexts due to: ", e) from e
return response