LlamaCppGenerator
LlamaCppGenerator 提供了一个接口,用于使用运行在 Llama.cpp 上的 LLM 生成文本。
| pipeline 中的最常见位置 | 在 PromptBuilder 之后 |
| 必需的初始化变量 | "model": 要使用的模型路径 |
| 强制运行变量 | “prompt”:一个包含 LLM 提示的字符串 |
| 输出变量 | “replies”:一个包含 LLM 生成的所有回复的字符串列表 “meta”: 一个包含与每个回复相关的元数据的字典列表,例如 token 数量等。 |
| API 参考 | Llama.cpp |
| GitHub 链接 | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/llama_cpp |
概述
Llama.cpp 是一个用 C/C++ 编写的库,用于高效地进行大型语言模型的推理。它利用高效的量化 GGUF 格式,大大降低了内存需求并加速了推理。这意味着可以在标准机器上(即使没有 GPU)高效地运行 LLM。
Llama.cpp 使用 LLM 的量化二进制文件(GGUF 格式),这些文件可以从 Hugging Face 下载。LlamaCppGenerator 支持在Llama.cpp 上运行的模型,在初始化时将本地保存的 GGUF 文件的路径作为model 参数传入。
安装
安装llama-cpp-haystack 包
pip install llama-cpp-haystack
使用不同的计算后端
默认安装行为是在 Linux 和 Windows 上为 CPU 构建llama.cpp,并在 MacOS 上使用 Metal。要使用其他计算后端,请
- 按照 llama.cpp 安装页面 上的说明,为您首选的计算后端安装 llama-cpp-python。
- 使用上面的命令安装 llama-cpp-haystack。
例如,要使用llama-cpp-haystack 并配合 **cuBLAS 后端**,您需要运行以下命令:
export GGML_CUDA=1
CMAKE_ARGS="-DGGML_CUDA=on" pip install llama-cpp-python
pip install llama-cpp-haystack
用法
- 您需要下载所需 LLM 的 GGUF 版本。流行的模型 GGUF 版本可以从 Hugging Face 下载。
- 初始化一个
LlamaCppGenerator,传入 GGUF 文件路径,并指定所需的模型和文本生成参数。
from haystack_integrations.components.generators.llama_cpp import LlamaCppGenerator
generator = LlamaCppGenerator(
model="/content/openchat-3.5-1210.Q3_K_S.gguf",
n_ctx=512,
n_batch=128,
model_kwargs={"n_gpu_layers": -1},
generation_kwargs={"max_tokens": 128, "temperature": 0.1},
)
generator.warm_up()
prompt = f"Who is the best American actor?"
result = generator.run(prompt)
传递其他模型参数
该model, n_ctx, n_batch 参数已公开,可作为关键字参数直接在初始化时传递给 Generator。请注意,model 对应于llama.cpp 的model_path 参数。
该model_kwargs 参数可以在初始化模型时传递其他参数。如果重复,这些参数将覆盖model, n_ctx 和n_batch 的初始化参数。
有关可用模型参数的更多信息,请参阅 Llama.cpp 的 LLM 文档。
例如,在初始化时将模型卸载到 GPU
from haystack_integrations.components.generators.llama_cpp import LlamaCppGenerator
generator = LlamaCppGenerator(
model="/content/openchat-3.5-1210.Q3_K_S.gguf",
n_ctx=512,
n_batch=128,
model_kwargs={"n_gpu_layers": -1}
)
generator.warm_up()
prompt = f"Who is the best American actor?"
result = generator.run(prompt, generation_kwargs={"max_tokens": 128})
generated_text = result["replies"][0]
print(generated_text)
传递文本生成参数
该generation_kwargs 参数可以在推理期间将其他生成参数,如max_tokens, temperature, top_k, top_p 等传递给模型。
有关可用生成参数的更多信息,请参阅 Llama.cpp 的 Completion API 文档。
例如,设置max_tokens 和temperature:
from haystack_integrations.components.generators.llama_cpp import LlamaCppGenerator
generator = LlamaCppGenerator(
model="/content/openchat-3.5-1210.Q3_K_S.gguf",
n_ctx=512,
n_batch=128,
generation_kwargs={"max_tokens": 128, "temperature": 0.1},
)
generator.warm_up()
prompt = f"Who is the best American actor?"
result = generator.run(prompt)
该generation_kwargs 也可以直接传递给生成器的run 方法。
from haystack_integrations.components.generators.llama_cpp import LlamaCppGenerator
generator = LlamaCppGenerator(
model="/content/openchat-3.5-1210.Q3_K_S.gguf",
n_ctx=512,
n_batch=128,
)
generator.warm_up()
prompt = f"Who is the best American actor?"
result = generator.run(
prompt,
generation_kwargs={"max_tokens": 128, "temperature": 0.1},
)
在管道中使用
我们将在一个检索增强生成(RAG)管道中使用LlamaCppGenerator,该管道基于 HuggingFace 的 Simple Wikipedia 数据集,并使用 OpenChat-3.5 LLM 生成答案。
加载数据集
# Install HuggingFace Datasets using "pip install datasets"
from datasets import load_dataset
from haystack import Document, Pipeline
from haystack.components.builders.answer_builder import AnswerBuilder
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack.components.embedders import SentenceTransformersDocumentEmbedder, SentenceTransformersTextEmbedder
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
from haystack.components.writers import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore
# Import LlamaCppGenerator
from haystack_integrations.components.generators.llama_cpp import LlamaCppGenerator
# Load first 100 rows of the Simple Wikipedia Dataset from HuggingFace
dataset = load_dataset("pszemraj/simple_wikipedia", split="validation[:100]")
docs = [
Document(
content=doc["text"],
meta={
"title": doc["title"],
"url": doc["url"],
},
)
for doc in dataset
]
使用InMemoryDocumentStore 将文档索引到SentenceTransformersDocumentEmbedder 并DocumentWriter:
doc_store = InMemoryDocumentStore(embedding_similarity_function="cosine")
doc_embedder = SentenceTransformersDocumentEmbedder(model="sentence-transformers/all-MiniLM-L6-v2")
# Indexing Pipeline
indexing_pipeline = Pipeline()
indexing_pipeline.add_component(instance=doc_embedder, name="DocEmbedder")
indexing_pipeline.add_component(instance=DocumentWriter(document_store=doc_store), name="DocWriter")
indexing_pipeline.connect(connect_from="DocEmbedder", connect_to="DocWriter")
indexing_pipeline.run({"DocEmbedder": {"documents": docs}})
创建检索增强生成(RAG)管道,并将LlamaCppGenerator 添加到其中。
# Prompt Template for the https://hugging-face.cn/openchat/openchat-3.5-1210 LLM
prompt_template = """GPT4 Correct User: Answer the question using the provided context.
Question: {{question}}
Context:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
<|end_of_turn|>
GPT4 Correct Assistant:
"""
rag_pipeline = Pipeline()
text_embedder = SentenceTransformersTextEmbedder(model="sentence-transformers/all-MiniLM-L6-v2")
# Load the LLM using LlamaCppGenerator
model_path = "openchat-3.5-1210.Q3_K_S.gguf"
generator = LlamaCppGenerator(model=model_path, n_ctx=4096, n_batch=128)
rag_pipeline.add_component(
instance=text_embedder,
name="text_embedder",
)
rag_pipeline.add_component(instance=InMemoryEmbeddingRetriever(document_store=doc_store, top_k=3), name="retriever")
rag_pipeline.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
rag_pipeline.add_component(instance=generator, name="llm")
rag_pipeline.add_component(instance=AnswerBuilder(), name="answer_builder")
rag_pipeline.connect("text_embedder", "retriever")
rag_pipeline.connect("retriever", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder", "llm")
rag_pipeline.connect("llm.replies", "answer_builder.replies")
rag_pipeline.connect("retriever", "answer_builder.documents")
运行管道
question = "Which year did the Joker movie release?"
result = rag_pipeline.run(
{
"text_embedder": {"text": question},
"prompt_builder": {"question": question},
"llm": {"generation_kwargs": {"max_tokens": 128, "temperature": 0.1}},
"answer_builder": {"query": question},
}
)
generated_answer = result["answer_builder"]["answers"][0]
print(generated_answer.data)
# The Joker movie was released on October 4, 2019.
更新于 9 个月前
