DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio
API Reference

Uses Large Language Models directly in your pipelines.

Module prompt_node

PromptNode

class PromptNode(BaseComponent)

The PromptNode class is the central abstraction in Haystack's large language model (LLM) support. PromptNode supports multiple NLP tasks out of the box. You can use it to perform tasks such as summarization, question answering, question generation, and more, using a single, unified model within the Haystack framework.

One of the benefits of PromptNode is that you can use it to define and add additional prompt templates the model supports. Defining additional prompt templates makes it possible to extend the model's capabilities and use it for a broader range of NLP tasks in Haystack. Prompt engineers define templates for each NLP task and register them with PromptNode. The burden of defining templates for each task rests on the prompt engineers, not the users.

Using an instance of the PromptModel class, you can create multiple PromptNodes that share the same model, saving the memory and time required to load the model multiple times.

PromptNode also supports multiple model invocation layers:

  • Hugging Face transformers (all text2text-generation models)
  • OpenAI InstructGPT models
  • Azure OpenAI InstructGPT models

But you're not limited to the models listed above, as you can register additional custom model invocation layers.

We recommend using LLMs fine-tuned on a collection of datasets phrased as instructions, otherwise we find that the LLM does not "follow" prompt instructions well. The list of instruction-following models increases every month, and the current list includes: Flan, OpenAI InstructGPT, opt-iml, bloomz, and mt0 models.

For more details, see PromptNode.

PromptNode.__init__

def __init__(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,
             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)

Creates a PromptNode instance.

