Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix)
Browse files Browse the repository at this point in the history
- schema corrected for the database
- parameters included in the main submit_feedback function
- insertion corrected to utilise the correct datetime.now() function

Signed-off-by: Kannav02 <[email protected]>
Kannav02 committed Nov 26, 2024

Verified

This commit was signed with the committer’s verified signature.
Kannav02 Kannav Sethi
1 parent 7a8b2a2 commit c403a59
Showing 1 changed file with 55 additions and 21 deletions.
76 changes: 55 additions & 21 deletions common/mongoClient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pymongo import MongoClient
from pymongo.database import Database
from dotenv import load_dotenv
from datetime import datetime
import os

load_dotenv()
@@ -25,7 +26,13 @@ def get_mongo_db_client() -> Database:
# this is the database that is returned by the client
return client["feedback_db"]

def submit_feedback():
def submit_feedback(
question: str,
answer: str,
sources: list[str],
context: list[str],
issue: str,
version: str,):
"""
Submit feedback Record to the MongoDB database.
@@ -41,14 +48,14 @@ def submit_feedback():
- None
"""

feedback_db_client = get_mongo_client()
feedback_db_client = get_mongo_db_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'],
'required': ['question', 'answer', 'sources', 'context', 'issue', 'version', 'timestamp'],
'properties': {
'question': {
'bsonType': 'string',
@@ -92,26 +99,53 @@ def submit_feedback():
}
})
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'
}
create_collection(context_collection_name, feedback_db_client, {
'$jsonSchema': {
'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'
}
}
)

}
})
# inserting the records

sources_ids = []
for source in sources:
print(source)
sources_ids.append(feedback_db_client[context_collection_name].insert_one({
'source': str(source),
'metadata': {
'url': str(source)
},
'timestamp': datetime.now()
}).inserted_id)

feedback_db_client[feedback_collection_name].insert_one({
'question': question,
'answer': answer,
'sources': sources_ids,
'context': context,
'issue': issue,
'version': version,
'timestamp': datetime.now(),
'status': 'new'
})


print("Feedback submitted successfully")
return True
except Exception as e:
print(f"Failed to submit feedback: {e}")
return None

0 comments on commit c403a59

Please sign in to comment.