Skip to main content
llama.cpp python library is a simple Python bindings for @ggerganov llama.cpp. This package provides:
  • Low-level access to C API via ctypes interface.
  • High-level Python API for text completion
    • OpenAI-like API
    • LangChain compatibility
    • LlamaIndex compatibility
  • OpenAI compatible web server
    • Local Copilot replacement
    • Function Calling support
    • Vision API support
    • Multiple Models

Overview

Integration details

Model features

Setup

To get started and use all the features shown below, we recommend using a model that has been fine-tuned for tool-calling. We will use Hermes-2-Pro-Llama-3-8B-GGUF from NousResearch.
Hermes 2 Pro is an upgraded version of Nous Hermes 2, consisting of an updated and cleaned version of the OpenHermes 2.5 Dataset, as well as a newly introduced Function Calling and JSON Mode dataset developed in-house. This new version of Hermes maintains its excellent general task and conversation capabilities - but also excels at Function Calling
See our guides on local models to go deeper:

Installation

The LangChain LlamaCpp integration lives in the langchain-community and llama-cpp-python packages:

Instantiation

Now we can instantiate our model object and generate chat completions:

Invocation

Tool calling

Firstly, it works mostly the same as OpenAI Function Calling OpenAI has a tool calling (we use “tool calling” and “function calling” interchangeably here) API that lets you describe tools and their arguments, and have the model return a JSON object with a tool to invoke and the inputs to that tool. tool-calling is extremely useful for building tool-using chains and agents, and for getting structured outputs from models more generally. With ChatLlamaCpp.bind_tools, we can easily pass in Pydantic classes, dict schemas, LangChain tools, or even functions as tools to the model. Under the hood, these are converted to an OpenAI tool schema, which looks like:
and passed in every model invocation. However, it cannot automatically trigger a function/tool, we need to force it by specifying the ‘tool choice’ parameter. This parameter is typically formatted as described below. {"type": "function", "function": {"name": <<tool_name>>}}.

Structured output

Streaming


API reference

For detailed documentation of all ChatLlamaCpp features and configurations, head to the API reference: python.langchain.com/api_reference/community/chat_models/langchain_community.chat_models.llamacpp.ChatLlamaCpp.html
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.