-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Restructure Rerankers so both NVIDIA and Cohere work properly (#…
…5933) * Removing reference to nonexistent method * Restructuring rerankers to inherit from BaseDocumentCompressor. Adding Voyage AI reranker. * Removing Voyage AI component and dependency. * [autofix.ci] apply automated fixes * feat: Add method to compress documents as DataFrame in LCCompressorComponent * Changing description of abstract build_compressor method * [autofix.ci] apply automated fixes * Adding top_n as an argument to the NVIDIA reranker * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
- Loading branch information
1 parent
8ab74e0
commit 8bf7048
Showing
7 changed files
with
117 additions
and
716 deletions.
There are no files selected for viewing
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,64 @@ | ||
from abc import abstractmethod | ||
|
||
from langflow.custom import Component | ||
from langflow.field_typing import BaseDocumentCompressor | ||
from langflow.io import DataInput, IntInput, MultilineInput, SecretStrInput | ||
from langflow.schema import Data | ||
from langflow.schema.dataframe import DataFrame | ||
from langflow.template.field.base import Output | ||
|
||
|
||
class LCCompressorComponent(Component): | ||
inputs = [ | ||
MultilineInput( | ||
name="search_query", | ||
display_name="Search Query", | ||
tool_mode=True, | ||
), | ||
SecretStrInput( | ||
name="api_key", | ||
display_name="API Key", | ||
), | ||
DataInput( | ||
name="search_results", | ||
display_name="Search Results", | ||
info="Search Results from a Vector Store.", | ||
is_list=True, | ||
), | ||
IntInput(name="top_n", display_name="Top N", value=3, advanced=True), | ||
] | ||
|
||
outputs = [ | ||
Output( | ||
display_name="Data", | ||
name="compressed_documents", | ||
method="Compressed Documents", | ||
), | ||
Output( | ||
display_name="DataFrame", | ||
name="compressed_documents_as_dataframe", | ||
method="Compressed Documents as DataFrame", | ||
), | ||
] | ||
|
||
@abstractmethod | ||
def build_compressor(self) -> BaseDocumentCompressor: | ||
"""Builds the Base Document Compressor object.""" | ||
msg = "build_compressor method must be implemented." | ||
raise NotImplementedError(msg) | ||
|
||
async def compress_documents(self) -> list[Data]: | ||
"""Compresses the documents retrieved from the vector store.""" | ||
compressor = self.build_compressor() | ||
documents = compressor.compress_documents( | ||
query=self.search_query, | ||
documents=[passage.to_lc_document() for passage in self.search_results if isinstance(passage, Data)], | ||
) | ||
data = self.to_data(documents) | ||
self.status = data | ||
return data | ||
|
||
async def compress_documents_as_dataframe(self) -> DataFrame: | ||
"""Compresses the documents retrieved from the vector store and returns a pandas DataFrame.""" | ||
data_objs = await self.compress_documents() | ||
return DataFrame(data=data_objs) |
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.