Files
sundynix-agentix/sundynix-mcp-go/internal/rag/chat.go
T
Blizzard d623b8590e feat: GraphRAG — LLM 抽三元组建 Neo4j 图谱 + 混合检索加图谱第三路
混合检索从 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>
2026-06-11 11:10:22 +08:00

73 lines
1.9 KiB
Go

package rag
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// chatClient 是 OpenAI 兼容的非流式对话客户端,供图谱实体抽取用。
// 配置由控制面(chat kind)经 NATS 下发(与 Dispatcher 共用同一个模型)。
type chatClient struct {
baseURL string
apiKey string
model string
hc *http.Client
}
func newChatClient(baseURL, apiKey, model string) *chatClient {
if baseURL == "" || model == "" {
return nil
}
return &chatClient{baseURL: baseURL, apiKey: apiKey, model: model, hc: &http.Client{Timeout: 60 * time.Second}}
}
func (c *chatClient) ready() bool { return c != nil && c.baseURL != "" }
// complete 一次性补全(非流式),返回助手回复文本。
func (c *chatClient) complete(ctx context.Context, system, user string) (string, error) {
body, _ := json.Marshal(map[string]any{
"model": c.model,
"messages": []map[string]string{
{"role": "system", "content": system},
{"role": "user", "content": user},
},
"stream": false,
})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/chat/completions", bytes.NewReader(body))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
if c.apiKey != "" {
req.Header.Set("Authorization", "Bearer "+c.apiKey)
}
resp, err := c.hc.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
buf := new(bytes.Buffer)
_, _ = buf.ReadFrom(resp.Body)
return "", fmt.Errorf("chat http %d: %s", resp.StatusCode, buf.String())
}
var out struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return "", err
}
if len(out.Choices) == 0 {
return "", fmt.Errorf("chat: empty choices")
}
return out.Choices[0].Message.Content, nil
}