config

package
v0.9.69 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	APIKeysFile  = "api_keys.json"
	APIUsageFile = "api_usage.json"
)
View Source
const (
	DefaultServerURL           = "https://hub.opencsg.com"
	DefaultDisplayURL          = "https://opencsg.com"
	DefaultListenAddr          = ":11435"
	DefaultDesktopAPIAddr      = "127.0.0.1:11436"
	DefaultDesktopAPIBindAddr  = "0.0.0.0:11436"
	DefaultAuthCallbackAddr    = "127.0.0.1:11437"
	DefaultCloudProviderName   = "csghub"
	DefaultMarketplaceSource   = "opencsg"
	DefaultHuggingFaceEndpoint = "https://huggingface.co"
	ChinaHuggingFaceEndpoint   = "https://hf-mirror.com"
	DefaultModelScopeEndpoint  = "https://modelscope.cn"
	EnvServerURL               = "CSGHUB_LITE_SERVER_URL"
	EnvAIGatewayURL            = "CSGHUB_LITE_AI_GATEWAY_URL"
	EnvCloudProviderName       = "CSGHUB_LITE_CLOUD_PROVIDER_NAME"
	EnvOpenAIStreamDefault     = "CSGHUB_LITE_OPENAI_STREAM_DEFAULT"
	EnvHiddenNavItems          = "CSGHUB_LITE_HIDDEN_NAV_ITEMS"
	EnvHuggingFaceEndpoint     = "HF_ENDPOINT"
	EnvHuggingFaceToken        = "HF_TOKEN"
	EnvHuggingFaceHubToken     = "HUGGING_FACE_HUB_TOKEN"
	EnvModelScopeEndpoint      = "MODELSCOPE_ENDPOINT"
	EnvModelScopeToken         = "MODELSCOPE_API_TOKEN"
	EnvModelScopeAPIKey        = "MODELSCOPE_API_KEY"
	AppDir                     = ".csghub-lite"
	ConfigFile                 = "config.json"
	ModelsDir                  = "models"
	DatasetsDir                = "datasets"
	TmpDir                     = "tmp"
)
View Source
const (
	LogsDir                    = "logs"
	ServerLogFileName          = "csghub-lite.log"
	LlamaServerLogFileName     = "llama-server.log"
	EmbeddingWorkerLogFileName = "embedding-worker.log"
	DiffusersWorkerLogFileName = "diffusers-worker.log"

	LogStderrEnv          = "CSGHUB_LITE_LOG_STDERR"
	DisableFileLoggingEnv = "CSGHUB_LITE_DISABLE_FILE_LOGGING"
)
View Source
const (
	ProviderPoolPolicyPriorityWeight = "priority_weight"
	ProviderPoolPolicySemantic       = "semantic"
)
View Source
const DefaultObservabilityRetentionDays = 30
View Source
const ProviderModelAllowlistFile = "provider_model_allowlist.json"
View Source
const ProviderPoolsFile = "provider_pools.json"

ProviderPoolsFile stores user-defined model pools separately from provider credentials so existing providers.json remains backward compatible.

View Source
const ProvidersFile = "providers.json"

ProvidersFile is the filename for storing third-party providers

Variables

View Source
var ErrProviderModelSelectionDuplicate = errors.New("provider model id already exists")

Functions

func AddProviderModelAllowlist

func AddProviderModelAllowlist(providerID, modelID string) error

func AddProviderModelSelection

func AddProviderModelSelection(providerID string, selection ProviderModelSelection) error

func AppHome

func AppHome() (string, error)

func ApplyEnvironmentDefaults

func ApplyEnvironmentDefaults(cfg *Config)

func ConfigPath

func ConfigPath() (string, error)

func DatasetDirForStorage

func DatasetDirForStorage(storageDir string) string

func DefaultDatasetDir

func DefaultDatasetDir() (string, error)

func DefaultModelDir

func DefaultModelDir() (string, error)

func DefaultStorageDir

func DefaultStorageDir() (string, error)

func DeleteProviderModelAllowlist

func DeleteProviderModelAllowlist(providerID string) error

