Skip to content

Commit

Permalink
Merge pull request #1335 from kom-senapati/feature/mongodb-connection…
Browse files Browse the repository at this point in the history
…-refactor

refactor: use MongoDB singleton for connection management
  • Loading branch information
dartpain authored Oct 31, 2024
2 parents 45e14bc + 82189b0 commit ed12c2d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions application/api/answer/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from flask import Blueprint, current_app, make_response, request, Response
from flask_restx import fields, Namespace, Resource

from pymongo import MongoClient

from application.core.mongo_db import MongoDB
from application.core.settings import settings
from application.error import bad_request
from application.extensions import api
Expand All @@ -22,7 +22,7 @@

logger = logging.getLogger(__name__)

mongo = MongoClient(settings.MONGO_URI)
mongo = MongoDB.get_client()
db = mongo["docsgpt"]
conversations_collection = db["conversations"]
sources_collection = db["sources"]
Expand Down
4 changes: 2 additions & 2 deletions application/api/internal/routes.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
import datetime
from flask import Blueprint, request, send_from_directory
from pymongo import MongoClient
from werkzeug.utils import secure_filename
from bson.objectid import ObjectId

from application.core.mongo_db import MongoDB
from application.core.settings import settings

mongo = MongoClient(settings.MONGO_URI)
mongo = MongoDB.get_client()
db = mongo["docsgpt"]
conversations_collection = db["conversations"]
sources_collection = db["sources"]
Expand Down
4 changes: 2 additions & 2 deletions application/api/user/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
from bson.objectid import ObjectId
from flask import Blueprint, jsonify, make_response, request
from flask_restx import inputs, fields, Namespace, Resource
from pymongo import MongoClient
from werkzeug.utils import secure_filename

from application.api.user.tasks import ingest, ingest_remote

from application.core.mongo_db import MongoDB
from application.core.settings import settings
from application.extensions import api
from application.utils import check_required_fields
from application.vectorstore.vector_creator import VectorCreator
from application.tts.google_tts import GoogleTTS

mongo = MongoClient(settings.MONGO_URI)
mongo = MongoDB.get_client()
db = mongo["docsgpt"]
conversations_collection = db["conversations"]
sources_collection = db["sources"]
Expand Down
24 changes: 24 additions & 0 deletions application/core/mongo_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from application.core.settings import settings
from pymongo import MongoClient


class MongoDB:
_client = None

@classmethod
def get_client(cls):
"""
Get the MongoDB client instance, creating it if necessary.
"""
if cls._client is None:
cls._client = MongoClient(settings.MONGO_URI)
return cls._client

@classmethod
def close_client(cls):
"""
Close the MongoDB client connection.
"""
if cls._client is not None:
cls._client.close()
cls._client = None
5 changes: 2 additions & 3 deletions application/usage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import sys
from pymongo import MongoClient
from datetime import datetime
from application.core.settings import settings
from application.core.mongo_db import MongoDB
from application.utils import num_tokens_from_string

mongo = MongoClient(settings.MONGO_URI)
mongo = MongoDB.get_client()
db = mongo["docsgpt"]
usage_collection = db["token_usage"]

Expand Down
4 changes: 2 additions & 2 deletions application/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import requests
from bson.objectid import ObjectId
from pymongo import MongoClient

from application.core.mongo_db import MongoDB
from application.core.settings import settings
from application.parser.file.bulk import SimpleDirectoryReader
from application.parser.open_ai_func import call_openai_api
Expand All @@ -18,7 +18,7 @@
from application.parser.token_func import group_split
from application.utils import count_tokens_docs

mongo = MongoClient(settings.MONGO_URI)
mongo = MongoDB.get_client()
db = mongo["docsgpt"]
sources_collection = db["sources"]

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"cancel": "Cancel",
"help": "Help",
"emailUs": "Email us",
"documentation": "documentation",
"documentation": "Documentation",
"demo": [
{
"header": "Learn about DocsGPT",
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/locale/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"cancel": "Cancelar",
"help": "Asistencia",
"emailUs": "Envíanos un correo",
"documentation": "documentación",
"documentation": "Documentación",
"demo": [
{
"header": "Aprende sobre DocsGPT",
Expand Down

0 comments on commit ed12c2d

Please sign in to comment.