Agnes 2.0-flash是推理模型,推理token占大量预算。 800仍不够→JSON截断(parse error: unexpected end of JSON input)。 1200确保推理+正文都够用。
255 lines
7.0 KiB
Go
255 lines
7.0 KiB
Go
// 织忆 MemoryWeave — Go Client SDK
|
|
// Hermes / OpenClaw 的客户端库,零外部依赖
|
|
//
|
|
// 使用:
|
|
// client := NewClient("http://localhost:7821", "mw-your-api-key")
|
|
// mid, err := client.Commit("hermes", "shared", "牧尘用 Deepin 25", "system_fact")
|
|
// results, err := client.Recall("牧尘系统是什么", 5)
|
|
package routes
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// ─── ZhiYiClient ────────────────────────────────────────
|
|
|
|
type ZhiYiClient struct {
|
|
BaseURL string
|
|
APIKey string
|
|
HTTP *http.Client
|
|
}
|
|
|
|
func NewClient(baseURL, apiKey string) *ZhiYiClient {
|
|
return &ZhiYiClient{
|
|
BaseURL: baseURL,
|
|
APIKey: apiKey,
|
|
HTTP: &http.Client{Timeout: 30 * time.Second},
|
|
}
|
|
}
|
|
|
|
// NewClientFromEnv 从环境变量创建客户端(Hermes/OpenClaw 集成用)
|
|
// 环境变量: ZHIYI_URL (默认 http://localhost:7821), ZHIYI_API_KEY
|
|
func NewClientFromEnv() *ZhiYiClient {
|
|
url := os.Getenv("ZHIYI_URL")
|
|
if url == "" {
|
|
url = "http://localhost:7821"
|
|
}
|
|
key := os.Getenv("ZHIYI_API_KEY")
|
|
if key == "" {
|
|
key = os.Getenv("API_KEY") // fallback
|
|
}
|
|
return NewClient(url, key)
|
|
}
|
|
|
|
func (c *ZhiYiClient) do(method, path string, body interface{}) ([]byte, error) {
|
|
var r io.Reader
|
|
if body != nil {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
r = bytes.NewReader(data)
|
|
}
|
|
|
|
req, err := http.NewRequest(method, c.BaseURL+path, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-API-Key", c.APIKey)
|
|
|
|
resp, err := c.HTTP.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("zhiyi request: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if resp.StatusCode >= 400 {
|
|
return nil, fmt.Errorf("zhiyi %s %s: %d %s", method, path, resp.StatusCode, string(respBody))
|
|
}
|
|
return respBody, nil
|
|
}
|
|
|
|
// ─── 记忆写入 ──────────────────────────────────────────
|
|
|
|
// Commit 提交一条记忆
|
|
func (c *ZhiYiClient) Commit(agentID, namespace, content, category string) (string, error) {
|
|
resp, err := c.do("POST", "/api/v1/commit", map[string]string{
|
|
"agent_id": agentID, "namespace": namespace,
|
|
"content": content, "category": category,
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var result map[string]string
|
|
json.Unmarshal(resp, &result)
|
|
return result["episode_id"], nil
|
|
}
|
|
|
|
// BatchCommit 批量提交记忆
|
|
func (c *ZhiYiClient) BatchCommit(items []CommitItem) ([]string, error) {
|
|
type item struct {
|
|
AgentID string `json:"agent_id"`
|
|
Namespace string `json:"namespace"`
|
|
Content string `json:"content"`
|
|
Category string `json:"category"`
|
|
}
|
|
var batch struct {
|
|
Items []item `json:"items"`
|
|
}
|
|
for _, it := range items {
|
|
batch.Items = append(batch.Items, item{
|
|
AgentID: it.AgentID, Namespace: it.Namespace,
|
|
Content: it.Content, Category: it.Category,
|
|
})
|
|
}
|
|
resp, err := c.do("POST", "/api/v1/batch-commit", batch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result struct {
|
|
IDs []string `json:"ids"`
|
|
}
|
|
json.Unmarshal(resp, &result)
|
|
return result.IDs, nil
|
|
}
|
|
|
|
type CommitItem struct {
|
|
AgentID, Namespace, Content, Category string
|
|
}
|
|
|
|
// ─── 记忆召回 ──────────────────────────────────────────
|
|
|
|
// RecallResult 召回结果
|
|
type ClientRecallResult struct {
|
|
ID string `json:"id"`
|
|
Content string `json:"content"`
|
|
Category string `json:"category"`
|
|
Score float64 `json:"score"`
|
|
}
|
|
|
|
// Recall 召回记忆
|
|
func (c *ZhiYiClient) Recall(query string, limit int) ([]ClientRecallResult, error) {
|
|
if limit <= 0 {
|
|
limit = 10
|
|
}
|
|
resp, err := c.do("POST", "/api/v1/recall", map[string]interface{}{
|
|
"query": query, "limit": limit,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result struct {
|
|
Results []ClientRecallResult `json:"results"`
|
|
}
|
|
json.Unmarshal(resp, &result)
|
|
return result.Results, nil
|
|
}
|
|
|
|
// Bootstrap 冷启动引导
|
|
func (c *ZhiYiClient) Bootstrap(agentID string, limit int) ([]ClientRecallResult, error) {
|
|
resp, err := c.do("GET", fmt.Sprintf("/api/v1/bootstrap?agent_id=%s&limit=%d", agentID, limit), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result struct {
|
|
Results []ClientRecallResult `json:"results"`
|
|
}
|
|
json.Unmarshal(resp, &result)
|
|
return result.Results, nil
|
|
}
|
|
|
|
// ─── 反馈 ──────────────────────────────────────────────
|
|
|
|
func (c *ZhiYiClient) MarkUseful(memoryID string) error {
|
|
_, err := c.do("POST", "/api/v1/feedback/useful", map[string]string{"memory_id": memoryID})
|
|
return err
|
|
}
|
|
|
|
func (c *ZhiYiClient) MarkNotUseful(memoryID string) error {
|
|
_, err := c.do("POST", "/api/v1/feedback/not-useful", map[string]string{"memory_id": memoryID})
|
|
return err
|
|
}
|
|
|
|
func (c *ZhiYiClient) Correct(memoryID, newContent, source string) error {
|
|
_, err := c.do("POST", "/api/v1/feedback/correct", map[string]string{
|
|
"memory_id": memoryID, "new_content": newContent, "source": source,
|
|
})
|
|
return err
|
|
}
|
|
|
|
// ─── 管理 ──────────────────────────────────────────────
|
|
|
|
// Stats 获取统计
|
|
func (c *ZhiYiClient) Stats() (map[string]interface{}, error) {
|
|
resp, err := c.do("GET", "/api/v1/stats", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result map[string]interface{}
|
|
json.Unmarshal(resp, &result)
|
|
return result, nil
|
|
}
|
|
|
|
// Metrics 获取自优化指标
|
|
func (c *ZhiYiClient) Metrics() (map[string]float64, error) {
|
|
resp, err := c.do("GET", "/api/v1/metrics/self", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result map[string]float64
|
|
json.Unmarshal(resp, &result)
|
|
return result, nil
|
|
}
|
|
|
|
// Health 健康检查
|
|
func (c *ZhiYiClient) Health() (map[string]string, error) {
|
|
resp, err := c.do("GET", "/health", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result map[string]string
|
|
json.Unmarshal(resp, &result)
|
|
return result, nil
|
|
}
|
|
|
|
// ─── 图谱 ──────────────────────────────────────────────
|
|
|
|
func (c *ZhiYiClient) GraphNavigate(entity string, maxHops int) ([]map[string]interface{}, error) {
|
|
resp, err := c.do("POST", "/api/v1/graph/navigate", map[string]interface{}{
|
|
"entity": entity, "max_hops": maxHops,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result struct {
|
|
Paths []map[string]interface{} `json:"paths"`
|
|
}
|
|
json.Unmarshal(resp, &result)
|
|
return result.Paths, nil
|
|
}
|
|
|
|
// ─── 自调参 ────────────────────────────────────────────
|
|
|
|
func (c *ZhiYiClient) AutoTune() ([]map[string]interface{}, error) {
|
|
resp, err := c.do("POST", "/api/v1/tuning/run", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result struct {
|
|
Events []map[string]interface{} `json:"events"`
|
|
}
|
|
json.Unmarshal(resp, &result)
|
|
return result.Events, nil
|
|
}
|