From 72ba8efb2584a866eb4425be9ae6c66e67f4af80 Mon Sep 17 00:00:00 2001 From: Havoc412 <2993167370@qq.com> Date: Sun, 24 Nov 2024 00:43:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(model=5Fes):=20=E4=BC=98=E5=8C=96=20En?= =?UTF-8?q?counter.TopK=20=E5=87=BD=E6=95=B0=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 Encounter.TopK 函数中添加了处理 ES 中 embedding 为 null 的情况的注释 - 调整了 body 字符串的格式,提高了可读性 - 移除了 web.go 中的多余导入和注释 --- app/model_es/encounter.go | 3 ++- routers/web.go | 3 +-- test/es/encounter_embedding/main.go | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 test/es/encounter_embedding/main.go diff --git a/app/model_es/encounter.go b/app/model_es/encounter.go index 3edaf74..82bf726 100644 --- a/app/model_es/encounter.go +++ b/app/model_es/encounter.go @@ -192,6 +192,7 @@ func (e *Encounter) TopK(embedding []float64, k int) ([]Encounter, error) { return nil, err } + // UPDATE 这里无法处理 ES 中 embedding 为 null 的情况。 body := fmt.Sprintf(`{ "size": %d, "query": { @@ -203,7 +204,7 @@ func (e *Encounter) TopK(embedding []float64, k int) ([]Encounter, error) { } } }, - "_source":["id"] + "_source": ["id"] }`, k, string(paramsJSON)) hits, err := model_handler.SearchRequest(body, e.IndexName()) diff --git a/routers/web.go b/routers/web.go index 8d1d305..871ae09 100644 --- a/routers/web.go +++ b/routers/web.go @@ -3,12 +3,10 @@ package routers import ( "catface/app/global/consts" "catface/app/global/variable" - // "catface/app/http/controller/captcha" // 验证码组件 // "catface/app/http/middleware/authorization" "catface/app/http/middleware/cors" validatorFactory "catface/app/http/validator/core/factory" - // TODO validatorFactory "catface/app/http/validator/core/factory" "catface/app/utils/gin_release" "net/http" @@ -16,6 +14,7 @@ import ( "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" "go.uber.org/zap" + ) // 该路由主要设置 后台管理系统等后端应用路由 diff --git a/test/es/encounter_embedding/main.go b/test/es/encounter_embedding/main.go new file mode 100644 index 0000000..2a9bac8 --- /dev/null +++ b/test/es/encounter_embedding/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math/rand" + "strings" +) + +func main() { + embedding := make([]float64, 768) + for i := range embedding { + embedding[i] = rand.Float64() + } + + // 将嵌入向量转换为字符串,每个元素之间用逗号隔开 + embeddingStr := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(embedding)), ","), "[]") + + // 打印前几个元素以验证 + fmt.Println(embeddingStr) +}