Documentation
¶
Overview ¶
Example (WithOptions) ¶
Example_withOptions 演示如何使用选项模式
config := DefaultServiceConfig()
fmt.Printf("Default config:\n")
fmt.Printf("Host: %s, Port: %d\n", config.Host, config.Port)
fmt.Printf("Context Size: %d, Cont Batching: %t\n", config.ContextSize, config.ContBatching)
fmt.Printf("Batch Size: %d, Threads: %d\n", config.BatchSize, config.Threads)
// 应用选项
options := []Option{
WithHost("0.0.0.0"),
WithPort(9090),
WithContextSize(8192),
WithContBatching(false),
WithBatchSize(2048),
WithThreads(16),
WithDebug(true),
}
for _, option := range options {
option(config)
}
fmt.Printf("\nAfter applying options:\n")
fmt.Printf("Host: %s, Port: %d\n", config.Host, config.Port)
fmt.Printf("Context Size: %d, Cont Batching: %t\n", config.ContextSize, config.ContBatching)
fmt.Printf("Batch Size: %d, Threads: %d\n", config.BatchSize, config.Threads)
Index ¶
- Constants
- Variables
- func GetDefaultEmbeddingModelPath() string
- func GetDefaultYakBinaryPath() string
- func GetLlamaServerPath() (string, error)
- func GetModelPath(modelName string) (string, error)
- func GetSupportedModelNames() []string
- func IsDefaultModelAvailable() bool
- func IsModelSupported(modelName string) bool
- func StartChat(address string, options ...Option) error
- func StartEmbedding(address string, options ...Option) error
- func StopAllServices() error
- func StopService(serviceName string) error
- func ValidateModelPath(modelPath string) error
- func WaitForEmbedding(address string, timeoutSeconds float64) error
- type Config
- type Manager
- func (m *Manager) GetCurrentBinaryPathFromManager() string
- func (m *Manager) GetLocalModelPath(modelName string) (string, error)
- func (m *Manager) GetServiceStatus(serviceName string) (*ServiceInfo, error)
- func (m *Manager) IsLocalModelExists(modelName string) bool
- func (m *Manager) ListLocalModels() []string
- func (m *Manager) ListServices() []*ServiceInfo
- func (m *Manager) SetCurrentBinaryPath(path string)
- func (m *Manager) StartChatService(address string, options ...Option) error
- func (m *Manager) StartEmbeddingService(address string, options ...Option) error
- func (m *Manager) StartService(address string, options ...Option) error
- func (m *Manager) StopAllServices() error
- func (m *Manager) StopService(serviceName string) error
- func (m *Manager) WaitForEmbeddingService(address string, timeoutSeconds float64) error
- type Model
- type ModelConfig
- type ModelManager
- type Option
- func WithArgs(args ...string) Option
- func WithBatchSize(size int) Option
- func WithContBatching(enabled bool) Option
- func WithContextSize(size int) Option
- func WithDebug(debug bool) Option
- func WithHost(host string) Option
- func WithLlamaServerPath(path string) Option
- func WithModel(model string) Option
- func WithModelPath(path string) Option
- func WithModelType(modelType string) Option
- func WithPooling(pooling string) Option
- func WithPort(port int32) Option
- func WithStartupTimeout(timeout time.Duration) Option
- func WithThreads(threads int) Option
- type ProcessInfo
- type Service
- type ServiceConfig
- type ServiceInfo
- type ServiceStatus
- type ServiceType
- type Status
Examples ¶
Constants ¶
const ( // 服务状态常量 Stopped = StatusStopped Starting = StatusStarting Running = StatusRunning Stopping = StatusStopping Error = StatusError )
导出的常量
Variables ¶
var ( // GetManager 获取管理器单例 GetManagerInstance = GetManager // NewManager 创建管理器 (已废弃) New = NewManager // 配置相关 DefaultConfig = DefaultServiceConfig GetModels = GetSupportedModels FindModel = FindModelConfig ValidateModel = ValidateModelPath GetModel = GetModelPath GetLlamaServer = GetLlamaServerPath )
导出的函数
var ErrServiceNotFound = errors.New("service not found")
ErrServiceNotFound 服务不存在错误
Functions ¶
func GetDefaultEmbeddingModelPath ¶
func GetDefaultEmbeddingModelPath() string
func GetDefaultYakBinaryPath ¶
func GetDefaultYakBinaryPath() string
GetDefaultYakBinaryPath 获取默认的 yak 二进制文件路径
func GetLlamaServerPath ¶
GetLlamaServerPath 获取 llama-server 路径
func GetSupportedModelNames ¶
func GetSupportedModelNames() []string
GetSupportedModelNames 获取支持的模型名称列表
func IsDefaultModelAvailable ¶
func IsDefaultModelAvailable() bool
func StartEmbedding ¶
StartEmbedding 启动嵌入服务(便捷函数)
func WaitForEmbedding ¶
WaitForEmbedding 等待嵌入服务启动(便捷函数)
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 本地模型管理器
func GetManagerWithDefaults ¶
func GetManagerWithDefaults() *Manager
GetManagerWithDefaults 获取管理器单例实例
func NewManager ¶
func NewManager() *Manager
NewManager 创建新的管理器实例 (已废弃,使用 GetManager 代替) Deprecated: Use GetManager() instead
func NewManagerWithDefaults ¶
func NewManagerWithDefaults() *Manager
NewManagerWithDefaults 创建带默认配置的管理器 (已废弃) Deprecated: Use GetManagerWithDefaults() instead
func (*Manager) GetCurrentBinaryPathFromManager ¶
GetCurrentBinaryPathFromManager 获取当前设置的二进制文件路径
func (*Manager) GetLocalModelPath ¶
GetLocalModelPath 获取本地模型路径
func (*Manager) GetServiceStatus ¶
func (m *Manager) GetServiceStatus(serviceName string) (*ServiceInfo, error)
GetServiceStatus 获取服务状态
func (*Manager) IsLocalModelExists ¶
IsLocalModelExists 检查本地模型是否存在
func (*Manager) ListLocalModels ¶
ListLocalModels 列出本地可用的模型
func (*Manager) SetCurrentBinaryPath ¶
SetCurrentBinaryPath 设置当前二进制文件路径(用于 Detached 模式)
func (*Manager) StartChatService ¶
StartChatService 启动聊天服务
func (*Manager) StartEmbeddingService ¶
StartEmbeddingService 启动嵌入服务
Example ¶
ExampleManager_StartEmbeddingService 演示如何使用管理器启动嵌入服务
// 获取管理器单例
manager := GetManager()
// 启动嵌入服务
err := manager.StartEmbeddingService(
"127.0.0.1:11434",
WithModel("Qwen3-Embedding-0.6B-Q4_K_M"),
WithDebug(true),
WithModelPath("/tmp/Qwen3-Embedding-0.6B-Q4_K_M.gguf"),
WithContextSize(4096),
WithContBatching(true),
WithBatchSize(1024),
WithThreads(8),
)
if err != nil {
fmt.Printf("Failed to start embedding service: %v\n", err)
return
}
fmt.Println("Embedding service started successfully")
// 等待一段时间
time.Sleep(2 * time.Second)
// 查看服务状态
services := manager.ListServices()
for _, service := range services {
fmt.Printf("Service: %s, Status: %s\n", service.Name, service.Status)
}
// 停止所有服务
err = manager.StopAllServices()
if err != nil {
fmt.Printf("Failed to stop services: %v\n", err)
return
}
fmt.Println("All services stopped")
func (*Manager) StartService ¶
StartService 启动服务的通用方法
func (*Manager) StopService ¶
StopService 停止指定服务
type ModelConfig ¶
type ModelConfig struct {
Name string `json:"name"`
Type string `json:"type"` // embedding, llm, etc.
FileName string `json:"fileName"`
DownloadURL string `json:"downloadURL"`
Description string `json:"description"`
DefaultPort int32 `json:"defaultPort"`
}
ModelConfig 模型配置
func FindModelConfig ¶
func FindModelConfig(modelName string) (*ModelConfig, error)
FindModelConfig 查找模型配置
Example ¶
ExampleFindModelConfig 演示如何查找模型配置
modelName := "Qwen3-Embedding-0.6B-Q4_K_M"
model, err := FindModelConfig(modelName)
if err != nil {
fmt.Printf("Model not found: %v\n", err)
return
}
fmt.Printf("Found model: %s\n", model.Name)
fmt.Printf("Type: %s\n", model.Type)
fmt.Printf("Description: %s\n", model.Description)
fmt.Printf("Default Port: %d\n", model.DefaultPort)
func GetModelsByType ¶
func GetModelsByType(modelType string) []*ModelConfig
GetModelsByType 根据类型获取模型列表
func GetSupportedModels ¶
func GetSupportedModels() []*ModelConfig
GetSupportedModels 获取支持的模型列表
Example ¶
ExampleGetSupportedModels 演示如何获取支持的模型列表
models := GetSupportedModels()
fmt.Printf("Supported models (%d):\n", len(models))
for _, model := range models {
fmt.Printf("- %s (%s): %s\n", model.Name, model.Type, model.Description)
fmt.Printf(" Default Port: %d\n", model.DefaultPort)
fmt.Printf(" File: %s\n", model.FileName)
fmt.Println()
}
type Option ¶
type Option func(*ServiceConfig)
Option 定义选项函数类型
func WithLlamaServerPath ¶
WithLlamaServerPath 设置 llama-server 路径
func WithModelType ¶
func WithStartupTimeout ¶
WithStartupTimeout 设置启动超时时间
type ProcessInfo ¶
ProcessInfo 进程信息结构
type ServiceConfig ¶
type ServiceConfig struct {
Host string `json:"host"`
Port int32 `json:"port"`
Model string `json:"model"`
ModelType string `json:"modelType"`
ModelPath string `json:"modelPath"`
LlamaServerPath string `json:"llamaServerPath"`
ContextSize int `json:"contextSize"`
ContBatching bool `json:"contBatching"` // 连续批处理
BatchSize int `json:"batchSize"` // 批处理大小
Threads int `json:"threads"` // 线程数
Debug bool `json:"debug"`
Pooling string `json:"pooling"` // 池化方式
StartupTimeout time.Duration `json:"startupTimeout"`
Args []string `json:"args"`
}
ServiceConfig 服务配置
func DefaultServiceConfig ¶
func DefaultServiceConfig() *ServiceConfig
DefaultServiceConfig 返回默认服务配置
type ServiceInfo ¶
type ServiceInfo struct {
Name string `json:"name"`
Type ServiceType `json:"type"`
Status ServiceStatus `json:"status"`
Config *ServiceConfig `json:"config"`
ProcessID int `json:"processID"`
StartTime time.Time `json:"startTime"`
Process *exec.Cmd `json:"-"`
LastError string `json:"lastError,omitempty"`
// contains filtered or unexported fields
}
ServiceInfo 服务信息
func GetServiceStatus ¶
func GetServiceStatus(serviceName string) (*ServiceInfo, error)
GetServiceStatus 获取服务状态(便捷函数)
type ServiceStatus ¶
type ServiceStatus int
ServiceStatus 服务状态
Example ¶
ExampleServiceStatus 演示服务状态
statuses := []ServiceStatus{
StatusStopped,
StatusStarting,
StatusRunning,
StatusStopping,
StatusError,
}
fmt.Println("Service status values:")
for _, status := range statuses {
fmt.Printf("- %d: %s\n", status, status.String())
}
const ( StatusStopped ServiceStatus = iota StatusStarting StatusRunning StatusStopping StatusError )
func (ServiceStatus) String ¶
func (s ServiceStatus) String() string
type ServiceType ¶
type ServiceType string
ServiceType 服务类型
const ( ServiceTypeEmbedding ServiceType = "embedding" ServiceTypeChat ServiceType = "aichat" )
func (ServiceType) String ¶
func (t ServiceType) String() string