func DiffusersWorkerLogPath added in v0.9.13

func DiffusersWorkerLogPath() (string, error)

func EmbeddingWorkerLogPath added in v0.9.13

func EmbeddingWorkerLogPath() (string, error)

func FileLoggingEnabled

func FileLoggingEnabled() bool

func GenerateProviderID

func GenerateProviderID() string

GenerateProviderID generates a random ID for a new provider

func GetProviderModelAllowlist

func GetProviderModelAllowlist(providerID string) []string

func HasProviderModelSelections added in v0.9.7

func HasProviderModelSelections(providerID string) bool

func IsSupportedMarketplaceDatasetSource added in v0.9.53

func IsSupportedMarketplaceDatasetSource(value string) bool

func IsSupportedMarketplaceModelSource added in v0.9.53

func IsSupportedMarketplaceModelSource(value string) bool

func LlamaServerLogPath

func LlamaServerLogPath() (string, error)

func LogDir

func LogDir() (string, error)

func LogStderrEnabled

func LogStderrEnabled() bool

func ModelDirForStorage

func ModelDirForStorage(storageDir string) string

func NormalizeCloudProviderName

func NormalizeCloudProviderName(name string) string

func NormalizeMarketplaceDatasetSource added in v0.9.53

func NormalizeMarketplaceDatasetSource(value string) string

func NormalizeMarketplaceModelSource added in v0.9.53

func NormalizeMarketplaceModelSource(value string) string

func NormalizeProviderPoolPolicy added in v0.9.57

func NormalizeProviderPoolPolicy(policy string) string

func ObservabilityRetentionDays added in v0.9.40

func ObservabilityRetentionDays(cfg ObservabilityConfig) int

func ProviderModelAllowlistPath

func ProviderModelAllowlistPath() (string, error)

func ProviderPoolsPath added in v0.9.36

func ProviderPoolsPath() (string, error)

func ProvidersPath

func ProvidersPath() (string, error)

ProvidersPath returns the path to the providers config file

func RemoveProviderModelAllowlist

func RemoveProviderModelAllowlist(providerID, modelID string) (bool, error)

func ReplaceProviderModelAllowlist

func ReplaceProviderModelAllowlist(providerID string, models []string) error

func ReplaceProviderModelSelections

func ReplaceProviderModelSelections(providerID string, models []ProviderModelSelection) error

func Reset

func Reset()

func ResetProviderModelAllowlist

func ResetProviderModelAllowlist()

func ResetProviderPools added in v0.9.36

func ResetProviderPools()

func ResetProviders

func ResetProviders()

ResetProviders resets the providers state (for testing)

func ResolveHuggingFaceEndpoint added in v0.9.54

func ResolveHuggingFaceEndpoint(configured string) string

func Save

func Save(cfg *Config) error

func SaveProviderPools added in v0.9.36

func SaveProviderPools(pools []ProviderPool) error

func SaveProviders

func SaveProviders(provs []ThirdPartyProvider) error

SaveProviders saves the providers to the config file

func ServerLogPath

func ServerLogPath() (string, error)

func StorageDir

func StorageDir(modelDir, datasetDir string) string

func TempDirForStorage

func TempDirForStorage(storageDir string) string

Types

type APIKeyRecord

type APIKeyRecord struct {
	ID         string    `json:"id"`
	Name       string    `json:"name"`
	Prefix     string    `json:"prefix"`
	Hash       string    `json:"hash"`
	CreatedAt  time.Time `json:"created_at"`
	LastUsedAt time.Time `json:"last_used_at,omitempty"`
}

type APIKeyState

type APIKeyState struct {
	AuthEnabled bool           `json:"auth_enabled"`
	Keys        []APIKeyRecord `json:"keys"`
}

type APIKeyStore

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

func NewAPIKeyStore

func NewAPIKeyStore(appHome string) *APIKeyStore

func (*APIKeyStore) Create

func (s *APIKeyStore) Create(name string) (APIKeyRecord, string, error)

func (*APIKeyStore) Delete

func (s *APIKeyStore) Delete(id string) (bool, error)

