embedding

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0, MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrImagesUnsupported = errors.New("embedding model does not accept images")

ErrImagesUnsupported is returned when an embedder is asked for an image vector its model or endpoint cannot produce.

Functions

This section is empty.

Types

type Config

type Config struct {
	Source                    types.ModelSource `json:"source"`
	BaseURL                   string            `json:"base_url"`
	ModelName                 string            `json:"model_name"`
	APIKey                    string            `json:"api_key"`
	TruncatePromptTokens      int               `json:"truncate_prompt_tokens"`
	Dimensions                int               `json:"dimensions"`
	SupportsDimensionOverride bool              `json:"supports_dimension_override"`
	ModelID                   string            `json:"model_id"`
	Provider                  string            `json:"provider"`
	// MaxConcurrency caps concurrent background calls to this model; 0 falls
	// back to the process-wide default (see limiter.GateN).
	MaxConcurrency int                      `json:"max_concurrency"`
	Spec           *types.ModelSpecOverride `json:"spec,omitempty"`
	ExtraConfig    map[string]string        `json:"extra_config"`
	// CustomHeaders 允许在调用远程 API 时附加自定义 HTTP 请求头(类似 OpenAI Python SDK 的 extra_headers)。
	CustomHeaders map[string]string `json:"custom_headers"`
	AppID         string
	AppSecret     string // 加密值,工厂函数调用方传入,使用前已解密
}

Config represents the embedder configuration

func ConfigFromModel

func ConfigFromModel(m *types.Model, appID, appSecret string) Config

ConfigFromModel 根据 types.Model 构造 embedding.Config。 生产路径(从 DB 拉起)和测试连接路径(临时表单)共享这份映射。 appID / appSecret 是已解密的 WeKnoraCloud 凭证,调用方负责传入。

type Embedder

type Embedder interface {
	// Embed converts text to vector
	Embed(ctx context.Context, text string) ([]float32, error)

	// BatchEmbed converts multiple texts to vectors in batch
	BatchEmbed(ctx context.Context, texts []string) ([][]float32, error)

	// GetModelName returns the model name
	GetModelName() string

	// GetDimensions returns the vector dimensions
	GetDimensions() int

	// GetModelID returns the model ID
	GetModelID() string

	EmbedderPooler
}

Embedder defines the interface for text vectorization

func NewEmbedder

func NewEmbedder(config Config, pooler EmbedderPooler, ollamaService *ollama.OllamaService) (Embedder, error)

NewEmbedder creates an embedder based on the configuration

type EmbedderPooler

type EmbedderPooler interface {
	BatchEmbedWithPool(ctx context.Context, model Embedder, texts []string) ([][]float32, error)
}

func NewBatchEmbedder

func NewBatchEmbedder(pool *ants.Pool) EmbedderPooler

type EmbedderType

type EmbedderType string

EmbedderType represents the embedder type

type Image

type Image = api.EmbedImage

Image is one image to embed: its bytes and MIME type.

type ImageEmbedder

type ImageEmbedder interface {
	// AcceptsImages reports whether both the model and its endpoint take
	// images.
	AcceptsImages() bool
	// ImageLimits are the vendor's documented per-image limits. The caller
	// shrinks or converts an image to fit; an image outside them is refused.
	ImageLimits() ImageLimits
	// BatchEmbedImages converts images to vectors comparable with Embed's.
	BatchEmbedImages(ctx context.Context, images []Image) ([][]float32, error)
}

ImageEmbedder is the image side of an Embedder whose model maps images into the same space as its text vectors, so that a text query can find an image. It is a separate interface because most embedders have no image side; use AsImageEmbedder rather than a type assertion, since every decorator implements it whether or not the model underneath does.

func AsImageEmbedder

func AsImageEmbedder(e Embedder) (ImageEmbedder, bool)

AsImageEmbedder returns e's image side when its model accepts images.

type ImageLimits

type ImageLimits = imageprep.Limits

ImageLimits describes what one image may be; see imageprep.Limits.

type OllamaEmbedRequest

type OllamaEmbedRequest struct {
	Model                string `json:"model"`
	Prompt               string `json:"prompt"`
	TruncatePromptTokens int    `json:"truncate_prompt_tokens"`
}

OllamaEmbedRequest represents an Ollama embedding request

type OllamaEmbedResponse

type OllamaEmbedResponse struct {
	Embedding []float32 `json:"embedding"`
}

OllamaEmbedResponse represents an Ollama embedding response

type OllamaEmbedder

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

OllamaEmbedder implements text vectorization functionality using Ollama

func NewOllamaEmbedder

func NewOllamaEmbedder(baseURL,
	modelName string,
	truncatePromptTokens int,
	dimensions int,
	modelID string,
	pooler EmbedderPooler,
	ollamaService *ollama.OllamaService,
) (*OllamaEmbedder, error)

NewOllamaEmbedder creates a new Ollama embedder

func (*OllamaEmbedder) BatchEmbed

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

BatchEmbed converts multiple texts to vectors in batch

func (*OllamaEmbedder) Embed

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

Embed converts text to vector

func (*OllamaEmbedder) GetDimensions

func (e *OllamaEmbedder) GetDimensions() int

GetDimensions returns the vector dimensions

func (*OllamaEmbedder) GetModelID

func (e *OllamaEmbedder) GetModelID() string

GetModelID returns the model ID

func (*OllamaEmbedder) GetModelName

func (e *OllamaEmbedder) GetModelName() string

GetModelName returns the model name

func (*OllamaEmbedder) SetSupportsDimensionOverride

func (e *OllamaEmbedder) SetSupportsDimensionOverride(supported bool)

Jump to

Keyboard shortcuts

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