-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from 20001LastOrder/reranking
Reranking
- Loading branch information
Showing
18 changed files
with
347 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Different methods for reranking the results of a search query. | ||
""" | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, Callable | ||
|
||
import numpy as np | ||
from numpy.typing import ArrayLike | ||
from pydantic import BaseModel | ||
|
||
|
||
def cosine_similarity(v1: ArrayLike, v2: ArrayLike) -> float: | ||
return np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) | ||
|
||
|
||
class BaseReranking(ABC, BaseModel): | ||
@abstractmethod | ||
def rerank(self, documents: list[str], **kwargs) -> str: | ||
pass | ||
|
||
|
||
class RerankingByQuery(BaseReranking): | ||
embeddings: Any # takes an Embedding Object from LangChain, use Any since it is not compatible with Pydantic 2 yet | ||
distance_metric: Callable[[ArrayLike, ArrayLike], float] = cosine_similarity | ||
|
||
def rerank(self, documents: list[str], query: str) -> str: | ||
query_embedding = self.embeddings.embed_query(query) | ||
document_embeddings = self.embeddings.embed_documents(documents) | ||
|
||
# Calculate the similarity between the query and each document | ||
similarities = [ | ||
self.distance_metric(query_embedding, doc_embedding) | ||
for doc_embedding in document_embeddings | ||
] | ||
|
||
# Sort the documents by similarity | ||
sorted_documents = [ | ||
doc for _, doc in sorted(zip(similarities, documents), reverse=True) | ||
] | ||
|
||
return sorted_documents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.