d623b8590e
混合检索从 2 路(向量+全文)升级为 3 路(+图谱)。入库时 LLM 抽实体/关系建 Neo4j 图,检索时图谱路(实体关联三元组)融进 RRF;UI 可视化图谱。 - mcp-go rag: chat.go(OpenAI 兼容非流式 chat 客户端,抽取用) + graph.go(neo4j-go-driver 连接 + LLM 抽三元组 + MERGE 实体/关系 + 图谱召回/全量三元组) + rag.go(Config 结构; graph+chat 路;Ingest 加 抽实体/写Neo4j 阶段;Search 三路 RRF 融合;SetChat 热更新) - mcp-go: Neo4j env(默认 neo4j://localhost:7687, neo4j/sundynix);订阅 chat 控制面配置 (复用 DeepSeek 做抽取);新工具 kb_graph(返回三元组) - gateway: GET /api/v1/kb/graph;frontend KbView 知识图谱面板(实体—关系→实体) - 验证: 全模块 build✓ + e2e PASS; live——入库'sundynix用Milvus...'→DeepSeek 抽 4 三元组 →Neo4j(8 实体);检索三路融合 向量=4 全文=2 图谱=1;浏览器图谱面板渲染 4 三元组 - 边界: 实体链接用 CONTAINS 朴素匹配(可升级 LLM 查询实体抽取);全文/图谱重启随入库重建 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
104 lines
3.6 KiB
Go
104 lines
3.6 KiB
Go
// Command server 启动 sundynix-mcp-go —— 第 5 层 Go I/O 型 MCP 工具微服务。
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
sharedbus "github.com/sundynix/sundynix-shared/bus"
|
|
"github.com/sundynix/sundynix-shared/contract"
|
|
|
|
"github.com/sundynix/sundynix-mcp-go/internal/history"
|
|
"github.com/sundynix/sundynix-mcp-go/internal/mcp"
|
|
"github.com/sundynix/sundynix-mcp-go/internal/memory"
|
|
"github.com/sundynix/sundynix-mcp-go/internal/rag"
|
|
"github.com/sundynix/sundynix-mcp-go/internal/search"
|
|
)
|
|
|
|
func main() {
|
|
natsURL := envOr("NATS_URL", "nats://localhost:4222")
|
|
pgDSN := envOr("POSTGRES_DSN", "postgres://sundynix:sundynix@localhost:5432/sundynix?sslmode=disable")
|
|
redisAddr := envOr("REDIS_ADDR", "localhost:6379")
|
|
milvusAddr := envOr("MILVUS_ADDR", "localhost:19530")
|
|
embBase := envOr("EMBED_BASE_URL", "") // OpenAI 兼容 embeddings 端点(空=向量检索降级)
|
|
embKey := envOr("EMBED_API_KEY", "")
|
|
embModel := envOr("EMBED_MODEL", "")
|
|
rerankBase := envOr("RERANK_BASE_URL", "") // DashScope 文本重排端点(空=不启用 rerank)
|
|
rerankKey := envOr("RERANK_API_KEY", "")
|
|
rerankModel := envOr("RERANK_MODEL", "")
|
|
neo4jURI := envOr("NEO4J_URI", "neo4j://localhost:7687") // GraphRAG 图谱(连不上则降级)
|
|
neo4jUser := envOr("NEO4J_USER", "neo4j")
|
|
neo4jPass := envOr("NEO4J_PASS", "sundynix")
|
|
|
|
b, err := sharedbus.Connect(natsURL)
|
|
if err != nil {
|
|
log.Fatalf("[mcp_go] nats connect: %v", err)
|
|
}
|
|
defer b.Close()
|
|
log.Printf("[mcp_go] connected %s", natsURL)
|
|
|
|
engine := search.NewHybrid() // LLM Wiki 混合检索:Bleve + Milvus + Neo4j
|
|
mem := memory.Open(pgDSN) // 偏好记忆:sundynix_user_profile(连不上则降级)
|
|
defer mem.Close()
|
|
hist := history.Open(redisAddr) // 会话短期历史:Redis(连不上则降级)
|
|
defer hist.Close()
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
// RAG 核心链:embedding + Milvus(向量) + Bleve(全文) + Neo4j(图谱) + 可选 rerank
|
|
ragEngine := rag.Open(ctx, rag.Config{
|
|
MilvusAddr: milvusAddr,
|
|
EmbedBase: embBase, EmbedKey: embKey, EmbedModel: embModel,
|
|
RerankBase: rerankBase, RerankKey: rerankKey, RerankModel: rerankModel,
|
|
Neo4jURI: neo4jURI, Neo4jUser: neo4jUser, Neo4jPass: neo4jPass,
|
|
})
|
|
defer ragEngine.Close()
|
|
|
|
// 配置控制面:取激活 embedding(向量) + chat(图谱抽取) 配置并订阅热更新。
|
|
applyEmbed := func(cfg *contract.ModelConfig) {
|
|
if cfg != nil {
|
|
ragEngine.SetEmbedding(cfg.BaseURL, cfg.APIKey, cfg.Model)
|
|
}
|
|
}
|
|
applyChat := func(cfg *contract.ModelConfig) {
|
|
if cfg != nil {
|
|
ragEngine.SetChat(cfg.BaseURL, cfg.APIKey, cfg.Model)
|
|
}
|
|
}
|
|
cctx, ccancel := context.WithTimeout(ctx, 3*time.Second)
|
|
if cfg, _ := b.RequestConfig(cctx, contract.ConfigKindEmbedding); cfg != nil {
|
|
applyEmbed(cfg)
|
|
} else {
|
|
log.Println("[mcp_go] 未取到 embedding 控制面配置(用 env 或降级)")
|
|
}
|
|
if cfg, _ := b.RequestConfig(cctx, contract.ConfigKindChat); cfg != nil {
|
|
applyChat(cfg)
|
|
}
|
|
ccancel()
|
|
if _, err := b.SubscribeConfigUpdated(contract.ConfigKindChat, applyChat); err != nil {
|
|
log.Printf("[mcp_go] subscribe chat config: %v", err)
|
|
}
|
|
if _, err := b.SubscribeConfigUpdated(contract.ConfigKindEmbedding, applyEmbed); err != nil {
|
|
log.Printf("[mcp_go] subscribe embedding config: %v", err)
|
|
}
|
|
|
|
gw := mcp.NewGateway(b, engine, mem, hist, ragEngine)
|
|
|
|
log.Println("[mcp_go] serving MCP over sundynix.tools.go.* (Ctrl-C to quit)")
|
|
if err := gw.Serve(ctx); err != nil && err != context.Canceled {
|
|
log.Fatalf("[mcp_go] exit: %v", err)
|
|
}
|
|
}
|
|
|
|
func envOr(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|