mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-12-30 08:37:20 +00:00
docs: Update PromptNode API docs (#4549)
* Update docstrings * adapt test to changed logging message --------- Co-authored-by: Julian Risch <julian.risch@deepset.ai>
This commit is contained in:
parent
3782ebc835
commit
815dcdebbd
@ -10,7 +10,7 @@ processors:
|
||||
do_not_filter_modules: false
|
||||
skip_empty_modules: true
|
||||
- type: filter
|
||||
expression: "name not in ['PromptModelInvocationLayer', 'StopWordsCriteria', 'HFLocalInvocationLayer', 'OpenAIInvocationLayer']"
|
||||
expression: "name not in ['PromptModelInvocationLayer', 'StopWordsCriteria', 'HFLocalInvocationLayer', 'OpenAIInvocationLayer', 'ValidationVisitor', 'FstringParamsTransformer']"
|
||||
- type: smart
|
||||
- type: crossref
|
||||
renderer:
|
||||
|
||||
@ -79,7 +79,7 @@ def to_strings(items: List[Union[str, Document, Answer]], pattern=None, str_repl
|
||||
|
||||
class PromptTemplateValidationError(NodeError):
|
||||
"""
|
||||
Error raised when a prompt template is invalid.
|
||||
The error raised when a PromptTemplate is invalid.
|
||||
"""
|
||||
|
||||
pass
|
||||
@ -87,9 +87,9 @@ class PromptTemplateValidationError(NodeError):
|
||||
|
||||
class _ValidationVisitor(ast.NodeVisitor):
|
||||
"""
|
||||
This class is used to validate the prompt text for a prompt template.
|
||||
This class is used to validate the prompt text for a PromptTemplate.
|
||||
It checks that the prompt text is a valid f-string and that it only uses allowed functions.
|
||||
Useful information extracted from the AST is stored in the class attributes (e.g. `prompt_params` and `used_functions`)
|
||||
Useful information extracted from the AST is stored in the class attributes (for example, `prompt_params` and `used_functions`).
|
||||
"""
|
||||
|
||||
def __init__(self, prompt_template_name: str):
|
||||
@ -102,21 +102,21 @@ class _ValidationVisitor(ast.NodeVisitor):
|
||||
def prompt_params(self) -> List[str]:
|
||||
"""
|
||||
The names of the variables used in the prompt text.
|
||||
E.g. for the prompt text `f"Hello {name}"`, the prompt_params would be `["name"]`
|
||||
For example, for the prompt text `f"Hello {name}"`, the prompt_params is `["name"]`.
|
||||
"""
|
||||
return list(set(self.used_names) - set(self.used_functions) - set(self.comprehension_targets))
|
||||
|
||||
def visit_Name(self, node: ast.Name) -> None:
|
||||
"""
|
||||
Stores the name of the variable used in the prompt text. This also includes function and method names.
|
||||
E.g. for the prompt text `f"Hello {func(name)}"`, the used_names would be `["func", "name"]`
|
||||
For example, for the prompt text `f"Hello {func(name)}"`, the used_names are `["func", "name"]`.
|
||||
"""
|
||||
self.used_names.append(node.id)
|
||||
|
||||
def visit_comprehension(self, node: ast.comprehension) -> None:
|
||||
"""
|
||||
Stores the name of the variable used in comprehensions.
|
||||
E.g. for the prompt text `f"Hello {[name for name in names]}"`, the comprehension_targets would be `["name"]`
|
||||
For example, for the prompt text `f"Hello {[name for name in names]}"`, the comprehension_targets is `["name"]`.
|
||||
"""
|
||||
super().generic_visit(node)
|
||||
if isinstance(node.target, ast.Name):
|
||||
@ -127,9 +127,9 @@ class _ValidationVisitor(ast.NodeVisitor):
|
||||
def visit_Call(self, node: ast.Call) -> None:
|
||||
"""
|
||||
Stores the name of functions and methods used in the prompt text and validates that only allowed functions are used.
|
||||
E.g. for the prompt text `f"Hello {func(name)}"`, the used_functions would be `["func"]`
|
||||
For example, for the prompt text `f"Hello {func(name)}"`, the used_functions is `["func"]`.
|
||||
|
||||
raises: PromptTemplateValidationError if an invalid function is used in the prompt text
|
||||
raises: PromptTemplateValidationError if the prompt text contains an invalid function.
|
||||
"""
|
||||
super().generic_visit(node)
|
||||
if isinstance(node.func, ast.Name) and node.func.id in PROMPT_TEMPLATE_ALLOWED_FUNCTIONS:
|
||||
@ -147,10 +147,10 @@ class _ValidationVisitor(ast.NodeVisitor):
|
||||
|
||||
class _FstringParamsTransformer(ast.NodeTransformer):
|
||||
"""
|
||||
This class is used to transform an AST for f-strings into a format that can be used by the PromptTemplate.
|
||||
It replaces all f-string expressions with a unique id and stores the corresponding expression in a dictionary.
|
||||
Transforms an AST for f-strings into a format the PromptTemplate can use.
|
||||
It replaces all f-string expressions with a unique ID and stores the corresponding expressions in a dictionary.
|
||||
|
||||
The stored expressions can be evaluated using the `eval` function given the `prompt_params` (see _ValidatorVisitor) .
|
||||
You can evaluate the stored expressions using the `eval` function given the `prompt_params` (see _ValidatorVisitor).
|
||||
PromptTemplate determines the number of prompts to generate and renders them using the evaluated expressions.
|
||||
"""
|
||||
|
||||
@ -159,7 +159,7 @@ class _FstringParamsTransformer(ast.NodeTransformer):
|
||||
|
||||
def visit_FormattedValue(self, node: ast.FormattedValue) -> Optional[ast.AST]:
|
||||
"""
|
||||
Replaces the f-string expression with a unique id and stores the corresponding expression in a dictionary.
|
||||
Replaces the f-string expression with a unique ID and stores the corresponding expression in a dictionary.
|
||||
If the expression is the raw `documents` variable, it is encapsulated into a call to `documents_to_strings` to ensure that the documents get rendered correctly.
|
||||
"""
|
||||
super().generic_visit(node)
|
||||
@ -181,7 +181,7 @@ class _FstringParamsTransformer(ast.NodeTransformer):
|
||||
|
||||
class BaseOutputParser(Shaper):
|
||||
"""
|
||||
A output parser defines in `PromptTemplate` how to parse the model output and convert it into Haystack primitives.
|
||||
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.
|
||||
"""
|
||||
|
||||
@ -192,20 +192,20 @@ class BaseOutputParser(Shaper):
|
||||
|
||||
class AnswerParser(BaseOutputParser):
|
||||
"""
|
||||
AnswerParser is used to parse the model output to extract the answer into a proper `Answer` object using regex patterns.
|
||||
AnswerParser enriches the `Answer` object with the used prompts and the document_ids of the documents that were used to generate the answer.
|
||||
You can pass a reference_pattern to extract the document_ids of the answer from the model output.
|
||||
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.
|
||||
"""
|
||||
|
||||
def __init__(self, pattern: Optional[str] = None, reference_pattern: Optional[str] = None):
|
||||
"""
|
||||
:param pattern: The regex pattern to use for parsing the answer.
|
||||
Examples:
|
||||
`[^\\n]+$` will find "this is an answer" in string "this is an argument.\nthis is an answer".
|
||||
`Answer: (.*)` will find "this is an answer" in string "this is an argument. Answer: this is an answer".
|
||||
If None, the whole string is used as the answer. If not None, the first group of the regex is used as the answer. If there is no group, the whole match is used as the answer.
|
||||
`[^\\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.
|
||||
:param reference_pattern: The regex pattern to use for parsing the document references.
|
||||
Example: `\\[(\\d+)\\]` will find "1" in string "this is an answer[1]".
|
||||
Example: `\\[(\\d+)\\]` finds "1" in string "this is an answer[1]".
|
||||
If None, no parsing is done and all documents are referenced.
|
||||
"""
|
||||
self.pattern = pattern
|
||||
@ -220,7 +220,7 @@ class AnswerParser(BaseOutputParser):
|
||||
|
||||
class PromptTemplate(BasePromptTemplate, ABC):
|
||||
"""
|
||||
PromptTemplate is a template for a 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 such prompt template may look like:
|
||||
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:
|
||||
|
||||
```python
|
||||
PromptTemplate(name="sentiment-analysis",
|
||||
@ -229,16 +229,18 @@ class PromptTemplate(BasePromptTemplate, ABC):
|
||||
```
|
||||
|
||||
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}`.
|
||||
the prompt_text for the model to perform the task. For example, in the template above, there's one prompt parameter, `documents`.
|
||||
|
||||
At runtime, these variables are filled in with 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.
|
||||
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}`.
|
||||
|
||||
Note that other than strict f-string syntax, you can safely use the following backslash characters in text parts of the prompt text: `\n`, `\t`, `\r`.
|
||||
If you want to use them in f-string expressions, use `new_line`, `tab`, `carriage_return` instead.
|
||||
Double quotes (e.g. `"`) will be automatically replaced with single quotes (e.g. `'`) in the prompt text. If you want to use double quotes in the prompt text, use `{double_quote}` instead.
|
||||
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
|
||||
[PromptNode](https://docs.haystack.deepset.ai/docs/prompt_node).
|
||||
[PromptTemplates](https://docs.haystack.deepset.ai/docs/prompt_node#prompttemplates).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@ -247,10 +249,10 @@ class PromptTemplate(BasePromptTemplate, ABC):
|
||||
"""
|
||||
Creates a PromptTemplate instance.
|
||||
|
||||
:param name: The name of the prompt template (for example, sentiment-analysis, question-generation). You can specify your own name but it must be unique.
|
||||
:param name: The name of the prompt template (for example, "sentiment-analysis", "question-generation"). You can specify your own name but it must be unique.
|
||||
:param prompt_text: The prompt text, including prompt parameters.
|
||||
:param output_parser: A parser that will be applied to the model output.
|
||||
For example, if you want to convert the model output to an Answer object, you can use `AnswerParser`.
|
||||
:param 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: (.*)"}},
|
||||
@ -301,7 +303,7 @@ class PromptTemplate(BasePromptTemplate, ABC):
|
||||
|
||||
def prepare(self, *args, **kwargs) -> Dict[str, Any]:
|
||||
"""
|
||||
Prepares and verifies the prompt template with input parameters.
|
||||
Prepares and verifies the PromtpTemplate with input parameters.
|
||||
|
||||
:param args: Non-keyword arguments to fill the parameters in the prompt text of a PromptTemplate.
|
||||
:param kwargs: Keyword arguments to fill the parameters in the prompt text of a PromptTemplate.
|
||||
@ -340,7 +342,7 @@ class PromptTemplate(BasePromptTemplate, ABC):
|
||||
|
||||
def post_process(self, prompt_output: List[str], **kwargs) -> List[Any]:
|
||||
"""
|
||||
Post-processes the output of the prompt template.
|
||||
Post-processes the output of the PromptTemplate.
|
||||
:param args: Non-keyword arguments to use for post-processing the prompt output.
|
||||
:param kwargs: Keyword arguments to use for post-processing the prompt output.
|
||||
:return: A dictionary with the post-processed output.
|
||||
@ -366,7 +368,7 @@ class PromptTemplate(BasePromptTemplate, ABC):
|
||||
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.
|
||||
|
||||
:param args: Non-keyword arguments to fill the parameters in the prompt text. Their order must match the order of appearance of the parameters in prompt text.
|
||||
:param 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.
|
||||
:param kwargs: Keyword arguments to fill the parameters in the prompt text.
|
||||
:return: An iterator of prompt texts.
|
||||
"""
|
||||
@ -393,17 +395,17 @@ class PromptTemplate(BasePromptTemplate, ABC):
|
||||
|
||||
class PromptModel(BaseComponent):
|
||||
"""
|
||||
The PromptModel class is a component that uses a pre-trained model to perform tasks based on a prompt. Out of
|
||||
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 is possible to use PromptModel to make prompt invocations on the underlying model, use
|
||||
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 [PromptNode](https://docs.haystack.deepset.ai/docs/prompt_node).
|
||||
For more details, refer to [PromptModels](https://docs.haystack.deepset.ai/docs/prompt_node#models).
|
||||
"""
|
||||
|
||||
outgoing_edges = 1
|
||||
@ -433,7 +435,7 @@ class PromptModel(BaseComponent):
|
||||
|
||||
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). These parameters should be supplied
|
||||
azure_deployment_name (the name of the Azure OpenAI API deployment). You should add these parameters
|
||||
in the `model_kwargs` dictionary.
|
||||
"""
|
||||
super().__init__()
|
||||
@ -449,8 +451,8 @@ class PromptModel(BaseComponent):
|
||||
is_instruction_following: bool = any(m in model_name_or_path for m in instruction_following_models())
|
||||
if not is_instruction_following:
|
||||
logger.warning(
|
||||
"PromptNode has been potentially initialized with a language model not fine-tuned on instruction following tasks. "
|
||||
"Many of the default prompts and PromptTemplates will likely not work as intended. "
|
||||
"PromptNode has been potentially initialized with a language model not fine-tuned on instruction-following tasks. "
|
||||
"Many of the default prompts and PromptTemplates may not work as intended. "
|
||||
"Use custom prompts and PromptTemplates specific to the %s model",
|
||||
model_name_or_path,
|
||||
)
|
||||
@ -486,11 +488,11 @@ class PromptModel(BaseComponent):
|
||||
|
||||
def invoke(self, prompt: Union[str, List[str], List[Dict[str, str]]], **kwargs) -> List[str]:
|
||||
"""
|
||||
It takes in a prompt, and returns a list of responses using the underlying invocation layer.
|
||||
Takes in a prompt and returns a list of responses using the underlying invocation layer.
|
||||
|
||||
:param prompt: The prompt to use for the invocation. It can be a single prompt or a list of prompts.
|
||||
:param kwargs: Additional keyword arguments to pass to the invocation layer.
|
||||
:return: A list of model generated responses for the prompt or prompts.
|
||||
:return: A list of model-generated responses for the prompt or prompts.
|
||||
"""
|
||||
output = self.model_invocation_layer.invoke(prompt=prompt, **kwargs)
|
||||
return output
|
||||
@ -638,7 +640,7 @@ def get_predefined_prompt_templates() -> List[PromptTemplate]:
|
||||
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
|
||||
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.
|
||||
|
||||
@ -656,14 +658,14 @@ class PromptNode(BaseComponent):
|
||||
- OpenAI InstructGPT models
|
||||
- Azure OpenAI InstructGPT models
|
||||
|
||||
However, users are not limited to above-mentioned models only as there is a built-in ability to register
|
||||
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 instructions following models increases every month,
|
||||
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](https://docs.haystack.deepset.ai/docs/prompt_node).
|
||||
For more details, see [PromptNode](https://docs.haystack.deepset.ai/docs/prompt_node).
|
||||
"""
|
||||
|
||||
outgoing_edges: int = 1
|
||||
@ -689,20 +691,20 @@ class PromptNode(BaseComponent):
|
||||
:param model_name_or_path: The name of the model to use or an instance of the PromptModel.
|
||||
:param default_prompt_template: The default prompt template to use for the model.
|
||||
:param 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, default name is `results`.
|
||||
If not set, PromptNode uses PromptTemplate's output_variable. If PromptTemplate's output_variable is not set, the default name is `results`.
|
||||
:param max_length: The maximum length of the generated text output.
|
||||
:param api_key: The API key to use for the model.
|
||||
:param use_auth_token: The authentication token to use for the model.
|
||||
:param use_gpu: Whether to use GPU or not.
|
||||
:param devices: The devices to use for the model.
|
||||
:param top_k: The number of independently generated texts to return per prompt. For example, if you set top_k=3, the model's going to generate three answers to the query.
|
||||
:param 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.
|
||||
:param stop_words: Stops text generation if any of the stop words is generated.
|
||||
:param model_kwargs: Additional keyword arguments passed when loading the model specified in `model_name_or_path`.
|
||||
|
||||
Note that Azure OpenAI InstructGPT models require two additional parameters: azure_base_url (The URL for the
|
||||
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).
|
||||
These parameters should be supplied in the `model_kwargs` dictionary.
|
||||
azure_deployment_name (the name of the Azure OpenAI API deployment).
|
||||
You should specify these parameters in the `model_kwargs` dictionary.
|
||||
|
||||
"""
|
||||
send_event(
|
||||
@ -728,7 +730,7 @@ class PromptNode(BaseComponent):
|
||||
raise ValueError(
|
||||
f"Prompt template {self.default_prompt_template} is not supported. "
|
||||
f"Select one of: {self.get_prompt_template_names()} "
|
||||
f"or first register a new prompt template using the add_prompt_template method."
|
||||
f"or register a new prompt template first using the add_prompt_template() method."
|
||||
)
|
||||
|
||||
if isinstance(model_name_or_path, str):
|
||||
@ -806,7 +808,7 @@ class PromptNode(BaseComponent):
|
||||
def add_prompt_template(self, prompt_template: PromptTemplate) -> None:
|
||||
"""
|
||||
Adds a prompt template to the list of supported prompt templates.
|
||||
:param prompt_template: PromptTemplate object to be added.
|
||||
:param prompt_template: The PromptTemplate object to be added.
|
||||
:return: None
|
||||
"""
|
||||
if prompt_template.name in self.prompt_templates:
|
||||
@ -868,12 +870,12 @@ class PromptNode(BaseComponent):
|
||||
Resolves a prompt template.
|
||||
|
||||
:param 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
|
||||
- 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.
|
||||
"""
|
||||
@ -890,7 +892,7 @@ class PromptNode(BaseComponent):
|
||||
# if it's not a string or looks like a prompt template name
|
||||
if not isinstance(prompt_template, str) or re.fullmatch(r"[-a-zA-Z0-9_]+", prompt_template):
|
||||
raise ValueError(
|
||||
f"{prompt_template} not supported, please select one of: {self.get_prompt_template_names()} or pass a PromptTemplate instance for prompting."
|
||||
f"{prompt_template} not supported, select one of: {self.get_prompt_template_names()} or pass a PromptTemplate instance for prompting."
|
||||
)
|
||||
|
||||
if "prompt_text:" in prompt_template:
|
||||
@ -928,8 +930,8 @@ class PromptNode(BaseComponent):
|
||||
prompt_template: Optional[Union[str, PromptTemplate]] = None,
|
||||
) -> Tuple[Dict, str]:
|
||||
"""
|
||||
Runs the PromptNode on these inputs parameters. Returns the output of the prompt model.
|
||||
The parameters `query`, `file_paths`, `labels`, `documents` and `meta` are added to the invocation context
|
||||
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.
|
||||
|
||||
@ -944,12 +946,12 @@ class PromptNode(BaseComponent):
|
||||
PromptTemplate.
|
||||
:param invocation_context: The invocation context to be used for the prompt.
|
||||
:param prompt_template: The prompt template to use. You can choose between the following types:
|
||||
- None: use 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
|
||||
- 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.
|
||||
"""
|
||||
# prompt_collector is an empty list, it's passed to the PromptNode that will fill it with the rendered prompts,
|
||||
# so that they can be returned by `run()` as part of the pipeline's debug output.
|
||||
@ -997,28 +999,28 @@ class PromptNode(BaseComponent):
|
||||
"""
|
||||
Runs PromptNode in batch mode.
|
||||
|
||||
- If you provide a list containing a single query (and/or invocation context)...
|
||||
- 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 (and/or multiple invocation contexts)...
|
||||
- ... and a single list of Documents, each query (and/or invocation context) is applied to each Document individually.
|
||||
- ... and a list of lists of Documents, each query (and/or invocation context) is applied to its corresponding list of Documents
|
||||
- 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 (and/or invocation context) is applied directly to the PromptTemplate.
|
||||
- If you provide no Documents, then each query (or invocation context) is applied directly to the PromptTemplate.
|
||||
|
||||
:param queries: List of queries.
|
||||
:param documents: Single list of Documents or list of lists of Documents in which to search for the answers.
|
||||
:param invocation_contexts: List of invocation contexts.
|
||||
:param prompt_templates: The prompt templates to use. You can choose between the following types:
|
||||
- None: use 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
|
||||
- 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.
|
||||
"""
|
||||
inputs = PromptNode._flatten_inputs(queries, documents, invocation_contexts, prompt_templates)
|
||||
all_results: Dict[str, List] = defaultdict(list)
|
||||
@ -1050,17 +1052,17 @@ class PromptNode(BaseComponent):
|
||||
) -> Dict[str, List]:
|
||||
"""Flatten and copy the queries, documents, and invocation contexts into lists of equal length.
|
||||
|
||||
- If you provide a list containing a single query (and/or invocation context)...
|
||||
- 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 (and/or multiple invocation contexts)...
|
||||
- ... and a single list of Documents, each query (and/or invocation context) is applied to each Document individually.
|
||||
- ... and a list of lists of Documents, each query (and/or invocation context) is applied to its corresponding list of Documents
|
||||
- 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 (and/or invocation context) is applied to the PromptTemplate.
|
||||
- If you provide no Documents, then each query (or invocation context) is applied to the PromptTemplate.
|
||||
|
||||
:param queries: List of queries.
|
||||
:param documents: Single list of Documents or list of lists of Documents in which to search for the answers.
|
||||
|
||||
@ -101,7 +101,7 @@ class PromptModelInvocationLayer:
|
||||
Checks if the given model is supported by this invocation layer.
|
||||
|
||||
:param model_name_or_path: The name or path of the model.
|
||||
:param kwargs: additional keyword arguments passed to the underlying model which might be used to determine
|
||||
:param kwargs: Additional keyword arguments passed to the underlying model which might be used to determine
|
||||
if the model is supported.
|
||||
:return: True if this invocation layer supports the model, False otherwise.
|
||||
"""
|
||||
@ -234,8 +234,8 @@ class HFLocalInvocationLayer(PromptModelInvocationLayer):
|
||||
|
||||
def invoke(self, *args, **kwargs):
|
||||
"""
|
||||
It takes a prompt and returns a list of generated text using the local Hugging Face transformers model
|
||||
:return: A list of generated text.
|
||||
It takes a prompt and returns a list of generated texts using the local Hugging Face transformers model
|
||||
:return: A list of generated texts.
|
||||
|
||||
Note: Only kwargs relevant to Text2TextGenerationPipeline and TextGenerationPipeline are passed to
|
||||
Hugging Face as model_input_kwargs. Other kwargs are ignored.
|
||||
@ -302,8 +302,8 @@ class HFLocalInvocationLayer(PromptModelInvocationLayer):
|
||||
return prompt
|
||||
|
||||
logger.warning(
|
||||
"The prompt has been truncated from %s tokens to %s tokens such that the prompt length and "
|
||||
"answer length (%s tokens) fits within the max token limit (%s tokens). "
|
||||
"The prompt has been truncated from %s tokens to %s tokens so that the prompt length and "
|
||||
"answer length (%s tokens) fit within the max token limit (%s tokens). "
|
||||
"Shorten the prompt to prevent it from being cut off",
|
||||
n_prompt_tokens,
|
||||
self.pipe.tokenizer.model_max_length - n_answer_tokens,
|
||||
@ -487,8 +487,8 @@ class OpenAIInvocationLayer(PromptModelInvocationLayer):
|
||||
return prompt
|
||||
|
||||
logger.warning(
|
||||
"The prompt has been truncated from %s tokens to %s tokens such that the prompt length and "
|
||||
"answer length (%s tokens) fits within the max token limit (%s tokens). "
|
||||
"The prompt has been truncated from %s tokens to %s tokens so that the prompt length and "
|
||||
"answer length (%s tokens) fit within the max token limit (%s tokens). "
|
||||
"Reduce the length of the prompt to prevent it from being cut off.",
|
||||
n_prompt_tokens,
|
||||
self.max_tokens_limit - n_answer_tokens,
|
||||
@ -517,8 +517,8 @@ class AzureOpenAIInvocationLayer(OpenAIInvocationLayer):
|
||||
Azure OpenAI Invocation Layer
|
||||
|
||||
This layer is used to invoke the OpenAI API on Azure. It is essentially the same as the OpenAIInvocationLayer
|
||||
with additional two parameters: azure_base_url and azure_deployment_name. The azure_base_url is the URL of the Azure OpenAI
|
||||
endpoint and the azure_deployment_name is the name of the deployment.
|
||||
with additional two parameters: `azure_base_url` and `azure_deployment_name`. The `azure_base_url` is the URL of the Azure OpenAI
|
||||
endpoint and the `azure_deployment_name` is the name of the deployment.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@ -547,7 +547,7 @@ class AzureOpenAIInvocationLayer(OpenAIInvocationLayer):
|
||||
@classmethod
|
||||
def supports(cls, model_name_or_path: str, **kwargs) -> bool:
|
||||
"""
|
||||
Ensures Azure OpenAI Invocation Layer is selected when azure_base_url and azure_deployment_name are provided in
|
||||
Ensures Azure OpenAI Invocation Layer is selected when `azure_base_url` and `azure_deployment_name` are provided in
|
||||
addition to a list of supported models.
|
||||
"""
|
||||
valid_model = any(m for m in ["ada", "babbage", "davinci", "curie"] if m in model_name_or_path)
|
||||
|
||||
@ -1066,7 +1066,7 @@ class TestTokenLimit:
|
||||
with caplog.at_level(logging.WARNING):
|
||||
_ = prompt_node.prompt(prompt_template, documents=["Berlin is an amazing city."])
|
||||
assert "The prompt has been truncated from 812 tokens to 412 tokens" in caplog.text
|
||||
assert "and answer length (100 tokens) fits within the max token limit (512 tokens)." in caplog.text
|
||||
assert "and answer length (100 tokens) fit within the max token limit (512 tokens)." in caplog.text
|
||||
|
||||
@pytest.mark.integration
|
||||
@pytest.mark.skipif(
|
||||
@ -1079,7 +1079,7 @@ class TestTokenLimit:
|
||||
with caplog.at_level(logging.WARNING):
|
||||
_ = prompt_node.prompt(tt, documents=["Berlin is an amazing city."])
|
||||
assert "The prompt has been truncated from" in caplog.text
|
||||
assert "and answer length (2000 tokens) fits within the max token limit (2049 tokens)." in caplog.text
|
||||
assert "and answer length (2000 tokens) fit within the max token limit (2049 tokens)." in caplog.text
|
||||
|
||||
|
||||
class TestRunBatch:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user