Skip to content

Commit 3ee5bfe

Browse files
committed
test: fix tests
1 parent 5565614 commit 3ee5bfe

File tree

2 files changed

+81
-23
lines changed

2 files changed

+81
-23
lines changed

docs/guide/embedding.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ const documents = [
172172
"Cleaning the house is a good way to keep it tidy"
173173
];
174174

175-
const query = "Tell me a nature geographical fact";
175+
const query = "Tell me a geographical fact";
176176
const rankedDocuments = await context.rankAndSort(query, documents);
177177

178178
const topDocument = rankedDocuments[0]!;
@@ -185,7 +185,7 @@ console.log("Ranked documents:", rankedDocuments);
185185
```
186186
> This example will produce this output:
187187
> ```
188-
> query: Tell me a nature geographical fact
188+
> query: Tell me a geographical fact
189189
> Top document: Mount Everest is the tallest mountain in the world
190190
> Second document: The capital of France is Paris
191191
> ```

test/modelDependent/bgeReranker/rank.test.ts

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {getTestLlama} from "../../utils/getTestLlama.js";
44

55
describe("bgeReranker", () => {
66
describe("rank", () => {
7-
test("simple ranking", {timeout: 1000 * 60 * 60 * 2}, async () => {
7+
test("simple ranking", {timeout: 1000 * 60 * 60 * 2}, async (test) => {
8+
if (process.platform !== "darwin")
9+
test.skip(); // the scores are a bit different on different platforms, so skipping on other platforms due to flakiness
10+
811
const modelPath = await getModelFile("bge-reranker-v2-m3-Q8_0.gguf");
912
const llama = await getTestLlama();
1013

@@ -28,7 +31,7 @@ describe("bgeReranker", () => {
2831
"Cleaning the house is a good way to keep it tidy"
2932
];
3033

31-
const query = "Tell me a nature geographical fact";
34+
const query = "Tell me a geographical fact";
3235

3336
const ranks = await Promise.all(
3437
documents.map((doc) => rankingContext.rank(query, doc))
@@ -40,24 +43,27 @@ describe("bgeReranker", () => {
4043
const highestRankDocument = documents[highestRankIndex];
4144
expect(highestRankDocument).to.eql("Mount Everest is the tallest mountain in the world");
4245

43-
expect(simplifyRanks([highestRank])[0]).toMatchInlineSnapshot("0.0024726231566347743");
46+
expect(simplifyRanks([highestRank])[0]).toMatchInlineSnapshot("0.026596993576865856");
4447
expect(simplifyRanks(ranks)).toMatchInlineSnapshot(`
4548
[
4649
0.00002039908727992137,
4750
0.00006772414961977023,
4851
0.00003716893710288947,
52+
0.004496273160941178,
4953
0.00003716893710288947,
54+
0.026596993576865856,
5055
0.00003716893710288947,
51-
0.0024726231566347743,
52-
0.00003716893710288947,
53-
0.00003716893710288947,
56+
0.00002039908727992137,
5457
0.00002039908727992137,
5558
0.00003716893710288947,
5659
]
5760
`);
5861
});
5962

60-
test("rank all", {timeout: 1000 * 60 * 60 * 2}, async () => {
63+
test("rank all", {timeout: 1000 * 60 * 60 * 2}, async (test) => {
64+
if (process.platform !== "darwin")
65+
test.skip(); // the scores are a bit different on different platforms, so skipping on other platforms due to flakiness
66+
6167
const modelPath = await getModelFile("bge-reranker-v2-m3-Q8_0.gguf");
6268
const llama = await getTestLlama();
6369

@@ -81,7 +87,7 @@ describe("bgeReranker", () => {
8187
"Cleaning the house is a good way to keep it tidy"
8288
];
8389

84-
const query = "Tell me a nature geographical fact";
90+
const query = "Tell me a geographical fact";
8591

8692
const ranks = await rankingContext.rankAll(query, documents);
8793

@@ -91,24 +97,27 @@ describe("bgeReranker", () => {
9197
const highestRankDocument = documents[highestRankIndex];
9298
expect(highestRankDocument).to.eql("Mount Everest is the tallest mountain in the world");
9399

94-
expect(simplifyRanks([highestRank])[0]).toMatchInlineSnapshot("0.0024726231566347743");
100+
expect(simplifyRanks([highestRank])[0]).toMatchInlineSnapshot("0.026596993576865856");
95101
expect(simplifyRanks(ranks)).toMatchInlineSnapshot(`
96102
[
97103
0.00002039908727992137,
98104
0.00006772414961977023,
99105
0.00003716893710288947,
106+
0.004496273160941178,
100107
0.00003716893710288947,
108+
0.026596993576865856,
101109
0.00003716893710288947,
102-
0.0024726231566347743,
103-
0.00003716893710288947,
104-
0.00003716893710288947,
110+
0.00002039908727992137,
105111
0.00002039908727992137,
106112
0.00003716893710288947,
107113
]
108114
`);
109115
});
110116

111-
test("rank and sort", {timeout: 1000 * 60 * 60 * 2}, async () => {
117+
test("rank and sort", {timeout: 1000 * 60 * 60 * 2}, async (test) => {
118+
if (process.platform !== "darwin")
119+
test.skip(); // the scores are a bit different on different platforms, so skipping on other platforms due to flakiness
120+
112121
const modelPath = await getModelFile("bge-reranker-v2-m3-Q8_0.gguf");
113122
const llama = await getTestLlama();
114123

@@ -130,7 +139,7 @@ describe("bgeReranker", () => {
130139
"Cleaning the house is a good way to keep it tidy"
131140
];
132141

133-
const query = "Tell me a nature geographical fact";
142+
const query = "Tell me a geographical fact";
134143

135144
const rankedDocuments = await rankingContext.rankAndSort(query, documents);
136145

@@ -141,21 +150,25 @@ describe("bgeReranker", () => {
141150
expect(simplifySortedRanks([topDocument])[0]).toMatchInlineSnapshot(`
142151
{
143152
"document": "Mount Everest is the tallest mountain in the world",
144-
"score": 0.0024726231566347743,
153+
"score": 0.026596993576865856,
145154
}
146155
`);
147156
expect(simplifySortedRanks(rankedDocuments)).toMatchInlineSnapshot(`
148157
[
149158
{
150159
"document": "Mount Everest is the tallest mountain in the world",
151-
"score": 0.0024726231566347743,
160+
"score": 0.026596993576865856,
161+
},
162+
{
163+
"document": "The capital of France is Paris",
164+
"score": 0.004496273160941178,
152165
},
153166
{
154167
"document": "I love eating pizza with extra cheese",
155168
"score": 0.00006772414961977023,
156169
},
157170
{
158-
"document": "The capital of France is Paris",
171+
"document": "A warm cup of tea is perfect for a cold winter day",
159172
"score": 0.00003716893710288947,
160173
},
161174
{
@@ -166,10 +179,6 @@ describe("bgeReranker", () => {
166179
"document": "Cleaning the house is a good way to keep it tidy",
167180
"score": 0.00003716893710288947,
168181
},
169-
{
170-
"document": "A warm cup of tea is perfect for a cold winter day",
171-
"score": 0.00003716893710288947,
172-
},
173182
{
174183
"document": "Not all the things that shine are made of gold",
175184
"score": 0.00002039908727992137,
@@ -181,6 +190,51 @@ describe("bgeReranker", () => {
181190
]
182191
`);
183192
});
193+
194+
test("rank and sort without scores", {timeout: 1000 * 60 * 60 * 2}, async () => {
195+
const modelPath = await getModelFile("bge-reranker-v2-m3-Q8_0.gguf");
196+
const llama = await getTestLlama();
197+
198+
const model = await llama.loadModel({
199+
modelPath
200+
});
201+
const rankingContext = await model.createRankingContext({
202+
contextSize: 512
203+
});
204+
205+
const documents = [
206+
"The sky is clear and blue today",
207+
"I love eating pizza with extra cheese",
208+
"Dogs love to play fetch with their owners",
209+
"The capital of France is Paris",
210+
"Mount Everest is the tallest mountain in the world",
211+
"A warm cup of tea is perfect for a cold winter day",
212+
"Not all the things that shine are made of gold",
213+
"Cleaning the house is a good way to keep it tidy"
214+
];
215+
216+
const query = "Tell me a geographical fact";
217+
218+
const rankedDocuments = await rankingContext.rankAndSort(query, documents);
219+
220+
const topDocument = rankedDocuments[0]!;
221+
222+
expect(topDocument.document).to.eql("Mount Everest is the tallest mountain in the world");
223+
224+
expect(onlyDocuments([topDocument])[0]).toMatchInlineSnapshot('"Mount Everest is the tallest mountain in the world"');
225+
expect(onlyDocuments(rankedDocuments)).toMatchInlineSnapshot(`
226+
[
227+
"Mount Everest is the tallest mountain in the world",
228+
"The capital of France is Paris",
229+
"I love eating pizza with extra cheese",
230+
"A warm cup of tea is perfect for a cold winter day",
231+
"Dogs love to play fetch with their owners",
232+
"Cleaning the house is a good way to keep it tidy",
233+
"Not all the things that shine are made of gold",
234+
"The sky is clear and blue today",
235+
]
236+
`);
237+
});
184238
});
185239
});
186240

@@ -195,6 +249,10 @@ function simplifySortedRanks<const T extends {document: string, score: number}[]
195249
})) as T;
196250
}
197251

252+
function onlyDocuments(values: {document: string, score: number}[]): string[] {
253+
return values.map((item) => item.document);
254+
}
255+
198256
function simplifyScore(score: number) {
199257
return toSigmoid(parseFloat(roundToPrecision(toLogit(score), 0.6).toFixed(1)));
200258
}

0 commit comments

Comments
 (0)