llama-cpp-python: Python Bindings for llama.cpp

This repository profile is provided by osrepos.com, an open source repository discovery platform.

llama-cpp-python: Python Bindings for llama.cpp

Summary

llama-cpp-python provides robust Python bindings for the popular llama.cpp library, enabling efficient local inference with large language models. It offers a high-level API compatible with OpenAI's API, facilitating easy integration into existing applications. The project also includes a powerful web server for local deployment and supports various hardware acceleration backends.

Repository Information

Analyzed by OSRepos on November 11, 2025

Topics

Click on any tag to explore related repositories

Use at your own risk

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.

Introduction

llama-cpp-python is a crucial project that brings the power of llama.cpp to the Python ecosystem. It offers simple yet comprehensive Python bindings, allowing developers to interact with large language models (LLMs) locally. This package is designed to provide both low-level access to the C API via ctypes and a high-level Python API for common tasks like text completion, chat completion, and embeddings. With support for OpenAI-like API, LangChain, and LlamaIndex compatibility, llama-cpp-python makes local LLM deployment and experimentation accessible to a broader audience.

Installation

Getting started with llama-cpp-python is straightforward. The primary method involves installing directly via pip, which also builds llama.cpp from source to optimize for your system.

pip install llama-cpp-python

For basic CPU support, pre-built wheels are also available:

pip install llama-cpp-python \
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu

To leverage hardware acceleration like CUDA, Metal (MPS), or OpenBLAS, you can set CMAKE_ARGS environment variables during installation. For example, with CUDA:

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

Detailed instructions for various backends and pre-built CUDA/Metal wheels can be found in the official documentation.

Examples

llama-cpp-python offers a high-level API designed for ease of use, mimicking the OpenAI API for familiar workflows.

Text Completion:

from llama_cpp import Llama

llm = Llama(
      model_path="./models/7B/llama-model.gguf",
      # n_gpu_layers=-1, # Uncomment to use GPU acceleration
)
output = llm(
      "Q: Name the planets in the solar system? A: ",
      max_tokens=32,
      stop=["Q:", "\n"],
      echo=True
)
print(output)

Chat Completion:

The API supports various chat formats, making it easy to interact with models designed for conversational AI.

from llama_cpp import Llama
llm = Llama(
      model_path="path/to/llama-2/llama-model.gguf",
      chat_format="llama-2"
)
llm.create_chat_completion(
      messages = [
          {"role": "system", "content": "You are an assistant who perfectly describes images."},
          {
              "role": "user",
              "content": "Describe this image in detail please."
          }
      ]
)

Multi-modal Models (e.g., LLaVA):

The library also supports multi-modal models, allowing for image and text input.

from llama_cpp import Llama
from llama_cpp.llama_chat_format import Llava15ChatHandler
chat_handler = Llava15ChatHandler(clip_model_path="path/to/llava/mmproj.bin")
llm = Llama(
  model_path="./path/to/llava/llama-model.gguf",
  chat_handler=chat_handler,
  n_ctx=2048,
)
llm.create_chat_completion(
    messages = [
        {"role": "system", "content": "You are an assistant who perfectly describes images."},
        {
            "role": "user",
            "content": [
                {"type" : "text", "text": "What's in this image?"},
                {"type": "image_url", "image_url": {"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" } }
            ]
        }
    ]
)

Why Use It

llama-cpp-python stands out for several reasons:

  • Local Inference: Run powerful LLMs directly on your machine, ensuring data privacy and reducing reliance on cloud APIs.
  • OpenAI API Compatibility: Seamlessly integrate with existing applications built for the OpenAI API, minimizing code changes.
  • Hardware Acceleration: Supports various backends like CUDA, Metal, OpenBLAS, and ROCm, optimizing performance on different hardware.
  • Rich Feature Set: Beyond basic completion, it offers chat completion, function calling, multi-modal support, JSON mode, speculative decoding, and embeddings.
  • Web Server: Includes an OpenAI-compatible web server for easy local deployment and access from any client.
  • Active Development: The project is actively maintained and welcomes contributions, ensuring continuous improvement and new features.

Links

Related repositories

Similar repositories that may be relevant next.

dify-official-plugins: Extending Dify with AI Models, Tools, and Agent Strategies

dify-official-plugins: Extending Dify with AI Models, Tools, and Agent Strategies

August 18, 2026

The `dify-official-plugins` repository hosts a collection of official plugins for Dify, an open-source platform for developing LLM-powered AI applications. These plugins, including models, tools, agent strategies, and extensions, enhance Dify's capabilities and are maintained by the official Dify team. They are designed to help developers efficiently build, deploy, and manage AI-driven solutions.

PythonAILLM
Agent Skills: A Standardized Way to Give AI Agents New Capabilities

Agent Skills: A Standardized Way to Give AI Agents New Capabilities

August 18, 2026

Agent Skills provides a lightweight, open format for extending AI agent capabilities with specialized knowledge and workflows. It allows packaging procedural knowledge and context into portable, version-controlled folders that agents load on demand. This enables agents to gain domain expertise, follow repeatable workflows, and reuse skills across various compatible AI tools.

agent-skillsPythonAI
A-MEM: Self-Evolving Memory for Coding Agents

A-MEM: Self-Evolving Memory for Coding Agents

August 17, 2026

A-MEM is an innovative self-evolving memory system designed for coding agents, organizing knowledge into a dynamic Zettelkasten-style graph. It allows memories to evolve and connect over time, enhancing an agent's ability to recall and utilize information effectively. This system offers both semantic and structural search capabilities for a richer knowledge base.

PythonAILLM
Agent Sandbox: Secure Local Development for AI Coding Agents

Agent Sandbox: Secure Local Development for AI Coding Agents

August 17, 2026

Agent Sandbox provides a robust and secure local development environment specifically designed for collaborating with AI coding agents. It ensures minimal filesystem access, configurable network egress policies, and secure secret injection, protecting your local machine from potentially risky agent operations. This project supports various AI agents and integrates seamlessly with both CLI and popular IDE devcontainer setups.

agent-harnessagent-sandboxagents

Source repository

Open the original repository on GitHub.

10 counted GitHub visits

View on GitHub
OS
OSRepos

Analysis and discovery of open source repositories. Find interesting projects and follow their updates.

Monitor your website with YourWebsiteScore

OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of third-party repository code is at your own risk. Always review source code, dependencies, licenses, and security implications before running anything.

© 2025 OSRepos. Built with Nuxt 3 and lots of ❤️