forked from GoogleCloudPlatform/dfcx-scrapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_stores.py
174 lines (148 loc) · 6.05 KB
/
data_stores.py
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
"""Data Stores Resource functions for Vertex Search and Conversation."""
# Copyright 2023 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
#
# https://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 logging
from typing import Dict, List
from google.longrunning.operations_pb2 import Operation
from google.cloud.discoveryengine import (
DataStore,
DataStoreServiceClient,
ListDataStoresRequest,
GetDataStoreRequest,
CreateDataStoreRequest,
DeleteDataStoreRequest
)
from dfcx_scrapi.core import scrapi_base
# logging config
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
class DataStores(scrapi_base.ScrapiBase):
"""Core Class for Data Store Resource functions."""
def __init__(
self,
project_id: str,
creds_path: str = None,
creds_dict: Dict = None,
creds=None,
scope=False,
):
super().__init__(
creds_path=creds_path,
creds_dict=creds_dict,
creds=creds,
scope=scope,
)
self.project_id = project_id
@staticmethod
def _get_content_config(content_type: str) -> DataStore.ContentConfig:
"""Build the Content Config used for the Data Store."""
cc_map = {
"unstructured": 1,
"structured": 2,
"website": 3
}
cc = DataStore.ContentConfig(cc_map[content_type])
return cc
def get_data_stores_map(self, location: str = "global",
reverse: bool = False) -> Dict[str,str]:
"""Get a user friendly mapping for Data Store Names and IDs."""
if reverse:
data_store_dict = {
datastore.display_name: datastore.name
for datastore in self.list_data_stores(location=location)
}
else:
data_store_dict = {
datastore.name: datastore.display_name
for datastore in self.list_data_stores(location=location)
}
return data_store_dict
def list_data_stores(
self, location: str = "global") -> List[DataStore]:
"""List all data stores for a given project and location."""
parent = self._build_data_store_parent(location)
client_options = self._client_options_discovery_engine(
f"projects/{self.project_id}/locations/{location}"
)
client = DataStoreServiceClient(
credentials=self.creds, client_options=client_options
)
request = ListDataStoresRequest(parent=parent)
page_result = client.list_data_stores(request=request)
datastores = []
for datastore in page_result:
datastores.append(datastore)
return datastores
def get_data_store(self, data_store_id: str) -> DataStore:
"""Get a single Data Store by specified ID."""
client_options = self._client_options_discovery_engine(data_store_id)
client = DataStoreServiceClient(
credentials=self.creds, client_options=client_options
)
request = GetDataStoreRequest(
name=data_store_id
)
response = client.get_data_store(request)
return response
def create_datastore(
self, display_name: str, solution_type: str = "chat",
datastore_type: str = "website", advanced_site_search: bool = True,
location: str = "global") -> Operation:
"""Create a new Data Store type specified by the user.
Args:
display_name: The data store display name. This field must be a UTF-8
encoded string with a length limit of 128 characters. Otherwise, an
INVALID_ARGUMENT error is returned.
solution_type: ONEOF [`chat`, `search`, `recommendation`]
datastore_type: ONEOF [`website`, `structured`, `unstructured`]
advanced_site_search: A boolean flag indicating whether user wants to
directly create an advanced data store for site search. If the data
store is not configured as site search (GENERIC vertical and
PUBLIC_WEBSITE content_config), this flag will be ignored.
location: the GCP region to create the Data Store in
"""
parent = self._build_data_store_parent(location)
client_options = self._client_options_discovery_engine(
f"projects/{self.project_id}/locations/{location}"
)
client = DataStoreServiceClient(
credentials=self.creds, client_options=client_options
)
data_store = DataStore()
data_store.display_name = display_name
data_store.industry_vertical = 1
data_store.solution_types = [self._get_solution_type(solution_type)]
data_store.content_config = self._get_content_config(datastore_type)
request = CreateDataStoreRequest(
parent=parent,
data_store=data_store,
data_store_id=data_store.display_name,
create_advanced_site_search=advanced_site_search
)
operation = client.create_data_store(request=request)
return operation.operation
def delete_datastore(self, data_store_id: str):
"""Delete the specified Data Store by ID."""
client_options = self._client_options_discovery_engine(data_store_id)
client = DataStoreServiceClient(
credentials=self.creds, client_options=client_options
)
request = DeleteDataStoreRequest(
name=data_store_id
)
operation = client.delete_data_store(request=request)
return operation.operation