Add unified backend abstraction layer supporting multiple LLM providers: Backend (Go): - New backends package with interface, registry, and adapters - Ollama adapter wrapping existing functionality - OpenAI-compatible adapter for llama.cpp and LM Studio - Unified API routes under /api/v1/ai/* - SSE to NDJSON streaming conversion for OpenAI backends - Auto-discovery of backends on default ports Frontend (Svelte 5): - New backendsState store for backend management - Unified LLM client routing through backend API - AI Providers tab combining Backends and Models sub-tabs - Backend-aware chat streaming (uses appropriate client) - Model name display for non-Ollama backends in top nav - Persist and restore last selected backend Key features: - Switch between backends without restart - Conditional UI based on backend capabilities - Models tab only visible when Ollama active - llama.cpp/LM Studio show loaded model name
99 lines
3.5 KiB
Go
99 lines
3.5 KiB
Go
package backends
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// LLMBackend defines the interface for LLM backend implementations.
|
|
// All backends (Ollama, llama.cpp, LM Studio) must implement this interface.
|
|
type LLMBackend interface {
|
|
// Type returns the backend type identifier
|
|
Type() BackendType
|
|
|
|
// Config returns the backend configuration
|
|
Config() BackendConfig
|
|
|
|
// HealthCheck verifies the backend is reachable and operational
|
|
HealthCheck(ctx context.Context) error
|
|
|
|
// ListModels returns all models available from this backend
|
|
ListModels(ctx context.Context) ([]Model, error)
|
|
|
|
// StreamChat sends a chat request and returns a channel for streaming responses.
|
|
// The channel is closed when the stream completes or an error occurs.
|
|
// Callers should check ChatChunk.Error for stream errors.
|
|
StreamChat(ctx context.Context, req *ChatRequest) (<-chan ChatChunk, error)
|
|
|
|
// Chat sends a non-streaming chat request and returns the final response
|
|
Chat(ctx context.Context, req *ChatRequest) (*ChatChunk, error)
|
|
|
|
// Capabilities returns what features this backend supports
|
|
Capabilities() BackendCapabilities
|
|
|
|
// Info returns detailed information about the backend including status
|
|
Info(ctx context.Context) BackendInfo
|
|
}
|
|
|
|
// ModelManager extends LLMBackend with model management capabilities.
|
|
// Only Ollama implements this interface.
|
|
type ModelManager interface {
|
|
LLMBackend
|
|
|
|
// PullModel downloads a model from the registry.
|
|
// Returns a channel for progress updates.
|
|
PullModel(ctx context.Context, name string) (<-chan PullProgress, error)
|
|
|
|
// DeleteModel removes a model from local storage
|
|
DeleteModel(ctx context.Context, name string) error
|
|
|
|
// CreateModel creates a custom model with the given Modelfile content
|
|
CreateModel(ctx context.Context, name string, modelfile string) (<-chan CreateProgress, error)
|
|
|
|
// CopyModel creates a copy of an existing model
|
|
CopyModel(ctx context.Context, source, destination string) error
|
|
|
|
// ShowModel returns detailed information about a specific model
|
|
ShowModel(ctx context.Context, name string) (*ModelDetails, error)
|
|
}
|
|
|
|
// EmbeddingProvider extends LLMBackend with embedding capabilities.
|
|
type EmbeddingProvider interface {
|
|
LLMBackend
|
|
|
|
// Embed generates embeddings for the given input
|
|
Embed(ctx context.Context, model string, input []string) ([][]float64, error)
|
|
}
|
|
|
|
// PullProgress represents progress during model download
|
|
type PullProgress struct {
|
|
Status string `json:"status"`
|
|
Digest string `json:"digest,omitempty"`
|
|
Total int64 `json:"total,omitempty"`
|
|
Completed int64 `json:"completed,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// CreateProgress represents progress during model creation
|
|
type CreateProgress struct {
|
|
Status string `json:"status"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// ModelDetails contains detailed information about a model
|
|
type ModelDetails struct {
|
|
Name string `json:"name"`
|
|
ModifiedAt string `json:"modified_at"`
|
|
Size int64 `json:"size"`
|
|
Digest string `json:"digest"`
|
|
Format string `json:"format"`
|
|
Family string `json:"family"`
|
|
Families []string `json:"families"`
|
|
ParamSize string `json:"parameter_size"`
|
|
QuantLevel string `json:"quantization_level"`
|
|
Template string `json:"template"`
|
|
System string `json:"system"`
|
|
License string `json:"license"`
|
|
Modelfile string `json:"modelfile"`
|
|
Parameters map[string]string `json:"parameters"`
|
|
}
|