Files

142 lines
3.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 织忆 MemoryWeave — L3 skill 草稿生成器(t_fcadee04)
//
// 生产实现复用本地 2B(llama 队列代理 8081,免费腿,与 zhiyi-standing-answers.py 同源同通道)。
// 接口化:测试用确定性 fake(zero 网络 / CGO / 真模型),单测机械可复算。
package skillgen
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/xiaoxue/memoryweave/internal/pattern"
)
// Local2BGenerator 用本地 2B 生成 skill 草稿。
// env:LLM_ENDPOINT(默认 http://127.0.0.1:8081/v1/chat/completions)
// LLM_MODEL (默认 local/minicpm5-2b)
// LLM_TIMEOUT (默认 120s)
type Local2BGenerator struct {
endpoint string
model string
timeout time.Duration
client *http.Client
}
// NewLocal2BGenerator 读 env 构造本地 2B 生成器。
func NewLocal2BGenerator() *Local2BGenerator {
endpoint := os.Getenv("LLM_ENDPOINT")
if endpoint == "" {
endpoint = "http://127.0.0.1:8081/v1/chat/completions"
}
model := os.Getenv("LLM_MODEL")
if model == "" {
model = "local/minicpm5-2b"
}
to := 120 * time.Second
if v := os.Getenv("LLM_TIMEOUT"); v != "" {
if d, err := time.ParseDuration(v); err == nil && d > 0 {
to = d
}
}
return &Local2BGenerator{
endpoint: endpoint, model: model, timeout: to,
client: &http.Client{Timeout: to},
}
}
// Generate 把 pattern 转成一段可复用 skill 草稿 markdown。
func (g *Local2BGenerator) Generate(p pattern.Pattern) (string, error) {
if g == nil {
return "", fmt.Errorf("skillgen: 生成器为 nil")
}
prompt := draftPrompt(p)
body, _ := json.Marshal(map[string]interface{}{
"model": g.model,
"messages": []map[string]string{{"role": "user", "content": prompt}},
"max_tokens": 600,
"temperature": 0.3,
})
req, err := http.NewRequest("POST", g.endpoint, bytes.NewReader(body))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := g.client.Do(req)
if err != nil {
log.Printf("[skillgen] 本地 2B 调用失败: %v", err)
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("本地 2B 返回 %d", resp.StatusCode)
}
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 || out.Choices[0].Message.Content == "" {
return "", fmt.Errorf("本地 2B 空输出")
}
return out.Choices[0].Message.Content, nil
}
// draftPrompt 生成 skill 草稿的提示词。
func draftPrompt(p pattern.Pattern) string {
return fmt.Sprintf(`你是技能草稿作者。请把下面这条【已验证的重复模式】整理成一份可复用的 markdown 技能草稿。
要求:用简体中文;结构包含「适用场景 / 关键步骤 / 命令或操作要点 / 注意事项」;直接给正文,不要标题外的寒暄。
模式类型:%s
模式名:%s
置信度:%.2f
模式内容:%s
支撑任务数:%d
涉及工具:%s
技能草稿:`,
p.Type, p.Name, p.Confidence, p.Data, len(p.SourceTasks), joinTools(p.Tools))
}
func joinTools(tools []string) string {
if len(tools) == 0 {
return "-"
}
out := ""
for i, t := range tools {
if i > 0 {
out += " "
}
out += t
}
return out
}
// FakeGenerator 确定性生成器(测试专用):返回固定 markdown,不走网络。
type FakeGenerator struct {
Out string
Err error
Call int // 调用计数(断言生成次数用)
}
// Generate 返回固定草稿内容(含调用计数累加)。
func (f *FakeGenerator) Generate(p pattern.Pattern) (string, error) {
f.Call++
if f.Err != nil {
return "", f.Err
}
if f.Out != "" {
return f.Out, nil
}
return fmt.Sprintf("## 适用场景\n%s 的重复处理\n\n## 关键步骤\n1. 确认输入\n2. 执行工具组合\n\n## 注意事项\n已由 L2 模式验证,可直接复用。",
p.Name), nil
}