func (*APIKeyStore) SetAuthEnabled

func (s *APIKeyStore) SetAuthEnabled(enabled bool) (APIKeyState, error)

func (*APIKeyStore) State

func (s *APIKeyStore) State() (APIKeyState, error)

func (*APIKeyStore) Validate

func (s *APIKeyStore) Validate(plain string) (APIKeyRecord, bool, error)

type APIUsageEvent

type APIUsageEvent struct {
	APIKeyID       string
	APIKeyName     string
	Model          string
	Source         string
	SourceType     string
	SourceName     string
	PoolID         string
	PoolName       string
	PoolModel      string
	ActualMemberID string
	MemberModel    string
	EstimatedCost  float64
	CostCurrency   string
	CostKnown      bool
	FallbackCount  int64
	LimitedCount   int64
	InputTokens    int64
	OutputTokens   int64
	CreatedAt      time.Time
}

type APIUsageEventRecord

type APIUsageEventRecord struct {
	APIKeyID       string    `json:"api_key_id"`
	APIKeyName     string    `json:"api_key_name"`
	Model          string    `json:"model"`
	Source         string    `json:"source,omitempty"`
	SourceType     string    `json:"source_type,omitempty"`
	SourceName     string    `json:"source_name,omitempty"`
	PoolID         string    `json:"pool_id,omitempty"`
	PoolName       string    `json:"pool_name,omitempty"`
	PoolModel      string    `json:"pool_model,omitempty"`
	ActualMemberID string    `json:"actual_member_id,omitempty"`
	MemberModel    string    `json:"member_model,omitempty"`
	EstimatedCost  float64   `json:"estimated_cost,omitempty"`
	CostCurrency   string    `json:"cost_currency,omitempty"`
	CostKnown      bool      `json:"cost_known"`
	FallbackCount  int64     `json:"fallback_count,omitempty"`
	LimitedCount   int64     `json:"limited_count,omitempty"`
	Requests       int64     `json:"requests,omitempty"`
	InputTokens    int64     `json:"input_tokens"`
	OutputTokens   int64     `json:"output_tokens"`
	TotalTokens    int64     `json:"total_tokens"`
	CreatedAt      time.Time `json:"created_at"`
}

type APIUsageListOptions

type APIUsageListOptions struct {
	Since    *time.Time
	Until    *time.Time
	Provider string
	Pool     string
}

type APIUsageRecord

type APIUsageRecord struct {
	APIKeyID       string    `json:"api_key_id"`
	APIKeyName     string    `json:"api_key_name"`
	Model          string    `json:"model"`
	Source         string    `json:"source,omitempty"`
	SourceType     string    `json:"source_type,omitempty"`
	SourceName     string    `json:"source_name,omitempty"`
	PoolID         string    `json:"pool_id,omitempty"`
	PoolName       string    `json:"pool_name,omitempty"`
	PoolModel      string    `json:"pool_model,omitempty"`
	ActualMemberID string    `json:"actual_member_id,omitempty"`
	MemberModel    string    `json:"member_model,omitempty"`
	EstimatedCost  float64   `json:"estimated_cost,omitempty"`
	CostCurrency   string    `json:"cost_currency,omitempty"`
	CostKnown      bool      `json:"cost_known"`
	FallbackCount  int64     `json:"fallback_count,omitempty"`
	LimitedCount   int64     `json:"limited_count,omitempty"`
	Requests       int64     `json:"requests"`
	InputTokens    int64     `json:"input_tokens"`
	OutputTokens   int64     `json:"output_tokens"`
	TotalTokens    int64     `json:"total_tokens"`
	LastUsedAt     time.Time `json:"last_used_at"`
}

type APIUsageState

type APIUsageState struct {
	Records []APIUsageRecord      `json:"records"`
	Events  []APIUsageEventRecord `json:"events,omitempty"`
}

type APIUsageStore

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

func NewAPIUsageStore

func NewAPIUsageStore(appHome string) *APIUsageStore

func (*APIUsageStore) Add

func (s *APIUsageStore) Add(event APIUsageEvent) error

