Merge pull request #6777 from uinstinct/reranker-scores-error
fix: case when reranker returns invalid scores array
This commit is contained in:
commit
a5d11ff807
3 changed files with 28 additions and 7 deletions
|
|
@ -43,9 +43,13 @@ class DocsContextProvider extends BaseContextProvider {
|
|||
try {
|
||||
const scores = await reranker.rerank(fullInput, chunksCopy);
|
||||
|
||||
chunksCopy.sort(
|
||||
(a, b) => scores[chunksCopy.indexOf(b)] - scores[chunksCopy.indexOf(a)],
|
||||
);
|
||||
if (Array.isArray(scores)) {
|
||||
// reranker model has returned a valid array
|
||||
chunksCopy.sort(
|
||||
(a, b) =>
|
||||
scores[chunksCopy.indexOf(b)] - scores[chunksCopy.indexOf(a)],
|
||||
);
|
||||
}
|
||||
|
||||
chunksCopy = chunksCopy.splice(
|
||||
0,
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ export default class RerankerRetrievalPipeline extends BaseRetrievalPipeline {
|
|||
|
||||
let embeddingsChunks: Chunk[] = [];
|
||||
try {
|
||||
embeddingsChunks = !!config.selectedModelByRole.embed
|
||||
embeddingsChunks = Boolean(config.selectedModelByRole.embed)
|
||||
? await this.retrieveEmbeddings(input, nRetrieve)
|
||||
: [];
|
||||
} catch (error) {
|
||||
|
|
@ -116,7 +116,7 @@ export default class RerankerRetrievalPipeline extends BaseRetrievalPipeline {
|
|||
const chunkIndexMap = new Map<Chunk, number>();
|
||||
chunks.forEach((chunk, idx) => chunkIndexMap.set(chunk, idx));
|
||||
|
||||
results.sort(
|
||||
results?.sort(
|
||||
(a, b) => scores[chunkIndexMap.get(b)!] - scores[chunkIndexMap.get(a)!],
|
||||
);
|
||||
results = results.slice(0, this.options.nFinal);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,21 @@
|
|||
import { Chunk, LLMOptions } from "../../index.js";
|
||||
import { z } from "zod";
|
||||
|
||||
import { Chunk, LLMOptions } from "../../index.js";
|
||||
import OpenAI from "./OpenAI.js";
|
||||
|
||||
/**used to check a valid response from voyage is received
|
||||
* reference: https://docs.voyageai.com/reference/reranker-api
|
||||
*/
|
||||
const VoyageRerankSuccessResponseSchema = z.object({
|
||||
data: z.array(
|
||||
z.object({
|
||||
index: z.number(),
|
||||
relevance_score: z.number(),
|
||||
document: z.string(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
class Voyage extends OpenAI {
|
||||
static providerName = "voyage";
|
||||
static defaultOptions: Partial<LLMOptions> | undefined = {
|
||||
|
|
@ -36,7 +50,10 @@ class Voyage extends OpenAI {
|
|||
const data = (await resp.json()) as {
|
||||
data: Array<{ index: number; relevance_score: number }>;
|
||||
};
|
||||
const results = data.data.sort((a, b) => a.index - b.index);
|
||||
|
||||
const parsedData = VoyageRerankSuccessResponseSchema.parse(data);
|
||||
|
||||
const results = parsedData.data.sort((a, b) => a.index - b.index);
|
||||
return results.map((result) => result.relevance_score);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue