Skip to content

Commit

Permalink
Merge pull request #261 from aws-samples/v1.6.0_spy
Browse files Browse the repository at this point in the history
add index init
  • Loading branch information
supinyu authored Aug 19, 2024
2 parents 6603f4d + 606cc39 commit d3dcc45
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
3 changes: 3 additions & 0 deletions application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
async def http_authenticate(request: Request, call_next):
# print('---HTTP REQUEST---', vars(request), request.headers)

if request.url.path == "/":
return await call_next(request)

if request.method == "OPTIONS":
response = Response(status_code=status.HTTP_200_OK)
response.headers["Access-Control-Allow-Origin"] = "*"
Expand Down
2 changes: 1 addition & 1 deletion application/nlq/data_access/dynamo_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def to_dict(self):
def get_secrets_manager_name(self):
"""Generate a random Secrets Manager name"""
random_str = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
return f"{self.conn_name}-{random_str}"
return f"GenBI-{self.conn_name}-{random_str}"


class ConnectConfigDao:
Expand Down
4 changes: 2 additions & 2 deletions application/nlq/data_access/opensearch_query_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def __init__(self):
opensearch_info["port"],
opensearch_info["username"], opensearch_info["password"],
opensearch_info["region"])
if not self.exists():
self.create_index()
# if not self.exists():
# self.create_index()

def exists(self):
is_exist = check_opensearch_index(self.opensearch_client, QUERY_LOG_TABLE_NAME)
Expand Down
5 changes: 5 additions & 0 deletions application/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def authenticate(access_token, id_token, refresh_token):
if refresh_token and refresh_token.startswith("Bearer "):
refresh_token = refresh_token[len("Bearer "):]

if access_token is None or id_token is None or refresh_token is None:
response = {}
response['X-Status-Code'] = status.HTTP_401_UNAUTHORIZED
return response

if len(access_token.strip()) < 2 or len(id_token.strip()) < 2 or len(refresh_token.strip()) < 2:
response = {}
response['X-Status-Code'] = status.HTTP_401_UNAUTHORIZED
Expand Down
2 changes: 2 additions & 0 deletions application/utils/env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ def get_bedrock_parameter():
'embedding_dimension': EMBEDDING_DIMENSION
}

query_log_name = os.getenv("QUERY_LOG_INDEX_NAME", "genbi_query_logging")

bedrock_ak_sk_info = get_bedrock_parameter()
47 changes: 46 additions & 1 deletion application/utils/opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from opensearchpy.helpers import bulk
import logging
from utils.llm import create_vector_embedding_with_bedrock, create_vector_embedding_with_sagemaker
from utils.env_var import opensearch_info, SAGEMAKER_ENDPOINT_EMBEDDING, AOS_INDEX_NER
from utils.env_var import opensearch_info, SAGEMAKER_ENDPOINT_EMBEDDING, AOS_INDEX_NER, query_log_name

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -319,6 +319,47 @@ def update_index_mapping(opensearch_client, index_name, dimension):
return bool(response['acknowledged'])


def create_log_index(opensearch_client):
mapping = {
"mappings": {
"properties": {
"log_id": {
"type": "keyword"
},
"profile_name": {
"type": "keyword"
},
"user_id": {
"type": "keyword"
},
"session_id": {
"type": "keyword"
},
"sql": {
"type": "text"
},
"query": {
"type": "text"
},
"intent": {
"type": "keyword"
},
"log_info": {
"type": "text"
},
"time_str": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"log_type": {
"type": "keyword"
}
}
}
}
opensearch_client.indices.create(index="genbi_query_logging", body=mapping)


def upload_results_to_opensearch(region_name, domain, opensearch_user, opensearch_password, index_name, query, sql,
host='', port=443):

Expand Down Expand Up @@ -381,6 +422,10 @@ def opensearch_index_init():
logger.info(f"check index flag: {check_flag}")
if not check_flag:
update_index_mapping(opensearch_client, index_name, dimension)
exists = check_opensearch_index(opensearch_client, query_log_name)
if not exists:
logger.info("genbi_query_logging not exit")
create_log_index(opensearch_client)
return index_create_success
except Exception as e:
logger.error("create index error")
Expand Down
6 changes: 4 additions & 2 deletions source/resources/lib/ecs/ecs-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ export class ECSStack extends cdk.Stack {
// Add secrets manager access policy
const opensearchHostUrlSecretAccessPolicy = new iam.PolicyStatement({
actions: [
"secretsmanager:GetSecretValue"
"secretsmanager:GetSecretValue",
"secretsmanager:CreateSecret",
],
resources: [
`arn:${this.partition}:secretsmanager:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:secret:opensearch-host-url*`,
`arn:${this.partition}:secretsmanager:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:secret:opensearch-master-user*`
`arn:${this.partition}:secretsmanager:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:secret:opensearch-master-user*`,
`arn:${this.partition}:secretsmanager:${cdk.Aws.REGION}:${cdk.Aws.ACCOUNT_ID}:secret:GenBI-*`
]
});
taskRole.addToPolicy(opensearchHostUrlSecretAccessPolicy);
Expand Down

0 comments on commit d3dcc45

Please sign in to comment.