embedding

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package embedding defines the stable text-to-vector protocol and its single-method provider SPI.

Build input with NewRequest and attach Options only for per-call overrides. Provider defaults and identity are fixed when constructing an implementation, not exposed through Model. Dimension discovery belongs to the consuming workflow because it requires an actual embedding request. Float32Vector bridges the protocol's float64 representation to storage SDKs that require float32. Provider options use Options.SetExtension so Extensions remains JSON-safe; Request has no arbitrary parameter bag.

Example
package main

import (
	"fmt"

	"github.com/Tangerg/scope/core/embedding"
)

func main() {
	request, err := embedding.NewRequest([]string{"scope", "wild cat"})
	if err != nil {
		panic(err)
	}
	options := embedding.Options{Model: "text-embedding-model"}
	err = options.Validate()
	if err != nil {
		panic(err)
	}
	dimensions := int64(3)
	options.Dimensions = &dimensions
	request.Options = options

	fmt.Println(len(request.Texts), request.Options.Model, *request.Options.Dimensions)
}
Output:
2 text-embedding-model 3

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOptions  = errors.New("embedding: invalid options")
	ErrInvalidRequest  = errors.New("embedding: invalid request")
	ErrInvalidResponse = errors.New("embedding: invalid response")
)

Functions

func Float32Vector

func Float32Vector(source []float64) []float32

Float32Vector narrows Core's canonical vector representation for stores that index in single precision. Conversion belongs at that store boundary rather than in providers that cannot know the eventual index format.

func PlaceOutput added in v0.16.0

func PlaceOutput(outputs []*Output, index int, embedding []float64, outputMetadata metadata.Map) error

PlaceOutput stores one provider result at the position it belongs to.

outputs must already be sized to the input count. Providers that tag each embedding with its own index may answer out of order, so placing by index is what restores the correspondence Response.Outputs declares; appending in arrival order silently pairs texts with the wrong vectors. An index outside the request and a position claimed twice are both rejected, and a position left unfilled fails when the Response is built.

Types

type Model

type Model interface {
	// Call performs one embedding request after validating the complete batch.
	// It must not retain or mutate request, returns outputs in input order, and
	// transfers ownership of the response to the caller. Context cancellation
	// remains identifiable through errors.Is.
	Call(ctx context.Context, request *Request) (*Response, error)
}

Model is the complete provider-neutral embedding SPI. Call implementations validate requests before I/O, reject explicit options they cannot represent, preserve context error identity, and return responses that pass Validate. Defaults, identity, observability, batching, and dimension discovery are independent concerns.

type ModelFunc

type ModelFunc func(context.Context, *Request) (*Response, error)

ModelFunc lets an ordinary function satisfy Model without declaring a named type, which is what keeps middleware and test doubles from each inventing their own adapter.

func (ModelFunc) Call

func (m ModelFunc) Call(ctx context.Context, request *Request) (*Response, error)

type Options

type Options struct {
	// Model is the provider model identifier
	// (e.g. "text-embedding-3-small").
	Model string `json:"model"`

	// Dimensions requests an explicit output vector size. nil leaves it
	// up to the provider's default.
	Dimensions *int64 `json:"dimensions,omitempty"`

	// Extensions carries JSON-safe provider-specific options unknown to this
	// struct.
	Extensions metadata.Extensions `json:"extensions,omitzero"`
}

Options holds per-request configuration for an embedding call. Pointer fields use nil to preserve the distinction between an override and a provider default. Resolve snapshots mutable values and overlays only fields explicitly supplied by the request.

func (Options) Clone

func (o Options) Clone() Options

func (Options) MarshalJSON

func (o Options) MarshalJSON() ([]byte, error)

func (Options) Resolve

func (o Options) Resolve(override Options) (Options, error)

func (*Options) UnmarshalJSON

func (o *Options) UnmarshalJSON(data []byte) error

func (Options) Validate

func (o Options) Validate() error

type Output

type Output struct {
	// Embedding is the vector representation of the input.
	Embedding []float64 `json:"embedding"`

	// Metadata carries provider-specific per-output extras.
	Metadata metadata.Map `json:"metadata,omitzero"`
}

Output is one embedding plus its metadata.

func NewOutput

func NewOutput(embedding []float64, outputMetadata metadata.Map) (*Output, error)

NewOutput validates and snapshots one provider result before it enters a Response.

func (Output) MarshalJSON

func (o Output) MarshalJSON() ([]byte, error)

func (*Output) UnmarshalJSON

func (o *Output) UnmarshalJSON(data []byte) error

func (*Output) Validate

func (o *Output) Validate() error

type Request

type Request struct {
	// Texts is the input list. Each entry produces one embedding.
	Texts []string `json:"texts,omitzero"`

	Options Options `json:"options,omitzero"`
}

Request is one embedding call: the input texts and explicit options.

func NewRequest

func NewRequest(texts []string) (*Request, error)

NewRequest preserves the provider-neutral batch shape and clones the input, so later caller mutation cannot change a request already in flight.

func (Request) MarshalJSON

func (r Request) MarshalJSON() ([]byte, error)

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(data []byte) error

func (*Request) Validate

func (r *Request) Validate() error

type Response

type Response struct {
	// Outputs holds one entry per input text, in the same order.
	Outputs []*Output `json:"outputs,omitzero"`

	Metadata *ResponseMetadata `json:"metadata,omitempty"`
}

Response is the full embedding output: one *Output per input plus shared response metadata.

func NewResponse

func NewResponse(outputs []*Output, responseMetadata *ResponseMetadata) (*Response, error)

NewResponse validates a complete provider result at the protocol boundary.

func (*Response) First

func (r *Response) First() *Output

func (Response) MarshalJSON

func (r Response) MarshalJSON() ([]byte, error)

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(data []byte) error

func (*Response) Validate

func (r *Response) Validate() error

func (*Response) ValidateFor added in v0.16.0

func (r *Response) ValidateFor(request *Request) error

ValidateFor checks a provider result against the request it answers.

Outputs declares one entry per input text in the same order, and that correspondence is the whole basis for using an embedding: a response one vector short leaves every later text paired with its neighbor's vector, and nothing downstream can notice. Validate alone cannot see it, because the input count is not part of the response, so this is the check that makes the declared correspondence enforceable rather than aspirational.

type ResponseMetadata

type ResponseMetadata struct {
	// Model is the model name actually served.
	Model string `json:"model"`

	// Usage breaks down token consumption. nil means the provider did not
	// report usage.
	Usage *Usage `json:"usage,omitempty"`

	// CreatedAt is the provider-reported creation timestamp.
	CreatedAt time.Time `json:"created_at,omitzero"`

	// Extra carries JSON-safe provider-specific metadata.
	Extra metadata.Map `json:"extra,omitzero"`
}

ResponseMetadata holds response-level metadata: the model actually used, token usage, creation time, and provider extras.

func (ResponseMetadata) MarshalJSON

func (r ResponseMetadata) MarshalJSON() ([]byte, error)

func (*ResponseMetadata) UnmarshalJSON

func (r *ResponseMetadata) UnmarshalJSON(data []byte) error

type Usage

type Usage struct {
	// InputTokens are tokens consumed embedding the inputs.
	InputTokens int64 `json:"input_tokens"`
}

Usage records the token consumption an embedding request reported back. Embedding is input-only — there is no completion, reasoning, or cache dimension — so a single count is the whole story. Providers that report a "total" figure map it here: for embeddings every token is input.

func (Usage) MarshalJSON

func (u Usage) MarshalJSON() ([]byte, error)

func (*Usage) UnmarshalJSON

func (u *Usage) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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