-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponentGitee.go
163 lines (151 loc) · 4.4 KB
/
componentGitee.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
152
153
154
155
156
157
158
159
160
161
162
163
// 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>.
// 百度千帆平台,主要适配Reranker模型
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"time"
)
// GiteeDocumentChunkReranker 百度千帆对DocumentChunks进行重新排序
type GiteeDocumentChunkReranker struct {
APIKey string `json:"api_key,omitempty"`
Model string `json:"model,omitempty"`
BaseURL string `json:"base_url,omitempty"`
DefaultHeaders map[string]string `json:"defaultHeaders,omitempty"`
Timeout int `json:"timeout,omitempty"`
// Query 需要查询的关键字
Query string `json:"query,omitempty"`
// TopN 检索多少条
TopN int `json:"top_n,omitempty"`
// Score ranker的score匹配分数
Score float32 `json:"score,omitempty"`
client *http.Client `json:"-"`
}
func (component *GiteeDocumentChunkReranker) Initialization(ctx context.Context, input map[string]interface{}) error {
if component.Timeout == 0 {
component.Timeout = 180
}
component.client = &http.Client{
Timeout: time.Second * time.Duration(component.Timeout),
}
if component.APIKey == "" {
component.APIKey = config.AIAPIkey
}
if component.BaseURL == "" {
if config.AIBaseURL == "" {
return nil
}
index := strings.Index(config.AIBaseURL, "/v1")
if index <= 0 {
return nil
}
component.BaseURL = config.AIBaseURL[:index] + "/api/serverless/bge-reranker-v2-m3/rerank"
}
if component.DefaultHeaders == nil {
component.DefaultHeaders = make(map[string]string, 0)
}
return nil
}
func (component *GiteeDocumentChunkReranker) Run(ctx context.Context, input map[string]interface{}) error {
topN := 0
var score float32 = 0.0
dcs, has := input["documentChunks"]
if !has || dcs == nil {
err := errors.New(funcT("input['documentChunks'] cannot be empty"))
input[errorKey] = err
return err
}
queryObj, has := input["query"]
if !has {
return errors.New(funcT("input['query'] cannot be empty"))
}
query := queryObj.(string)
if query == "" {
return errors.New(funcT("input['query'] cannot be empty"))
}
tId, has := input["topN"]
if has {
topN = tId.(int)
}
if topN == 0 {
topN = component.TopN
}
if topN == 0 {
topN = 5
}
disId, has := input["score"]
if has {
score = disId.(float32)
}
if score <= 0 {
score = component.Score
}
documentChunks := dcs.([]DocumentChunk)
if topN > len(documentChunks) {
topN = len(documentChunks)
}
if len(documentChunks) < 1 { //没有文档,不需要重排
return nil
}
documents := make([]string, 0)
for i := 0; i < len(documentChunks); i++ {
documents = append(documents, documentChunks[i].Markdown)
}
bodyMap := map[string]interface{}{
"model": component.Model,
"query": query,
"top_n": topN,
"documents": documents,
}
rsStringByte, err := httpPostJsonBody(component.client, component.APIKey, component.BaseURL, component.DefaultHeaders, bodyMap)
if err != nil {
input[errorKey] = err
return err
}
rs := struct {
Results []struct {
Document struct {
Text string `json:"text,omitempty"`
} `json:"document,omitempty"`
RelevanceScore float32 `json:"relevance_score,omitempty"`
} `json:"results,omitempty"`
}{}
err = json.Unmarshal(rsStringByte, &rs)
if err != nil {
input[errorKey] = err
return err
}
rerankerDCS := make([]DocumentChunk, 0)
for i := 0; i < len(rs.Results); i++ {
markdown := rs.Results[i].Document.Text
for j := 0; j < len(documentChunks); j++ {
dc := documentChunks[j]
if markdown == dc.Markdown { //相等
dc.Score = rs.Results[i].RelevanceScore
rerankerDCS = append(rerankerDCS, dc)
break
}
}
}
rerankerDCS = sortDocumentChunksScore(rerankerDCS, topN, score)
input["documentChunks"] = rerankerDCS
return nil
}