func (*APIUsageStore) List

type Config

type Config struct {
	ServerURL                string                             `json:"server_url"`
	AIGatewayURL             string                             `json:"ai_gateway_url,omitempty"`
	CloudProviderName        string                             `json:"cloud_provider_name,omitempty"`
	Token                    string                             `json:"token,omitempty"`
	OpenCSGAPIKey            string                             `json:"opencsg_api_key,omitempty"`
	HuggingFaceEndpoint      string                             `json:"huggingface_endpoint,omitempty"`
	HuggingFaceToken         string                             `json:"huggingface_token,omitempty"`
	ModelScopeEndpoint       string                             `json:"modelscope_endpoint,omitempty"`
	ModelScopeToken          string                             `json:"modelscope_token,omitempty"`
	MarketplaceModelSource   string                             `json:"marketplace_model_source,omitempty"`
	MarketplaceDatasetSource string                             `json:"marketplace_dataset_source,omitempty"`
	ListenAddr               string                             `json:"listen_addr"`
	ModelDir                 string                             `json:"model_dir"`
	DatasetDir               string                             `json:"dataset_dir"`
	OpenAIStreamDefault      bool                               `json:"-"`
	HiddenNavItems           []string                           `json:"-"`
	AIAppPreferredModels     map[string]string                  `json:"ai_app_preferred_models,omitempty"`
	AIAppPreferredSources    map[string]string                  `json:"ai_app_preferred_sources,omitempty"`
	AIAppModelBindings       map[string][]api.AIAppModelBinding `json:"ai_app_model_bindings,omitempty"`
	WebSearch                WebSearchConfig                    `json:"web_search,omitempty"`
	Observability            ObservabilityConfig                `json:"observability,omitempty"`
	Inference                InferenceConfig                    `json:"inference,omitempty"`
	DesktopMode              bool                               `json:"-"`
	DesktopToken             string                             `json:"-"`
	DesktopSessionToken      string                             `json:"-"`
	DesktopControlToken      string                             `json:"-"`
	DesktopInstanceID        string                             `json:"-"`
	ListenAddrOverride       string                             `json:"-"`
	BoundAddr                string                             `json:"-"`
	DesktopAPIAddr           string                             `json:"-"`
	DesktopAPIBindAddr       string                             `json:"-"`
	DesktopAPIBoundAddr      string                             `json:"-"`
	AuthCallbackAddr         string                             `json:"-"`
}

func Get

func Get() *Config

func Load

func Load() (*Config, error)

func (*Config) DisplayURL

func (c *Config) DisplayURL() string

func (*Config) EffectiveListenAddr added in v0.9.31

func (c *Config) EffectiveListenAddr() string

func (*Config) RuntimeAPIAddr added in v0.9.31

func (c *Config) RuntimeAPIAddr() string

func (*Config) RuntimeDockerAPIAddr added in v0.9.31

func (c *Config) RuntimeDockerAPIAddr() string

func (*Config) RuntimeListenAddr added in v0.9.31

func (c *Config) RuntimeListenAddr() string

func (*Config) StorageDir

func (c *Config) StorageDir() string

func (*Config) TempDir

func (c *Config) TempDir() string

type InferenceConfig added in v0.9.54

type InferenceConfig struct {
	LlamaUseModelMaxCtx bool `json:"llama_use_model_max_ctx,omitempty"`

	// ModelNumCtx holds per-model context windows keyed by model ID. A model
	// listed here overrides the global default but still loses to a context
	// length sent with the request. It lives in the app config rather than in
	// the model directory so that re-downloading a model keeps the setting.
	ModelNumCtx map[string]int `json:"model_num_ctx,omitempty"`
}

InferenceConfig groups process-wide inference defaults that apply equally to the Web UI and external API clients.

type ObservabilityConfig added in v0.9.40

type ObservabilityConfig struct {
	// RetentionDays is nil for legacy configurations and 0 for unlimited retention.
	RetentionDays *int `json:"retention_days,omitempty"`
}

type ProviderHeader added in v0.9.43

