TimeWeightedVectorStoreRetriever
Last updated
Last updated
Author: Youngjun Cho
Design:
Peer Review :
This is a part of LangChain Open Tutorial
TimeWeightedVectorStoreRetriever
is a retriever that uses a combination of semantic similarity and a time decay.
By doing so, it considers both the " freshness " and " relevance " of the documents or data in its results.
The algorithm for scoring them is:
$\text{semantic_similarity} + (1.0 - \text{decay_rate})^{hours_passed}$
semantic_similarity
indicates the semantic similarity between documents or data.
decay_rate
represents the ratio at which the score decreases over time.
hours_passed
is the number of hours elapsed since the object was last accessed.
The key feature of this approach is that it evaluates the “ freshness of information ” based on the last time the object was accessed.
In other words, objects that are accessed frequently maintain a higher score over time, increasing the likelihood that frequently used or important information will appear near the top of search results. This allows the retriever to provide dynamic results that account for both recency and relevance.
Importantly, in this context, decay_rate
is determined by the time since the object was last accessed , not since it was created.
Hence, any objects that are accessed frequently remain "fresh."
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorial
is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.
You can checkout the langchain-opentutorial
for more details.
You can alternatively set API keys such as OPENAI_API_KEY
in a .env
file and load them.
[Note] This is not necessary if you've already set the required API keys in previous steps.
A low decay_rate
(In this example, we'll set it to an extreme value close to 0) means that memories are retained for a longer period .
A decay_rate
of 0 means that memories are never forgotten , which makes this retriever equivalent to a vector lookup.
Let's first initialize the TimeWeightedVectorStoreRetriever
with a very small decay_rate
and k=1
(where k
is the number of vectors to retrieve).
Let's add a simple example data.
The document "Please subscribe to LangChain Youtube" appears first because it is the most salient .
Since the decay_rate
is close to 0, the document is still considered recent .
When a high decay_rate
is used (e.g., 0.9999...), the recency score rapidly converges to 0.
If this value were set to 1, all objects would end up with a recency
value of 0, resulting in the same outcome as a standard vector lookup.
Initialize the retriever using TimeWeightedVectorStoreRetriever
, setting the decay_rate
to 0.999 to adjust the time-based weight decay rate.
Add new documents again.
In this case, when you invoke the retriever, "Will you subscribe to LangChain Youtube? Please!" is returned first.
Because decay_rate
is high (close to 1), older documents (like the one from yesterday) are nearly forgotten.
when the decay_rate
is set to a very small value, such as 0.000001:
The decay rate (i.e., the rate at which information is forgotten) is extremely low, so information is hardly forgotten.
As a result, there is almost no difference in time-based weights between more or less recently accessed information . In this case, similarity scores are given higher priority.
When the decay_rate
is set close to 1, such as 0.999:
The decay rate is very high, so most of the recently unaccessed information is almost completely forgotten.
As a result, in such cases, higher scores are given to more recently accessed information.
LangChain
provides some utilities that allow you to test time-based components by mocking the current time.
The mock_now
function is a utility function provided by LangChain, used to mock the current time.
[NOTE]
Inside the with statement, all datetime.now
calls return the mocked time . Once you exit the with block, it reverts back to the original time .
By using the mock_now
function, you can shift the current time and see how the search results change.
This helps you find an appropriate decay_rate
for your use case.
[Note]
If you set the time too far in the past, an error might occur during decay_rate
calculations.