10
10
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
11
11
# and limitations under the License.
12
12
#
13
+ from pathlib import Path
14
+ from aiohttp import ClientError
13
15
from langchain_community .vectorstores import OpenSearchVectorSearch
14
- from opensearchpy import RequestsHttpConnection
16
+ # from opensearchpy import RequestsHttpConnection
15
17
from llms import get_embeddings_llm
16
18
import requests
17
19
import os
@@ -57,7 +59,7 @@ class JobStatus(Enum):
57
59
base64 .b64encode ("Sorry, it seems an issue happened on my end, and I'm not able to answer your question. Please contact an administrator to understand why !" .encode ('utf-8' ))
58
60
)
59
61
ERROR_SEMANTIC_SEARCH = (
60
- 'Exception during simialirty search, Please verify model for the selected modality' ,
62
+ 'Exception during similarity search, Please verify model for the selected modality' ,
61
63
base64 .b64encode ("Sorry, it seems an issue happened on my end, and I'm not able to answer your question. Please contact an administrator to understand why !" .encode ('utf-8' ))
62
64
)
63
65
@@ -97,7 +99,8 @@ def load_vector_db_opensearch(region: str,
97
99
opensearch_domain_endpoint : str ,
98
100
opensearch_index : str ,
99
101
secret_id : str ,
100
- model_id : str ) -> OpenSearchVectorSearch :
102
+ model_id : str ,
103
+ modality : str ) -> OpenSearchVectorSearch :
101
104
print (f"load_vector_db_opensearch, region={ region } , "
102
105
f"opensearch_domain_endpoint={ opensearch_domain_endpoint } , opensearch_index={ opensearch_index } " )
103
106
@@ -114,16 +117,17 @@ def load_vector_db_opensearch(region: str,
114
117
opensearch_api_name ,
115
118
session_token = credentials .token ,
116
119
)
117
- embedding_function = get_embeddings_llm (model_id )
120
+ embedding_function = get_embeddings_llm (model_id , modality )
118
121
119
122
opensearch_url = opensearch_domain_endpoint if opensearch_domain_endpoint .startswith ("https://" ) else f"https://{ opensearch_domain_endpoint } "
120
- vector_db = OpenSearchVectorSearch (index_name = opensearch_index ,
121
- embedding_function = embedding_function ,
122
- opensearch_url = opensearch_url ,
123
- http_auth = http_auth ,
124
- use_ssl = True ,
125
- verify_certs = True ,
126
- connection_class = RequestsHttpConnection )
123
+ # vector_db = OpenSearchVectorSearch(index_name=opensearch_index,
124
+ # embedding_function=embedding_function,
125
+ # opensearch_url=opensearch_url,
126
+ # http_auth=http_auth,
127
+ # use_ssl = True,
128
+ # verify_certs = True,
129
+ # connection_class = RequestsHttpConnection)
130
+ vector_db = ""
127
131
print (f"returning handle to OpenSearchVectorSearch, vector_db={ vector_db } " )
128
132
return vector_db
129
133
@@ -155,7 +159,8 @@ def send_job_status(variables):
155
159
156
160
print (request )
157
161
158
- GRAPHQL_URL = os .environ ['GRAPHQL_URL' ]
162
+ #GRAPHQL_URL = os.environ['GRAPHQL_URL']
163
+ GRAPHQL_URL = "https://j2uzmlvujbhbzoduvpctgkpu2e.appsync-api.us-east-1.amazonaws.com/graphql"
159
164
HEADERS = {
160
165
"Content-Type" : "application/json" ,
161
166
}
@@ -180,4 +185,21 @@ def get_presigned_url(bucket,key) -> str:
180
185
return url
181
186
except Exception as exception :
182
187
print (f"Reason: { exception } " )
183
- return ""
188
+ return ""
189
+
190
+ def download_file (bucket ,key )-> str :
191
+ try :
192
+ file_path = "/tmp/" + os .path .basename (key )
193
+ s3 .download_file (bucket , key ,file_path )
194
+ print (f"file downloaded { file_path } " )
195
+ return file_path
196
+ except ClientError as client_err :
197
+ print (f"Couldn\' t download file { client_err .response ['Error' ]['Message' ]} " )
198
+
199
+ except Exception as exp :
200
+ print (f"Couldn\' t download file : { exp } " )
201
+
202
+ def encode_image_to_base64 (image_file_path ,image_file ) -> str :
203
+ with open (image_file_path , "rb" ) as image_file :
204
+ b64_image = base64 .b64encode (image_file .read ()).decode ('utf8' )
205
+ return b64_image
0 commit comments