Create a Custom Component
Create components tailored specifically to your use case and use them in your pipelines.
About This Task
A component is a Python code snippet that follows our template and performs a specific task on your data. When you create and upload a custom component to deepset Cloud, it becomes accessible to your entire organization. Any member can then use the component in their pipelines.
Custom components are based on Haystack components. Haystack is deepset's open source AI framework, which also powers deepset Cloud. To learn more, visit the Haystack website.
We provide a template for creating your custom components, which are available as a GitHub repository. This template serves as a custom components library for your organization. Components created in the ./dc-custom-component-template/src/dc_custom_component/example_components/
folder and imported into deepset Cloud are the components you can use in your pipelines. Only the components present in the most recently uploaded template are available for use.
For example, if someone in your organization creates a component called WelcomeTextGenerator
and uploads it to deepset Cloud, everyone in the organization can use it. However, if later someone adds two new components, GoodbyeTextGenerator
and CharacterSplitter
, and deletes WelcomeTextGenerator
, only the new components will be available to use in your pipelines. WelcomeTextGenerator
will no longer be accessible.
Currently, you can't delete custom components.
Prerequisites
- Read the following resources before completing this task:
- You should have a basic understanding of working with GitHub repositories.
- Generate a deepset Cloud API key to upload the created component to your workspace. For instructions, see Generate an API Key.
Create a Component
Prepare the Template
- Fork the dc-custom-component-template GitHub repository. This will let you version control your changes.
- Navigate to the directory where you cloned the repository and open the
./dc-custom-component-template/src/dc_custom_component/example_components/
directory. Thepreprocessors
andrankers
folders are examples you can modify or remove as needed. - Create a new folder or rename one of the example folders to match your custom component's name and open it. There's a
.py
file inside. This is where you'll write your component code. You can rename this file as well.
Example:
To create a custom component calledWelcomeTextGenerator
:- Rename
./dc-custom-component-template/src/dc_custom_component/example_components/preprocessors
to./dc-custom-component-template/src/dc_custom_component/components/generators
. - Open the
generators
folder and rename the example filecharacter_splitter.py
towelcome_text_generator.py
. - Delete the
rankers
folder if it's not needed. If you're creating multiple components, use the folder structure to keep them organized.
Note: You can create all your components in one.py
file or you can have a separate folder with a.py
file in it for each custom component. That's up to you.Component Folder as Group Name
The folder name containing your component code becomes the component group name in Pipeline Builder. For example, if you place your component in
./dc-custom-component-template/src/dc_custom_component/components/generators
, it will appear in the Generators group in the Pipeline Builder component library.
- Rename
Set Up a Virtual Environment
Creating a virtual environment isolates your project's dependencies from other Python projects and your system Python installation.
The template uses Hatch, a Python project manager, to set up virtual environments. For details, see the Hatch website.
- Install Hatch by running:
pip install hatch
. This installs all the necessary packages, including pytest. - Create a virtual environment by running:
hatch shell
.
Implement the Component
- Write the component code in the
.py
file. Use the recipe below as a starting point:
- If your component has dependencies, add them in the
./dc-custom-component-template/pyproject.toml
file in thedependencies
section:
dependencies = [
"haystack-ai>=2.0.0"
]
Note: Do not modify versions of dependencies already listed in this file.
- From the project root directory, run the
hatch run code-quality:all
command to format your code. - Update the component version in the
./dc-custom-component-template/src/dc_custom_component/__about__.py
file. You can specify version numbers in any way you like, but we suggest you adopt the major.minor.micro format, for example, 1.1.0. Have a look at Hatch versioning for guidelines.
The version number applies to all components. Even if you have multiple components, you only need to specify one version number.
Components Connecting to Third-Party Providers
If your component connects to a third-party provider that requires authentication, store the API key in an environment variable and then retrieve it in the component's run()
method.
The following component shows how to do this. First, you add the env_var_name
init parameter and then you retrieve this value in the run()
method using os.getenv(self.env_var_name)
. The retrieved value is stored in loaded_api_key
.
@component
class CustomComponent:
def __init__(self, env_var_name: str = "API_KEY"):
self.env_var_name = env_var_name
@component.output_types(answers=List[GeneratedAnswer])
def run(self, query: str) -> Dict[str, List[GeneratedAnswer]]:
loaded_api_key = os.getenv(self.env_var_name)
# use the key
pass
Test Your Component
When you upload your component to deepset Cloud, we verify the structure and version of the uploaded .zip file. We recommend that you test your component before uploading.
- Add unit and integration tests in the
./dc-custom-component-template/tests
folder to ensure everything works fine. - Run your tests using:
hatch run tests
. If the tests pass, your component is ready.
Import the Component to deepset Cloud
There are two ways to import your component:
- Through REST API (supported for all systems)
- Using commands (currently supported for Linux and macOS, support for Windows is coming soon)
Import with REST API:
- Zip the repository from the template folder. The zipped repository should contain the same files in the same hierarchy as the
dc-custom-component-template
repository.
zip -r ../custom_component.zip ./*
Compress-Archive -Path .\* -DestinationPath ..\custom_component.zip -Force
This command creates a zip file called custom_component.zip
in the parent directory.
- Upload the .zip file to deepset Cloud using the Import Custom Components endpoint. Here's a sample code you can use as a starting point for your request:
curl
--request POST \
--url https://api.cloud.deepset.ai/api/v2/custom_components \
--header 'accept: application/json' \
--header 'Authorization: Bearer api_XXX' \
--form 'file=@"/path/to/custom/component/custom_component.zip";type=application/zip'
Import with Commands
This method works only on Linux and macOS systems.
- Set your deepset Cloud API key:
export API_KEY=<YOUR_API_KEY>
- From within this project, run the following command to upload your custom component:
hatch run dc:build-and-push
This command creates a .zip file called custom_component.zip
in the dist
directory and uploads it to deepset Cloud.
Verify the Import
Check the component status using the Get Custom Components endpoint. If the status is finished
, your component is ready for use in your pipelines. Here is a sample code you can use:
curl --request GET \
--url 'https://api.cloud.deepset.ai/api/v2/custom_components?limit=10&page_number=1&field=created_at&order=DESC' \
--header 'accept: application/json'
--header 'Authorization: Bearer api_XXX'
It takes a while for the custom component to be available in Pipeline Builder. Refresh the page and wait a couple of minutes.
Add the Component to Your Pipeline
-
Open the pipeline where you want to add the component in Pipeline builder. (On the Pipelines page, click More actions next to the pipeline and choose Edit.)
-
In the components library, expand the component group that contains your component.
Tip: The group's name is the same as the name of the template folder where you saved your component. For example, if your component code is in./dc-custom-component-template/src/dc_custom_component/rankers/my_ranker.py
, you'll find it in the Rankers group. -
Drag your component onto the canvas and configure its parameters and connections with other components.
Update a Component
You update custom components by uploading their new version to deepset Cloud:
- Pull the latest version of the dc-custom-component-template repository.
- Update the component code in the
./dc-custom-component-template/src/dc_custom_component/components/<your_components_folder>/<custom_component>.py
file. - Update the component version in the
./dc-custom-component-template/src/dc_custom_component/__about__.py
file.
How do I check the current version?
If you don't know what version is currently uploaded to deepset Cloud, use the Get Custom Components endpoint. Version is listed in the
version
parameter of a successful response.
- If the component has any dependencies, add them in the
dependencies
section of the./dc-custom-component-template/pyproject.toml
file. Do not modify versions of dependencies already listed in this file. - Upload the updated component with one of these methods:
- With REST API: Zip the repository and upload it using the Import Custom Components endpoint.
- With a command (currently only supported for Linux and macOS):
- Set your deepset Cloud API key:
export API_KEY=<YOUR_API_KEY>
hatch run dc:build-and-push
. This creates acustom_component.zip
file and uploads it to deepset Cloud.
- Set your deepset Cloud API key:
- Check the upload status using the Get Custom Components endpoint.
All new pipelines automatically use the latest version of your custom components. However, running pipelines continue to use the version that was current when they were deployed. To update the component version in running pipelines, undeploy and redeploy them.
Compare Different Versions
To evaluate which version of your component performs better in a pipeline, you can upload two versions of the component simultaneously, each with a unique name.
- Pull the latest version of the dc-custom-component-template repository.
- Add two versions of the component to the
./dc-custom-component-template/src/dc_custom_component/components/<your_component_folder>/<your_component_name>.py
file, treating them as separate components and giving each version a distinct name. - Update the component version in the
./dc-custom-component-template/src/dc_custom_component/__about__.py
file. - Zip the repository and upload it with one of these methods:
- With REST API: Zip the repository and upload it using the Import Custom Components endpoint.
- With a command (currently only supported for Linux and macOS):
- Set your deepset Cloud API key:
export API_KEY=<YOUR_API_KEY>
hatch run dc:build-and-push
. This creates acustom_component.zip
file and uploads it to deepset Cloud.
- Set your deepset Cloud API key:
- Check the upload status using the Get Custom Component endpoint.
Now, you can create two pipelines—one using the first version and another using the second—to compare their performance.
Troubleshooting Custom Components
If you're having issues with your component, check the pipeline logs for details. To access pipeline logs:
-
Go to Pipelines and click the pipeline you want to troubleshoot.
-
Click Logs on the Pipeline Details page to view all information messages, warnings, and errors your pipeline produced.
Updated 3 days ago