LLM framework to build customizable, production-ready LLM applications with pipelines connecting models, vector DBs, and data processors.
—
Text generation components for creating answers, summaries, and other text outputs using Large Language Models (LLMs). The primary generation component in Haystack is PromptNode, which provides a unified interface for multiple LLM providers and supports prompt templates for various NLP tasks.
from haystack.nodes import PromptNode, PromptTemplate, PromptModel
from haystack.nodes.prompt import BaseOutputParser, AnswerParser
from haystack.nodes.prompt.invocation_layer import PromptModelInvocationLayerThe central abstraction for LLM support in Haystack, providing unified access to multiple model providers including OpenAI, Azure OpenAI, Cohere, and Hugging Face transformers.
class PromptNode(BaseComponent):
def __init__(
self,
model_name_or_path: Union[str, PromptModel] = "google/flan-t5-base",
default_prompt_template: Optional[Union[str, PromptTemplate]] = None,
output_variable: Optional[str] = None,
max_length: Optional[int] = 100,
api_key: Optional[str] = None,
api_base: Optional[str] = None,
timeout: Optional[float] = None,
use_auth_token: Optional[Union[str, bool]] = None,
use_gpu: Optional[bool] = None,
devices: Optional[List[Union[str, "torch.device"]]] = None,
stop_words: Optional[List[str]] = None,
top_k: int = 1,
debug: Optional[bool] = False,
model_kwargs: Optional[Dict] = None,
truncate: bool = True,
):
"""
Creates a PromptNode instance for text generation tasks.
Args:
model_name_or_path: Model identifier or PromptModel instance
default_prompt_template: Default template for prompts
output_variable: Variable name for storing results
max_length: Maximum tokens in generated output
api_key: API key for external services
api_base: Base URL for API endpoints
timeout: Request timeout in seconds
use_auth_token: Authentication token for Hugging Face
use_gpu: Whether to use GPU acceleration
devices: Specific devices to use
stop_words: Words that stop generation
top_k: Number of outputs to generate per prompt
debug: Enable debug logging
model_kwargs: Additional model parameters
truncate: Whether to truncate long inputs
"""
def predict(self, query: str, documents: List[Document] = None, **kwargs) -> List[str]:
"""Generate text based on query and optional documents."""
def prompt(self, prompt_template: Union[str, PromptTemplate], **kwargs) -> List[str]:
"""Generate text using a specific prompt template."""Template system for defining reusable prompts with placeholders and output parsing.
class PromptTemplate:
def __init__(
self,
prompt: str,
output_parser: BaseOutputParser = None,
output_variable: str = "results",
name: str = None
):
"""
Create a prompt template with placeholder variables.
Args:
prompt: Template string with {variable} placeholders
output_parser: Parser for structured output extraction
output_variable: Name for the output variable
name: Template identifier
"""
def fill(self, **kwargs) -> str:
"""Fill template placeholders with provided values."""Shared model instance that can be used across multiple PromptNodes to save memory and loading time.
class PromptModel:
def __init__(
self,
model_name_or_path: str = "google/flan-t5-base",
max_length: Optional[int] = 100,
api_key: Optional[str] = None,
use_auth_token: Optional[Union[str, bool]] = None,
use_gpu: Optional[bool] = None,
devices: Optional[List[Union[str, "torch.device"]]] = None,
model_kwargs: Optional[Dict] = None,
):
"""
Create a reusable model instance for multiple PromptNodes.
Args:
model_name_or_path: Model identifier
max_length: Maximum output length
api_key: API key for external services
use_auth_token: Hugging Face authentication
use_gpu: GPU usage preference
devices: Specific devices to use
model_kwargs: Additional model parameters
"""from haystack.nodes import PromptNode
# Initialize with OpenAI GPT
generator = PromptNode(
model_name_or_path="text-davinci-003",
api_key="your-openai-key",
max_length=200
)
# Generate text
result = generator.predict("Explain quantum computing in simple terms")
print(result[0]) # Generated explanationfrom haystack.nodes import PromptNode, PromptTemplate
from haystack import Document
# Create QA template
qa_template = PromptTemplate(
prompt="Given the context below, answer the question.\n\nContext: {join(documents)}\n\nQuestion: {query}\n\nAnswer:",
output_variable="answer"
)
# Initialize generator
generator = PromptNode(
model_name_or_path="google/flan-t5-large",
default_prompt_template=qa_template
)
# Generate answer with context
docs = [Document(content="Python is a programming language known for its simplicity.")]
answer = generator.predict("What is Python known for?", documents=docs)
print(answer[0]) # "Python is known for its simplicity."from haystack.nodes import PromptNode, PromptTemplate
# Create summarization template
summary_template = PromptTemplate(
prompt="Summarize the following text in 2-3 sentences:\n\n{documents}\n\nSummary:",
output_variable="summary"
)
# Initialize summarizer
summarizer = PromptNode(
model_name_or_path="google/flan-t5-base",
default_prompt_template=summary_template,
max_length=150
)
# Summarize documents
docs = [Document(content="Long text content to be summarized...")]
summary = summarizer.predict(documents=docs)
print(summary[0])# OpenAI GPT
openai_generator = PromptNode(
model_name_or_path="gpt-3.5-turbo",
api_key="your-openai-key"
)
# Hugging Face model
hf_generator = PromptNode(
model_name_or_path="google/flan-t5-base",
use_gpu=True
)
# Azure OpenAI
azure_generator = PromptNode(
model_name_or_path="gpt-35-turbo",
api_key="your-azure-key",
api_base="https://your-resource.openai.azure.com/"
)from typing import Union, List, Dict, Optional, Any
from haystack.schema import Document
# Output parser base class
class BaseOutputParser:
def parse(self, model_output: str, prompt_template: PromptTemplate) -> Any:
"""Parse structured output from model response."""
# Answer-specific parser
class AnswerParser(BaseOutputParser):
def parse(self, model_output: str, prompt_template: PromptTemplate) -> List[Answer]:
"""Parse model output into Answer objects."""
# Model invocation layer for custom providers
class PromptModelInvocationLayer:
def invoke(self, prompt: str, **kwargs) -> List[str]:
"""Invoke model with prompt and return generated text."""Install with Tessl CLI
npx tessl i tessl/pypi-farm-haystack