registry

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: Apache-2.0 Imports: 30 Imported by: 1 module, 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 registry has the providers of models, by names which the registry gives them, and finds the models across them

Index

Constants

View Source
const ProviderTable = "provider"

ProviderTable is the table of the events of providers, whose rows are their names in the registry

Variables

View Source
var (
	// PingInterval is how long the result of a provider's ping is used, and
	// how often Run pings the providers. A provider which downloads models,
	// such as Ollama, has its models listed again as often, as they can be
	// downloaded or deleted without the registry, and listing them is quick.
	PingInterval = time.Minute

	// ModelCacheInterval is how long a provider's models are cached, unless
	// it downloads models
	ModelCacheInterval = 6 * time.Hour
)

Functions

This section is empty.

Types

type CredentialsProvider

type CredentialsProvider interface {
	// Expand returns the value with each placeholder replaced with its
	// credential, or an error when a credential doesn't exist
	Expand(ctx context.Context, value string) (string, error)
}

CredentialsProvider expands credentials, such as a provider's API key, whose placeholders, such as ${OPENAI_KEY} or $OPENAI_KEY, are replaced with the credentials they name

type Opt

type Opt func(*opt) error

Opt is a functional option for the registry

func WithClientOpts

func WithClientOpts(opts ...client.ClientOpt) Opt

WithClientOpts sets the options of the HTTP clients of the providers which the registry creates, such as their tracer, user agent and timeout

func WithCredentialsProvider

func WithCredentialsProvider(credentials CredentialsProvider) Opt

WithCredentialsProvider sets the provider which expands credentials, such as the API key of a provider which is created, whose placeholders, such as ${OPENAI_KEY}, are replaced with the credentials they name. Without one, or with nil, the credentials are environment variables.

func WithStore

func WithStore(store Store) Opt

WithStore sets the store of the providers, which the registry's providers are kept the same as, both ways, as Run syncs them, such as a database, which other servers share, or a file, so they're kept when the server restarts

func WithToolkit

func WithToolkit(tools *toolkit.Toolkit) Opt

WithToolkit sets the toolkit of the tools which the registry's models can call

func WithTracer

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer, which traces the registry's operations, such as listing models, and generating messages

type ProviderOpt

type ProviderOpt func(*cached) error

ProviderOpt is an option of a provider which is added to the registry

func WithExclude

func WithExclude(patterns ...string) ProviderOpt

WithExclude excludes the models whose names match one of the patterns, which are regular expressions, such as -mini$

func WithInclude

func WithInclude(patterns ...string) ProviderOpt

WithInclude includes only the models whose names match one of the patterns, which are regular expressions, such as ^gpt-, or every model when there are none

type Registry

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

Registry is the providers of models, by names which it gives them, so there can be more than one provider of a type, such as two Ollama servers. Providers can be added and removed. It's safe to use concurrently.

func New

func New(opts ...Opt) (*Registry, error)

New returns a registry without providers, which are added with Add, with the options, such as its tracer

func (*Registry) Add

func (r *Registry) Add(name string, provider llm.Provider, opts ...ProviderOpt) error

Add adds the provider, with the name, which must be an identifier, such as my-ollama, which starts with a letter, followed by letters, numbers, underscores or hyphens, or with its own name, such as its type, when the name is empty, and the options, such as the patterns of the names of the models which are included, or excluded. Returns ErrConflict when a provider with the name, or the provider itself, is added already, and ErrBadParameter when a pattern isn't valid. Watchers are sent that it was created.

func (*Registry) Ask

Ask returns the reply of the request's model to its messages, which include earlier turns of the conversation. The model replies after the system prompt, with the options, and can call the tools, which are got from the registry's toolkit, and whose calls are in its reply, for the caller to run, rather than run. When the request has a schema, the model replies by calling the respond tool, whose result is the reply's result: a model which replies with text is asked once to call it, and a result which doesn't conform to the schema is returned to the model to correct, until it's been asked respondAttempts times. The reply is streamed to the function as it's generated, when it isn't nil, with each of the model's replies separated by a blank line. Returns ErrBadParameter when the request isn't valid, ErrNotFound when the model, or a tool, isn't found, and the errors of WithoutSession.

