llama-cpp-python

repository·main·Indexed 27 days ago

https://github.com/abetlen/llama-cpp-python

Python bindings for the llama.cpp library, enabling high-performance LLM inference. It provides low-level C API access via ctypes, a high-level Python API for text and chat completion compatible with OpenAI, LangChain, and LlamaIndex, and an OpenAI-compatible web server. Supports hardware acceleration via CUDA, Metal, HIP (ROCm), Vulkan, and OpenBLAS.

Tokens
8.6K
Snippets
29
Records
49
Agent score
91%

What's inside llama-cpp-python

  1. Manually choose a Llama model from Hugging Face

    main

    To use a specific model from Hugging Face instead of the defaults, follow these steps:

    1. Download the model: Use the provided script to download a model from a specific user and file type. For example, to download from TheBloke with type llama: python3 ./hug_model.py -a TheBloke -t llama

    2. Prepare the symlink: Ensure the downloaded file is symlinked to model.bin in your current directory so the Docker build process can find it. Example structure:

      -rw-rw-r-- 1 user user 4.8G <downloaded-model-file>.bin
      lrwxrwxrwx 1 user user 24 model.bin -> <downloaded-model-file>.bin
    3. Disk Space Warning: Ensure you have at least TWICE the size of the model in available disk space, as the model is downloaded and then copied into the Docker image.

    Estimated Quantized Sizes:

    ModelQuantized size
    3B3 GB
    7B5 GB
    13B10 GB
    33B25 GB
    65B50 GB
    python3 ./hug_model.py -a TheBloke -t llama
  2. Install llama-cpp-python with Metal (Apple Silicon) support

    main

    To enable Metal (MPS) acceleration on MacOS:

    Build from source:

    CMAKE_ARGS="-DGGML_METAL=on" pip install llama-cpp-python

    Pre-built wheels: Requirements:

    • MacOS 11.0 or later
    • Python 3.10, 3.11, or 3.12
    pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/metal
  3. Pull GGUF models from Hugging Face Hub

    main

    You can download models in gguf format directly using the Llama.from_pretrained method. This requires the huggingface-hub package (pip install huggingface-hub). Models are downloaded to the Hugging Face cache directory by default.

    llm = Llama.from_pretrained(
        repo_id="lmstudio-community/Qwen3.5-0.8B-GGUF",
        filename="*Q8_0.gguf",
        verbose=False
    )
  4. Install and run the OpenAI Compatible Web Server

    main

    You can run a web server that acts as a drop-in replacement for the OpenAI API. This allows any OpenAI-compatible client to use your local llama.cpp models.

    Basic Installation

    Install the server package using pip:

    pip install 'llama-cpp-python[server]'

    Running the Server

    Start the server by pointing to a GGUF model file:

    python3 -m llama_cpp.server --model models/7B/llama-model.gguf

    Configuration Options

    • GPU Acceleration (cuBLAS): To install with GPU support, use:
      CMAKE_ARGS="-DGGML_CUDA=on" FORCE_CMAKE=1 pip install 'llama-cpp-python[server]'
      python3 -m llama_cpp.server --model models/7B/llama-model.gguf --n_gpu_layers 35
    • Host and Port: Bind to all interfaces for remote connections using --host 0.0.0.0 and change the port with --port (default is 8000).
    • Chat Format: Set the prompt format (e.g., chatml) to ensure the model receives prompts in the expected structure:
      python3 -m llama_cpp.server --model models/7B/llama-model.gguf --chat_format chatml
    • Hugging Face Hub: If huggingface-hub is installed, load models directly from HF using --hf_model_repo_id and --model (to specify the file pattern):
      python3 -m llama_cpp.server --hf_model_repo_id lmstudio-community/Qwen3.5-0.8B-GGUF --model '*Q8_0.gguf'

    OpenAPI documentation is available at http://localhost:8000/docs.

    pip install 'llama-cpp-python[server]'
    python3 -m llama_cpp.server --model models/7B/llama-model.gguf
  5. Implement Function Calling

    main

    The API supports OpenAI-compatible tool/function calling. This is typically achieved using the chatml-function-calling format or functionary models.

    For Functionary v2 models, you must provide a LlamaHFTokenizer (initialized via LlamaHFTokenizer.from_pretrained) to handle tokenizer discrepancies between llama.cpp and Hugging Face.

    from llama_cpp import Llama
    from llama_cpp.llama_tokenizer import LlamaHFTokenizer
    llm = Llama.from_pretrained(
      repo_id="meetkai/functionary-small-v2.2-GGUF",
      filename="functionary-small-v2.2.q4_0.gguf",
      chat_format="functionary-v2",
      tokenizer=LlamaHFTokenizer.from_pretrained("meetkai/functionary-small-v2.2-GGUF")
    )
  6. Use Speculative Decoding

    main

    Speed up generation by using a draft model via the LlamaPromptLookupDecoding class. Pass this instance to the draft_model parameter during Llama initialization.

    • num_pred_tokens=10: Generally good for GPU.
    • num_pred_tokens=2: Performs better for CPU-only machines.
    from llama_cpp import Llama
    from llama_cpp.llama_speculative import LlamaPromptLookupDecoding
    
    lama = Llama(
        model_path="path/to/model.gguf",
        draft_model=LlamaPromptLookupDecoding(num_pred_tokens=10)
    )
  7. Run "Open-Llama-in-a-box"

    main

    This option automates the process of downloading an Apache V2.0 licensed 3B parameter Open LLaMA model and installing it into a Docker image running an OpenBLAS-enabled llama-cpp-python server.

    Steps:

    1. Navigate to the directory: cd ./open_llama
    2. Run the build script: ./build.sh
    3. Run the start script: ./start.sh
    $ cd ./open_llama
    ./build.sh
    ./start.sh
  8. Install llama-cpp-python server using CUDA (GPU)

    main

    Use the cuda_simple Dockerfile to build and run a server with NVIDIA GPU acceleration via CuBLAS. This method keeps the model file outside the Docker image.

    Requirements:

    • NVIDIA GPU with sufficient VRAM.
    • Docker NVIDIA support installed (NVIDIA Container Toolkit).
    • The model file must exist on the host system.

    Steps:

    1. Navigate to the directory: cd ./cuda_simple
    2. Build the image: docker build -t cuda_simple .
    3. Run the container using the --gpus=all flag and mapping your host model path to /var/model inside the container.
    cd ./cuda_simple
    docker build -t cuda_simple .
    docker run --gpus=all --cap-add SYS_RESOURCE -e USE_MLOCK=0 -e MODEL=/var/model/<model-path> -v <model-root-path>:/var/model -t cuda_simple
  9. Configure hardware acceleration via CMAKE_ARGS

    main

    You can configure llama.cpp build options (like hardware acceleration backends) using the CMAKE_ARGS environment variable or the -C / --config-settings CLI flag during installation.

    Using Environment Variables (Linux/Mac):

    CMAKE_ARGS="-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS" pip install llama-cpp-python

    Using Environment Variables (Windows PowerShell):

    $env:CMAKE_ARGS = "-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS"
    pip install llama-cpp-python

    Using CLI flags:

    pip install llama-cpp-python -C cmake.args="-DGGML_BLAS=ON;-DGGML_BLAS_VENDOR=OpenBLAS"
  10. Configure GitHub Copilot code completion

    main

    You can use llama-cpp-python as a backend for GitHub Copilot. This requires a code completion model in GGUF format and a significantly increased context size (--n_ctx).

    1. Run the server with high context:
    python3 -m llama_cpp.server --model <model_path> --n_ctx 16192
    1. Update your .vscode/settings.json to point to your local server host and port:
    {
        "github.copilot.advanced": {
            "debug.testOverrideProxyUrl": "http://<host>:<port>",
            "debug.overrideProxyUrl": "http://<host>:<port>"
        }
    }
  11. Set up Function Calling with Functionary

    main

    Structured function calling is supported via the OpenAI function calling API. To use models like functionary, you must specify the functionary-v1 or functionary-v2 chat_format. Because these models require a HuggingFace tokenizer, you must also provide the path to the tokenizer using --hf_pretrained_model_name_or_path.

    python3 -m llama_cpp.server --model <model_path_to_functionary_v2_model> --chat_format functionary-v2 --hf_pretrained_model_name_or_path <model_path_to_functionary_v2_tokenizer>