Havoc412 ae7edb5e8d 🎨 refactor(rag): 重构 RAG 模型相关代码
- 重构了 rag_controller.go 中的逻辑,使用新的 DocumentHub 结构
- 修改了 encounter.go 中的 Encounter 结构,增加了 explain 标签
- 重写了 rag_websocket.go 中的逻辑,使用新的 DocumentHub 结构
- 新增了 curd_es/encounter_es_curd.go 文件,实现了 Encounter 的 CURD 操作
- 更新了 nlp/func.go 中的 ChatRAG 函数,使用新的 DocumentHub 结构
- 新增了 curd/docs_hub.go 文件,实现了 DocumentHub 的 TopK 方法
- 新增了 utils/data_explain/data_explain_rag.go 文件,实现了结构体到解释字符串的转换
2024-11-20 19:30:11 +08:00

51 lines
1.3 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.

package main
import (
"fmt"
"reflect"
"strings"
"time"
)
// EncounterResult 结构体定义
type EncounterResult struct {
Id int64 `json:"id"`
Title string `json:"title" explain:"路遇笔记标题"`
Content string `json:"content" explain:"路遇笔记内容"`
UpdatedAt *time.Time `json:"updated_at" explain:"最后更新时间"`
NoTag string `json:"no_tag"` // 没有 explain 标签的字段
}
// StructToString 使用反射将结构体的内容组织为字符串,忽略没有 explain 标签的字段
func StructToString(v interface{}) string {
val := reflect.ValueOf(v)
typ := val.Type()
var result []string
for i := 0; i < val.NumField(); i++ {
field := typ.Field(i)
tag := field.Tag.Get("explain")
if tag == "" {
continue // 跳过没有 explain 标签的字段
}
value := val.Field(i).Interface()
result = append(result, fmt.Sprintf("%s%v", tag, value))
}
return strings.Join(result, "")
}
func main() {
// 示例数据
updatedAt := time.Date(2023, 10, 1, 12, 0, 0, 0, time.UTC)
encounter := EncounterResult{
Id: 1,
Title: "遇见小猫",
Content: "今天在公园遇到了一只可爱的小猫。",
UpdatedAt: &updatedAt,
NoTag: "这个字段没有 explain 标签",
}
// 调用 StructToString 函数
fmt.Println(StructToString(encounter))
}