-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceDocument.go
151 lines (136 loc) · 4.46 KB
/
serviceDocument.go
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
// Copyright (c) 2025 minRAG Authors.
//
// This file is part of minRAG.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses>.
package main
import (
"context"
"errors"
"strings"
"gitee.com/chunanyong/zorm"
)
// updateDocumentChunk 运行indexPipeline流水线,更新Document,DocumentChunk,VecDocumentChunk
func updateDocumentChunk(ctx context.Context, document *Document) (bool, error) {
input := make(map[string]interface{})
input["document"] = document
indexPipeline, has := componentMap["indexPipeline"]
if !has || indexPipeline == nil {
return false, errors.New("indexPipeline is empty")
}
err := indexPipeline.Run(ctx, input)
if err != nil {
return false, err
}
errObj, has := input[errorKey]
if has || errObj != nil {
return false, errObj.(error)
}
return true, nil
}
// findDocumentIdByFilePath 根据文档路径查询文档ID
func findDocumentIdByFilePath(ctx context.Context, filePath string) (string, error) {
finder := zorm.NewSelectFinder(tableDocumentName, "id").Append("WHERE filePath=?", filePath)
id := ""
_, err := zorm.QueryRow(ctx, finder, &id)
return id, err
}
// findDocumentChunkMarkDown 查询DocumentChunk的markdown
func findDocumentChunkMarkDown(ctx context.Context, documentChunks []DocumentChunk) ([]DocumentChunk, error) {
for i := 0; i < len(documentChunks); i++ {
documentChunk := documentChunks[i]
finder := zorm.NewSelectFinder(tableDocumentChunkName, "markdown").Append("WHERE id=?", documentChunk.Id)
markdown := ""
_, err := zorm.QueryRow(ctx, finder, &markdown)
if err != nil {
return documentChunks, err
}
documentChunks[i].Markdown = markdown
}
return documentChunks, nil
}
// funcDeleteDocumentById 根据文档ID删除 Document,DocumentChunk,VecDocumentChunk
func funcDeleteDocumentById(ctx context.Context, id string) error {
_, err := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {
f1 := zorm.NewDeleteFinder(tableDocumentName).Append("WHERE id=?", id)
count, err := zorm.UpdateFinder(ctx, f1)
if err != nil {
return count, err
}
f2 := zorm.NewDeleteFinder(tableDocumentChunkName).Append("WHERE documentID=?", id)
count, err = zorm.UpdateFinder(ctx, f2)
if err != nil {
return count, err
}
f3 := zorm.NewDeleteFinder(tableVecDocumentChunkName).Append("WHERE documentID=?", id)
return zorm.UpdateFinder(ctx, f3)
})
return err
}
// recursiveSplit 递归分割实现
func (component *DocumentSplitter) recursiveSplit(text string, depth int) []string {
chunks := make([]string, 0)
// 终止条件:处理完所有分隔符
if depth >= len(component.SplitBy) {
if text != "" {
return append(chunks, text)
}
return chunks
}
currentSep := component.SplitBy[depth]
parts := strings.Split(text, currentSep)
for i, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
partContent := part
if i < len(parts)-1 { //不是最后一个
partContent = partContent + currentSep
}
// 处理超长内容
if len(part) >= component.SplitLength {
partLeaf := component.recursiveSplit(partContent, depth+1)
if len(partLeaf) > 0 {
chunks = append(chunks, partLeaf...)
}
continue
} else {
chunks = append(chunks, partContent)
}
}
return chunks
}
// mergeChunks 合并短内容
func (component *DocumentSplitter) mergeChunks(chunks []string) []string {
// 合并短内容
for i := 0; i < len(chunks); i++ {
chunk := chunks[i]
if len(chunk) >= component.SplitLength || i+1 >= len(chunks) {
continue
}
nextChunk := chunks[i+1]
// 汉字字符占位3个长度
if (len(chunk) + len(nextChunk)) > (component.SplitLength*18)/10 {
continue
}
chunks[i] = chunk + nextChunk
if i+2 >= len(chunks) { //倒数第二个元素,去掉最后一个
chunks = chunks[:len(chunks)-1]
} else { // 去掉 i+1 索引元素,合并到了 i 索引
chunks = append(chunks[:i+1], chunks[i+2:]...)
}
}
return chunks
}