From 053978b5d4dda8d88619125ef9222d842d6cd7da Mon Sep 17 00:00:00 2001 From: "Michael B. Klein" Date: Mon, 11 Nov 2024 20:44:21 +0000 Subject: [PATCH] Make sure to strip the scheme from the index URL --- chat/src/setup.py | 13 +++++++++++-- node/src/handlers/middleware.js | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/chat/src/setup.py b/chat/src/setup.py index a2a7a3a4..60cd6f91 100644 --- a/chat/src/setup.py +++ b/chat/src/setup.py @@ -2,6 +2,7 @@ from handlers.opensearch_neural_search import OpenSearchNeuralSearch from opensearchpy import OpenSearch, RequestsHttpConnection from requests_aws4auth import AWS4Auth +from urllib.parse import urlparse import os import boto3 @@ -16,10 +17,18 @@ def openai_chat_client(**kwargs): **kwargs, ) +def opensearch_endpoint(): + endpoint = os.getenv("OPENSEARCH_ENDPOINT") + parsed = urlparse(endpoint) + if parsed.netloc != '': + return parsed.netloc + else: + return endpoint + def opensearch_client(region_name=os.getenv("AWS_REGION")): session = boto3.Session(region_name=region_name) awsauth = AWS4Auth(region=region_name, service="es", refreshable_credentials=session.get_credentials()) - endpoint = os.getenv("OPENSEARCH_ENDPOINT") + endpoint = opensearch_endpoint() return OpenSearch( hosts=[{'host': endpoint, 'port': 443}], @@ -35,7 +44,7 @@ def opensearch_vector_store(region_name=os.getenv("AWS_REGION")): docsearch = OpenSearchNeuralSearch( index=prefix("dc-v2-work"), model_id=os.getenv("OPENSEARCH_MODEL_ID"), - endpoint=os.getenv("OPENSEARCH_ENDPOINT"), + endpoint=opensearch_endpoint(), connection_class=RequestsHttpConnection, http_auth=awsauth, text_field= "id" diff --git a/node/src/handlers/middleware.js b/node/src/handlers/middleware.js index fec735e8..f183e790 100644 --- a/node/src/handlers/middleware.js +++ b/node/src/handlers/middleware.js @@ -59,7 +59,12 @@ const _initializeEnvironment = async function () { secrets[Name.split("/").reverse()[0]] = JSON.parse(SecretString); } - putenv("OPENSEARCH_ENDPOINT", secrets.index?.endpoint); + let { endpoint } = secrets.index; + if (URL.canParse(endpoint)) { + endpoint = new URL(endpoint).hostname; + } + + putenv("OPENSEARCH_ENDPOINT", endpoint); putenv("OPENSEARCH_MODEL_ID", secrets.index?.embedding_model); putenv("NUSSO_API_KEY", secrets.nusso?.api_key); putenv("NUSSO_BASE_URL", secrets.nusso?.base_url);