-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_data_store.py
67 lines (56 loc) · 2.3 KB
/
create_data_store.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
from google.cloud.discoveryengine_v1beta import IndustryVertical
from google.cloud.discoveryengine_v1beta import SolutionType
from google.cloud.discoveryengine_v1beta import DataStore
from src.config.logging import logger
from src.config.setup import config
from typing import Dict, Any
import requests
def create_data_store(data_store_display_name: str, data_store_id: str) -> Dict[str, Any]:
"""
Create a data store in GCP Vertex AI Search with a specified display name and ID.
Args:
data_store_display_name (str): The display name for the new data store.
data_store_id (str): The unique identifier for the new data store.
Returns:
Dict[str, Any]: The response from the GCP API.
Raises:
Exception: If the data store ID or display name already exists.
"""
url = f"https://discoveryengine.googleapis.com/v1alpha/projects/{config.PROJECT_ID}/locations/global/collections/default_collection/dataStores?dataStoreId={data_store_id}"
headers = {
'Authorization': f'Bearer {config.ACCESS_TOKEN}',
'Content-Type': 'application/json',
'X-Goog-User-Project': config.PROJECT_ID
}
data = {
'displayName': data_store_display_name,
'industryVertical': IndustryVertical.GENERIC,
'solutionTypes': SolutionType.SOLUTION_TYPE_SEARCH,
'contentConfig': DataStore.ContentConfig.CONTENT_REQUIRED,
'documentProcessingConfig': {
'defaultParsingConfig': {
'layoutParsingConfig': {}
}
}
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
if response.status_code == 409:
logger.error("Data store ID or display name already exists. Choose a different name or ID.")
else:
logger.error(f"HTTP Error occurred: {e}")
raise
except requests.exceptions.RequestException as e:
logger.error(f"Request failed: {e}")
raise
return response.json()
if __name__ == '__main__':
display_name = 'quarterly-reports'
store_id = 'quarterly-reports'
try:
response = create_data_store(display_name, store_id)
print(response)
except Exception as e:
logger.error(f"An error occurred: {e}")