Pipelines

Pipelines define the schema for the transformation of documents. Different Pipelines can be used for different tasks.

Defining Schema

New Pipelines require schema. Here are a few examples of variations of schema along with common use cases.

For the following section we will assume we have documents that have the structure:

content_copy link edit
{
"id": "Each document has a unique id",
"title": "Each document has a title",
"body": "Each document has some body text"
}

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline", {
title: {
full_text_search: { configuration: "english" },
},
body: {
splitter: { model: "recursive_character" },
semantic_search: {
model: "hkunlp/instructor-base",
parameters: {
instruction: "Represent the Wikipedia document for retrieval: ",
}
},
},
});

content_copy link edit
pipeline = Pipeline(
"test_pipeline",
{
"title": {
"full_text_search": {"configuration": "english"},
},
"body": {
"splitter": {"model": "recursive_character"},
"semantic_search": {
"model": "hkunlp/instructor-base",
"parameters": {
"instruction": "Represent the Wikipedia document for retrieval: ",
},
},
},
},
)

This Pipeline does two things. For each document in the Collection, it converts all titles into tsvectors enabling full text search, and splits and embeds the body text enabling semantic search using vectors. This kind of Pipeline would be great for site search utilizing hybrid keyword and semantic search.

For a more simple RAG use case, the following Pipeline would work well.

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline", {
body: {
splitter: { model: "recursive_character" },
semantic_search: {
model: "hkunlp/instructor-base",
parameters: {
instruction: "Represent the Wikipedia document for retrieval: ",
}
},
},
});

content_copy link edit
pipeline = Pipeline(
"test_pipeline",
{
"body": {
"splitter": {"model": "recursive_character"},
"semantic_search": {
"model": "hkunlp/instructor-base",
"parameters": {
"instruction": "Represent the Wikipedia document for retrieval: ",
},
},
},
},
)

This Pipeline splits and embeds the body text enabling semantic search using vectors. This is a very popular Pipeline for RAG.

We support most every open source model on Hugging Face, and OpenAI's embedding models. To use a model from OpenAI specify the source as openai, and make sure and set the environment variable OPENAI_API_KEY.

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline", {
body: {
splitter: { model: "recursive_character" },
semantic_search: {
model: "text-embedding-ada-002",
source: "openai"
},
},
});

content_copy link edit
pipeline = Pipeline(
"test_pipeline",
{
"body": {
"splitter": {"model": "recursive_character"},
"semantic_search": {"model": "text-embedding-ada-002", "source": "openai"},
},
},
)

Customizing the Indexes

By default the SDK uses HNSW indexes to efficiently perform vector recall. The default HNSW index sets m to 16 and ef_construction to 64. These defaults can be customized in the Pipeline schema. See pgvector for more information on vector indexes.

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline", {
body: {
splitter: { model: "recursive_character" },
semantic_search: {
model: "intfloat/e5-small",
hnsw: {
m: 100,
ef_construction: 200
}
},
},
});

content_copy link edit
pipeline = Pipeline(
"test_pipeline",
{
"body": {
"splitter": {"model": "recursive_character"},
"semantic_search": {
"model": "intfloat/e5-small",
"hnsw": {"m": 100, "ef_construction": 200},
},
},
},
)

Adding Pipelines to a Collection

The first time a Pipeline is added to a Collection it will automatically chunk and embed any documents already in that Collection.

content_copy link edit
await collection.add_pipeline(pipeline)

content_copy link edit
await collection.add_pipeline(pipeline)

Note: After a Pipeline has been added to a Collection instances of the Pipeline object can be created without specifying a schema:

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline")

content_copy link edit
pipeline = Pipeline("test_pipeline")

Searching with Pipelines

There are two different forms of search that can be done after adding a Pipeline to a Collection

See their respective pages for more information on searching.

Disable a Pipeline

Pipelines can be disabled or removed to prevent them from running automatically when documents are upserted.

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline")
const collection = pgml.newCollection("test_collection")
await collection.disable_pipeline(pipeline)

content_copy link edit
pipeline = Pipeline("test_pipeline")
collection = Collection("test_collection")
await collection.disable_pipeline(pipeline)

Disabling a Pipeline prevents it from running automatically, but leaves all tsvectors, chunks, and embeddings already created by that Pipeline in the database.

Enable a Pipeline

Disabled Pipelines can be re-enabled.

content_copy link edit
const pipeline = pgml.newPipeline("test_pipeline")
const collection = pgml.newCollection("test_collection")
await collection.enable_pipeline(pipeline)

content_copy link edit
pipeline = Pipeline("test_pipeline")
collection = Collection("test_collection")
await collection.enable_pipeline(pipeline)

Enabling a Pipeline will cause it to automatically run on all documents it may have missed while disabled.

Remove a Pipeline

const pipeline = pgml.newPipeline("test_pipeline")
const collection = pgml.newCollection("test_collection")
await collection.remove_pipeline(pipeline)

content_copy link edit
pipeline = Pipeline("test_pipeline")
collection = Collection("test_collection")
await collection.remove_pipeline(pipeline)

Removing a Pipeline deletes it and all associated data from the database. Removed Pipelines cannot be re-enabled but can be recreated.