embedder

package
v0.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package embedder provides implementations of the rag.Embedder interface for converting text into dense vector embeddings. Each implementation talks to a different backend (OpenAI, Azure OpenAI, Ollama) via plain HTTP — no additional SDK dependencies are required.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDimensions

func DefaultDimensions(backend string) int

DefaultDimensions returns the correct default embedding vector size for the given backend name. Callers that need to pre-configure a vector store (e.g. Qdrant collection creation) should use this rather than hardcoding a value. EMBEDDING_DIMENSIONS always takes precedence when set.

func NewFromEnv

func NewFromEnv() (rag.Embedder, error)

NewFromEnv constructs a rag.Embedder using cascading defaults that inherit from the chat provider configuration when embedding-specific overrides are not set.

Resolution order:

  1. EMBEDDING_PROVIDER — if unset, inherits MODEL_PROVIDER (default: ollama)
  2. Per-backend credentials are inherited from the chat provider's env vars
  3. EMBEDDING_MODEL — overrides the default model for the resolved backend
  4. EMBEDDING_API_KEY — overrides the inherited API key
  5. EMBEDDING_ENDPOINT — overrides the inherited endpoint
  6. EMBEDDING_DIMENSIONS — overrides the default dimensions (ollama: 768, openai/azure: 1536)

func ValidateForRAG

func ValidateForRAG(log *slog.Logger) error

ValidateForRAG checks that the embedder configuration is safe to use when QDRANT_HOST is set. It returns an error if the configuration is clearly broken (e.g. azure embedder with no API key), and logs a warning if EMBEDDING_MODEL looks like a chat model rather than an embedding model.

This is a pre-flight check — call it before constructing the embedder or the Qdrant store so operators get a clear error at startup rather than a cryptic failure during the first embed call.

Types

type OllamaConfig

type OllamaConfig struct {
	// Host is the Ollama server base URL (e.g. "http://localhost:11434").
	Host string
	// Model is the embedding model name (e.g. "nomic-embed-text").
	Model string
}

OllamaConfig holds the settings for constructing an OllamaEmbedder.

type OllamaEmbedder

type OllamaEmbedder struct {
	// contains filtered or unexported fields
}

OllamaEmbedder implements rag.Embedder using the Ollama /api/embed endpoint. It is safe for concurrent use. No API key is required — Ollama runs locally.

func NewOllamaEmbedder

func NewOllamaEmbedder(cfg *OllamaConfig) *OllamaEmbedder

NewOllamaEmbedder constructs an OllamaEmbedder from the given config.

func (*OllamaEmbedder) Embed

func (e *OllamaEmbedder) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed converts a batch of texts into their corresponding embeddings. The returned slice is parallel to the input slice.

type OpenAIConfig

type OpenAIConfig struct {
	// BaseURL is the API base URL. For OpenAI: "https://api.openai.com/v1".
	// For Azure: "https://<resource>.openai.azure.com/openai".
	BaseURL string
	// APIKey is the authentication key.
	APIKey string
	// Model is the embedding model name (e.g. "text-embedding-3-small").
	Model string
	// Dimensions is the desired vector length (0 = model default).
	Dimensions int
	// Azure enables Azure OpenAI mode (api-key header + api-version param).
	Azure bool
	// APIVersion is the Azure OpenAI API version (e.g. "2025-04-01-preview").
	// Ignored when Azure is false.
	APIVersion string
}

OpenAIConfig holds the settings for constructing an OpenAIEmbedder.

type OpenAIEmbedder

type OpenAIEmbedder struct {
	// contains filtered or unexported fields
}

OpenAIEmbedder implements rag.Embedder using the OpenAI (or Azure OpenAI) embeddings REST API. It is safe for concurrent use.

func NewOpenAIEmbedder

func NewOpenAIEmbedder(cfg *OpenAIConfig) *OpenAIEmbedder

NewOpenAIEmbedder constructs an OpenAIEmbedder from the given config.

func (*OpenAIEmbedder) Embed

func (e *OpenAIEmbedder) Embed(ctx context.Context, texts []string) ([][]float32, error)

Embed converts a batch of texts into their corresponding embeddings. The returned slice is parallel to the input slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL