OutputAdapter
This component helps the output of one component fit smoothly into the input of another. It uses Jinja expressions to define how this adaptation occurs.
Name | OutputAdapter |
Folder path | /converters/ |
Most common position in a pipeline | Flexible |
Mandatory input variables | “**kwargs”: Input variables to be used in Jinja expression |
Output variables | The output is specified under the “output” key dictionary |
Overview
To use OutputAdapter
, you need to specify the adaptation rule that includes:
template
: A Jinja template string that defines how to adapt the input data.output_type
: The type of the output data (such asstr
,List[int]
..).custom_filters
: An optional dictionary of custom Jinja filters to be used in the template.
Usage
On its own
This component is primarily meant to be used in pipelines.
In this example, OutputAdapter
simply outputs the content field of the first document in the arrays of documents:
from haystack import Document
from haystack.components.converters import OutputAdapter
adapter = OutputAdapter(template="{{ documents[0].content }}", output_type=str)
input_data = {"documents": [Document(content="Test content")]}
expected_output = {"output": "Test content"}
assert adapter.run(**input_data) == expected_output
In a pipeline
The example below demonstrates a straightforward pipeline that uses the OutputAdapter
to capitalize the first document in the list. If needed, you can also utilize the predefined Jinja filters.
from haystack import Pipeline, component, Document
from haystack.components.converters import OutputAdapter
@component
class DocumentProducer:
@component.output_types(documents=dict)
def run(self):
return {"documents": [Document(content="haystack")]}
pipe = Pipeline()
pipe.add_component(
name="output_adapter",
instance=OutputAdapter(template="{{ documents[0].content | capitalize}}", output_type=str),
)
pipe.add_component(name="document_producer", instance=DocumentProducer())
pipe.connect("document_producer", "output_adapter")
result = pipe.run(data={})
assert result["output_adapter"]["output"] == "Haystack"
You can also define your own custom filters, which can then be added to an OutputAdapter
instance through its init method and used in templates. Here’s an example of this approach:
from haystack import Pipeline, component, Document
from haystack.components.converters import OutputAdapter
def reverse_string(s):
return s[::-1]
@component
class DocumentProducer:
@component.output_types(documents=dict)
def run(self):
return {"documents": [Document(content="haystack")]}
pipe = Pipeline()
pipe.add_component(
name="output_adapter",
instance=OutputAdapter(template="{{ documents[0].content | reverse_string}}",
output_type=str,
custom_filters={"reverse_string": reverse_string}))
pipe.add_component(name="document_producer", instance=DocumentProducer())
pipe.connect("document_producer", "output_adapter")
result = pipe.run(data={})
assert result["output_adapter"]["output"] == "kcatsyah"
Updated 6 months ago