func (*Registry) CreateProvider

func (r *Registry) CreateProvider(ctx context.Context, req schema.Provider) (_ *schema.ProviderStatus, err error)

CreateProvider creates a provider of the type, such as ollama, and adds it to the registry, with its name, or its type when it has none, once it's validated, as newProvider does, and pinged. The request is kept, as the provider's definition, with its API key's placeholder, rather than its credentials, and its serial is 1, whatever the request's. Returns ErrBadParameter when the type is unknown, ErrServiceUnavailable when it can't be reached, as it can't be used, and ErrConflict when a provider with its name is added already.

func (*Registry) DeleteModel

func (r *Registry) DeleteModel(ctx context.Context, model llm.Model) (err error)

DeleteModel deletes the model, with its provider, which must be in the registry, and download models

func (*Registry) DeleteProvider

func (r *Registry) DeleteProvider(ctx context.Context, name string) (_ *schema.ProviderStatus, err error)

DeleteProvider removes the provider with the name from the registry, and returns it, or returns ErrNotFound when there's none. It isn't pinged.

func (*Registry) Embed

func (r *Registry) Embed(ctx context.Context, model llm.Model, messages []llm.Message, opts ...llm.Opt) (_ []llm.Vector, _ llm.Usage, err error)

Embed returns a vector for each of the messages, from the model's provider, which must be in the registry, and embed messages. Returns ErrNotImplemented when the model is known not to embed, and the provider's error as providerErr does.

func (*Registry) GetModel

func (r *Registry) GetModel(ctx context.Context, name string) (_ llm.Model, err error)

GetModel returns the model with the name, which is its canonical name, such as ollama:llama3.2:latest, which is its provider's name in the registry, and its name, or otherwise its name, or an alias, which every provider's models are searched for at once. A model must match its provider's patterns. The models are cached, as ListModels does. Returns ErrNotFound when no provider has it, and ErrConflict when more than one does, so its canonical name is needed. When no provider has it, and a provider can't be asked, such as when it can't be reached, that provider's error is returned.

func (*Registry) GetProvider

func (r *Registry) GetProvider(ctx context.Context, name string) (_ *schema.ProviderStatus, err error)

GetProvider returns the provider with the name, with whether it can be reached, when it was last pinged, within PingInterval, or ErrNotFound when there's none

func (*Registry) ListModels

func (r *Registry) ListModels(ctx context.Context, providers ...string) (_ []llm.Model, err error)

ListModels returns the models of every provider, or of the providers with the names, when there are any, which must exist, which match their patterns, in order of their providers' names, and then their names. The models are cached, and are listed again, at once, when they're stale. When a provider's models can't be listed, such as when it can't be reached, and none are cached, the others' models are returned, with an error saying which providers' couldn't be.

func (*Registry) ListProviders

func (r *Registry) ListProviders(ctx context.Context, req schema.ProviderListRequest) (_ *schema.ProviderList, err error)

ListProviders returns the providers, in order of their names, of the type which the request selects, or every type, which are pinged at once, so it's known which can't be reached

func (*Registry) LoadModel

func (r *Registry) LoadModel(ctx context.Context, name string, progress llm.DownloadProgressFn) (_ llm.Model, err error)

LoadModel downloads the model with the name, with its provider, which must download models, and returns it. As the model may not exist yet, the name must be its canonical name, such as ollama:llama3.2:latest, unless only one of the providers downloads models.

func (*Registry) Model

func (r *Registry) Model(model llm.Model) *schema.Model

Model returns the model, with the name of its provider in the registry, and its canonical name