type ProviderHeader struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

type ProviderModelAllowlist

type ProviderModelAllowlist struct {
	Version   int                                 `json:"version"`
	Providers map[string][]ProviderModelSelection `json:"providers"`
}

func LoadProviderModelAllowlist

func LoadProviderModelAllowlist() (ProviderModelAllowlist, error)

type ProviderModelSelection

type ProviderModelSelection struct {
	Model              string   `json:"model"`
	OriginalModel      string   `json:"original_model,omitempty"`
	CatalogDisplayName string   `json:"catalog_display_name,omitempty"`
	DisplayName        string   `json:"display_name,omitempty"`
	Description        string   `json:"description,omitempty"`
	PipelineTag        string   `json:"pipeline_tag,omitempty"`
	InputModalities    []string `json:"input_modalities,omitempty"`
	OutputModalities   []string `json:"output_modalities,omitempty"`
}

func GetProviderModelSelections

func GetProviderModelSelections(providerID string) []ProviderModelSelection

func UpdateProviderModelSelection

func UpdateProviderModelSelection(providerID, modelID string, update ProviderModelSelectionUpdate) (ProviderModelSelection, bool, error)

func (*ProviderModelSelection) UnmarshalJSON

func (s *ProviderModelSelection) UnmarshalJSON(data []byte) error

type ProviderModelSelectionUpdate

type ProviderModelSelectionUpdate struct {
	Model       *string
	DisplayName *string
	Description *string
}

type ProviderPool added in v0.9.36

type ProviderPool struct {
	ID      string               `json:"id"`
	Name    string               `json:"name"`
	Model   string               `json:"model"`
	Enabled bool                 `json:"enabled"`
	Policy  string               `json:"policy,omitempty"`
	Members []ProviderPoolMember `json:"members"`
}

ProviderPool exposes one public model ID and routes it to member models.

func GetProviderPools added in v0.9.36

func GetProviderPools() []ProviderPool

func LoadProviderPools added in v0.9.36

func LoadProviderPools() ([]ProviderPool, error)

type ProviderPoolMember added in v0.9.36

type ProviderPoolMember struct {
	ID            string `json:"id"`
	Source        string `json:"source"`
	Model         string `json:"model"`
	Priority      int    `json:"priority,omitempty"`
	Weight        int    `json:"weight,omitempty"`
	RequestsPM    int    `json:"requests_per_minute,omitempty"`
	TokensPM      int    `json:"tokens_per_minute,omitempty"`
	MaxConcurrent int    `json:"max_concurrent,omitempty"`
}

ProviderPoolMember identifies a concrete model source. Source is local, cloud, or provider:<provider-id>; Model is the model ID sent upstream.

type ThirdPartyProvider

type ThirdPartyProvider struct {
	ID       string           `json:"id"`
	Name     string           `json:"name"`
	BaseURL  string           `json:"base_url"`
	APIKey   string           `json:"api_key"`
	Provider string           `json:"provider,omitempty"`
	Enabled  bool             `json:"enabled"`
	Headers  []ProviderHeader `json:"headers,omitempty"`
}

ThirdPartyProvider represents a third-party API provider configuration

func GetProviders

func GetProviders() []ThirdPartyProvider

GetProviders returns the loaded third-party providers

func LoadProviders

func LoadProviders() ([]ThirdPartyProvider, error)

LoadProviders loads third-party providers from the config file

type WebSearchConfig

type WebSearchConfig struct {
	Enabled        bool     `json:"enabled,omitempty"`
	MaxResults     int      `json:"max_results,omitempty"`
	Language       string   `json:"language,omitempty"`
	Providers      []string `json:"providers,omitempty"`
	SafeSearch     int      `json:"safe_search,omitempty"`
	TimeoutSeconds int      `json:"timeout_seconds,omitempty"`
}

func DefaultWebSearchConfig

func DefaultWebSearchConfig() WebSearchConfig

func NormalizeWebSearchConfig

func NormalizeWebSearchConfig(cfg WebSearchConfig) WebSearchConfig

Jump to

Keyboard shortcuts

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