llama.cpp Integration
🎯 Trigger Conditions
Use when asked about running DSPy programs with llama.cpp, CPU inference, or GGUF model support.
📚 Prerequisites
llama-cpp-pythonpackage installed- GGUF model available
- Python 3.8+
🛠️ llama.cpp Integration Patterns
1. Basic llama.cpp Setup
from llama_cpp import Llama
# Load GGUF model
llm = Llama(
model_path="./model.gguf",
n_ctx=2048,
n_threads=8
)
# Generate
output = llm(
"Your prompt",
max_tokens=1024,
temperature=0.7
)
2. llama.cpp with DSPy
import dspy
from llama_cpp import Llama
# Create llama.cpp engine
llama = Llama(model_path="./model.gguf")
# Create DSPy LM wrapper
class LlamaCPP_LM(dspy.LM):
def __init__(self, llama):
super().__init__()
self.llama = llama
def __call__(self, prompt, **kwargs):
# Convert to llama.cpp format
output = self.llama(prompt, **kwargs)
return [output["choices"][0]["text"]]
# Use with DSPy
llama_lm = LlamaCPP_LM(llama)
dspy.settings.configure(lm=llama_lm)
3. Quantization Options
# Different quantization levels
models = {
"q4_k_m": "./model-q4_k_m.gguf", # Good balance
"q8_0": "./model-q8_0.gguf", # Higher quality
"q2_k": "./model-q2_k.gguf" # Lower quality, smaller size
}
llm = Llama(model_path=models["q4_k_m"])
4. Performance Tuning
llm = Llama(
model_path="./model.gguf",
# CPU settings
n_threads=8,
n_batch=512,
# GPU offload
n_gpu_layers=35,
# Context
n_ctx=2048
)
⚠️ Pitfalls
- Speed: llama.cpp is slower than GPU inference
- Quality: Quantization affects output quality
- Memory: Large models require significant RAM
- Compatibility: Not all models support GGUF