diff --git a/src/app.py b/src/app.py index ae6d67e..0611d04 100644 --- a/src/app.py +++ b/src/app.py @@ -8,8 +8,14 @@ from qdrant_client.http import models from qdrant_client.models import PointStruct from werkzeug.exceptions import BadRequest from qdrant_services.qdrant import Qdrant +from config import LOGGING_LEVEL, LOGGING_FORMAT +logging.basicConfig( + level=LOGGING_LEVEL, + format=LOGGING_FORMAT, +) + logger = logging.getLogger(__name__) # load_dotenv() @@ -72,6 +78,74 @@ def add(): return Response(status=202) + + +@app.post("/delete") +def delete(): + + if request.content_type == 'application/json': + try: + data = request.get_json(force=True) + if data is None: + raise BadRequest + except BadRequest: + return jsonify({"error": "Invalid JSON"}), 400 + else: + + data = request.form.to_dict() + + id = data.get("id", None) + filter_ = data.get("filter", None) + wait = data.get("wait", True) + + if id is None and filter_ is None: + return jsonify({"error": "id or filter is required"}), 400 + if id is not None and filter_ is not None: + return jsonify({"error": "id and filter cannot be used together"}), 400 + + try: + if id: + result = Qdrant.get_client().delete( + points_selector=models.PointIdsList( + points=[id], + ), + wait=wait, + ) + + if filter_: + filter_ = json.loads(filter_) + filter_ = filter_[0] + if filter_['type'] != 'match': + return jsonify({"error": "unsupported filter type"}), 400 + + f = models.Filter( + must=[ + models.FieldCondition( + key=filter_['key'], match=models.MatchValue(value=filter_['value']) + ), + ], + ) + + filter_ = f + result = Qdrant.get_client().delete( + points_selector=models.FilterSelector( + filter=filter_, + ), + wait=wait, + ) + + if wait: + return jsonify({"message": "Success"}),200 + else: + return jsonify({"message": "Accepted"}), 202 + + except Exception as e: + logger.error(f"Qdrant deletion failed: {str(e)}") + return jsonify({"error": "Failed to delete from Qdrant"}), 500 + + + + @app.get("/recommend") def recommend(): try: diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..6aa505e --- /dev/null +++ b/src/config.py @@ -0,0 +1,6 @@ +import logging + +from util.config import env + +LOGGING_LEVEL = env('LOGGING_LEVEL', default=logging.INFO) +LOGGING_FORMAT = env('LOGGING_FORMAT', default='%(asctime)s %(name)s %(levelname)s %(message)s')