Arguments:

  • model_name_or_path: The name of the model to use or an instance of the PromptModel.
  • default_prompt_template: The default prompt template to use for the model.
  • output_variable: The name of the output variable in which you want to store the inference results. If not set, PromptNode uses PromptTemplate's output_variable. If PromptTemplate's output_variable is not set, the default name is results.
  • max_length: The maximum number of tokens the generated text output can have.
  • api_key: The API key to use for the model.
  • use_auth_token: The authentication token to use for the model.
  • use_gpu: Whether to use GPU or not.
  • devices: The devices to use for the model.
  • top_k: The number of independently generated texts to return per prompt. For example, if you set top_k=3, the model will generate three answers to the query.
  • stop_words: Stops text generation if any of the stop words is generated.
  • model_kwargs: Additional keyword arguments passed when loading the model specified in model_name_or_path.
  • debug: Whether to include the used prompts as debug information in the output under the key _debug. Note that Azure OpenAI InstructGPT models require two additional parameters: azure_base_url (the URL for the Azure OpenAI API endpoint, usually in the form https://<your-endpoint>.openai.azure.com') and azure_deployment_name (the name of the Azure OpenAI API deployment). You should specify these parameters in the model_kwargs` dictionary.

PromptNode.__call__

def __call__(*args, **kwargs) -> List[Any]

This method is invoked when the component is called directly, for example:

    PromptNode pn = ...
    sa = pn.set_default_prompt_template("sentiment-analysis")
    sa(documents=[Document("I am in love and I feel great!")])

PromptNode.prompt

def prompt(prompt_template: Optional[Union[str, PromptTemplate]], *args,
           **kwargs) -> List[Any]

Prompts the model and represents the central API for the PromptNode. It takes a prompt template,

a list of non-keyword and keyword arguments, and returns a list of strings - the responses from the underlying model.

If you specify the optional prompt_template parameter, it takes precedence over the default PromptTemplate for this PromptNode.

Arguments:

  • prompt_template: The name or object of the optional PromptTemplate to use.

Returns:

A list of strings as model responses.

PromptNode.add_prompt_template

def add_prompt_template(prompt_template: PromptTemplate) -> None

Adds a prompt template to the list of supported prompt templates.

Arguments:

  • prompt_template: The PromptTemplate object to be added.

Returns:

None

PromptNode.remove_prompt_template

def remove_prompt_template(prompt_template: str) -> PromptTemplate

Removes a prompt template from the list of supported prompt templates.

Arguments:

  • prompt_template: Name of the prompt template to be removed.

Returns:

PromptTemplate object that was removed.

PromptNode.set_default_prompt_template

def set_default_prompt_template(
        prompt_template: Union[str, PromptTemplate]) -> "PromptNode"

Sets the default prompt template for the node.

Arguments:

  • prompt_template: The prompt template to be set as default.

Returns:

The current PromptNode object.

PromptNode.get_prompt_templates

def get_prompt_templates() -> List[PromptTemplate]

Returns the list of supported prompt templates.

Returns:

List of supported prompt templates.

PromptNode.get_prompt_template_names

def get_prompt_template_names() -> List[str]

Returns the list of supported prompt template names.

Returns:

List of supported prompt template names.

PromptNode.is_supported_template

def is_supported_template(prompt_template: Union[str, PromptTemplate]) -> bool

Checks if a prompt template is supported.

Arguments:

  • prompt_template: The prompt template to be checked.

Returns:

True if the prompt template is supported, False otherwise.

PromptNode.get_prompt_template

def get_prompt_template(
    prompt_template: Union[str, PromptTemplate, None] = None
) -> Optional[PromptTemplate]

Resolves a prompt template.

Arguments:

  • prompt_template: The prompt template to be resolved. You can choose between the following types:
  • None: Returns the default prompt template.
  • PromptTemplate: Returns the given prompt template object.
  • str: Parses the string depending on its content:
    • prompt template name: Returns the prompt template registered with the given name.
    • prompt template yaml: Returns a prompt template specified by the given YAML.
    • prompt text: Returns a copy of the default prompt template with the given prompt text.

:return: The prompt template object.

PromptNode.prompt_template_params

def prompt_template_params(prompt_template: str) -> List[str]

Returns the list of parameters for a prompt template.

Arguments:

  • prompt_template: The name of the prompt template.

Returns:

The list of parameters for the prompt template.

PromptNode.run

def run(query: Optional[str] = None,
        file_paths: Optional[List[str]] = None,
        labels: Optional[MultiLabel] = None,
        documents: Optional[List[Document]] = None,
        meta: Optional[dict] = None,
        invocation_context: Optional[Dict[str, Any]] = None,
        prompt_template: Optional[Union[str, PromptTemplate]] = None,
        generation_kwargs: Optional[Dict[str,
                                         Any]] = None) -> Tuple[Dict, str]

Runs the PromptNode on these input parameters. Returns the output of the prompt model.

The parameters query, file_paths, labels, documents, and meta are added to the invocation context before invoking the prompt model. PromptNode uses these variables only if they are present as parameters in the PromptTemplate.

Arguments:

  • query: The PromptNode usually ignores the query, unless it's used as a parameter in the prompt template.
  • file_paths: The PromptNode usually ignores the file paths, unless they're used as a parameter in the prompt template.
  • labels: The PromptNode usually ignores the labels, unless they're used as a parameter in the prompt template.
  • documents: The documents to be used for the prompt.
  • meta: PromptNode usually ignores meta information, unless it's used as a parameter in the PromptTemplate.
  • invocation_context: The invocation context to be used for the prompt.
  • prompt_template: The prompt template to use. You can choose between the following types:
  • None: Use the default prompt template.
  • PromptTemplate: Use the given prompt template object.
  • str: Parses the string depending on its content:
    • prompt template name: Uses the prompt template registered with the given name.
    • prompt template yaml: Uses the prompt template specified by the given YAML.
    • prompt text: Uses a copy of the default prompt template with the given prompt text.
  • generation_kwargs: The generation_kwargs are used to customize text generation for the underlying pipeline.

PromptNode.run_batch

def run_batch(queries: Optional[List[str]] = None,
              documents: Optional[Union[List[Document],
                                        List[List[Document]]]] = None,
              invocation_contexts: Optional[List[Dict[str, Any]]] = None,
              prompt_templates: Optional[List[Union[str,
                                                    PromptTemplate]]] = None)

Runs PromptNode in batch mode.

  • If you provide a list containing a single query (or invocation context)...

    • ... and a single list of Documents, the query is applied to each Document individually.
    • ... and a list of lists of Documents, the query is applied to each list of Documents and the results are aggregated per Document list.
  • If you provide a list of multiple queries (or multiple invocation contexts)...

    • ... and a single list of Documents, each query (or invocation context) is applied to each Document individually.
    • ... and a list of lists of Documents, each query (or invocation context) is applied to its corresponding list of Documents and the results are aggregated per query-Document pair.
  • If you provide no Documents, then each query (or invocation context) is applied directly to the PromptTemplate.

Arguments:

  • queries: List of queries.
  • documents: Single list of Documents or list of lists of Documents in which to search for the answers.
  • invocation_contexts: List of invocation contexts.
  • prompt_templates: The prompt templates to use. You can choose between the following types:
  • None: Use the default prompt template.
  • PromptTemplate: Use the given prompt template object.
  • str: Parses the string depending on its content:
    • prompt template name: Uses the prompt template registered with the given name.
    • prompt template yaml: Uuses the prompt template specified by the given YAML.
    • prompt text: Uses a copy of the default prompt template with the given prompt text.

Module prompt_model

PromptModel

class PromptModel(BaseComponent)

The PromptModel class is a component that uses a pre-trained model to perform tasks defined in a prompt. Out of the box, it supports model invocation layers for:

  • Hugging Face transformers (all text2text-generation and text-generation models)
  • OpenAI InstructGPT models
  • Azure OpenAI InstructGPT models

Although it's possible to use PromptModel to make prompt invocations on the underlying model, use PromptNode to interact with the model. PromptModel instances are a way for multiple PromptNode instances to use a single PromptNode, and thus save computational resources.

For more details, refer to PromptModels.

PromptModel.__init__

def __init__(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,
             invocation_layer_class: Optional[
                 Type[PromptModelInvocationLayer]] = None,
             model_kwargs: Optional[Dict] = None)

Creates an instance of PromptModel.

Arguments:

  • model_name_or_path: The name or path of the underlying model.
  • max_length: The maximum number of tokens the output text generated by the model can have.
  • api_key: The API key to use for the model.
  • use_auth_token: The Hugging Face token to use.
  • use_gpu: Whether to use GPU or not.
  • devices: The devices to use where the model is loaded.
  • invocation_layer_class: The custom invocation layer class to use. If None, known invocation layers are used.
  • model_kwargs: Additional keyword arguments passed to the underlying model. Note that Azure OpenAI InstructGPT models require two additional parameters: azure_base_url (The URL for the Azure OpenAI API endpoint, usually in the form https://<your-endpoint>.openai.azure.com') and azure_deployment_name (the name of the Azure OpenAI API deployment). You should add these parameters in the model_kwargs` dictionary.

PromptModel.invoke

def invoke(prompt: Union[str, List[str], List[Dict[str, str]]],
           **kwargs) -> List[str]

Takes in a prompt and returns a list of responses using the underlying invocation layer.

Arguments:

  • prompt: The prompt to use for the invocation. It can be a single prompt or a list of prompts.
  • kwargs: Additional keyword arguments to pass to the invocation layer.

Returns:

A list of model-generated responses for the prompt or prompts.

Module prompt_template

PromptTemplate

class PromptTemplate(BasePromptTemplate, ABC)

PromptTemplate is a template for the prompt you feed to the model to instruct it what to do. For example, if you want the model to perform sentiment analysis, you simply tell it to do that in a prompt. Here's what a prompt template may look like:

    PromptTemplate(name="sentiment-analysis",
               prompt_text="Give a sentiment for this context. Answer with positive, negative"
               "or neutral. Context: {documents}; Answer:")

Optionally, you can declare prompt parameters using f-string syntax in the PromptTemplate. Prompt parameters are input parameters that need to be filled in the prompt_text for the model to perform the task. For example, in the template above, there's one prompt parameter, documents.

You declare prompt parameters by adding variables to the prompt text. These variables should be in the format: {variable}. In the template above, the variable is {documents}.

At runtime, the variables you declared in prompt text are filled in with the arguments passed to the fill() method of the PromptTemplate. So in the example above, the {documents} variable will be filled with the Documents whose sentiment you want the model to analyze.

Note that other than strict f-string syntax, you can safely use the following backslash characters in the text parts of the prompt text: \n, \t, \r. In f-string expressions, use new_line, tab, carriage_return instead. Double quotes (") are automatically replaced with single quotes (') in the prompt text. If you want to use double quotes in the prompt text, use {double_quote} instead.

For more details on how to use PromptTemplate, see PromptTemplates.

PromptTemplate.__init__

def __init__(name: str,
             prompt_text: str,
             output_parser: Optional[Union[BaseOutputParser,
                                           Dict[str, Any]]] = None)

Creates a PromptTemplate instance.

Arguments:

  • name: The name of the prompt template (for example, "sentiment-analysis", "question-generation"). You can specify your own name but it must be unique.
  • prompt_text: The prompt text, including prompt parameters.
  • output_parser: A parser that applied to the model output. For example, to convert the model output to an Answer object, you can use AnswerParser. Instead of BaseOutputParser instances, you can also pass dictionaries defining the output parsers. For example:
output_parser={"type": "AnswerParser", "params": {"pattern": "Answer: (.*)"}},

PromptTemplate.prepare

def prepare(*args, **kwargs) -> Dict[str, Any]

Prepares and verifies the PromtpTemplate with input parameters.

Arguments:

  • args: Non-keyword arguments to fill the parameters in the prompt text of a PromptTemplate.
  • kwargs: Keyword arguments to fill the parameters in the prompt text of a PromptTemplate.

Returns:

A dictionary with the prompt text and the prompt parameters.

PromptTemplate.post_process

def post_process(prompt_output: List[str], **kwargs) -> List[Any]

Post-processes the output of the PromptTemplate.

Arguments:

  • args: Non-keyword arguments to use for post-processing the prompt output.
  • kwargs: Keyword arguments to use for post-processing the prompt output.

Returns:

A dictionary with the post-processed output.

PromptTemplate.fill

def fill(*args, **kwargs) -> Iterator[str]

Fills the parameters defined in the prompt text with the arguments passed to it and returns the iterator prompt text.

You can pass non-keyword (args) or keyword (kwargs) arguments to this method. If you pass non-keyword arguments, their order must match the left-to-right order of appearance of the parameters in the prompt text. For example, if the prompt text is: Come up with a question for the given context and the answer. Context: {documents}; Answer: {answers}; Question:, then the first non-keyword argument fills the {documents} variable and the second non-keyword argument fills the {answers} variable.

If you pass keyword arguments, the order of the arguments doesn't matter. Variables in the prompt text are filled with the corresponding keyword argument.

Arguments:

  • args: Non-keyword arguments to fill the parameters in the prompt text. Their order must match the order of appearance of the parameters in the prompt text.
  • kwargs: Keyword arguments to fill the parameters in the prompt text.

Returns:

An iterator of prompt texts.

Module shapers

BaseOutputParser

class BaseOutputParser(Shaper)

An output parser in PromptTemplate defines how to parse the model output and convert it into Haystack primitives (answers, documents, or labels). BaseOutputParser is the base class for output parser implementations.

AnswerParser

class AnswerParser(BaseOutputParser)

Parses the model output to extract the answer into a proper Answer object using regex patterns. AnswerParser adds the document_ids of the documents used to generate the answer and the prompts used to the Answer object. You can pass a reference_pattern to extract the document_ids of the answer from the model output.

AnswerParser.__init__

def __init__(pattern: Optional[str] = None,
             reference_pattern: Optional[str] = None)

Arguments:

  • pattern: The regex pattern to use for parsing the answer. Examples: [^\\n]+$ finds "this is an answer" in string "this is an argument.\nthis is an answer". Answer: (.*) finds "this is an answer" in string "this is an argument. Answer: this is an answer". If not specified, the whole string is used as the answer. If specified, the first group of the regex is used as the answer. If there is no group, the whole match is used as the answer.
  • reference_pattern: The regex pattern to use for parsing the document references. Example: \\[(\\d+)\\] finds "1" in string "this is an answer[1]". If None, no parsing is done and all documents are referenced.