Home / File/ time_weighted_retriever.py — langchain Source File

time_weighted_retriever.py — langchain Source File

Architecture documentation for time_weighted_retriever.py, a python file in the langchain codebase. 9 imports, 0 dependents.

File python CoreAbstractions Serialization 9 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  62a40383_e373_8755_3cbc_6de970aa1a3e["time_weighted_retriever.py"]
  af34f08b_0ede_2b87_0db6_983d74ed0249["datetime"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> af34f08b_0ede_2b87_0db6_983d74ed0249
  e874d8a4_cef0_9d0b_d1ee_84999c07cc2c["copy"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> e874d8a4_cef0_9d0b_d1ee_84999c07cc2c
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  38bc5323_3713_7377_32f8_091293bea54b["langchain_core.retrievers"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> 38bc5323_3713_7377_32f8_091293bea54b
  d55af636_303c_0eb6_faee_20d89bd952d5["langchain_core.vectorstores"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> d55af636_303c_0eb6_faee_20d89bd952d5
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  62a40383_e373_8755_3cbc_6de970aa1a3e --> 91721f45_4909_e489_8c1f_084f8bd87145
  style 62a40383_e373_8755_3cbc_6de970aa1a3e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import datetime
from copy import deepcopy
from typing import Any

from langchain_core.callbacks import (
    AsyncCallbackManagerForRetrieverRun,
    CallbackManagerForRetrieverRun,
)
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever
from langchain_core.vectorstores import VectorStore
from pydantic import ConfigDict, Field
from typing_extensions import override


def _get_hours_passed(time: datetime.datetime, ref_time: datetime.datetime) -> float:
    """Get the hours passed between two datetimes."""
    return (time - ref_time).total_seconds() / 3600


class TimeWeightedVectorStoreRetriever(BaseRetriever):
    """Time Weighted Vector Store Retriever.

    Retriever that combines embedding similarity with recency in retrieving values.
    """

    vectorstore: VectorStore
    """The `VectorStore` to store documents and determine salience."""

    search_kwargs: dict = Field(default_factory=lambda: {"k": 100})
    """Keyword arguments to pass to the `VectorStore` similarity search."""

    # TODO: abstract as a queue
    memory_stream: list[Document] = Field(default_factory=list)
    """The memory_stream of documents to search through."""

    decay_rate: float = Field(default=0.01)
    """The exponential decay factor used as `(1.0-decay_rate)**(hrs_passed)`."""

    k: int = 4
    """The maximum number of documents to retrieve in a given call."""

    other_score_keys: list[str] = []
    """Other keys in the metadata to factor into the score, e.g. 'importance'."""

    default_salience: float | None = None
    """The salience to assign memories not retrieved from the vector store.

    None assigns no salience to documents not fetched from the vector store.
    """

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
    )

    def _document_get_date(self, field: str, document: Document) -> datetime.datetime:
        """Return the value of the date field of a document."""
        if field in document.metadata:
            if isinstance(document.metadata[field], float):
                return datetime.datetime.fromtimestamp(document.metadata[field])
// ... (139 more lines)

Subdomains

Dependencies

  • copy
  • datetime
  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.retrievers
  • langchain_core.vectorstores
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does time_weighted_retriever.py do?
time_weighted_retriever.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What functions are defined in time_weighted_retriever.py?
time_weighted_retriever.py defines 1 function(s): _get_hours_passed.
What does time_weighted_retriever.py depend on?
time_weighted_retriever.py imports 9 module(s): copy, datetime, langchain_core.callbacks, langchain_core.documents, langchain_core.retrievers, langchain_core.vectorstores, pydantic, typing, and 1 more.
Where is time_weighted_retriever.py in the architecture?
time_weighted_retriever.py is located at libs/langchain/langchain_classic/retrievers/time_weighted_retriever.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain/langchain_classic/retrievers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free