feat: embedding 配置搬上控制面 — 数据源页可视化配置 + 热更新
embedding 从 env 改为控制面驱动(持久化+可视化),复用 chat 模型同套范式: 配置控制面泛化为按 kind(chat/embedding),加 embedding kind。 - shared: 配置 subjects 泛化 sundynix.config.<kind>.get/.updated;bus 方法改 kind 参数 (RequestConfig/ServeConfig/PublishConfigUpdated/SubscribeConfigUpdated) - gateway: sundynix_model 加 kind 列(每 kind 唯一激活)+旧行回填 chat;admin 按 kind 增删改/激活/列表,测试连接 embedding 走 POST /embeddings;main 按 kind ServeConfig; 变更广播各 kind - dispatcher: 取 chat 配置(kind 化) - mcp-go: rag.Engine.SetEmbedding 热更新(RWMutex);main 取/订阅 embedding 控制面配置 (覆盖 env) - admin 控制台: api 按 kind;抽出复用 ModelManager;ModelsPage(chat)+新 DatasourcesPage (embedding + 向量/图库占位);routes 数据源页就绪 - 验证: 全模块 build✓ + e2e PASS + 控制台 npm build✓;live 全跑通——chat(DeepSeek 回填 kind 仍工作);mcp-go 不带 EMBED env 启动→控制台配 embedding(百炼)→测试连接✓→激活 →NATS 热更新 mcp-go→入库+语义检索'存向量的数据库'→Milvus;浏览器数据源页拉到激活配置 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -24,15 +24,18 @@ func main() {
|
||||
bus := nats.MustConnect(natsURL) // 接入 NATS 零拷贝骨干网 + 声明任务流
|
||||
defer bus.Close()
|
||||
|
||||
// 配置控制面:响应 Dispatcher 对当前激活模型配置的请求。
|
||||
if _, err := bus.ServeModelConfig(func() *contract.ModelConfig {
|
||||
row, _ := db.GetActiveModel(context.Background())
|
||||
if row == nil {
|
||||
return nil
|
||||
// 配置控制面:按 kind 响应消费方(Dispatcher=chat / mcp-go=embedding)的配置请求。
|
||||
for _, kind := range []string{contract.ConfigKindChat, contract.ConfigKindEmbedding} {
|
||||
k := kind
|
||||
if _, err := bus.ServeConfig(k, func() *contract.ModelConfig {
|
||||
row, _ := db.GetActiveModel(context.Background(), k)
|
||||
if row == nil {
|
||||
return nil
|
||||
}
|
||||
return &contract.ModelConfig{Provider: row.Provider, BaseURL: row.BaseURL, APIKey: row.APIKey, Model: row.Model}
|
||||
}); err != nil {
|
||||
log.Printf("[gateway] serve %s config: %v", k, err)
|
||||
}
|
||||
return &contract.ModelConfig{Provider: row.Provider, BaseURL: row.BaseURL, APIKey: row.APIKey, Model: row.Model}
|
||||
}); err != nil {
|
||||
log.Printf("[gateway] serve model config: %v", err)
|
||||
}
|
||||
|
||||
r := router.New(db, cache, bus)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -17,15 +19,16 @@ import (
|
||||
|
||||
type modelBody struct {
|
||||
ID uint `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Provider string `json:"provider"`
|
||||
BaseURL string `json:"base_url"`
|
||||
APIKey string `json:"api_key"`
|
||||
Model string `json:"model"`
|
||||
}
|
||||
|
||||
// ListModels: GET /api/v1/admin/models —— 列出模型(api_key 脱敏)。
|
||||
// ListModels: GET /api/v1/admin/models?kind=chat|embedding —— 列出模型(api_key 脱敏)。
|
||||
func (h *Handler) ListModels(c *gin.Context) {
|
||||
rows, err := h.db.ListModels(c.Request.Context())
|
||||
rows, err := h.db.ListModels(c.Request.Context(), c.Query("kind"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -33,7 +36,7 @@ func (h *Handler) ListModels(c *gin.Context) {
|
||||
out := make([]gin.H, 0, len(rows))
|
||||
for _, m := range rows {
|
||||
out = append(out, gin.H{
|
||||
"id": m.ID, "provider": m.Provider, "base_url": m.BaseURL,
|
||||
"id": m.ID, "kind": m.Kind, "provider": m.Provider, "base_url": m.BaseURL,
|
||||
"model": m.Model, "active": m.Active, "api_key": mask(m.APIKey),
|
||||
})
|
||||
}
|
||||
@@ -51,12 +54,16 @@ func (h *Handler) SaveModel(c *gin.Context) {
|
||||
if provider == "" {
|
||||
provider = "openai-compatible"
|
||||
}
|
||||
m := &store.LLMModel{ID: b.ID, Provider: provider, BaseURL: b.BaseURL, APIKey: b.APIKey, Model: b.Model}
|
||||
kind := b.Kind
|
||||
if kind == "" {
|
||||
kind = contract.ConfigKindChat
|
||||
}
|
||||
m := &store.LLMModel{ID: b.ID, Kind: kind, Provider: provider, BaseURL: b.BaseURL, APIKey: b.APIKey, Model: b.Model}
|
||||
if err := h.db.SaveModel(c.Request.Context(), m); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
h.broadcastActiveModel(c.Request.Context())
|
||||
h.broadcastActive(c.Request.Context())
|
||||
c.JSON(http.StatusOK, gin.H{"id": m.ID})
|
||||
}
|
||||
|
||||
@@ -67,7 +74,7 @@ func (h *Handler) SetActiveModel(c *gin.Context) {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
h.broadcastActiveModel(c.Request.Context())
|
||||
h.broadcastActive(c.Request.Context())
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok", "active": id})
|
||||
}
|
||||
|
||||
@@ -78,7 +85,7 @@ func (h *Handler) DeleteModel(c *gin.Context) {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
h.broadcastActiveModel(c.Request.Context())
|
||||
h.broadcastActive(c.Request.Context())
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
|
||||
@@ -92,7 +99,7 @@ func (h *Handler) TestModel(c *gin.Context) {
|
||||
// 若传了已存的 id 但未带 key,用库里的真实 key。
|
||||
key := b.APIKey
|
||||
if key == "" && b.ID != 0 {
|
||||
if rows, _ := h.db.ListModels(c.Request.Context()); rows != nil {
|
||||
if rows, _ := h.db.ListModels(c.Request.Context(), ""); rows != nil {
|
||||
for _, m := range rows {
|
||||
if m.ID == b.ID {
|
||||
key = m.APIKey
|
||||
@@ -100,9 +107,17 @@ func (h *Handler) TestModel(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 8*time.Second)
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, b.BaseURL+"/models", nil)
|
||||
var req *http.Request
|
||||
if b.Kind == contract.ConfigKindEmbedding {
|
||||
// embedding 端点多无 /models,发一个最小 /embeddings 探测。
|
||||
payload, _ := json.Marshal(map[string]any{"model": b.Model, "input": []string{"ping"}})
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, b.BaseURL+"/embeddings", bytes.NewReader(payload))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodGet, b.BaseURL+"/models", nil)
|
||||
}
|
||||
if key != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+key)
|
||||
}
|
||||
@@ -115,15 +130,17 @@ func (h *Handler) TestModel(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": resp.StatusCode < 400, "message": "HTTP " + resp.Status})
|
||||
}
|
||||
|
||||
// broadcastActiveModel 读当前激活配置并经 NATS 广播,触发 Dispatcher 热更新。
|
||||
func (h *Handler) broadcastActiveModel(ctx context.Context) {
|
||||
row, _ := h.db.GetActiveModel(ctx)
|
||||
if row == nil {
|
||||
return
|
||||
// broadcastActive 重新广播各 kind 当前激活配置,触发对应消费方热更新。
|
||||
func (h *Handler) broadcastActive(ctx context.Context) {
|
||||
for _, kind := range []string{contract.ConfigKindChat, contract.ConfigKindEmbedding} {
|
||||
row, _ := h.db.GetActiveModel(ctx, kind)
|
||||
if row == nil {
|
||||
continue
|
||||
}
|
||||
_ = h.bus.PublishConfigUpdated(kind, &contract.ModelConfig{
|
||||
Provider: row.Provider, BaseURL: row.BaseURL, APIKey: row.APIKey, Model: row.Model,
|
||||
})
|
||||
}
|
||||
_ = h.bus.PublishModelConfigUpdated(&contract.ModelConfig{
|
||||
Provider: row.Provider, BaseURL: row.BaseURL, APIKey: row.APIKey, Model: row.Model,
|
||||
})
|
||||
}
|
||||
|
||||
func mask(s string) string {
|
||||
|
||||
@@ -48,14 +48,14 @@ func (b *Bus) CallTool(ctx context.Context, subject string, call *contract.ToolC
|
||||
return b.inner.CallTool(ctx, subject, call)
|
||||
}
|
||||
|
||||
// ServeModelConfig 让网关作为配置控制面,响应 Dispatcher 的模型配置请求。
|
||||
func (b *Bus) ServeModelConfig(provide func() *contract.ModelConfig) (func() error, error) {
|
||||
return b.inner.ServeModelConfig(provide)
|
||||
// ServeConfig 让网关作为配置控制面,响应某 kind 的配置请求。
|
||||
func (b *Bus) ServeConfig(kind string, provide func() *contract.ModelConfig) (func() error, error) {
|
||||
return b.inner.ServeConfig(kind, provide)
|
||||
}
|
||||
|
||||
// PublishModelConfigUpdated 广播模型配置变更。
|
||||
func (b *Bus) PublishModelConfigUpdated(cfg *contract.ModelConfig) error {
|
||||
return b.inner.PublishModelConfigUpdated(cfg)
|
||||
// PublishConfigUpdated 广播某 kind 的配置变更。
|
||||
func (b *Bus) PublishConfigUpdated(kind string, cfg *contract.ModelConfig) error {
|
||||
return b.inner.PublishConfigUpdated(kind, cfg)
|
||||
}
|
||||
|
||||
func (b *Bus) Close() { b.inner.Close() }
|
||||
|
||||
@@ -6,26 +6,31 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// LLMModel 是一个 LLM 后端配置(控制面:管理员在此登记可用模型)。
|
||||
// 表名 sundynix_model(遵守前缀约定)。同一时刻仅一条 Active=true。
|
||||
// LLMModel 是一个模型后端配置(控制面:管理员在此登记可用模型)。
|
||||
// 表名 sundynix_model(遵守前缀约定)。每个 kind 同一时刻仅一条 Active=true。
|
||||
type LLMModel struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Provider string `gorm:"size:32"` // openai-compatible / vllm
|
||||
BaseURL string `gorm:"size:255"` // 如 https://api.deepseek.com/v1
|
||||
Kind string `gorm:"size:16;index"` // chat / embedding
|
||||
Provider string `gorm:"size:32"` // openai-compatible / vllm
|
||||
BaseURL string `gorm:"size:255"` // 如 https://api.deepseek.com
|
||||
APIKey string `gorm:"size:255"`
|
||||
Model string `gorm:"size:64"` // 如 deepseek-chat
|
||||
Model string `gorm:"size:64"` // 如 deepseek-chat / text-embedding-v3
|
||||
Active bool
|
||||
}
|
||||
|
||||
func (LLMModel) TableName() string { return "sundynix_model" }
|
||||
|
||||
// ListModels 列出全部模型配置。
|
||||
func (p *Postgres) ListModels(ctx context.Context) ([]LLMModel, error) {
|
||||
// ListModels 列出某 kind 的模型配置(kind 空则全部)。
|
||||
func (p *Postgres) ListModels(ctx context.Context, kind string) ([]LLMModel, error) {
|
||||
if p.db == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var rows []LLMModel
|
||||
err := p.db.WithContext(ctx).Order("id").Find(&rows).Error
|
||||
q := p.db.WithContext(ctx).Order("id")
|
||||
if kind != "" {
|
||||
q = q.Where("kind = ?", kind)
|
||||
}
|
||||
err := q.Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
@@ -37,26 +42,30 @@ func (p *Postgres) SaveModel(ctx context.Context, m *LLMModel) error {
|
||||
return p.db.WithContext(ctx).Save(m).Error
|
||||
}
|
||||
|
||||
// SetActiveModel 把指定模型设为激活(其余取消),事务保证唯一激活。
|
||||
// SetActiveModel 把指定模型设为激活(同 kind 内其余取消),事务保证每 kind 唯一激活。
|
||||
func (p *Postgres) SetActiveModel(ctx context.Context, id uint) error {
|
||||
if p.db == nil {
|
||||
return errStoreDisabled
|
||||
}
|
||||
return p.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&LLMModel{}).Where("active = ?", true).Update("active", false).Error; err != nil {
|
||||
var m LLMModel
|
||||
if err := tx.First(&m, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(&LLMModel{}).Where("kind = ? AND active = ?", m.Kind, true).Update("active", false).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Model(&LLMModel{}).Where("id = ?", id).Update("active", true).Error
|
||||
})
|
||||
}
|
||||
|
||||
// GetActiveModel 返回当前激活模型(无则 nil)。
|
||||
func (p *Postgres) GetActiveModel(ctx context.Context) (*LLMModel, error) {
|
||||
// GetActiveModel 返回某 kind 当前激活模型(无则 nil)。
|
||||
func (p *Postgres) GetActiveModel(ctx context.Context, kind string) (*LLMModel, error) {
|
||||
if p.db == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var m LLMModel
|
||||
err := p.db.WithContext(ctx).Where("active = ?", true).First(&m).Error
|
||||
err := p.db.WithContext(ctx).Where("kind = ? AND active = ?", kind, true).First(&m).Error
|
||||
if err != nil {
|
||||
return nil, nil // 未配置激活模型
|
||||
}
|
||||
|
||||
@@ -38,6 +38,8 @@ func OpenPostgres(dsn string) *Postgres {
|
||||
log.Printf("[store] postgres AutoMigrate 失败,降级运行: %v", err)
|
||||
return &Postgres{}
|
||||
}
|
||||
// 回填:kind 列新增前的旧模型行默认归为 chat(幂等)。
|
||||
db.Model(&LLMModel{}).Where("kind = '' OR kind IS NULL").Update("kind", "chat")
|
||||
log.Println("[store] postgres connected, migrated sundynix_user / sundynix_task")
|
||||
return &Postgres{db: db}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user