func (*Registry) Name

func (r *Registry) Name(model llm.Model) string

Name returns the canonical name of the model, which is the name of its provider in the registry, and its name, such as my-ollama:llama3.2:latest, or its provider's own name, when its provider isn't in the registry

func (*Registry) Names

func (r *Registry) Names() []string

Names returns the names of the providers, in order

func (*Registry) Ping

func (r *Registry) Ping(ctx context.Context) (err error)

Ping pings every provider at once, and returns when they've replied, with the errors of those which failed joined, each saying which provider it is, or nil when every ping succeeded. The providers are pinged, whenever they were last.

func (*Registry) Provider

func (r *Registry) Provider(name string) llm.Provider

Provider returns the provider with the name, or nil when there's none

func (*Registry) Providers

func (r *Registry) Providers() []llm.Provider

Providers returns the providers, in order of their names

func (*Registry) Recognize

func (r *Registry) Recognize(ctx context.Context, model llm.Model, resource llm.Resource, opts ...llm.Opt) (_ llm.Document, _ llm.Usage, err error)

Recognize returns the text in the resource, such as an image or a PDF, as a document, from the model's provider, which must be in the registry, and recognize text. Returns ErrNotImplemented when the model is known not to recognize text, and the provider's error as providerErr does.

func (*Registry) Remove

func (r *Registry) Remove(name string) (llm.Provider, error)

Remove removes the provider with the name, and returns it, so it can be closed, or returns ErrNotFound when there's none. Calls which have started already, such as listing models, still use it. Watchers are sent that it was deleted.

func (*Registry) Run

func (r *Registry) Run(ctx context.Context, logger *slog.Logger) error

Run keeps the registry current, until the context is cancelled, when it returns nil: it pings the providers, and lists the models of those whose models are stale, when it starts, and every PingInterval, so the providers' state, and their models, are kept current, and with a store, it syncs the registry's providers with the store's, when it starts, as either changes, and every PingInterval, as an event can be missed. The errors syncing them, which are synced again later, are logged with the logger, or aren't when it's nil. When the store stops, it returns its error. The registry is used without it, when its models are listed as they're needed. When it stops, the registry's watches end, as there will be no more events, and it isn't watched until it runs again.

func (*Registry) Toolkit

func (r *Registry) Toolkit() *toolkit.Toolkit

Toolkit returns the toolkit of the tools which the registry's models can call, or nil when it has none

func (*Registry) Transcribe

func (r *Registry) Transcribe(ctx context.Context, model llm.Model, audio llm.Resource, stream llm.TranscriberStreamFn, opts ...llm.Opt) (_ llm.Transcript, _ llm.Usage, err error)

Transcribe returns the speech in the audio, from the model's provider, which must be in the registry, and transcribe audio, and streams its pieces to the function, when it isn't nil. Returns ErrNotImplemented when the model is known not to transcribe, and the provider's error as providerErr does.

func (*Registry) Translate

func (r *Registry) Translate(ctx context.Context, model llm.Model, audio llm.Resource, stream llm.TranscriberStreamFn, opts ...llm.Opt) (_ llm.Transcript, _ llm.Usage, err error)

Translate returns the speech in the audio as English text, from the model's provider, which must be in the registry, and transcribe audio. Returns ErrNotImplemented when the model can't translate, or is known not to, and the provider's error as providerErr does.

func (*Registry) UnloadModel

func (r *Registry) UnloadModel(ctx context.Context, model llm.Model) (err error)

UnloadModel unloads the model from memory, with its provider, which must be in the registry, and load models, keeping it stored, so its provider's models aren't listed again. Returns ErrNotImplemented when the provider doesn't load models, or can't unload them.

func (*Registry) UpdateProvider

func (r *Registry) UpdateProvider(ctx context.Context, name string, meta schema.ProviderMeta) (_ *schema.ProviderStatus, err error)

