def generate_text_embeddings()

in use-cases/rag-pipeline/embedding-models/multimodal-embedding/src/blip2_server.py [0:0]


def generate_text_embeddings():
    """Generates text embeddings for a given caption.

    This endpoint accepts a POST request with a JSON payload containing the `caption`.

    Args:
        None (from request body):
            caption: The input caption as a string.

    Returns:
        A JSON response containing the text embeddings.
            {
                "text_embeds": [embedding values]
            }

    Raises:
        400 Bad Request: If the request format is invalid or missing required data.
        405 Method Not Allowed: If the request method is not POST.
    """
    if request.method == "POST":
        if request.is_json:
            try:
                json_req = request.get_json()
            except Exception as e:
                return jsonify({"error": f"Invalid JSON payload: {e}"}), 400
            if "caption" not in json_req:
                return jsonify({"error": "No caption provided"}), 400
            try:
                text_features = get_text_embedding(json_req["caption"])
            except Exception as e:
                return jsonify({"error": str(e)}), 400
            logger.info("Text embeddings generated successfully.")
            return jsonify(
                {
                    "text_embeds": text_features.tolist()[0][0],
                }
            )
        else:
            return jsonify({"error": "Invalid request format"}), 400
    else:
        return jsonify({"error": "Invalid request method"}), 405