embedding

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package embedding provides a unified, high-level API for computing text embeddings through the Aleph Alpha inference service.

Overview

The package exposes a single public entrypoint, Client, which hides all low-level HTTP details, endpoint paths, authentication, and model-specific behavior.

A client is constructed using:

client, err := embedding.NewClient(cfg)

Once created, the client can generate embeddings via:

client.Create(ctx, "model-name", "hello")

or batch embeddings via:

client.Create(ctx, "model-name", "a", "b", "c")

This package exclusively supports OpenAI-compatible embeddings via the /v1/embeddings endpoint.

Configuration

Configuration is sourced from environment variables and constructed by:

cfg := embedding.NewConfig()

Required variables:

  • EMBEDDING_ENDPOINT Base URL of the inference service (no trailing path or slash).

  • EMBEDDING_SERVICE_TOKEN Internal PHARIA service token for authentication.

Optional variables:

  • EMBEDDING_HTTP_TIMEOUT_SECONDS Request timeout (default: 30 seconds).

Configuration correctness can be verified via:

if err := cfg.Validate(); err != nil { ... }

Dependency Injection (Fx)

A ready-to-use Fx module is provided:

embedding.FXModule

which supplies:

  • *embedding.Config
  • *embedding.Client

and registers a lifecycle hook to clean up HTTP resources on shutdown.

Example:

app := fx.New(
    embedding.FXModule,
    fx.Invoke(func(c *embedding.Client) {
        // Use embeddings
    }),
)

Summary

The embedding package provides:

  • A clean, stable API for OpenAI-compatible embeddings.
  • A no-leak abstraction over the Aleph Alpha inference service.

Usage:

client := embedding.NewClient(cfg)
client.Create(ctx, "model-name", texts...)

Index

Constants

This section is empty.

Variables

FXModule wires the embedding system into Fx.

It provides:

  • Config (NewConfig)
  • Provider (via provider factory)
  • *Client (NewClient)
  • Lifecycle hook (RegisterEmbeddingLifecycle)

Functions

func RegisterEmbeddingLifecycle

func RegisterEmbeddingLifecycle(lc fx.Lifecycle, client *Client)

RegisterEmbeddingLifecycle ensures that the Client (and its provider) are properly cleaned up on application shutdown.

Types

type Client

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

Client is the public entrypoint for computing embeddings.

It hides all provider details (inference endpoints, HTTP, etc.) from the application layer.

func NewClient

func NewClient(cfg *Config) (*Client, error)

NewClient constructs a Client from Config. It validates the config and internally constructs the inference provider. Application code should depend on *Client, not on Provider or inferenceProvider.

func (*Client) Close

func (c *Client) Close() error

Close allows the client to release any internal resources used by the provider. Currently this is a no-op unless the provider implements Close().

func (*Client) Create

func (c *Client) Create(ctx context.Context, token, model string, texts ...string) ([][]float64, error)

Create executes an embedding request for one or more texts.

type Config

type Config struct {
	// Inference endpoint and auth
	Endpoint     string // Base URL of the Aleph Alpha inference API
	HTTPTimeoutS int    // HTTP timeout seconds (default 30)
}

func NewConfig

func NewConfig() *Config

NewConfig reads from environment variables.

func (*Config) Validate

func (c *Config) Validate() error

Validate ensures required fields are present.

type InferenceProvider

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

func (*InferenceProvider) Create

func (p *InferenceProvider) Create(ctx context.Context, token, model string, texts ...string) ([][]float64, error)

Create generates embeddings for the given texts using the specified model. It uses the OpenAI-compatible /v1/embeddings endpoint.

type Provider

type Provider interface {
	// Create generates embeddings for the given texts using the specified model.
	Create(ctx context.Context, token, model string, texts ...string) ([][]float64, error)
}

Provider contract

Jump to

Keyboard shortcuts

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