-
Notifications
You must be signed in to change notification settings - Fork 13
Design and add a MongoDB schema (Issue 75) #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a72dfdc
(chore) added .DS_Store to .gitignore
Kannav02 920c52f
changes made for my refernce
Kannav02 543b724
(chore) added requirements.txt file for common folder
Kannav02 4e44476
(feat): added the .env.example file for mongoDB
Kannav02 eb8fa44
(feat) added the basic mongoDB functions to run
Kannav02 a26d97e
(feat) design for mongoDB schemas made and added
Kannav02 18f0110
added a comment
Kannav02 afd2259
(chore) removed debug statements and added credentials.json to gitignore
Kannav02 e4ccf8d
(fix): fixed inconsistenicies in the code and removed debug statements
Kannav02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| from fastapi import FastAPI | ||
|
|
||
| from .routers import graphs, healthcheck | ||
|
|
||
| app = FastAPI() | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| MONGO_DB_URI = YOUR_MONGO_DB_URI |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| from pymongo import MongoClient | ||
| from pymongo.database import Database | ||
| from dotenv import load_dotenv | ||
| import os | ||
|
|
||
| load_dotenv() | ||
|
|
||
| # HARDCODED collection names | ||
| feedback_collection_name = "feedback" | ||
| context_collection_name = "context" | ||
|
|
||
| def get_mongo_db_client() -> Database: | ||
| """ | ||
| Get the MongoDB client. | ||
|
|
||
| Returns: | ||
| - DatabaseClient: The Database client. | ||
|
|
||
| Note: | ||
| 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. | ||
| """ | ||
|
|
||
| uri = os.getenv("MONGO_DB_URI") | ||
| client = MongoClient(uri) | ||
| # this is the database that is returned by the client | ||
| return client["feedback_db"] | ||
|
|
||
| def submit_feedback(): | ||
| """ | ||
| Submit feedback Record to the MongoDB database. | ||
|
|
||
| Args: | ||
| - question (str): The question for which feedback is being submitted. | ||
| - answer (str): The generated answer to the question. | ||
| - sources (list[str]): Source data used for the answer. | ||
| - context (list[str]): Additional context from the RAG. | ||
| - issue (str): Details about the issue. | ||
| - version (str): Version information. | ||
|
|
||
| Returns: | ||
| - None | ||
| """ | ||
|
|
||
| feedback_db_client = get_mongo_client() | ||
|
|
||
| try: | ||
| if not check_collection_exists(feedback_collection_name,feedback_db_client,): | ||
| create_collection(feedback_collection_name,feedback_db_client, validator={ | ||
| '$jsonSchema': { | ||
| 'bsonType': 'object', | ||
| 'required': ['question', 'answer', 'sources', 'context_ids', 'issue', 'version', 'timestamp'], | ||
| 'properties': { | ||
| 'question': { | ||
| 'bsonType': 'string', | ||
| 'description': 'must be a string and is required' | ||
| }, | ||
| 'answer': { | ||
| 'bsonType': 'string', | ||
| 'description': 'must be a string and is required' | ||
| }, | ||
| 'sources': { | ||
| 'bsonType': 'array', | ||
| 'items': { | ||
| 'bsonType': 'objectId' | ||
| }, | ||
| 'description': 'must be an array of ObjectIds referencing the sources and is required' | ||
| }, | ||
| 'context': { | ||
| 'bsonType': 'array', | ||
| 'items': { | ||
| 'bsonType': 'string' | ||
| }, | ||
| 'description': 'must be an array of strings and is required' | ||
| }, | ||
| 'issue': { | ||
| 'bsonType': 'string', | ||
| 'description': 'must be a string and is required' | ||
| }, | ||
| 'version': { | ||
| 'bsonType': 'string', | ||
| 'description': 'must be a string and is required' | ||
| }, | ||
| 'timestamp': { | ||
| 'bsonType': 'date', | ||
| 'description': 'must be a date and is required' | ||
| }, | ||
| 'status': { | ||
| 'enum': ['new', 'processing', 'resolved'], | ||
| 'description': 'can only be one of the enum values' | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| if not check_collection_exists(context_collection_name,feedback_db_client): | ||
| create_collection(context_collection_name,feedback_db_client,{ | ||
| 'bsonType': 'object', | ||
| 'required': ['source', 'timestamp'], | ||
| 'properties': { | ||
| 'source': { | ||
| 'bsonType': 'string', | ||
| 'description': 'must be a string and is required' | ||
| }, | ||
| 'metadata': { | ||
| 'bsonType': 'object', | ||
| 'description': 'additional metadata for the context' | ||
| }, | ||
| 'timestamp': { | ||
| 'bsonType': 'date', | ||
| 'description': 'must be a date and is required' | ||
| } | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| except Exception as e: | ||
| print(f"Failed to submit feedback: {e}") | ||
| return None | ||
|
|
||
| def check_collection_exists(collection_name:str,client_database:Database)->bool: | ||
| """ | ||
| Check if the collection exists in the database. | ||
|
|
||
| Args: | ||
| - collection_name (str): The name of the collection to check. | ||
| - client_database (Database): The database to check. | ||
|
|
||
| Returns: | ||
| - None | ||
| """ | ||
| return collection_name in client_database.list_collection_names() | ||
|
|
||
| def create_collection(collection_name:str,client_database:Database,validator:object)->None: | ||
| """ | ||
| Create a collection in the database. | ||
|
|
||
| Args: | ||
| - collection_name (str): The name of the collection to create. | ||
| - client_database (Database): The database to create the collection in. | ||
|
|
||
| Returns: | ||
| - None | ||
| """ | ||
| try: | ||
| client_database.create_collection(collection_name,validator=validator) | ||
| print("Collection created successfully") | ||
| except Exception as e: | ||
| print(f"Failed to create collection: {e}") | ||
| return None | ||
|
|
||
| if __name__ == "__main__": | ||
| submit_feedback() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pymongo==4.6.2 | ||
| python-dotenv==1.0.1 |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.