|
| 1 | +from pymongo import MongoClient |
| 2 | +from pymongo.database import Database |
| 3 | +from dotenv import load_dotenv |
| 4 | +import os |
| 5 | + |
| 6 | +load_dotenv() |
| 7 | + |
| 8 | +# HARDCODED collection names |
| 9 | +feedback_collection_name = "feedback" |
| 10 | +context_collection_name = "context" |
| 11 | + |
| 12 | +def get_mongo_db_client() -> Database: |
| 13 | + """ |
| 14 | + Get the MongoDB client. |
| 15 | +
|
| 16 | + Returns: |
| 17 | + - DatabaseClient: The Database client. |
| 18 | +
|
| 19 | + Note: |
| 20 | + MongoDB doesn't create a collection or a database until it gets content, so no need to check if the data already exists or not. |
| 21 | + """ |
| 22 | + |
| 23 | + uri = os.getenv("MONGO_DB_URI") |
| 24 | + client = MongoClient(uri) |
| 25 | + # this is the database that is returned by the client |
| 26 | + return client["feedback_db"] |
| 27 | + |
| 28 | +def submit_feedback(): |
| 29 | + """ |
| 30 | + Submit feedback Record to the MongoDB database. |
| 31 | +
|
| 32 | + Args: |
| 33 | + - question (str): The question for which feedback is being submitted. |
| 34 | + - answer (str): The generated answer to the question. |
| 35 | + - sources (list[str]): Source data used for the answer. |
| 36 | + - context (list[str]): Additional context from the RAG. |
| 37 | + - issue (str): Details about the issue. |
| 38 | + - version (str): Version information. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + - None |
| 42 | + """ |
| 43 | + |
| 44 | + feedback_db_client = get_mongo_client() |
| 45 | + |
| 46 | + try: |
| 47 | + if not check_collection_exists(feedback_collection_name,feedback_db_client,): |
| 48 | + create_collection(feedback_collection_name,feedback_db_client, validator={ |
| 49 | + '$jsonSchema': { |
| 50 | + 'bsonType': 'object', |
| 51 | + 'required': ['question', 'answer', 'sources', 'context_ids', 'issue', 'version', 'timestamp'], |
| 52 | + 'properties': { |
| 53 | + 'question': { |
| 54 | + 'bsonType': 'string', |
| 55 | + 'description': 'must be a string and is required' |
| 56 | + }, |
| 57 | + 'answer': { |
| 58 | + 'bsonType': 'string', |
| 59 | + 'description': 'must be a string and is required' |
| 60 | + }, |
| 61 | + 'sources': { |
| 62 | + 'bsonType': 'array', |
| 63 | + 'items': { |
| 64 | + 'bsonType': 'objectId' |
| 65 | + }, |
| 66 | + 'description': 'must be an array of ObjectIds referencing the sources and is required' |
| 67 | + }, |
| 68 | + 'context': { |
| 69 | + 'bsonType': 'array', |
| 70 | + 'items': { |
| 71 | + 'bsonType': 'string' |
| 72 | + }, |
| 73 | + 'description': 'must be an array of strings and is required' |
| 74 | + }, |
| 75 | + 'issue': { |
| 76 | + 'bsonType': 'string', |
| 77 | + 'description': 'must be a string and is required' |
| 78 | + }, |
| 79 | + 'version': { |
| 80 | + 'bsonType': 'string', |
| 81 | + 'description': 'must be a string and is required' |
| 82 | + }, |
| 83 | + 'timestamp': { |
| 84 | + 'bsonType': 'date', |
| 85 | + 'description': 'must be a date and is required' |
| 86 | + }, |
| 87 | + 'status': { |
| 88 | + 'enum': ['new', 'processing', 'resolved'], |
| 89 | + 'description': 'can only be one of the enum values' |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | + if not check_collection_exists(context_collection_name,feedback_db_client): |
| 95 | + create_collection(context_collection_name,feedback_db_client,{ |
| 96 | + 'bsonType': 'object', |
| 97 | + 'required': ['source', 'timestamp'], |
| 98 | + 'properties': { |
| 99 | + 'source': { |
| 100 | + 'bsonType': 'string', |
| 101 | + 'description': 'must be a string and is required' |
| 102 | + }, |
| 103 | + 'metadata': { |
| 104 | + 'bsonType': 'object', |
| 105 | + 'description': 'additional metadata for the context' |
| 106 | + }, |
| 107 | + 'timestamp': { |
| 108 | + 'bsonType': 'date', |
| 109 | + 'description': 'must be a date and is required' |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + except Exception as e: |
| 116 | + print(f"Failed to submit feedback: {e}") |
| 117 | + return None |
| 118 | + |
| 119 | +def check_collection_exists(collection_name:str,client_database:Database)->bool: |
| 120 | + """ |
| 121 | + Check if the collection exists in the database. |
| 122 | +
|
| 123 | + Args: |
| 124 | + - collection_name (str): The name of the collection to check. |
| 125 | + - client_database (Database): The database to check. |
| 126 | +
|
| 127 | + Returns: |
| 128 | + - None |
| 129 | + """ |
| 130 | + return collection_name in client_database.list_collection_names() |
| 131 | + |
| 132 | +def create_collection(collection_name:str,client_database:Database,validator:object)->None: |
| 133 | + """ |
| 134 | + Create a collection in the database. |
| 135 | +
|
| 136 | + Args: |
| 137 | + - collection_name (str): The name of the collection to create. |
| 138 | + - client_database (Database): The database to create the collection in. |
| 139 | +
|
| 140 | + Returns: |
| 141 | + - None |
| 142 | + """ |
| 143 | + try: |
| 144 | + client_database.create_collection(collection_name,validator=validator) |
| 145 | + print("Collection created successfully") |
| 146 | + except Exception as e: |
| 147 | + print(f"Failed to create collection: {e}") |
| 148 | + return None |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + submit_feedback() |
0 commit comments