-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrerank.go
More file actions
235 lines (209 loc) · 6.99 KB
/
Copy pathrerank.go
File metadata and controls
235 lines (209 loc) · 6.99 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
// Copyright 2025 Xavier Portilla Edo
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package bedrock
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
)
// RerankOptions configures a [Rerank] call. Pass it via
// [ai.RerankerRequest.Options].
type RerankOptions struct {
// TopN caps the number of ranked documents returned. If <= 0, every input
// document is returned, ordered by descending relevance.
TopN int `json:"topN,omitempty"`
}
// cohereRerankRequest is the Cohere Rerank InvokeModel body. api_version 2 is
// the current Bedrock contract.
type cohereRerankRequest struct {
Query string `json:"query"`
Documents []string `json:"documents"`
TopN int `json:"top_n"`
APIVersion int `json:"api_version"`
}
type cohereRerankResponse struct {
Results []cohereRerankResult `json:"results"`
}
type cohereRerankResult struct {
Index int `json:"index"`
RelevanceScore float64 `json:"relevance_score"`
}
const cohereRerankAPIVersion = 2
// Rerank reranks req.Documents by relevance to req.Query using a Bedrock
// reranking model, such as "cohere.rerank-v3-5:0". It returns documents in
// descending score order and attaches each score to [ai.RankedDocumentMetadata].
//
// Genkit Go does not yet expose a first-class reranker primitive, so this is a
// standalone helper. It looks up the initialized Bedrock plugin on g and reuses
// its configured Bedrock Runtime client.
func Rerank(ctx context.Context, g *genkit.Genkit, modelID string, req *ai.RerankerRequest) (*ai.RerankerResponse, error) {
if g == nil {
return nil, errors.New("bedrock.Rerank: Genkit instance required")
}
p, _ := genkit.LookupPlugin(g, provider).(*Bedrock)
if p == nil {
return nil, errors.New("bedrock.Rerank: bedrock plugin not registered")
}
p.mu.Lock()
initted := p.initted
client := p.client
requestTimeout := p.RequestTimeout
p.mu.Unlock()
if !initted {
return nil, errors.New("bedrock.Rerank: plugin not initialized")
}
if modelID == "" {
return nil, errors.New("bedrock.Rerank: model ID required")
}
if req == nil {
return nil, errors.New("bedrock.Rerank: request required")
}
ctx, cancel := withRequestTimeout(ctx, requestTimeout)
defer cancel()
return rerank(ctx, client, modelID, req)
}
func rerank(ctx context.Context, client BedrockClient, modelID string, req *ai.RerankerRequest) (*ai.RerankerResponse, error) {
if client == nil {
return nil, errors.New("bedrock.Rerank: Bedrock client required")
}
if modelID == "" {
return nil, errors.New("bedrock.Rerank: model ID required")
}
if req == nil {
return nil, errors.New("bedrock.Rerank: request required")
}
query := documentText(req.Query)
if query == "" {
return nil, errors.New("bedrock.Rerank: query has no text content")
}
docs := make([]string, 0, len(req.Documents))
for i, doc := range req.Documents {
text := documentText(doc)
if text == "" {
return nil, fmt.Errorf("bedrock.Rerank: document %d has no text content", i)
}
docs = append(docs, text)
}
if len(docs) == 0 {
return &ai.RerankerResponse{}, nil
}
topN := len(docs)
opts, err := rerankOptions(req.Options)
if err != nil {
return nil, err
}
if opts != nil && opts.TopN > 0 && opts.TopN < topN {
topN = opts.TopN
}
var resp cohereRerankResponse
if err := invokeRerankJSON(ctx, client, modelID, cohereRerankRequest{
Query: query,
Documents: docs,
TopN: topN,
APIVersion: cohereRerankAPIVersion,
}, &resp); err != nil {
return nil, err
}
return buildRerankResponse(resp, req.Documents)
}
func invokeRerankJSON(ctx context.Context, client BedrockClient, modelID string, req any, resp any) error {
body, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("bedrock.Rerank: failed to marshal request: %w", err)
}
out, err := client.InvokeModel(ctx, &bedrockruntime.InvokeModelInput{
ModelId: aws.String(modelID),
Body: body,
ContentType: aws.String("application/json"),
Accept: aws.String("application/json"),
})
if err != nil {
return fmt.Errorf("bedrock.Rerank: failed to invoke model: %w", err)
}
if out == nil {
return errors.New("bedrock.Rerank: empty invoke model response")
}
if err := json.Unmarshal(out.Body, resp); err != nil {
return fmt.Errorf("bedrock.Rerank: failed to unmarshal response: %w", err)
}
return nil
}
// buildRerankResponse maps Cohere's score results back onto the original
// documents, preserving reranked order and attaching each relevance score.
func buildRerankResponse(resp cohereRerankResponse, docs []*ai.Document) (*ai.RerankerResponse, error) {
out := &ai.RerankerResponse{
Documents: make([]*ai.RankedDocumentData, 0, len(resp.Results)),
}
for _, result := range resp.Results {
if result.Index < 0 || result.Index >= len(docs) {
return nil, fmt.Errorf("bedrock.Rerank: result index %d out of range for %d documents", result.Index, len(docs))
}
if docs[result.Index] == nil {
return nil, fmt.Errorf("bedrock.Rerank: result index %d references nil document", result.Index)
}
out.Documents = append(out.Documents, &ai.RankedDocumentData{
Content: docs[result.Index].Content,
Metadata: &ai.RankedDocumentMetadata{
Score: result.RelevanceScore,
},
})
}
return out, nil
}
func documentText(doc *ai.Document) string {
if doc == nil {
return ""
}
var texts []string
for _, part := range doc.Content {
if part != nil && part.IsText() && strings.TrimSpace(part.Text) != "" {
texts = append(texts, part.Text)
}
}
return strings.TrimSpace(strings.Join(texts, "\n"))
}
// rerankOptions extracts [RerankOptions] from the request's Options field,
// accepting either a value, pointer, or JSON-deserialized map. It returns nil
// options when options are absent.
func rerankOptions(o any) (*RerankOptions, error) {
switch v := o.(type) {
case nil:
return nil, nil
case *RerankOptions:
return v, nil
case RerankOptions:
return &v, nil
case map[string]any:
b, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("bedrock.Rerank: failed to marshal rerank options: %w", err)
}
var opts RerankOptions
if err := json.Unmarshal(b, &opts); err != nil {
return nil, fmt.Errorf("bedrock.Rerank: failed to unmarshal rerank options: %w", err)
}
return &opts, nil
default:
return nil, fmt.Errorf("bedrock.Rerank: unsupported rerank options type %T", o)
}
}