@@ -40,19 +40,19 @@ describe("bgeReranker", () => {
40
40
const highestRankDocument = documents [ highestRankIndex ] ;
41
41
expect ( highestRankDocument ) . to . eql ( "Mount Everest is the tallest mountain in the world" ) ;
42
42
43
- expect ( simplifyRanks ( [ highestRank ] ) [ 0 ] ) . toMatchInlineSnapshot ( "-4 " ) ;
43
+ expect ( simplifyRanks ( [ highestRank ] ) [ 0 ] ) . toMatchInlineSnapshot ( "0.01798620996209156 " ) ;
44
44
expect ( simplifyRanks ( ranks ) ) . toMatchInlineSnapshot ( `
45
45
[
46
- -11 ,
47
- -11 ,
48
- -11 ,
49
- -5.6 ,
50
- -11 ,
51
- -4 ,
52
- -11 ,
53
- -11 ,
54
- -11 ,
55
- -11 ,
46
+ 0.00001670142184809518 ,
47
+ 0.00001670142184809518 ,
48
+ 0.00001670142184809518 ,
49
+ 0.003684239899435989 ,
50
+ 0.00001670142184809518 ,
51
+ 0.01798620996209156 ,
52
+ 0.00001670142184809518 ,
53
+ 0.00001670142184809518 ,
54
+ 0.00001670142184809518 ,
55
+ 0.00001670142184809518 ,
56
56
]
57
57
` ) ;
58
58
} ) ;
@@ -91,19 +91,19 @@ describe("bgeReranker", () => {
91
91
const highestRankDocument = documents [ highestRankIndex ] ;
92
92
expect ( highestRankDocument ) . to . eql ( "Mount Everest is the tallest mountain in the world" ) ;
93
93
94
- expect ( simplifyRanks ( [ highestRank ] ) [ 0 ] ) . toMatchInlineSnapshot ( "-4 " ) ;
94
+ expect ( simplifyRanks ( [ highestRank ] ) [ 0 ] ) . toMatchInlineSnapshot ( "0.01798620996209156 " ) ;
95
95
expect ( simplifyRanks ( ranks ) ) . toMatchInlineSnapshot ( `
96
96
[
97
- -11 ,
98
- -11 ,
99
- -11 ,
100
- -5.6 ,
101
- -11 ,
102
- -4 ,
103
- -11 ,
104
- -11 ,
105
- -11 ,
106
- -11 ,
97
+ 0.00001670142184809518 ,
98
+ 0.00001670142184809518 ,
99
+ 0.00001670142184809518 ,
100
+ 0.003684239899435989 ,
101
+ 0.00001670142184809518 ,
102
+ 0.01798620996209156 ,
103
+ 0.00001670142184809518 ,
104
+ 0.00001670142184809518 ,
105
+ 0.00001670142184809518 ,
106
+ 0.00001670142184809518 ,
107
107
]
108
108
` ) ;
109
109
} ) ;
@@ -141,42 +141,42 @@ describe("bgeReranker", () => {
141
141
expect ( simplifySortedRanks ( [ topDocument ] ) [ 0 ] ) . toMatchInlineSnapshot ( `
142
142
{
143
143
"document": "Mount Everest is the tallest mountain in the world",
144
- "score": -4 ,
144
+ "score": 0.01798620996209156 ,
145
145
}
146
146
` ) ;
147
147
expect ( simplifySortedRanks ( rankedDocuments ) ) . toMatchInlineSnapshot ( `
148
148
[
149
149
{
150
150
"document": "Mount Everest is the tallest mountain in the world",
151
- "score": -4 ,
151
+ "score": 0.01798620996209156 ,
152
152
},
153
153
{
154
154
"document": "The capital of France is Paris",
155
- "score": -5.6 ,
155
+ "score": 0.003684239899435989 ,
156
156
},
157
157
{
158
158
"document": "Not all the things that shine are made of gold",
159
- "score": -11 ,
159
+ "score": 0.00001670142184809518 ,
160
160
},
161
161
{
162
162
"document": "I love eating pizza with extra cheese",
163
- "score": -11 ,
163
+ "score": 0.00001670142184809518 ,
164
164
},
165
165
{
166
166
"document": "Dogs love to play fetch with their owners",
167
- "score": -11 ,
167
+ "score": 0.00001670142184809518 ,
168
168
},
169
169
{
170
170
"document": "The sky is clear and blue today",
171
- "score": -11 ,
171
+ "score": 0.00001670142184809518 ,
172
172
},
173
173
{
174
174
"document": "Cleaning the house is a good way to keep it tidy",
175
- "score": -11 ,
175
+ "score": 0.00001670142184809518 ,
176
176
},
177
177
{
178
178
"document": "A warm cup of tea is perfect for a cold winter day",
179
- "score": -11 ,
179
+ "score": 0.00001670142184809518 ,
180
180
},
181
181
]
182
182
` ) ;
@@ -185,16 +185,28 @@ describe("bgeReranker", () => {
185
185
} ) ;
186
186
187
187
function simplifyRanks < const T extends number [ ] > ( ranks : T ) : T {
188
- return ranks . map ( ( rank ) => parseFloat ( roundToPrecision ( rank , 0.2 ) . toFixed ( 1 ) ) ) as T ;
188
+ return ranks . map ( ( rank ) => simplifyScore ( rank ) ) as T ;
189
189
}
190
190
191
191
function simplifySortedRanks < const T extends { document : string , score : number } [ ] > ( values : T ) : T {
192
192
return values . map ( ( item ) => ( {
193
193
document : item . document ,
194
- score : parseFloat ( roundToPrecision ( item . score , 0.2 ) . toFixed ( 1 ) )
194
+ score : simplifyScore ( item . score )
195
195
} ) ) as T ;
196
196
}
197
197
198
+ function simplifyScore ( score : number ) {
199
+ return toSigmoid ( parseFloat ( roundToPrecision ( toLogit ( score ) , 0.2 ) . toFixed ( 1 ) ) ) ;
200
+ }
201
+
198
202
function roundToPrecision ( value : number , precision : number ) : number {
199
203
return Math . round ( value / precision ) * precision ;
200
204
}
205
+
206
+ function toLogit ( sigmoid : number ) {
207
+ return Math . log ( sigmoid / ( 1 - sigmoid ) ) ;
208
+ }
209
+
210
+ function toSigmoid ( logit : number ) {
211
+ return 1 / ( 1 + Math . exp ( - logit ) ) ;
212
+ }
0 commit comments