> ## Documentation Index
> Fetch the complete documentation index at: https://docs.blobhub.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Vector Database

> The components that write text chunks to a vector database and search them, and the Pinecone provider.

The vector database family stores text chunks in a vector index and finds the ones closest to a query. Two components do
the work, `database.vector.ingest` and `database.vector.retrieve`. Neither talks to a database itself: each takes a
provider on its `database` input, and the provider makes the call. The one provider today is `database.vector.pinecone`.

```mermaid theme={null}
graph LR
    Chunk[text.chunk] --> Ingest[database.vector.ingest]
    Ingest --> Index[(Pinecone index)]
    Query[query message] --> Retrieve[database.vector.retrieve]
    Index --> Retrieve
    Retrieve --> Hits[chunks]
    Pinecone[database.vector.pinecone] -.->|database| Ingest
    Pinecone -.->|database| Retrieve
```

A consumer's `database` input takes the interface `database.vector.inference`, which `database.vector.pinecone` serves.
It is required. A definition with nothing wired to it fails [its
check](https://docs.blobhub.io/blob-types/workflow/operations/check-definition.md) with ``Port `database` (category: `provider_input`) is
required``. If the connection is missing when the step runs anyway, the step posts a `component.error` event, `Provider
"database" is not connected`, and takes its `failure` route.

### Wiring a provider

A provider joins a consumer through a connection written from the consumer: its `source` is the consumer, with
`source_port` `provider_input`, and its `target` is the provider, with `target_port` `provider_reference`. Its `route`
is the name of the consumer's provider input, `database` for both consumers here. The consumer finds its provider by
that route, so a connection with any other route leaves `database` unconnected and the step posts `Provider "database"
is not connected`.

## `database.vector.ingest`

Writes a `messages` value to the index as text records. Each chunk becomes one record: its `_id` is the chunk's
`attributes.id`, or a new id when the chunk has none, and its text sits under the field named by `text_field`. Chunks
are sent in batches of 96, and the step posts an output event for the total and for each batch. Wire the chunks of a
`text.chunk` here, or any `messages` whose messages carry text.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `namespace` | Data input | `message` | Required. The text of the message names the Pinecone namespace to write to. |
| `chunks` | Data input | `messages` | Required. The chunks to write. Each message must hold a `text` block. |
| `text_field` | Data input | `text`, default `text` | Required. The record field that holds a chunk's text. |
| `database` | Provider input | `database.vector.inference` | Required. A `database.vector.pinecone`. |
| `success` | Flow output | — | Every batch was accepted. |
| `failure` | Flow output | — | A batch was refused, the call could not be made, or `chunks` is empty. |

A batch the index refuses stops the step: it posts `Failed to ingest batch` and a `component.error` event carrying
Pinecone's own error message, and takes `failure`. The batches sent before it stay in the index, and no later batch is
sent. An empty `chunks` sends nothing, and the step takes `failure`. A chunk with no `text` block, a missing credential
or a request that fails without a reply also end in `failure`.

## `database.vector.retrieve`

Searches the index with a text query and returns the closest chunks as a `messages` value. The editor turns reranking on
for a new component.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `namespace` | Data input | `message` | Required. The text of the message names the namespace to search. |
| `query_text` | Data input | `message` | Required. The text of the message is the query. |
| `query_fields` | Data input | `text`, default `text` | Required. The record fields to return, separated by commas. |
| `query_top_k` | Data input | `integer` | Required. How many hits the search returns. |
| `rerank_enabled` | Data input | `boolean`, default `true` | Whether Pinecone reranks the hits. A definition that leaves it out does not rerank. |
| `rerank_model` | Data input | `text`, default `pinecone-rerank-v0` | The reranking model. |
| `rerank_top_n` | Data input | `integer` | How many hits reranking keeps. With reranking on and this unset, the request carries `"top_n": null`. Ignored with reranking off. |
| `rerank_fields` | Data input | `text`, default `text` | Required in a definition, whether or not reranking is on: a step with no `rerank_fields` port fails with no event. The record fields reranking reads, separated by commas. |
| `chunks` | Data output | `messages` | Required. The hits, in the order Pinecone returns them. |
| `database` | Provider input | `database.vector.inference` | Required. A `database.vector.pinecone`. |
| `success` | Flow output | — | The search succeeded, with or without hits. The step posts `Data retrieved successfully`. |
| `failure` | Flow output | — | The search was refused, or a hit lacks a requested field. |

With `rerank_enabled` true, the search asks Pinecone to rerank with `rerank_model`, keeping `rerank_top_n` hits and
reading `rerank_fields`. With it false, or left out of the definition, none of the `rerank_*` values is sent and every
hit comes back in Pinecone's order. The step splits `rerank_fields` on commas whenever it runs, so a definition always
carries that port, with reranking on or off.

Each hit becomes one message in `chunks`:

* Its `role` is `database`, and its `attributes.id` is the id of the record.
* Its `metadata` holds the hit's `score`.
* Its `content` holds one `text` block per name in `query_fields`, in that order, each with the field's value. A hit
  that lacks one of the fields fails the step.

A search with no hits is a success with an empty `chunks`. A refused search posts a `component.error` event carrying
Pinecone's error message and takes `failure`; `chunks` is not written.

## `database.vector.pinecone`

The provider for a Pinecone index. It calls Pinecone's index API, at
`https://<index_host>/records/namespaces/<namespace>`, with `upsert` for an ingest and `search` for a retrieve. Both use
the namespace of the consumer. The request carries the credential's key as `Api-Key` and `api_version` as
`X-Pinecone-API-Version`. A request waits at most 5 seconds to connect and 60 seconds for a reply.

| Route | Category | Value | Notes |
| :- | :- | :- | :- |
| `credential` | Data input | `credential` (`api_key`) | Required. An `api_key` credential whose key Pinecone accepts for the index ([credential value](https://docs.blobhub.io/blob-types/workflow/workflows/data-types.md#credential), [creating one](https://docs.blobhub.io/general/credentials.md)). |
| `index_host` | Data input | `message` | Required. The text of the message is the host of the index, without `https://`, which the provider adds. |
| `api_version` | Data input | `text`, default `2025-01` | Required. The Pinecone API version the requests ask for. |

A consumer wired to a provider whose `credential` names an alias that does not exist posts a `component.error` event,
`"<alias>" credential alias not found`, and takes `failure`.

### Example: a knowledge base for Corvel Freight

Corvel Freight keeps its carrier handbook in a Pinecone index. One flow reads the handbook, runs it through a
`text.chunk`, and a `database.vector.ingest` writes the chunks to the namespace `carrier-handbook` with the default
`text_field`. A second flow takes a driver's question and a `database.vector.retrieve` searches that namespace with
`query_top_k` set to 20, and with `rerank_top_n` set to 5 to keep the best five. Both consumers are wired to the same
`database.vector.pinecone`, whose `index_host` is `carrier-handbook-4f2a1c9.svc.aped-4627-b74a.pinecone.io` and whose
`credential` is `pinecone-prod`. The five messages in `chunks` go to a `genai.chat` as the context of the answer.

The retrieve side, wired to the provider, looks like this. The credential `pinecone-prod` is an `api_key` credential of
the organization.

```json theme={null}
{
  "components": [
    {"id": "search_handbook", "category": "processor", "type": "database.vector.retrieve", "name": "Search handbook",
     "position": {"x": 200, "y": 0},
     "ports": [
       {"category": "data_input", "route": "namespace",
        "value": {"type": "message", "message": {"role": "user", "content": [
          {"type": "text", "text": "carrier-handbook"}]}}},
       {"category": "data_input", "route": "query_text",
        "value": {"type": "message", "message": {"role": "user", "content": [
          {"type": "text", "text": "How long may a driver wait at a dock?"}]}}},
       {"category": "data_input", "route": "query_fields", "value": {"type": "text", "text": "text"}},
       {"category": "data_input", "route": "query_top_k", "value": {"type": "integer", "integer": 20}},
       {"category": "data_input", "route": "rerank_enabled", "value": {"type": "boolean", "flag": true}},
       {"category": "data_input", "route": "rerank_model", "value": {"type": "text", "text": "pinecone-rerank-v0"}},
       {"category": "data_input", "route": "rerank_top_n", "value": {"type": "integer", "integer": 5}},
       {"category": "data_input", "route": "rerank_fields", "value": {"type": "text", "text": "text"}}
     ]},
    {"id": "pinecone", "category": "provider", "type": "database.vector.pinecone", "name": "Handbook index",
     "position": {"x": 200, "y": 200},
     "ports": [
       {"category": "data_input", "route": "credential", "value": {"type": "credential", "alias": "pinecone-prod"}},
       {"category": "data_input", "route": "api_version", "value": {"type": "text", "text": "2025-01"}},
       {"category": "data_input", "route": "index_host",
        "value": {"type": "message", "message": {"role": "user", "content": [
          {"type": "text", "text": "carrier-handbook-4f2a1c9.svc.aped-4627-b74a.pinecone.io"}]}}}
     ]}
  ],
  "connections": [
    {"id": "c1", "source_id": "search_handbook", "target_id": "pinecone", "route": "database",
     "source_port": "provider_input", "target_port": "provider_reference"}
  ]
}
```

The flow connections that start `Search handbook` and follow its `success` and `failure` routes, and the `chunks` output
wired to a data component, are left out.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.