mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-22 09:58:06 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fddefc78a1 | |||
| 3f431063e2 | |||
| 18d7ad3a36 |
@ -34,15 +34,11 @@ type StaticEmbedder struct {
|
||||
jieba *gojieba.Jieba
|
||||
stopWords map[string]bool
|
||||
|
||||
// words 是词向量表。**用 float32 存**:源文件(fastText 文本格式)本身就是 float32,
|
||||
// 用 float64 存等于把 578 万……不,是 57.8 万词 × 300 维的常驻内存凭空翻倍
|
||||
// (实测生产:float64 → 1.29GB,float32 → 0.65GB)。相似度计算仍在 float64 里累加,
|
||||
// 精度不受影响。改回 float64 会被 TestStaticEmbedder_VectorMemIsFloat32 拦住。
|
||||
words map[string][]float32
|
||||
words map[string][]float64
|
||||
dim int
|
||||
loaded bool
|
||||
|
||||
unkVec []float32
|
||||
unkVec []float64
|
||||
unkNorm float64
|
||||
}
|
||||
|
||||
@ -154,7 +150,7 @@ func NewStaticEmbedder(modelPaths ...string) *StaticEmbedder {
|
||||
e := &StaticEmbedder{
|
||||
jieba: GetJieba(),
|
||||
stopWords: sw,
|
||||
words: make(map[string][]float32),
|
||||
words: make(map[string][]float64),
|
||||
}
|
||||
|
||||
if len(modelPaths) == 0 {
|
||||
@ -258,16 +254,15 @@ func (e *StaticEmbedder) load(spec string, primary bool) error {
|
||||
continue
|
||||
}
|
||||
|
||||
vec := make([]float32, dim)
|
||||
vec := make([]float64, dim)
|
||||
for i := 0; i < dim; i++ {
|
||||
// 源文件是 float32 精度的文本向量:用 32 位解析,与源数据一致。
|
||||
v, _ := strconv.ParseFloat(fields[i+1], 32)
|
||||
vec[i] = float32(v)
|
||||
v, _ := strconv.ParseFloat(fields[i+1], 64)
|
||||
vec[i] = v
|
||||
}
|
||||
e.words[word] = vec
|
||||
if primary {
|
||||
for i := range vecSum {
|
||||
vecSum[i] += float64(vec[i])
|
||||
vecSum[i] += vec[i]
|
||||
}
|
||||
count++
|
||||
}
|
||||
@ -281,13 +276,11 @@ func (e *StaticEmbedder) load(spec string, primary bool) error {
|
||||
for i := range vecSum {
|
||||
vecSum[i] /= float64(count)
|
||||
}
|
||||
e.unkVec = make([]float32, dim)
|
||||
for i, v := range vecSum {
|
||||
e.unkVec[i] = float32(v)
|
||||
}
|
||||
e.unkVec = make([]float64, dim)
|
||||
copy(e.unkVec, vecSum)
|
||||
var normSq float64
|
||||
for _, v := range e.unkVec {
|
||||
normSq += float64(v) * float64(v)
|
||||
normSq += v * v
|
||||
}
|
||||
e.unkNorm = float64(math.Sqrt(normSq))
|
||||
e.loaded = true
|
||||
@ -373,11 +366,11 @@ func (e *StaticEmbedder) Vectorize(text string) vector.Vector {
|
||||
|
||||
if !ok {
|
||||
for i, v := range unkVec {
|
||||
sum[i] += w * float64(v)
|
||||
sum[i] += w * v
|
||||
}
|
||||
} else {
|
||||
for i, v := range vec {
|
||||
sum[i] += w * float64(v)
|
||||
sum[i] += w * v
|
||||
}
|
||||
}
|
||||
weightSum += w
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// 词向量必须用 float32 存。
|
||||
//
|
||||
// 这条判据是拿生产内存换来的:向量本体 = 词数 × 维数 × 每元素字节数。
|
||||
// 生产配置加载了 200000(zh) + 378151(en) = 57.8 万词 × 300 维 ⇒
|
||||
// float64 = 1.29GB、float32 = 0.65GB(差 0.65GB 常驻)。
|
||||
// 源数据(fastText 文本格式)本身就是 float32 精度,用 float64 存没有任何收益。
|
||||
//
|
||||
// 若有人把类型改回 float64,本测试**编译失败**(`var vec []float32` 的类型断言),
|
||||
// 这正是想要的效果。
|
||||
func TestStaticEmbedder_VectorMemIsFloat32(t *testing.T) {
|
||||
e := newSynthEmbedder(t, 300)
|
||||
|
||||
words := 0
|
||||
bytes := 0
|
||||
for _, vec := range e.words {
|
||||
var typed []float32 = vec // 编译期断言:存储必须是 []float32
|
||||
if len(typed) != e.dim {
|
||||
t.Fatalf("维度不符: %d != %d", len(typed), e.dim)
|
||||
}
|
||||
words++
|
||||
bytes += len(typed) * int(unsafe.Sizeof(typed[0]))
|
||||
}
|
||||
if words == 0 {
|
||||
t.Fatal("合成模型应至少加载一个词")
|
||||
}
|
||||
// float32:每词 300×4 = 1200 字节;float64 会是 2400
|
||||
if want := words * e.dim * 4; bytes != want {
|
||||
t.Fatalf("向量本体字节数应 %d(float32),实际 %d", want, bytes)
|
||||
}
|
||||
}
|
||||
@ -138,39 +138,28 @@ func (p *Plugin) Stop() error {
|
||||
func (p *Plugin) registerTools(s *sdk.PluginSDK) {
|
||||
s.RegisterTool("plugin_install", sdk.ToolDef{
|
||||
Name: "plugin_install",
|
||||
Description: "安装 HomeAgent 插件包(.hmap)。两种来源:url(http/https 下载)或 path(本机路径,配合 plugindev_build 的产物用这个)。插件已存在时传 overwrite=true 原地更新(升级/降级/重装,保留配置表,无需卸载重装)。更新后需调用 plgreload 或重启生效。",
|
||||
Description: "从 URL 安装 HomeAgent 插件包(.hmap 文件)。插件已存在时传 overwrite=true 原地更新(升级/降级/重装,保留配置表,无需卸载重装)。更新后需调用 plgreload 或重启生效。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"url": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "插件包的下载 URL(http/https)",
|
||||
},
|
||||
"path": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "插件包在**本机**的路径(.hmap)。与 url 二选一;同时给出时以 path 为准",
|
||||
"description": "插件包的下载 URL",
|
||||
},
|
||||
"overwrite": map[string]interface{}{
|
||||
"type": "boolean",
|
||||
"description": "已存在时原地更新(保留配置)。默认 false",
|
||||
},
|
||||
},
|
||||
"required": []string{"url"},
|
||||
},
|
||||
}, func(args map[string]interface{}) (interface{}, error) {
|
||||
overwrite, _ := args["overwrite"].(bool)
|
||||
// path 优先:它对应"agent 自己构建出产物再装"的场景(plugindev_build → plugin_install)。
|
||||
if path, _ := args["path"].(string); strings.TrimSpace(path) != "" {
|
||||
pth := strings.TrimSpace(path)
|
||||
if st, err := os.Stat(pth); err != nil || st.IsDir() {
|
||||
return map[string]interface{}{"error": fmt.Sprintf("path 无效(必须是存在的 .hmap 文件): %s", pth)}, nil
|
||||
}
|
||||
return p.installFromPath(pth, overwrite)
|
||||
}
|
||||
url, _ := args["url"].(string)
|
||||
if strings.TrimSpace(url) == "" {
|
||||
return map[string]interface{}{"error": "需要 url 或 path(二选一)"}, nil
|
||||
if url == "" {
|
||||
return map[string]interface{}{"error": "url is required"}, nil
|
||||
}
|
||||
return p.installFromURL(strings.TrimSpace(url), overwrite)
|
||||
overwrite, _ := args["overwrite"].(bool)
|
||||
return p.installFromURL(url, overwrite)
|
||||
})
|
||||
|
||||
s.RegisterTool("plugin_list", sdk.ToolDef{
|
||||
@ -466,12 +455,12 @@ func (p *Plugin) installFromData(data []byte, overwrite bool) (interface{}, erro
|
||||
|
||||
if existing && !overwrite {
|
||||
return map[string]interface{}{
|
||||
"error": "plugin already exists",
|
||||
"name": pkg.Name,
|
||||
"version": pkg.Version,
|
||||
"current": oldVersion,
|
||||
"action": "remove_first",
|
||||
"hint": `传 "overwrite": true 可原地更新(保留配置)`,
|
||||
"error": "plugin already exists",
|
||||
"name": pkg.Name,
|
||||
"version": pkg.Version,
|
||||
"current": oldVersion,
|
||||
"action": "remove_first",
|
||||
"hint": `传 "overwrite": true 可原地更新(保留配置)`,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -486,7 +475,7 @@ func (p *Plugin) installFromData(data []byte, overwrite bool) (interface{}, erro
|
||||
os.RemoveAll(backup)
|
||||
if err := os.Rename(target, backup); err != nil {
|
||||
return map[string]interface{}{
|
||||
"error": "backup old plugin dir failed",
|
||||
"error": "backup old plugin dir failed",
|
||||
"details": err.Error(),
|
||||
}, nil
|
||||
}
|
||||
@ -495,8 +484,8 @@ func (p *Plugin) installFromData(data []byte, overwrite bool) (interface{}, erro
|
||||
os.RemoveAll(target)
|
||||
if rbErr := os.Rename(backup, target); rbErr != nil {
|
||||
return map[string]interface{}{
|
||||
"error": "extract failed AND rollback failed",
|
||||
"details": err.Error(),
|
||||
"error": "extract failed AND rollback failed",
|
||||
"details": err.Error(),
|
||||
"rollback": rbErr.Error(),
|
||||
}, nil
|
||||
}
|
||||
@ -518,15 +507,15 @@ func (p *Plugin) installFromData(data []byte, overwrite bool) (interface{}, erro
|
||||
action = "reinstalled"
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"status": "installed",
|
||||
"name": pkg.Name,
|
||||
"version": pkg.Version,
|
||||
"status": "installed",
|
||||
"name": pkg.Name,
|
||||
"version": pkg.Version,
|
||||
"previous_version": oldVersion,
|
||||
"entry": pkg.Entry,
|
||||
"checksum": checksum,
|
||||
"action": action,
|
||||
"reload_required": true,
|
||||
"config_kept": true,
|
||||
"entry": pkg.Entry,
|
||||
"checksum": checksum,
|
||||
"action": action,
|
||||
"reload_required": true,
|
||||
"config_kept": true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user