llm

package
v0.0.0-...-3d209df Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompletionRequest

type CompletionRequest struct {
	Model       string
	Messages    []Message
	MaxTokens   int
	Temperature float64
}

CompletionRequest describes a chat-completion call.

type CompletionResponse

type CompletionResponse struct {
	Content      string
	Model        string
	InputTokens  int
	OutputTokens int
}

CompletionResponse holds the result of a chat-completion call.

type Loader

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

Loader compiles and loads external LLM providers at runtime. It mirrors the approach used by storage.PluginLoader:

  1. Clone the Git repository.
  2. Flatten the optional actions/ subdirectory to the root.
  3. Remove the provider's go.mod/go.sum (the host module is used instead).
  4. Compile with go build -buildmode=plugin using the host module at appDir.
  5. Open the resulting .so and resolve the "Plugin" symbol (a Provider).

Compiled .so files are cached at cacheDir so restarts do not require recompilation (unless the provider was updated).

func NewLoader

func NewLoader(appDir, cacheDir, tempDir string) (*Loader, error)

NewLoader creates a Loader.

  • appDir: the directory that contains the host go.mod (usually /app).
  • cacheDir: where compiled .so files are cached between runs.
  • tempDir: temporary directory for git clones (cleaned up after each compile).

func (*Loader) CompileAndLoad

func (l *Loader) CompileAndLoad(name, repoURL, gitRef, commitHash string) (Provider, string, error)

CompileAndLoad clones repoURL at commitHash (or gitRef if commitHash is empty), compiles the provider, and returns a loaded Provider. If a valid cached .so already exists for commitHash it is used directly.

func (*Loader) LoadCached

func (l *Loader) LoadCached(name string) (Provider, error)

LoadCached opens a previously compiled .so without recompiling. Returns an error if the .so does not exist.

type Message

type Message struct {
	Role    string `json:"role"` // "system" | "user" | "assistant"
	Content string `json:"content"`
}

Message is a single turn in a chat conversation.

type Model

type Model struct {
	ID            string `json:"id"`
	Name          string `json:"name"`
	ContextLength int    `json:"context_length"`
	IsFree        bool   `json:"is_free"`
	ProviderName  string `json:"provider_name"`
}

Model represents an LLM model available from a provider.

type Provider

type Provider interface {
	// Name returns a human-readable identifier for this provider.
	Name() string
	// ListModels fetches the live model catalogue.  No caching is applied here;
	// caching is handled by Service.
	ListModels(ctx context.Context) ([]Model, error)
	// Complete sends a chat-completion request and returns the response.
	Complete(ctx context.Context, req CompletionRequest) (CompletionResponse, error)
}

Provider is the interface that all LLM back-ends must implement.

type Service

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

Service wraps a Provider with a TTL model cache and graceful degradation. A nil *Service is safe to use — all methods check the receiver.

func NewService

func NewService(provider Provider, defaultModel string, enabled bool) *Service

NewService creates a Service backed by provider. When enabled is false the service is present but all operations are no-ops.

func (*Service) GetExternalProvider

func (s *Service) GetExternalProvider(name string) (*models.ExternalLLMProvider, error)

GetExternalProvider returns metadata for a single external LLM provider.

func (*Service) GetProvider

func (s *Service) GetProvider(name string) (Provider, error)

GetProvider retrieves a provider from the registry by name.

func (*Service) InstallExternalProvider

func (s *Service) InstallExternalProvider(req *models.ExternalLLMProviderInstallRequest) (*models.ExternalLLMProvider, error)

InstallExternalProvider clones, compiles, and registers an LLM provider from a Git repository. The metadata is persisted so it survives restarts.

func (*Service) IsEnabled

func (s *Service) IsEnabled() bool

IsEnabled returns true when the service is configured and enabled. Safe to call on a nil receiver.

func (*Service) LabelEntityTypes

func (s *Service) LabelEntityTypes(ctx context.Context, names []string, sourceName string, contextCols []string) map[string]string

LabelEntityTypes assigns a PascalCase entity type to each name in names by batching them into groups of labelBatchSize and calling the LLM. It NEVER returns an error — failures are logged and a partial/empty map is returned so the caller can always proceed with heuristic behaviour.

func (*Service) ListExternalProviders

func (s *Service) ListExternalProviders() ([]*models.ExternalLLMProvider, error)

ListExternalProviders returns all persisted external LLM provider records.

func (*Service) ListModels

func (s *Service) ListModels(ctx context.Context) ([]Model, error)

ListModels returns the cached model list, refreshing it when the TTL has expired. Uses double-checked locking: fast read path avoids write lock.

func (*Service) ListRegisteredProviders

func (s *Service) ListRegisteredProviders() []string

ListRegisteredProviders returns the names of all registered providers.

func (*Service) LoadInstalledExternalProviders

func (s *Service) LoadInstalledExternalProviders() error

LoadInstalledExternalProviders re-registers all persisted external LLM providers on startup. If a cached .so exists it is used directly; otherwise the provider is recompiled from its recorded repository URL and commit hash.

func (*Service) ProviderName

func (s *Service) ProviderName() string

ProviderName returns the provider name, or "none" when not enabled.

func (*Service) RegisterProvider

func (s *Service) RegisterProvider(name string, p Provider)

RegisterProvider adds a named provider to the in-memory registry.

func (*Service) SetActiveProvider

func (s *Service) SetActiveProvider(p Provider, defaultModel string)

SetActiveProvider switches the service to the given provider without rebuilding the registry, store, or loader attachments.

func (*Service) UninstallExternalProvider

func (s *Service) UninstallExternalProvider(name string) error

UninstallExternalProvider removes the provider from the live registry and deletes its persisted metadata. The .so file is removed from the cache. Note: Go plugins cannot be unloaded from memory; a process restart is needed for removal to take full effect on in-flight calls.

func (*Service) WithLoader

func (s *Service) WithLoader(loader *Loader) *Service

WithLoader attaches a Loader for dynamic provider compilation.

func (*Service) WithStore

func (s *Service) WithStore(store metadatastore.MetadataStore) *Service

WithStore attaches a MetadataStore for external provider persistence.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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