-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_vectorstore.py
62 lines (43 loc) · 1.6 KB
/
generate_vectorstore.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
import os
from datetime import datetime
import boto3
from requests_aws4auth import AWS4Auth
from opensearchpy import RequestsHttpConnection
from langchain_community.embeddings import BedrockEmbeddings
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import OpenSearchVectorSearch
from dotenv import load_dotenv
load_dotenv()
opensearch_https = os.environ.get("OPENSEARCH_URL")
# embedding_model_name = os.environ.get("HF_EMBEDDING_MODEL")
bedrock_model_id = os.environ.get("BEDROCK_MODEL_ID")
region = "eu-west-3"
service = "aoss"
session = boto3.Session()
credentials = session.get_credentials()
auth_creds = AWS4Auth(
region=region,
service=service,
refreshable_credentials=credentials,
)
embedding_model = BedrockEmbeddings(model_id=bedrock_model_id, region_name=region)
def generate_rolling_vectorstore_name():
"""Generates a name name for the index based on todays day
Name is formatted as index_name_YYYYMMDD
Returns:
rolling_index_name (string): the name of the index with the date appended
"""
name_suffix = os.environ.get("OPENSEARCH_INDEX_SUFFIX")
today = datetime.now().strftime("%Y%m%d")
rolling_index_name = f"{name_suffix}_{today}"
# output the new name
print(f"New index name: {rolling_index_name}")
return rolling_index_name
index_name = generate_rolling_vectorstore_name()
vectorstore = OpenSearchVectorSearch(
index_name=index_name,
opensearch_url=opensearch_https,
http_auth=auth_creds,
embedding_function=embedding_model,
connection_class=RequestsHttpConnection,
)