@@ -33,16 +33,23 @@ import (
3333)
3434
3535type Config struct {
36- AK string
37- SK string
38-
36+ AK string
37+ SK string
38+ Domain string
39+ Model string
3940 Region string // default cn-north-1
4041}
4142
4243func NewReranker (config * Config ) rerank.Reranker {
4344 if config .Region == "" {
4445 config .Region = "cn-north-1"
4546 }
47+ if config .Domain == "" {
48+ config .Domain = domain
49+ }
50+ if config .Model == "" {
51+ config .Model = defaultModel
52+ }
4653 return & reranker {config : config }
4754}
4855
@@ -78,12 +85,32 @@ type rerankResp struct {
7885func (r * reranker ) Rerank (ctx context.Context , req * rerank.Request ) (* rerank.Response , error ) {
7986 rReq := & rerankReq {
8087 Datas : make ([]rerankData , 0 , len (req .Data )),
81- RerankModel : defaultModel ,
88+ RerankModel : r . config . Model ,
8289 }
83-
90+ sorted := make ([] * rerank. Data , 0 )
8491 var flat []* rerank.Data
92+ visited := map [string ]bool {}
8593 for _ , channel := range req .Data {
86- flat = append (flat , channel ... )
94+ if len (channel ) == 0 {
95+ continue
96+ }
97+ for _ , item := range channel {
98+ if item == nil || item .Document == nil {
99+ continue
100+ }
101+ if item .Document .ID == "" {
102+ sorted = append (sorted , & rerank.Data {
103+ Document : item .Document ,
104+ Score : 1 ,
105+ })
106+ continue
107+ }
108+ if visited [item .Document .ID ] {
109+ continue
110+ }
111+ visited [item .Document .ID ] = true
112+ flat = append (flat , item )
113+ }
87114 }
88115
89116 for _ , item := range flat {
@@ -117,7 +144,6 @@ func (r *reranker) Rerank(ctx context.Context, req *rerank.Request) (*rerank.Res
117144 return nil , fmt .Errorf ("[Rerank] failed, code=%d, msg=%v" , rResp .Code , rResp .Message )
118145 }
119146
120- sorted := make ([]* rerank.Data , 0 , len (rResp .Data .Scores ))
121147 for i , score := range rResp .Data .Scores {
122148 sorted = append (sorted , & rerank.Data {
123149 Document : flat [i ].Document ,
@@ -143,7 +169,7 @@ func (r *reranker) Rerank(ctx context.Context, req *rerank.Request) (*rerank.Res
143169func (r * reranker ) prepareRequest (body []byte ) * http.Request {
144170 u := url.URL {
145171 Scheme : "https" ,
146- Host : domain ,
172+ Host : r . config . Domain ,
147173 Path : "/api/knowledge/service/rerank" ,
148174 }
149175 req , _ := http .NewRequest (http .MethodPost , u .String (), bytes .NewReader (body ))
0 commit comments