Skip to content

SimilarityIndex#

SimilarityIndex is a main API for using similarity features of db-ally.

Explore Similarity Stores and Similarity Fetchers for ready to use integrations.

dbally.similarity.SimilarityIndex #

SimilarityIndex(store: SimilarityStore, fetcher: SimilarityFetcher)

Bases: AbstractSimilarityIndex

Merges the store and the fetcher to provide a simple interface for keeping the data store and the similarity store in sync and finding similar texts.

PARAMETER DESCRIPTION
store

stores values gathered by the fetcher

TYPE: SimilarityStore

fetcher

fetches unique values to be indexed

TYPE: SimilarityFetcher

Source code in src/dbally/similarity/index.py
def __init__(self, store: SimilarityStore, fetcher: SimilarityFetcher):
    """
    Args:
        store: stores values gathered by the fetcher
        fetcher: fetches unique values to be indexed
    """
    self.store = store
    self.fetcher = fetcher

store instance-attribute #

store = store

fetcher instance-attribute #

fetcher = fetcher

update async #

update() -> None

Updates the store with the latest data from the fetcher.

Source code in src/dbally/similarity/index.py
async def update(self) -> None:
    """
    Updates the store with the latest data from the fetcher.
    """
    data = await self.fetcher.fetch()
    await self.store.store(data)

similar async #

similar(text: str, event_tracker: Optional[EventTracker] = None) -> str

Finds the most similar text in the store or returns the original text if no similar text is found.

PARAMETER DESCRIPTION
text

The text to find similar to.

TYPE: str

event_tracker

The event tracker to use for auditing the similarity search.

TYPE: Optional[EventTracker] DEFAULT: None

RETURNS DESCRIPTION
str

The most similar text or the original text if no similar text is found.

TYPE: str

Source code in src/dbally/similarity/index.py
async def similar(self, text: str, event_tracker: Optional[EventTracker] = None) -> str:
    """
    Finds the most similar text in the store or returns the original text if no similar text is found.

    Args:
        text: The text to find similar to.
        event_tracker: The event tracker to use for auditing the similarity search.

    Returns:
        str: The most similar text or the original text if no similar text is found.
    """

    event_tracker = event_tracker or EventTracker()
    event = SimilarityEvent(input_value=text, store=repr(self.store), fetcher=repr(self.fetcher))

    async with event_tracker.track_event(event) as span:
        found = await self.store.find_similar(text)
        event.output_value = found
        span(event)

    return found if found else text