Skip to content

Commit

Permalink
feat(community): Incorporate BM25 score in the results (langchain-ai#…
Browse files Browse the repository at this point in the history
…7236)

Co-authored-by: Jacob Lee <[email protected]>
  • Loading branch information
2 people authored and FilipZmijewski committed Nov 27, 2024
1 parent f555135 commit 552d054
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion libs/langchain-community/src/retrievers/bm25.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BM25 } from "../utils/@furkantoprak/bm25/BM25.js";
export type BM25RetrieverOptions = {
docs: Document[];
k: number;
includeScore?: boolean;
} & BaseRetrieverInput;

/**
Expand All @@ -14,6 +15,8 @@ export type BM25RetrieverOptions = {
* The k parameter determines the number of documents to return for each query.
*/
export class BM25Retriever extends BaseRetriever {
includeScore = false;

static lc_name() {
return "BM25Retriever";
}
Expand All @@ -35,6 +38,7 @@ export class BM25Retriever extends BaseRetriever {
super(options);
this.docs = options.docs;
this.k = options.k;
this.includeScore = options.includeScore ?? this.includeScore;
}

private preprocessFunc(text: string): string[] {
Expand All @@ -53,6 +57,19 @@ export class BM25Retriever extends BaseRetriever {

scoredDocs.sort((a, b) => b.score - a.score);

return scoredDocs.slice(0, this.k).map((item) => item.document);
return scoredDocs.slice(0, this.k).map((item) => {
if (this.includeScore) {
return new Document({
...(item.document.id && { id: item.document.id }),
pageContent: item.document.pageContent,
metadata: {
bm25Score: item.score,
...item.document.metadata,
},
});
} else {
return item.document;
}
});
}
}

0 comments on commit 552d054

Please sign in to comment.