-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
257 lines (224 loc) · 6.53 KB
/
Copy pathutils.go
File metadata and controls
257 lines (224 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// utils.go
package knowledgesdk
import "time"
// PointerUtils 提供各种类型到指针的转换工具函数
// 这些函数主要用于解决GORM更新时零值不更新的问题
// String 将字符串转换为字符串指针
func String(s string) *string {
return &s
}
// StringPtr 返回字符串指针,如果字符串为空则返回nil
func StringPtr(s string) *string {
if s == "" {
return nil
}
return &s
}
// Int 将int转换为int指针
func Int(i int) *int {
return &i
}
// IntPtr 返回int指针,通常用于可能为0的值
func IntPtr(i int) *int {
return &i
}
// Int64 将int64转换为int64指针
func Int64(i int64) *int64 {
return &i
}
// Float32 将float32转换为float32指针
func Float32(f float32) *float32 {
return &f
}
// Float64 将float64转换为float64指针
func Float64(f float64) *float64 {
return &f
}
// Bool 将bool转换为bool指针
func Bool(b bool) *bool {
return &b
}
// Time 将time.Time转换为time.Time指针
func Time(t time.Time) *time.Time {
return &t
}
// TimePtr 返回time.Time指针,如果时间为零值则返回nil
func TimePtr(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}
// StringValue 安全地从字符串指针获取值,如果指针为nil则返回空字符串
func StringValue(s *string) string {
if s == nil {
return ""
}
return *s
}
// IntValue 安全地从int指针获取值,如果指针为nil则返回0
func IntValue(i *int) int {
if i == nil {
return 0
}
return *i
}
// Float32Value 安全地从float32指针获取值,如果指针为nil则返回0
func Float32Value(f *float32) float32 {
if f == nil {
return 0
}
return *f
}
// BoolValue 安全地从bool指针获取值,如果指针为nil则返回false
func BoolValue(b *bool) bool {
if b == nil {
return false
}
return *b
}
// TimeValue 安全地从time.Time指针获取值,如果指针为nil则返回零值
func TimeValue(t *time.Time) time.Time {
if t == nil {
return time.Time{}
}
return *t
}
// HasValue 检查指针是否有值(不为nil)
func HasStringValue(s *string) bool {
return s != nil
}
func HasIntValue(i *int) bool {
return i != nil
}
func HasFloat32Value(f *float32) bool {
return f != nil
}
func HasBoolValue(b *bool) bool {
return b != nil
}
func HasTimeValue(t *time.Time) bool {
return t != nil
}
// 创建结构体的工厂函数,方便创建带有指针字段的结构体
// NewKnowledgeBase 创建一个新的KnowledgeBase实例,会自动设置默认值
func NewKnowledgeBase(name, description string) *KnowledgeBase {
kb := &KnowledgeBase{
Name: String(name),
Description: String(description),
}
// 设置默认值
kb.SetDefaults()
return kb
}
// NewKnowledgeBaseWithDefaults 创建一个新的KnowledgeBase实例,允许覆盖默认值
func NewKnowledgeBaseWithDefaults(name, description string, overrides map[string]interface{}) *KnowledgeBase {
kb := NewKnowledgeBase(name, description)
// 应用覆盖值
if temperature, ok := overrides["temperature"].(float32); ok {
kb.Temperature = Float32(temperature)
}
if chunkSize, ok := overrides["chunk_size"].(int); ok {
kb.ChunkSize = Int(chunkSize)
}
if overlap, ok := overrides["overlap"].(int); ok {
kb.Overlap = Int(overlap)
}
if topK, ok := overrides["top_k"].(int); ok {
kb.TopK = Int(topK)
}
if similarityThreshold, ok := overrides["similarity_threshold"].(float32); ok {
kb.SimilarityThreshold = Float32(similarityThreshold)
}
if maxReferenceLength, ok := overrides["max_reference_length"].(int); ok {
kb.MaxReferenceLength = Int(maxReferenceLength)
}
if enableRigorousAnswer, ok := overrides["enable_rigorous_answer"].(bool); ok {
kb.EnableRigorousAnswer = Bool(enableRigorousAnswer)
}
if modelID, ok := overrides["model_id"].(string); ok {
kb.ModelID = String(modelID)
}
return kb
}
// NewDocument 创建一个新的Document实例,会自动设置默认值
func NewDocument(kbID, name, content, creatorID string) *Document {
doc := &Document{
KBID: String(kbID),
Name: String(name),
OriginalContent: String(content),
CreatorID: String(creatorID),
}
// 设置默认值
doc.SetDefaults()
return doc
}
// NewChunk 创建一个新的Chunk实例
func NewChunk(documentID string, chunkIndex int, content string) *Chunk {
return &Chunk{
DocumentID: String(documentID),
ChunkIndex: Int(chunkIndex),
Content: String(content),
}
}
// 更新助手函数,用于更新现有结构体的字段
// UpdateKnowledgeBaseFields 更新KnowledgeBase的字段(仅更新非nil的字段)
func (kb *KnowledgeBase) UpdateFields(updates map[string]interface{}) {
if name, ok := updates["name"].(string); ok {
kb.Name = String(name)
}
if description, ok := updates["description"].(string); ok {
kb.Description = String(description)
}
if modelID, ok := updates["model_id"].(string); ok {
kb.ModelID = String(modelID)
}
if temperature, ok := updates["temperature"].(float32); ok {
kb.Temperature = Float32(temperature)
}
if rigorousPrompt, ok := updates["rigorous_prompt"].(string); ok {
kb.RigorousPrompt = String(rigorousPrompt)
}
if enableRigorousAnswer, ok := updates["enable_rigorous_answer"].(bool); ok {
kb.EnableRigorousAnswer = Bool(enableRigorousAnswer)
}
if chunkSize, ok := updates["chunk_size"].(int); ok {
kb.ChunkSize = Int(chunkSize)
}
if overlap, ok := updates["overlap"].(int); ok {
kb.Overlap = Int(overlap)
}
if topK, ok := updates["top_k"].(int); ok {
kb.TopK = Int(topK)
}
if similarityThreshold, ok := updates["similarity_threshold"].(float32); ok {
kb.SimilarityThreshold = Float32(similarityThreshold)
}
if systemPromptTemplate, ok := updates["system_prompt_template"].(string); ok {
kb.SystemPromptTemplate = String(systemPromptTemplate)
}
if maxReferenceLength, ok := updates["max_reference_length"].(int); ok {
kb.MaxReferenceLength = Int(maxReferenceLength)
}
}
// UpdateDocumentFields 更新Document的字段(仅更新非nil的字段)
func (d *Document) UpdateFields(updates map[string]interface{}) {
if name, ok := updates["name"].(string); ok {
d.Name = String(name)
}
if originalContent, ok := updates["original_content"].(string); ok {
d.OriginalContent = String(originalContent)
}
if contentType, ok := updates["content_type"].(string); ok {
d.ContentType = String(contentType)
}
if status, ok := updates["status"].(string); ok {
d.Status = String(status)
}
}
// UpdateChunkFields 更新Chunk的字段(仅更新非nil的字段)
func (c *Chunk) UpdateFields(updates map[string]interface{}) {
if content, ok := updates["content"].(string); ok {
c.Content = String(content)
}
}