feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。
T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
This commit is contained in:
@@ -18,6 +18,7 @@ type Config struct {
|
||||
BaseURL string // API 基础 URL(OpenAI 兼容格式)
|
||||
Model string // 模型名称
|
||||
Dimensions int // 向量维度
|
||||
NoAuth bool // 本地部署:端点无需鉴权时置 true(不发送 Authorization 头)
|
||||
}
|
||||
|
||||
// Client embedding 客户端
|
||||
@@ -65,7 +66,8 @@ type embeddingResponse struct {
|
||||
|
||||
// GetEmbedding 获取单条文本的向量嵌入
|
||||
func (c *Client) GetEmbedding(ctx context.Context, text string) ([]float32, error) {
|
||||
if c.cfg.APIKey == "" {
|
||||
// 本地无鉴权端点(NoAuth)允许空密钥;否则必须配置密钥。
|
||||
if c.cfg.APIKey == "" && !c.cfg.NoAuth {
|
||||
return nil, fmt.Errorf("embedding API key not configured")
|
||||
}
|
||||
|
||||
@@ -94,7 +96,10 @@ func (c *Client) GetEmbedding(ctx context.Context, text string) ([]float32, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
|
||||
// 仅在配置了密钥时发送 Authorization 头;本地无鉴权端点不发送。
|
||||
if c.cfg.APIKey != "" {
|
||||
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
|
||||
}
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := c.httpClient.Do(httpReq)
|
||||
@@ -133,6 +138,7 @@ func (c *Client) GetEmbeddingBatch(ctx context.Context, texts []string) ([][]flo
|
||||
}
|
||||
|
||||
// IsConfigured 检查 embedding 服务是否已配置
|
||||
// 配置了密钥,或显式声明本地无鉴权(NoAuth),均视为可用。
|
||||
func (c *Client) IsConfigured() bool {
|
||||
return c.cfg.APIKey != ""
|
||||
return c.cfg.APIKey != "" || c.cfg.NoAuth
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package embedding
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsConfigured(t *testing.T) {
|
||||
// 有密钥 → 已配置
|
||||
if !NewClient(Config{APIKey: "k"}).IsConfigured() {
|
||||
t.Fatal("配置了密钥应视为已配置")
|
||||
}
|
||||
// 本地无鉴权 → 已配置
|
||||
if !NewClient(Config{NoAuth: true}).IsConfigured() {
|
||||
t.Fatal("NoAuth 应视为已配置")
|
||||
}
|
||||
// 都没有 → 未配置(保持优雅降级到关键词检索)
|
||||
if NewClient(Config{}).IsConfigured() {
|
||||
t.Fatal("既无密钥也非 NoAuth 应视为未配置")
|
||||
}
|
||||
}
|
||||
|
||||
// 本地无鉴权 embedding 端点:不发送 Authorization 头,且能取回向量。
|
||||
func TestGetEmbedding_LocalNoAuth(t *testing.T) {
|
||||
var sawAuthHeader bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, sawAuthHeader = r.Header["Authorization"]
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"data": []map[string]any{{"embedding": []float32{0.1, 0.2, 0.3}, "index": 0}},
|
||||
"usage": map[string]any{"total_tokens": 3},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(Config{BaseURL: srv.URL, Model: "bge-local", Dimensions: 3, NoAuth: true})
|
||||
vec, err := c.GetEmbedding(context.Background(), "政务文本")
|
||||
if err != nil {
|
||||
t.Fatalf("本地 embedding 取回失败: %v", err)
|
||||
}
|
||||
if len(vec) != 3 {
|
||||
t.Fatalf("向量维度不符: %d", len(vec))
|
||||
}
|
||||
if sawAuthHeader {
|
||||
t.Fatal("本地无鉴权端点不应发送 Authorization 头")
|
||||
}
|
||||
}
|
||||
|
||||
// 配置了密钥时应发送 Authorization 头。
|
||||
func TestGetEmbedding_SendsAuthWhenKeySet(t *testing.T) {
|
||||
var gotAuth string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"data": []map[string]any{{"embedding": []float32{1}, "index": 0}},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := NewClient(Config{APIKey: "sk-test", BaseURL: srv.URL, Model: "m", Dimensions: 1})
|
||||
if _, err := c.GetEmbedding(context.Background(), "x"); err != nil {
|
||||
t.Fatalf("取回失败: %v", err)
|
||||
}
|
||||
if gotAuth != "Bearer sk-test" {
|
||||
t.Fatalf("应发送 Bearer 密钥头,实际: %q", gotAuth)
|
||||
}
|
||||
}
|
||||
|
||||
// 既无密钥也非 NoAuth 时应直接报错(不发起请求)。
|
||||
func TestGetEmbedding_NoKeyNoAuthErrors(t *testing.T) {
|
||||
c := NewClient(Config{BaseURL: "http://localhost:9", Model: "m", Dimensions: 1})
|
||||
if _, err := c.GetEmbedding(context.Background(), "x"); err == nil {
|
||||
t.Fatal("无密钥且非 NoAuth 应返回错误")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user