UpdateProvider changes the provider with the name: its fields which can be set are replaced with the meta, apart from its API key, which is kept when the meta has none, as it isn't returned. Its type and name aren't changed. The provider is created again, as CreateProvider does, and replaces it, whether it can be reached or not, which it says, with its serial incremented, and watchers are sent that it was updated. A provider whose fields are the same is left as it is. Returns ErrNotFound when there's no provider with the name, ErrBadParameter when the meta isn't valid, and ErrConflict when the provider was added without a definition, such as with Add.

func (*Registry) Watch

func (r *Registry) Watch(ctx context.Context, ch chan<- schema.Event) (<-chan error, error)

Watch sends the registry's events to the channel, from when it returns, as it's watching before it does, until the context is cancelled, or the registry stops running, as there will be no more events. The events are of providers: when one is created, or deleted, and updated, when whether it can be reached changes, or its models do. It returns a channel which is sent nil when the context is cancelled, or ErrServiceUnavailable when the registry stops running, once the watch has ended, after which no more events are sent to the channel, so it can be closed. A registry which has stopped running isn't watched, and returns ErrServiceUnavailable, until it runs again. An event isn't waited for the channel to receive, so a channel which is full misses it, and one which needs every event should be buffered. Returns ErrBadParameter when there's no channel, and ErrConflict when the channel is watching already.

func (*Registry) WithoutSession

func (r *Registry) WithoutSession(ctx context.Context, model llm.Model, messages []llm.Message, tools []llm.Tool, stream llm.GeneratorStreamFn, opts ...llm.Opt) (_ llm.Message, _ llm.Usage, err error)

WithoutSession returns the model's reply to the messages, from the model's provider, which must be in the registry, and generate messages, or the provider's error as providerErr does. Returns ErrNotImplemented, without asking the provider, when the model is known not to reply, or there are tools, and it's known not to call them, or it's asked to think, and it's known not to, or the messages have images, and it's known not to see them.

type Store

type Store interface {
	// List returns the providers, with their serials, in any order
	List(context.Context) ([]schema.Provider, error)

	// Put creates the provider, when its serial is zero, and the store has
	// no provider with its name, or replaces the provider with its name,
	// when its serial is that provider's, and returns it, with its new
	// serial. Returns ErrConflict when its serial isn't the provider's, as
	// the provider was changed, or created, since it was seen.
	Put(context.Context, schema.Provider) (schema.Provider, error)

	// Delete deletes the provider with the name, when the serial is its
	// serial, or does nothing when there's none. Returns ErrConflict when
	// the serial isn't its serial, as it was changed since it was seen.
	Delete(ctx context.Context, name string, serial uint64) error

	// Watch sends the store's events to the channel, when its providers are
	// changed by others, such as other servers, from when it returns, until
	// the context is cancelled, as the registry's Watch does. It returns a
	// channel which is sent nil, or an error when the store stops, once the
	// watch has ended. A store which isn't changed by others, such as a
	// file, sends no events, as its providers are listed periodically.
	Watch(context.Context, chan<- providerschema.Event) (<-chan error, error)
}

Store has the definitions of providers, with their names, and their API keys' placeholders, such as ${OPENAI_KEY}, rather than their credentials, by their names, each with its serial, which the store sets, and changes each time the provider does, so a change is made to the provider as it was last seen, rather than a newer one, such as one which another server made

Directories

Path Synopsis
Package cmd has the registry's commands and flags
Package cmd has the registry's commands and flags
Package httpclient is a client of the registry's HTTP API
Package httpclient is a client of the registry's HTTP API
Package httphandler has the registry's HTTP API
Package httphandler has the registry's HTTP API
Package schema has the requests and responses of the registry's API
Package schema has the requests and responses of the registry's API
Package test has a registry and a HTTP server of its API, for tests
Package test has a registry and a HTTP server of its API, for tests

Jump to

Keyboard shortcuts

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