-
Notifications
You must be signed in to change notification settings - Fork 42
/
voyage-rerank.go
170 lines (142 loc) 路 3.81 KB
/
voyage-rerank.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
164
165
166
167
168
169
170
package transformer
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"os"
"github.com/henomis/lingoose/document"
"github.com/henomis/lingoose/types"
"github.com/henomis/restclientgo"
)
const (
defaultVoyageRerankEndpoint = "https://api.voyageai.com/v1"
defaultVoyageRerankModel = "rerank-lite-1"
VoyageRerankScoreMetdataKey = "voyage-rerank-score"
)
type VoyageRerank struct {
model string
restClient *restclientgo.RestClient
}
func NewVoyageRerank() *VoyageRerank {
apiKey := os.Getenv("VOYAGE_API_KEY")
return &VoyageRerank{
restClient: restclientgo.New(defaultVoyageRerankEndpoint).WithRequestModifier(
func(req *http.Request) *http.Request {
req.Header.Set("Authorization", "Bearer "+apiKey)
return req
}),
model: defaultVoyageRerankModel,
}
}
func (v *VoyageRerank) WithModel(model string) *VoyageRerank {
v.model = model
return v
}
func (v *VoyageRerank) Rerank(
ctx context.Context,
query string,
documents []document.Document,
) ([]document.Document, error) {
resp := &voyageRerankResponse{}
err := v.restClient.Post(
ctx,
&voyageRerankRequest{
Documents: v.documentsToStringSlice(documents),
Model: v.model,
Query: query,
},
resp,
)
if err != nil {
return nil, err
}
return v.rerankDocuments(documents, resp.Data), nil
}
func (v *VoyageRerank) rerankDocuments(
documents []document.Document,
results []voyageRerankResponseData,
) []document.Document {
rerankedDocuments := make([]document.Document, 0)
for _, result := range results {
index := result.Index
metadata := documents[index].Metadata
if metadata == nil {
metadata = make(types.Meta)
}
metadata[VoyageRerankScoreMetdataKey] = result.RelevanceScore
rerankedDocuments = append(
rerankedDocuments,
document.Document{
Content: documents[index].Content,
Metadata: metadata,
},
)
}
return rerankedDocuments
}
func (v *VoyageRerank) documentsToStringSlice(documents []document.Document) []string {
strings := make([]string, len(documents))
for i, d := range documents {
strings[i] = d.Content
}
return strings
}
// API
type voyageRerankRequest struct {
Model string `json:"model"`
Documents []string `json:"documents"`
Query string `json:"query"`
}
func (r *voyageRerankRequest) Path() (string, error) {
return "/rerank", nil
}
func (r *voyageRerankRequest) Encode() (io.Reader, error) {
jsonBytes, err := json.Marshal(r)
if err != nil {
return nil, err
}
return bytes.NewReader(jsonBytes), nil
}
func (r *voyageRerankRequest) ContentType() string {
return "application/json"
}
type voyageRerankResponse struct {
HTTPStatusCode int `json:"-"`
acceptContentType string `json:"-"`
Object string `json:"object"`
Data []voyageRerankResponseData `json:"data"`
Model string `json:"model"`
RawBody []byte `json:"-"`
}
type voyageRerankResponseData struct {
Object string `json:"object"`
RelevanceScore float64 `json:"relevance_score"`
Index int `json:"index"`
}
func (r *voyageRerankResponse) SetAcceptContentType(contentType string) {
r.acceptContentType = contentType
}
func (r *voyageRerankResponse) Decode(body io.Reader) error {
return json.NewDecoder(body).Decode(r)
}
func (r *voyageRerankResponse) SetBody(body io.Reader) error {
b, err := io.ReadAll(body)
if err != nil {
return err
}
r.RawBody = b
return nil
}
func (r *voyageRerankResponse) AcceptContentType() string {
if r.acceptContentType != "" {
return r.acceptContentType
}
return "application/json"
}
func (r *voyageRerankResponse) SetStatusCode(code int) error {
r.HTTPStatusCode = code
return nil
}
func (r *voyageRerankResponse) SetHeaders(_ restclientgo.Headers) error { return nil }