{"id":30328602,"url":"https://github.com/bgeneto/qwen3-reranker-gguf-api","last_synced_at":"2025-08-18T01:10:12.540Z","repository":{"id":305211250,"uuid":"1022269721","full_name":"bgeneto/qwen3-reranker-gguf-api","owner":"bgeneto","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-18T19:56:56.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-18T23:34:21.179Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bgeneto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-18T18:42:39.000Z","updated_at":"2025-07-18T19:56:59.000Z","dependencies_parsed_at":"2025-07-18T23:34:25.092Z","dependency_job_id":"ef0dfeb2-9764-4cfe-92ff-2ee1e196c498","html_url":"https://github.com/bgeneto/qwen3-reranker-gguf-api","commit_stats":null,"previous_names":["bgeneto/qwen3-reranker-gguf-api"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/bgeneto/qwen3-reranker-gguf-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgeneto%2Fqwen3-reranker-gguf-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgeneto%2Fqwen3-reranker-gguf-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgeneto%2Fqwen3-reranker-gguf-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgeneto%2Fqwen3-reranker-gguf-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bgeneto","download_url":"https://codeload.github.com/bgeneto/qwen3-reranker-gguf-api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bgeneto%2Fqwen3-reranker-gguf-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270929287,"owners_count":24669681,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-18T01:10:09.858Z","updated_at":"2025-08-18T01:10:12.285Z","avatar_url":"https://github.com/bgeneto.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n# Qwen3 GGUF Reranker API\n\nThis repository provides a production-grade Docker Compose stack for serving a Qwen3-based reranker model using `llama-cpp-python`.\n\nThe API is designed for performance and security, offloading the entire model to the GPU, protecting routes with bearer token authentication, and providing structured logging and Prometheus metrics.\n\n## Features\n\n*   **CUDA Enabled**: Runs `llama-cpp-python` with CUDA support to offload 100% of the model layers to the GPU for maximum performance.\n*   **Configuration Driven**: All settings are managed via a `.env` file (model path, GPU layers, logging, etc.).\n*   **Structured Logging**: Asynchronous JSON-L logging to a file, which can be enabled or disabled.\n*   **Prometheus Metrics**: Exposes a `/metrics` endpoint with counters for requests, failures, and a histogram for latency.\n*   **Authentication**: All routes (except `/health` and `/metrics`) are protected by bearer token authentication.\n*   **Dockerized**: Comes with a `Dockerfile` and `docker-compose.yml` for easy setup and deployment.\n\n## Directory Layout\n\n```\n.\n├── docker-compose.yml\n├── Dockerfile\n├── .env\n├── app/\n│   ├── __init__.py\n│   ├── main.py\n│   ├── auth.py\n│   ├── metrics.py\n│   ├── logger.py\n│   └── requirements.txt\n└── models/\n    └── qwen3-4b-reranker-q4_k_m.gguf\n```\n\n## Getting Started\n\n### 1. Prerequisites\n\n*   Docker\n*   Docker Compose\n*   NVIDIA GPU with CUDA drivers installed\n\n### 2. Setup\n\n1.  **Clone the repository:**\n    ```bash\n    git clone https://github.com/your-username/qwen3-reranker-gguf-api.git\n    cd qwen3-reranker-gguf-api\n    ```\n\n2.  **Download the Model:**\n\n    **Option 1: Manual Download**\n    Place your GGUF model file inside the `models/` directory. The filename should match what you set in `MODEL_FILENAME`.\n\n    **Option 2: Automatic Download (Recommended)**\n    Configure the download settings in your `.env` file. The application will automatically download the model from the provided URL if it's not found in the `/models` directory:\n\n    ```bash\n    MODEL_FILENAME=model-file.gguf\n    MODEL_LINK=https://example.com/path/to/model-file.gguf  # Direct download URL\n    ```\n\n3.  **Configure Environment:**\n    Rename the `example.env` file to `.env` and customize the values:\n\n    ```bash\n    # .env\n    # model\n    MODEL_FILENAME=Qwen3-Reranker-4B-q4_k_m.gguf         # Model filename\n    MODEL_LINK=https://huggingface.co/Mungert/Qwen3-Reranker-4B-GGUF/resolve/main/Qwen3-Reranker-4B-q4_k_m.gguf  # Direct download URL\n    N_CTX=8192               # Max context size for the model's memory\n    N_GPU_LAYERS=-1          # -1 = offload everything\n    N_BATCH=512\n    N_THREADS=0            # 0 = auto\n    ```\n\n    **Note**: The model will be automatically saved to `/models/{MODEL_FILENAME}`. You no longer need to specify a full `MODEL_PATH` - just set the filename and the application will handle the rest.\n\n    # logging\n    LOG_LEVEL=INFO\n    LOG_TO_FILE=true\n    LOG_FILE=/var/log/reranker.jsonl\n\n    # auth\n    API_TOKEN=change-me-please\n\n    # server\n    HOST=0.0.0.0\n    PORT=8000\n\n    # user permissions (for Docker volume mounts)\n    # Set these to match your host user to avoid permission issues\n    # On Linux: UID=$(id -u) GID=$(id -g)\n    UID=1000\n    GID=1000\n    ```\n\n    **Important**: Set `UID` and `GID` to match your host user to avoid file permission issues with Docker volume mounts. On Linux, you can get these values by running:\n    ```bash\n    ./get-uid-gid.sh\n    ```\n\n    **A note on `N_CTX`**: This value defines the model's context window size (in tokens), which is its short-term memory. It must be less than or equal to the model's maximum supported context size. A larger `N_CTX` requires more VRAM/RAM.\n\n### 3. Run the Service\n\nStart the application using Docker Compose:\n\n```bash\ndocker compose up --build -d\n```\n\nThe API will be available at `http://localhost:8000`.\n\n## API Usage\n\n### Rerank Endpoint\n\nTo use the reranker, send a `POST` request to the `/v1/rerank` endpoint with your query and a list of documents.\n\n**Authentication is required.** Include your `API_TOKEN` as a bearer token in the `Authorization` header.\n\n**Example Request:**\n\n```bash\ncurl -s http://localhost:8000/v1/rerank \\\n  -H \"Authorization: Bearer change-me-please\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n        \"query\": \"What is the capital of China?\",\n        \"documents\": [\n            {\"text\": \"The capital of France is Paris.\"},\n            {\"text\": \"The capital of China is Beijing.\"}\n        ],\n        \"top_n\": 1\n      }' | jq .\n```\n\n**Example Response:**\n\n```json\n{\n  \"model\": \"qwen3-reranker\",\n  \"results\": [\n    {\n      \"index\": 1,\n      \"relevance_score\": 0.998\n    }\n  ]\n}\n```\n\n### Other Endpoints\n\n*   **Health Check**: `GET /health` - Publicly accessible endpoint to check service status and model information.\n*   **Metrics**: `GET /metrics` - Publicly accessible endpoint for Prometheus metrics.\n\n## Automatic Model Download from Hugging Face\n\nThis application supports automatic model downloading from direct URLs, which eliminates the need to manually download and manage model files.\n\n### How It Works\n\n1. **First Startup**: If the model file doesn't exist in the `/models` directory, the application will automatically attempt to download it from the configured download URL.\n\n2. **Download Tools**: The application uses `wget` or `curl` (whichever is available) to download the model file.\n\n3. **Caching**: Once downloaded, the model is stored locally and won't be downloaded again unless you delete the local file.\n\n### Configuration\n\nConfigure these environment variables in your `.env` file:\n\n- `MODEL_FILENAME`: The name for the downloaded model file\n- `MODEL_LINK`: The direct download URL for the model\n\n### Example for Different Models\n\n**For a public GGUF model:**\n```bash\nMODEL_FILENAME=Qwen3-Reranker-4B-q4_k_m.gguf\nMODEL_LINK=https://huggingface.co/Mungert/Qwen3-Reranker-4B-GGUF/resolve/main/Qwen3-Reranker-4B-q4_k_m.gguf\n```\n\n**For another model:**\n```bash\nMODEL_FILENAME=llama-2-7b-chat.q4_k_m.gguf\nMODEL_LINK=https://example.com/path/to/llama-2-7b-chat.q4_k_m.gguf\n```\n\n### Download Requirements\n\nThe Docker container includes both `wget` and `curl` for downloading models. If running locally, ensure you have one of these tools installed.\n\n### Health Check Enhancement\n\nThe `/health` endpoint now provides detailed information about the model status:\n\n```bash\ncurl http://localhost:8000/health | jq .\n```\n\nResponse includes:\n- Whether the model is loaded\n- Local model path and existence\n- Configured model filename and download link\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbgeneto%2Fqwen3-reranker-gguf-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbgeneto%2Fqwen3-reranker-gguf-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbgeneto%2Fqwen3-reranker-gguf-api/lists"}