-
-
Save KyMidd/56c490f0ea2a1bcba219d4511096d3b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def write_audit_log(user_id, session_id, user_query, full_conversation, response, conversation_id, context): | |
| try: | |
| logs_client = boto3.client("logs", region_name="us-east-1") | |
| aws_account_id = context.invoked_function_arn.split(":")[4] | |
| log_entry = { | |
| "timestamp": datetime.now(timezone.utc).isoformat(), | |
| "bot_name": "VeraResearch", | |
| "aws_account_id": aws_account_id, | |
| "user_id": user_id, | |
| "session_id": session_id, | |
| "user_query": user_query, | |
| "full_conversation": full_conversation, | |
| "response": response, | |
| "model_used": model_id, | |
| "conversation_id": conversation_id, | |
| } | |
| log_stream_name = f"VeraResearch/{session_id}" | |
| try: | |
| logs_client.create_log_stream(logGroupName=audit_log_group_name, logStreamName=log_stream_name) | |
| except logs_client.exceptions.ResourceAlreadyExistsException: | |
| pass | |
| logs_client.put_log_events(logGroupName=audit_log_group_name, logStreamName=log_stream_name, | |
| logEvents=[{"timestamp": int(datetime.now(timezone.utc).timestamp() * 1000), "message": json.dumps(log_entry)}]) | |
| except Exception as error: | |
| print(f"Error writing audit log: {error}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment