Rag.VectorStore.Chunk (rag v0.3.4)

View Source

Ecto schema for storing text chunks with embeddings.

Each chunk represents a piece of text from a source document along with its vector embedding for semantic search.

Fields

  • content - The text content of the chunk
  • source - The source file or document path
  • embedding - The vector embedding (768 dimensions for Gemini)
  • metadata - Additional metadata as a map

Examples

chunk = Chunk.new(%{
  content: "def hello, do: :world",
  source: "lib/greeting.ex",
  metadata: %{line_start: 1, line_end: 1}
})

Summary

Functions

Creates a changeset for inserting or updating a chunk.

Creates a changeset for updating only the embedding.

Creates a new Chunk struct from the given attributes.

Converts a Chunk struct to a plain map.

Types

t()

@type t() :: %Rag.VectorStore.Chunk{
  __meta__: term(),
  content: String.t(),
  embedding: [float()] | nil,
  id: integer() | nil,
  inserted_at: NaiveDateTime.t() | nil,
  metadata: map(),
  source: String.t() | nil,
  updated_at: NaiveDateTime.t() | nil
}

Functions

changeset(chunk, attrs)

@spec changeset(t(), map()) :: Ecto.Changeset.t()

Creates a changeset for inserting or updating a chunk.

Validates that content is present and not blank.

Examples

iex> Chunk.changeset(%Chunk{}, %{content: "Hello", source: "test.ex"})
#Ecto.Changeset<...>

embedding_changeset(chunk, attrs)

@spec embedding_changeset(t(), map()) :: Ecto.Changeset.t()

Creates a changeset for updating only the embedding.

Use this when adding embeddings to existing chunks.

Examples

iex> Chunk.embedding_changeset(chunk, %{embedding: [0.1, 0.2, ...]})
#Ecto.Changeset<...>

new(attrs)

@spec new(map()) :: t()

Creates a new Chunk struct from the given attributes.

Parameters

  • attrs - Map with :content (required), :source, :embedding, :metadata

Examples

iex> Chunk.new(%{content: "Hello", source: "test.ex"})
%Chunk{content: "Hello", source: "test.ex", metadata: %{}}

to_map(chunk)

@spec to_map(t()) :: map()

Converts a Chunk struct to a plain map.

Useful for vector store operations that expect maps.

Examples

iex> Chunk.to_map(%Chunk{content: "Hi", source: "t.ex"})
%{content: "Hi", source: "t.ex", embedding: nil, metadata: %{}}