Rag.Embedding.Service (rag v0.3.4)

View Source

GenServer-based embedding service for batch processing.

This service manages embedding generation using the Gemini provider (the only provider with embedding support). It handles batching, statistics tracking, and integration with the VectorStore.

Usage

# Start the service
{:ok, pid} = Service.start_link(batch_size: 100)

# Embed a single text
{:ok, embedding} = Service.embed_text(pid, "Hello world")

# Embed multiple texts
{:ok, embeddings} = Service.embed_texts(pid, ["text1", "text2"])

# Embed chunks and add embeddings
{:ok, chunks_with_embeddings} = Service.embed_chunks(pid, chunks)

Configuration

  • :batch_size - Maximum texts per embedding request (default: 100)
  • :provider - Provider module to use (default: Rag.Ai.Gemini)

Summary

Functions

Returns a specification to start this module under a supervisor.

Embed chunks and prepare for database insertion.

Embed chunks and add embeddings to them.

Generate embedding for a single text.

Generate embeddings for multiple texts.

Get embedding service statistics.

Start the embedding service.

Types

t()

@type t() :: %Rag.Embedding.Service{
  batch_size: pos_integer(),
  provider: module(),
  provider_instance: struct(),
  stats: map()
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

embed_and_prepare(server, chunks)

@spec embed_and_prepare(GenServer.server(), [Rag.VectorStore.Chunk.t()]) ::
  {:ok, [map()]} | {:error, term()}

Embed chunks and prepare for database insertion.

Similar to embed_chunks/2 but converts chunks to maps ready for Ecto insert.

embed_chunks(server, chunks)

@spec embed_chunks(GenServer.server(), [Rag.VectorStore.Chunk.t()]) ::
  {:ok, [Rag.VectorStore.Chunk.t()]} | {:error, term()}

Embed chunks and add embeddings to them.

Extracts content from each chunk, generates embeddings, and returns chunks with embeddings attached.

Examples

iex> chunks = [%Chunk{content: "Hello"}, %Chunk{content: "World"}]
iex> Service.embed_chunks(pid, chunks)
{:ok, [%Chunk{content: "Hello", embedding: [...]}, ...]}

embed_text(server, text)

@spec embed_text(GenServer.server(), String.t()) ::
  {:ok, [float()]} | {:error, term()}

Generate embedding for a single text.

Examples

iex> Service.embed_text(pid, "Hello world")
{:ok, [0.1, 0.2, ...]}

embed_texts(server, texts)

@spec embed_texts(GenServer.server(), [String.t()]) ::
  {:ok, [[float()]]} | {:error, term()}

Generate embeddings for multiple texts.

Automatically batches large requests according to batch_size.

Examples

iex> Service.embed_texts(pid, ["Hello", "World"])
{:ok, [[0.1, ...], [0.2, ...]]}

get_stats(server)

@spec get_stats(GenServer.server()) :: map()

Get embedding service statistics.

Returns counts of texts embedded, batches processed, and errors.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the embedding service.

Options

  • :name - Optional process name
  • :batch_size - Maximum texts per batch (default: 100)
  • :provider - Provider module (default: Rag.Ai.Gemini)