Rag.VectorStore.Store behaviour (rag v0.3.4)

View Source

Behaviour for vector store backends.

This behaviour defines the interface for different vector storage implementations (pgvector, Pinecone, Qdrant, etc.).

Implementing a Custom Store

defmodule MyApp.CustomVectorStore do
  @behaviour Rag.VectorStore.Store

  defstruct [:connection]

  @impl true
  def insert(store, documents, opts) do
    # Insert documents with embeddings
    {:ok, count}
  end

  @impl true
  def search(store, embedding, opts) do
    # Search by embedding similarity
    {:ok, results}
  end

  @impl true
  def delete(store, ids, opts) do
    # Delete by IDs
    {:ok, count}
  end

  @impl true
  def get(store, ids, opts) do
    # Get by IDs
    {:ok, documents}
  end
end

Available Implementations

Summary

Callbacks

Delete documents by IDs.

Get documents by IDs.

Insert documents with embeddings into the store.

Search for similar documents by embedding.

Functions

Delete documents from the store.

Get documents from the store by IDs.

Insert documents into the store.

Search the store by embedding.

Types

document()

@type document() :: %{
  id: any() | nil,
  content: String.t(),
  embedding: embedding(),
  source: String.t() | nil,
  metadata: map()
}

embedding()

@type embedding() :: [float()]

result()

@type result() :: %{
  id: any(),
  content: String.t(),
  score: float(),
  source: String.t() | nil,
  metadata: map()
}

Callbacks

delete(store, ids, opts)

@callback delete(store :: struct(), ids :: [any()], opts :: keyword()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Delete documents by IDs.

Parameters

  • store - The store struct
  • ids - List of document IDs to delete
  • opts - Options specific to the implementation

Returns

  • {:ok, count} - Number of documents deleted
  • {:error, reason} - Error during deletion

get(store, ids, opts)

@callback get(store :: struct(), ids :: [any()], opts :: keyword()) ::
  {:ok, [document()]} | {:error, term()}

Get documents by IDs.

Parameters

  • store - The store struct
  • ids - List of document IDs to retrieve
  • opts - Options specific to the implementation

Returns

  • {:ok, [document]} - List of documents
  • {:error, reason} - Error during retrieval

insert(store, documents, opts)

@callback insert(store :: struct(), documents :: [document()], opts :: keyword()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Insert documents with embeddings into the store.

Parameters

  • store - The store struct
  • documents - List of documents with embeddings
  • opts - Options specific to the implementation

Returns

  • {:ok, count} - Number of documents inserted
  • {:error, reason} - Error during insertion

search(store, embedding, opts)

@callback search(store :: struct(), embedding :: embedding(), opts :: keyword()) ::
  {:ok, [result()]} | {:error, term()}

Search for similar documents by embedding.

Parameters

  • store - The store struct
  • embedding - Query embedding vector
  • opts - Options including :limit

Returns

  • {:ok, [result]} - List of results with similarity scores
  • {:error, reason} - Error during search

Functions

delete(store, ids, opts \\ [])

@spec delete(struct(), [any()], keyword()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Delete documents from the store.

get(store, ids, opts \\ [])

@spec get(struct(), [any()], keyword()) :: {:ok, [document()]} | {:error, term()}

Get documents from the store by IDs.

insert(store, documents, opts \\ [])

@spec insert(struct(), [document()], keyword()) ::
  {:ok, non_neg_integer()} | {:error, term()}

Insert documents into the store.

search(store, embedding, opts \\ [])

@spec search(struct(), embedding(), keyword()) :: {:ok, [result()]} | {:error, term()}

Search the store by embedding.