-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy path_gapic_utils.py
More file actions
775 lines (700 loc) · 29.9 KB
/
_gapic_utils.py
File metadata and controls
775 lines (700 loc) · 29.9 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
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# -*- coding: utf-8 -*-
# Copyright 2024 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.
#
import re
from typing import Any, Dict, Optional, Sequence, Union
from google.cloud.aiplatform_v1.types import api_auth
from google.cloud.aiplatform_v1.types import EncryptionSpec
from google.cloud.aiplatform_v1 import (
RagEmbeddingModelConfig as GapicRagEmbeddingModelConfig,
GoogleDriveSource,
ImportRagFilesConfig,
ImportRagFilesRequest,
RagEngineConfig as GapicRagEngineConfig,
RagFileChunkingConfig,
RagFileParsingConfig,
RagFileTransformationConfig,
RagCorpus as GapicRagCorpus,
RagFile as GapicRagFile,
RagManagedDbConfig as GapicRagManagedDbConfig,
SharePointSources as GapicSharePointSources,
SlackSource as GapicSlackSource,
JiraSource as GapicJiraSource,
RagVectorDbConfig as GapicRagVectorDbConfig,
VertexAiSearchConfig as GapicVertexAiSearchConfig,
)
from google.cloud.aiplatform import initializer
from google.cloud.aiplatform.utils import (
VertexRagDataAsyncClientWithOverride,
VertexRagDataClientWithOverride,
VertexRagClientWithOverride,
)
from vertexai.rag.utils.resources import (
Basic,
JiraSource,
LayoutParserConfig,
LlmParserConfig,
Pinecone,
RagCitedGenerationResponse,
RagCorpus,
RagEmbeddingModelConfig,
RagEngineConfig,
RagFile,
RagManagedDb,
RagManagedDbConfig,
RagVectorDbConfig,
Scaled,
SharePointSources,
SlackChannelsSource,
TransformationConfig,
Unprovisioned,
VertexAiSearchConfig,
VertexVectorSearch,
VertexPredictionEndpoint,
)
_VALID_RESOURCE_NAME_REGEX = "[a-z][a-zA-Z0-9._-]{0,127}"
_VALID_DOCUMENT_AI_PROCESSOR_NAME_REGEX = (
r"projects/[^/]+/locations/[^/]+/processors/[^/]+(?:/processorVersions/[^/]+)?"
)
def create_rag_data_service_client(
api_path_override: Optional[str] = None,
):
return initializer.global_config.create_client(
client_class=VertexRagDataClientWithOverride,
api_path_override=api_path_override,
).select_version("v1")
def create_rag_data_service_async_client(
api_path_override: Optional[str] = None,
):
return initializer.global_config.create_client(
client_class=VertexRagDataAsyncClientWithOverride,
api_path_override=api_path_override,
).select_version("v1")
def create_rag_service_client(
api_path_override: Optional[str] = None,
):
return initializer.global_config.create_client(
client_class=VertexRagClientWithOverride,
api_path_override=api_path_override,
).select_version("v1")
def convert_gapic_to_rag_embedding_model_config(
gapic_embedding_model_config: GapicRagEmbeddingModelConfig,
) -> RagEmbeddingModelConfig:
"""Convert GapicRagEmbeddingModelConfig to RagEmbeddingModelConfig."""
embedding_model_config = RagEmbeddingModelConfig()
path = gapic_embedding_model_config.vertex_prediction_endpoint.endpoint
publisher_model = re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/publishers/google/models/(?P<model_id>.+?)$",
path,
)
endpoint = re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/endpoints/(?P<endpoint>.+?)$",
path,
)
if publisher_model:
embedding_model_config.vertex_prediction_endpoint = VertexPredictionEndpoint(
publisher_model=path
)
if endpoint:
embedding_model_config.vertex_prediction_endpoint = VertexPredictionEndpoint(
endpoint=path,
model=gapic_embedding_model_config.vertex_prediction_endpoint.model,
model_version_id=gapic_embedding_model_config.vertex_prediction_endpoint.model_version_id,
)
return embedding_model_config
def _check_weaviate(gapic_vector_db: GapicRagVectorDbConfig) -> bool:
try:
return gapic_vector_db.__contains__("weaviate")
except AttributeError:
return gapic_vector_db.weaviate.ByteSize() > 0
def _check_rag_managed_db(gapic_vector_db: GapicRagVectorDbConfig) -> bool:
try:
return gapic_vector_db.__contains__("rag_managed_db")
except AttributeError:
return gapic_vector_db.rag_managed_db.ByteSize() > 0
def _check_vertex_feature_store(gapic_vector_db: GapicRagVectorDbConfig) -> bool:
try:
return gapic_vector_db.__contains__("vertex_feature_store")
except AttributeError:
return gapic_vector_db.vertex_feature_store.ByteSize() > 0
def _check_pinecone(gapic_vector_db: GapicRagVectorDbConfig) -> bool:
try:
return gapic_vector_db.__contains__("pinecone")
except AttributeError:
return gapic_vector_db.pinecone.ByteSize() > 0
def _check_vertex_vector_search(gapic_vector_db: GapicRagVectorDbConfig) -> bool:
try:
return gapic_vector_db.__contains__("vertex_vector_search")
except AttributeError:
return gapic_vector_db.vertex_vector_search.ByteSize() > 0
def _check_rag_embedding_model_config(
gapic_vector_db: GapicRagVectorDbConfig,
) -> bool:
try:
return gapic_vector_db.__contains__("rag_embedding_model_config")
except AttributeError:
return gapic_vector_db.rag_embedding_model_config.ByteSize() > 0
def convert_gapic_to_backend_config(
gapic_vector_db: GapicRagVectorDbConfig,
) -> RagVectorDbConfig:
"""Convert Gapic RagVectorDbConfig to VertexVectorSearch, Pinecone, or RagManagedDb."""
if not gapic_vector_db:
return None
vector_config = RagVectorDbConfig()
if _check_pinecone(gapic_vector_db):
vector_config.vector_db = Pinecone(
index_name=gapic_vector_db.pinecone.index_name,
api_key=gapic_vector_db.api_auth.api_key_config.api_key_secret_version,
)
elif _check_vertex_vector_search(gapic_vector_db):
vector_config.vector_db = VertexVectorSearch(
index_endpoint=gapic_vector_db.vertex_vector_search.index_endpoint,
index=gapic_vector_db.vertex_vector_search.index,
)
elif _check_rag_managed_db(gapic_vector_db):
vector_config.vector_db = RagManagedDb()
if _check_rag_embedding_model_config(gapic_vector_db):
vector_config.rag_embedding_model_config = (
convert_gapic_to_rag_embedding_model_config(
gapic_vector_db.rag_embedding_model_config
)
)
return vector_config
def convert_gapic_to_vertex_ai_search_config(
gapic_vertex_ai_search_config: GapicVertexAiSearchConfig,
) -> Optional[VertexAiSearchConfig]:
"""Convert Gapic VertexAiSearchConfig to VertexAiSearchConfig."""
if gapic_vertex_ai_search_config.ByteSize() > 0:
return VertexAiSearchConfig(
serving_config=gapic_vertex_ai_search_config.serving_config,
)
return None
def convert_gapic_to_rag_corpus(gapic_rag_corpus: GapicRagCorpus) -> RagCorpus:
"""Convert GapicRagCorpus to RagCorpus."""
rag_corpus = RagCorpus(
name=gapic_rag_corpus.name,
display_name=gapic_rag_corpus.display_name,
description=gapic_rag_corpus.description,
vertex_ai_search_config=convert_gapic_to_vertex_ai_search_config(
gapic_rag_corpus.vertex_ai_search_config
),
backend_config=convert_gapic_to_backend_config(
gapic_rag_corpus.vector_db_config
),
encryption_spec=gapic_rag_corpus.encryption_spec,
)
return rag_corpus
def convert_gapic_to_rag_corpus_no_embedding_model_config(
gapic_rag_corpus: GapicRagCorpus,
) -> RagCorpus:
"""Convert GapicRagCorpus without embedding model config (for UpdateRagCorpus) to RagCorpus."""
rag_vector_db_config_no_embedding_model_config = gapic_rag_corpus.vector_db_config
rag_vector_db_config_no_embedding_model_config.rag_embedding_model_config = None
rag_corpus = RagCorpus(
name=gapic_rag_corpus.name,
display_name=gapic_rag_corpus.display_name,
description=gapic_rag_corpus.description,
vertex_ai_search_config=convert_gapic_to_vertex_ai_search_config(
gapic_rag_corpus.vertex_ai_search_config
),
backend_config=convert_gapic_to_backend_config(
rag_vector_db_config_no_embedding_model_config
),
encryption_spec=gapic_rag_corpus.encryption_spec,
)
return rag_corpus
def convert_gapic_to_rag_file(gapic_rag_file: GapicRagFile) -> RagFile:
"""Convert GapicRagFile to RagFile."""
rag_file = RagFile(
name=gapic_rag_file.name,
display_name=gapic_rag_file.display_name,
description=gapic_rag_file.description,
)
return rag_file
def convert_json_to_rag_file(upload_rag_file_response: Dict[str, Any]) -> RagFile:
"""Converts a JSON response to a RagFile."""
rag_file = RagFile(
name=upload_rag_file_response.get("ragFile").get("name"),
display_name=upload_rag_file_response.get("ragFile").get("displayName"),
description=upload_rag_file_response.get("ragFile").get("description"),
)
return rag_file
def convert_tuple_to_rag_cited_generation_response(
cited_text: str, final_bibliography: str
) -> RagCitedGenerationResponse:
"""Converts a tuple to a RagCitedGenerationResponse."""
rag_cited_generation_response = RagCitedGenerationResponse(
cited_text=cited_text,
final_bibliography=final_bibliography,
)
return rag_cited_generation_response
def convert_path_to_resource_id(
path: str,
) -> Union[str, GoogleDriveSource.ResourceId]:
"""Converts a path to a Google Cloud storage uri or GoogleDriveSource.ResourceId."""
if path.startswith("gs://"):
# Google Cloud Storage source
return path
elif path.startswith("https://drive.google.com/"):
# Google Drive source
path_list = path.split("/")
if "file" in path_list:
index = path_list.index("file") + 2
resource_id = path_list[index].split("?")[0]
resource_type = GoogleDriveSource.ResourceId.ResourceType.RESOURCE_TYPE_FILE
elif "folders" in path_list:
index = path_list.index("folders") + 1
resource_id = path_list[index].split("?")[0]
resource_type = (
GoogleDriveSource.ResourceId.ResourceType.RESOURCE_TYPE_FOLDER
)
else:
raise ValueError("path %s is not a valid Google Drive url.", path)
return GoogleDriveSource.ResourceId(
resource_id=resource_id,
resource_type=resource_type,
)
else:
raise ValueError(
"path must be a Google Cloud Storage uri or a Google Drive url."
)
def convert_source_for_rag_import(
source: Union[SlackChannelsSource, JiraSource, SharePointSources],
) -> Union[GapicSlackSource, GapicJiraSource]:
"""Converts a SlackChannelsSource or JiraSource to a GapicSlackSource or GapicJiraSource."""
if isinstance(source, SlackChannelsSource):
result_source_channels = []
for channel in source.channels:
api_key = channel.api_key
cid = channel.channel_id
start_time = channel.start_time
end_time = channel.end_time
result_channels = GapicSlackSource.SlackChannels(
channels=[
GapicSlackSource.SlackChannels.SlackChannel(
channel_id=cid,
start_time=start_time,
end_time=end_time,
)
],
api_key_config=api_auth.ApiAuth.ApiKeyConfig(
api_key_secret_version=api_key
),
)
result_source_channels.append(result_channels)
return GapicSlackSource(
channels=result_source_channels,
)
elif isinstance(source, JiraSource):
result_source_queries = []
for query in source.queries:
api_key = query.api_key
custom_queries = query.custom_queries
projects = query.jira_projects
email = query.email
server_uri = query.server_uri
result_query = GapicJiraSource.JiraQueries(
custom_queries=custom_queries,
projects=projects,
email=email,
server_uri=server_uri,
api_key_config=api_auth.ApiAuth.ApiKeyConfig(
api_key_secret_version=api_key
),
)
result_source_queries.append(result_query)
return GapicJiraSource(
jira_queries=result_source_queries,
)
elif isinstance(source, SharePointSources):
result_source_share_point_sources = []
for share_point_source in source.share_point_sources:
sharepoint_folder_path = share_point_source.sharepoint_folder_path
sharepoint_folder_id = share_point_source.sharepoint_folder_id
drive_name = share_point_source.drive_name
drive_id = share_point_source.drive_id
client_id = share_point_source.client_id
client_secret = share_point_source.client_secret
tenant_id = share_point_source.tenant_id
sharepoint_site_name = share_point_source.sharepoint_site_name
result_share_point_source = GapicSharePointSources.SharePointSource(
client_id=client_id,
client_secret=api_auth.ApiAuth.ApiKeyConfig(
api_key_secret_version=client_secret
),
tenant_id=tenant_id,
sharepoint_site_name=sharepoint_site_name,
)
if sharepoint_folder_path is not None and sharepoint_folder_id is not None:
raise ValueError(
"sharepoint_folder_path and sharepoint_folder_id cannot both be set."
)
elif sharepoint_folder_path is not None:
result_share_point_source.sharepoint_folder_path = (
sharepoint_folder_path
)
elif sharepoint_folder_id is not None:
result_share_point_source.sharepoint_folder_id = sharepoint_folder_id
if drive_name is not None and drive_id is not None:
raise ValueError("drive_name and drive_id cannot both be set.")
elif drive_name is not None:
result_share_point_source.drive_name = drive_name
elif drive_id is not None:
result_share_point_source.drive_id = drive_id
else:
raise ValueError("Either drive_name and drive_id must be set.")
result_source_share_point_sources.append(result_share_point_source)
return GapicSharePointSources(
share_point_sources=result_source_share_point_sources,
)
else:
raise TypeError(
"source must be a SlackChannelsSource or JiraSource or SharePointSources."
)
def prepare_import_files_request(
corpus_name: str,
paths: Optional[Sequence[str]] = None,
source: Optional[Union[SlackChannelsSource, JiraSource, SharePointSources]] = None,
transformation_config: Optional[TransformationConfig] = None,
max_embedding_requests_per_min: int = 1000,
import_result_sink: Optional[str] = None,
partial_failures_sink: Optional[str] = None,
layout_parser: Optional[LayoutParserConfig] = None,
llm_parser: Optional[LlmParserConfig] = None,
) -> ImportRagFilesRequest:
if len(corpus_name.split("/")) != 6:
raise ValueError(
"corpus_name must be of the format `projects/{project}/locations/{location}/ragCorpora/{rag_corpus}`"
)
rag_file_parsing_config = RagFileParsingConfig()
if layout_parser is not None:
if (
re.fullmatch(
_VALID_DOCUMENT_AI_PROCESSOR_NAME_REGEX,
layout_parser.processor_name,
)
is None
):
raise ValueError(
"processor_name must be of the format"
" `projects/{project_id}/locations/{location}/processors/{processor_id}`or"
" `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}`,"
f" got {layout_parser.processor_name!r}"
)
rag_file_parsing_config.layout_parser = RagFileParsingConfig.LayoutParser(
processor_name=layout_parser.processor_name,
max_parsing_requests_per_min=layout_parser.max_parsing_requests_per_min,
)
if llm_parser is not None:
rag_file_parsing_config.llm_parser = RagFileParsingConfig.LlmParser(
model_name=llm_parser.model_name
)
if llm_parser.max_parsing_requests_per_min is not None:
rag_file_parsing_config.llm_parser.max_parsing_requests_per_min = (
llm_parser.max_parsing_requests_per_min
)
if llm_parser.custom_parsing_prompt is not None:
rag_file_parsing_config.llm_parser.custom_parsing_prompt = (
llm_parser.custom_parsing_prompt
)
chunk_size = 1024
chunk_overlap = 200
if transformation_config and transformation_config.chunking_config:
chunk_size = transformation_config.chunking_config.chunk_size
chunk_overlap = transformation_config.chunking_config.chunk_overlap
rag_file_transformation_config = RagFileTransformationConfig(
rag_file_chunking_config=RagFileChunkingConfig(
fixed_length_chunking=RagFileChunkingConfig.FixedLengthChunking(
chunk_size=chunk_size,
chunk_overlap=chunk_overlap,
),
),
)
import_rag_files_config = ImportRagFilesConfig(
rag_file_transformation_config=rag_file_transformation_config,
rag_file_parsing_config=rag_file_parsing_config,
max_embedding_requests_per_min=max_embedding_requests_per_min,
)
import_result_sink = import_result_sink or partial_failures_sink
if import_result_sink is not None:
if import_result_sink.startswith("gs://"):
import_rag_files_config.partial_failure_gcs_sink.output_uri_prefix = (
import_result_sink
)
elif import_result_sink.startswith("bq://"):
import_rag_files_config.partial_failure_bigquery_sink.output_uri = (
import_result_sink
)
else:
raise ValueError(
"import_result_sink must be a GCS path or a BigQuery table."
)
if source is not None:
gapic_source = convert_source_for_rag_import(source)
if isinstance(gapic_source, GapicSlackSource):
import_rag_files_config.slack_source = gapic_source
if isinstance(gapic_source, GapicJiraSource):
import_rag_files_config.jira_source = gapic_source
if isinstance(gapic_source, GapicSharePointSources):
import_rag_files_config.share_point_sources = gapic_source
else:
uris = []
resource_ids = []
for p in paths:
output = convert_path_to_resource_id(p)
if isinstance(output, str):
uris.append(p)
else:
resource_ids.append(output)
if uris:
import_rag_files_config.gcs_source.uris = uris
if resource_ids:
google_drive_source = GoogleDriveSource(
resource_ids=resource_ids,
)
import_rag_files_config.google_drive_source = google_drive_source
request = ImportRagFilesRequest(
parent=corpus_name, import_rag_files_config=import_rag_files_config
)
return request
def get_corpus_name(
name: str,
) -> str:
if name:
client = create_rag_data_service_client()
if client.parse_rag_corpus_path(name):
return name
elif re.match("^{}$".format(_VALID_RESOURCE_NAME_REGEX), name):
return client.rag_corpus_path(
project=initializer.global_config.project,
location=initializer.global_config.location,
rag_corpus=name,
)
else:
raise ValueError(
"name must be of the format `projects/{project}/locations/{location}/ragCorpora/{rag_corpus}` or `{rag_corpus}`"
)
return name
def get_file_name(
name: str,
corpus_name: str,
) -> str:
client = create_rag_data_service_client()
if client.parse_rag_file_path(name):
return name
elif re.match("^{}$".format(_VALID_RESOURCE_NAME_REGEX), name):
if not corpus_name:
raise ValueError(
"corpus_name must be provided if name is a `{rag_file}`, not a "
"full resource name (`projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file}`). "
)
return client.rag_file_path(
project=initializer.global_config.project,
location=initializer.global_config.location,
rag_corpus=get_corpus_name(corpus_name),
rag_file=name,
)
else:
raise ValueError(
"name must be of the format `projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file}` or `{rag_file}`"
)
def set_embedding_model_config(
embedding_model_config: RagEmbeddingModelConfig,
rag_corpus: GapicRagCorpus,
) -> None:
if embedding_model_config.vertex_prediction_endpoint is None:
return
if (
embedding_model_config.vertex_prediction_endpoint.publisher_model
and embedding_model_config.vertex_prediction_endpoint.endpoint
):
raise ValueError("publisher_model and endpoint cannot be set at the same time.")
if (
not embedding_model_config.vertex_prediction_endpoint.publisher_model
and not embedding_model_config.vertex_prediction_endpoint.endpoint
):
raise ValueError("At least one of publisher_model and endpoint must be set.")
parent = initializer.global_config.common_location_path(project=None, location=None)
if embedding_model_config.vertex_prediction_endpoint.publisher_model:
publisher_model = (
embedding_model_config.vertex_prediction_endpoint.publisher_model
)
full_resource_name = re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/publishers/google/models/(?P<model_id>.+?)$",
publisher_model,
)
resource_name = re.match(
r"^publishers/google/models/(?P<model_id>.+?)$",
publisher_model,
)
if full_resource_name:
rag_corpus.vector_db_config.rag_embedding_model_config.vertex_prediction_endpoint.endpoint = (
publisher_model
)
elif resource_name:
rag_corpus.vector_db_config.rag_embedding_model_config.vertex_prediction_endpoint.endpoint = (
parent + "/" + publisher_model
)
else:
raise ValueError(
"publisher_model must be of the format `projects/{project}/locations/{location}/publishers/google/models/{model_id}` or `publishers/google/models/{model_id}`"
)
if embedding_model_config.vertex_prediction_endpoint.endpoint:
endpoint = embedding_model_config.vertex_prediction_endpoint.endpoint
full_resource_name = re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/endpoints/(?P<endpoint>.+?)$",
endpoint,
)
resource_name = re.match(
r"^endpoints/(?P<endpoint>.+?)$",
endpoint,
)
if full_resource_name:
rag_corpus.vector_db_config.rag_embedding_model_config.vertex_prediction_endpoint.endpoint = (
endpoint
)
elif resource_name:
rag_corpus.vector_db_config.rag_embedding_model_config.vertex_prediction_endpoint.endpoint = (
parent + "/" + endpoint
)
else:
raise ValueError(
"endpoint must be of the format `projects/{project}/locations/{location}/endpoints/{endpoint}` or `endpoints/{endpoint}`"
)
def set_backend_config(
backend_config: Optional[
Union[
RagVectorDbConfig,
None,
]
],
rag_corpus: GapicRagCorpus,
) -> None:
"""Sets the vector db configuration for the rag corpus."""
if backend_config is None:
return
if backend_config.vector_db is not None:
vector_config = backend_config.vector_db
if vector_config is None or isinstance(vector_config, RagManagedDb):
rag_corpus.vector_db_config.rag_managed_db.CopyFrom(
GapicRagVectorDbConfig.RagManagedDb()
)
elif isinstance(vector_config, VertexVectorSearch):
index_endpoint = vector_config.index_endpoint
index = vector_config.index
rag_corpus.vector_db_config.vertex_vector_search.index_endpoint = (
index_endpoint
)
rag_corpus.vector_db_config.vertex_vector_search.index = index
elif isinstance(vector_config, Pinecone):
index_name = vector_config.index_name
api_key = vector_config.api_key
rag_corpus.vector_db_config.pinecone.index_name = index_name
rag_corpus.vector_db_config.api_auth.api_key_config.api_key_secret_version = (
api_key
)
else:
raise TypeError(
"backend_config must be a VertexFeatureStore,"
"RagManagedDb, or Pinecone."
)
if backend_config.rag_embedding_model_config:
set_embedding_model_config(
backend_config.rag_embedding_model_config, rag_corpus
)
def set_encryption_spec(
encryption_spec: EncryptionSpec,
rag_corpus: GapicRagCorpus,
) -> None:
"""Sets the encryption spec for the rag corpus."""
# Raises value error if encryption_spec.kms_key_name is None or empty,
if encryption_spec.kms_key_name is None or not encryption_spec.kms_key_name:
raise ValueError("kms_key_name must be set if encryption_spec is set.")
# Raises value error if encryption_spec.kms_key_name is not a valid KMS key name.
if not re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/keyRings/(?P<key_ring>.+?)/cryptoKeys/(?P<crypto_key>.+?)$",
encryption_spec.kms_key_name,
):
raise ValueError(
"kms_key_name must be of the format "
"`projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}`"
)
rag_corpus.encryption_spec = encryption_spec
def set_vertex_ai_search_config(
vertex_ai_search_config: VertexAiSearchConfig,
rag_corpus: GapicRagCorpus,
) -> None:
if not vertex_ai_search_config.serving_config:
raise ValueError("serving_config must be set.")
engine_resource_name = re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/collections/(?P<collection>.+?)/engines/(?P<engine>.+?)/servingConfigs/(?P<serving_config>.+?)$",
vertex_ai_search_config.serving_config,
)
data_store_resource_name = re.match(
r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)/collections/(?P<collection>.+?)/dataStores/(?P<data_store>.+?)/servingConfigs/(?P<serving_config>.+?)$",
vertex_ai_search_config.serving_config,
)
if engine_resource_name or data_store_resource_name:
rag_corpus.vertex_ai_search_config = GapicVertexAiSearchConfig(
serving_config=vertex_ai_search_config.serving_config,
)
else:
raise ValueError(
"serving_config must be of the format `projects/{project}/locations/{location}/collections/{collection}/engines/{engine}/servingConfigs/{serving_config}` or `projects/{project}/locations/{location}/collections/{collection}/dataStores/{data_store}/servingConfigs/{serving_config}`"
)
def convert_gapic_to_rag_engine_config(
response: GapicRagEngineConfig,
) -> RagEngineConfig:
"""Converts a GapicRagEngineConfig to a RagEngineConfig."""
rag_managed_db_config = RagManagedDbConfig()
# If future fields are added with similar names, beware that __contains__
# may match them.
if response.rag_managed_db_config.__contains__("basic"):
rag_managed_db_config.tier = Basic()
elif response.rag_managed_db_config.__contains__("unprovisioned"):
rag_managed_db_config.tier = Unprovisioned()
elif response.rag_managed_db_config.__contains__("scaled"):
rag_managed_db_config.tier = Scaled()
else:
raise ValueError("At least one of rag_managed_db_config must be set.")
return RagEngineConfig(
name=response.name,
rag_managed_db_config=rag_managed_db_config,
)
def convert_rag_engine_config_to_gapic(
rag_engine_config: RagEngineConfig,
) -> GapicRagEngineConfig:
"""Converts a RagEngineConfig to a GapicRagEngineConfig."""
rag_managed_db_config = GapicRagManagedDbConfig()
if (
rag_engine_config.rag_managed_db_config is None
or rag_engine_config.rag_managed_db_config.tier is None
):
rag_managed_db_config = GapicRagManagedDbConfig(
basic=GapicRagManagedDbConfig.Basic()
)
else:
if isinstance(rag_engine_config.rag_managed_db_config.tier, Basic):
rag_managed_db_config.basic = GapicRagManagedDbConfig.Basic()
elif isinstance(rag_engine_config.rag_managed_db_config.tier, Unprovisioned):
rag_managed_db_config.unprovisioned = (
GapicRagManagedDbConfig.Unprovisioned()
)
elif isinstance(rag_engine_config.rag_managed_db_config.tier, Scaled):
rag_managed_db_config.scaled = GapicRagManagedDbConfig.Scaled()
return GapicRagEngineConfig(
name=rag_engine_config.name,
rag_managed_db_config=rag_managed_db_config,
)