LlamaCppGenerator
Generate text using LLMs through llama.cpp.
Key Features
- Runs LLMs locally using llama.cpp, a C/C++ library for efficient LLM inference.
- Uses the quantized GGUF format, suitable for running on standard machines without GPUs.
- Accepts string prompts and returns string replies.
- Compatible with any GGUF-format model available on Hugging Face.
Configuration
Before using this component, download the GGUF version of the model you want from Hugging Face and save it locally.
- Drag the
LlamaCppGeneratorcomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the
modelparameter to the local path of the GGUF file (for example,/models/zephyr-7b-beta.Q4_0.gguf).
- Set the
- Go to the Advanced tab to configure
n_ctx,n_batch,model_kwargs, andgeneration_kwargs.
Connections
LlamaCppGenerator accepts a prompt string as input. Connect its prompt input to the prompt output of PromptBuilder.
It outputs replies as a list of strings and meta as a list of metadata dictionaries. Connect its replies output to AnswerBuilder.
Source Code
To check this component's source code, open generator.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
LlamaCppGenerator:
type: haystack_integrations.components.generators.llama_cpp.generator.LlamaCppGenerator
init_parameters:
model: /models/zephyr-7b-beta.Q4_0.gguf
n_ctx: 2048
n_batch: 512
generation_kwargs:
max_tokens: 256
temperature: 0.7
top_p: 0.9
Connections
This example shows a simple question-answering pipeline using LlamaCppGenerator with a locally hosted GGUF model:
components:
PromptBuilder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: |
Answer the following question concisely and accurately.
Question: {{ question }}
Answer:
LlamaCppGenerator:
type: haystack_integrations.components.generators.llama_cpp.generator.LlamaCppGenerator
init_parameters:
model: /models/zephyr-7b-beta.Q4_0.gguf
n_ctx: 2048
n_batch: 512
generation_kwargs:
max_tokens: 256
temperature: 0.7
top_p: 0.9
AnswerBuilder:
type: haystack.components.builders.answer_builder.AnswerBuilder
init_parameters:
pattern:
reference_pattern:
connections:
- sender: PromptBuilder.prompt
receiver: LlamaCppGenerator.prompt
- sender: LlamaCppGenerator.replies
receiver: AnswerBuilder.replies
max_runs_per_component: 100
metadata: {}
inputs:
question:
- PromptBuilder.question
- AnswerBuilder.query
outputs:
answers: AnswerBuilder.answers
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt | str | The prompt to be sent to the generative model. | |
generation_kwargs | Optional[Dict[str, Any]] | None | A dictionary containing keyword arguments to customize text generation. These kwargs are merged with any generation_kwargs set during initialization. For more information on the available kwargs, see llama.cpp documentation. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
replies | List[str] | The list of string replies generated by the model. | |
meta | List[Dict[str, Any]] | Metadata about the request, including completion details from llama.cpp. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | str | The path of a quantized model for text generation, for example, "zephyr-7b-beta.Q4_0.gguf". If the model path is also specified in the model_kwargs, this parameter is ignored. | |
n_ctx | Optional[int] | 0 | The number of tokens in the context. When set to 0, the context is taken from the model. |
n_batch | Optional[int] | 512 | Prompt processing maximum batch size. |
model_kwargs | Optional[Dict[str, Any]] | None | Dictionary containing keyword arguments used to initialize the LLM for text generation. These keyword arguments provide fine-grained control over the model loading. In case of duplication, these kwargs override model, n_ctx, and n_batch init parameters. For more information on the available kwargs, see llama.cpp documentation. |
generation_kwargs | Optional[Dict[str, Any]] | None | A dictionary containing keyword arguments to customize text generation. For more information on the available kwargs, see llama.cpp documentation. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt | str | The prompt to be sent to the generative model. | |
generation_kwargs | Optional[Dict[str, Any]] | None | A dictionary containing keyword arguments to customize text generation. For more information on the available kwargs, see llama.cpp documentation. |
Was this page helpful?