-
Notifications
You must be signed in to change notification settings - Fork 15.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core[minor],langchain[patch]: Move base indexing interface and logic to core #20667
Merged
Merged
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b6dbd1b
x
eyurtsev 7f126ed
x
eyurtsev 9858604
x
eyurtsev 26c4c4e
x
eyurtsev 806111a
x
eyurtsev 85525af
x
eyurtsev f9c2dcd
x
eyurtsev 3374424
x
eyurtsev 53196ba
x
eyurtsev f678ff2
x
eyurtsev 3763b71
Merge branch 'master' into eugene/indexing
eyurtsev 67e3a90
x
eyurtsev 46682b4
x
eyurtsev 5af7267
x
eyurtsev 005a4f5
empty commit
eyurtsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""Code to help indexing data into a vectorstore. | ||
|
||
This package contains helper logic to help deal with indexing data into | ||
a vectorstore while avoiding duplicated content and over-writing content | ||
if it's unchanged. | ||
""" | ||
from langchain_core.indexing.api import IndexingResult, aindex, index | ||
from langchain_core.indexing.base import RecordManager | ||
|
||
__all__ = [ | ||
"aindex", | ||
"index", | ||
"IndexingResult", | ||
"RecordManager", | ||
] |
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,105 @@ | ||
import time | ||
from typing import Dict, List, Optional, Sequence, TypedDict | ||
|
||
from langchain_core.indexing.base import RecordManager | ||
|
||
|
||
class _Record(TypedDict): | ||
group_id: Optional[str] | ||
updated_at: float | ||
|
||
|
||
class InMemoryRecordManager(RecordManager): | ||
"""An in-memory record manager for testing purposes.""" | ||
|
||
def __init__(self, namespace: str) -> None: | ||
super().__init__(namespace) | ||
# Each key points to a dictionary | ||
# of {'group_id': group_id, 'updated_at': timestamp} | ||
self.records: Dict[str, _Record] = {} | ||
self.namespace = namespace | ||
|
||
def create_schema(self) -> None: | ||
"""In-memory schema creation is simply ensuring the structure is initialized.""" | ||
|
||
async def acreate_schema(self) -> None: | ||
"""In-memory schema creation is simply ensuring the structure is initialized.""" | ||
|
||
def get_time(self) -> float: | ||
"""Get the current server time as a high resolution timestamp!""" | ||
return time.time() | ||
|
||
async def aget_time(self) -> float: | ||
"""Get the current server time as a high resolution timestamp!""" | ||
return self.get_time() | ||
|
||
def update( | ||
self, | ||
keys: Sequence[str], | ||
*, | ||
group_ids: Optional[Sequence[Optional[str]]] = None, | ||
time_at_least: Optional[float] = None, | ||
) -> None: | ||
if group_ids and len(keys) != len(group_ids): | ||
raise ValueError("Length of keys must match length of group_ids") | ||
for index, key in enumerate(keys): | ||
group_id = group_ids[index] if group_ids else None | ||
if time_at_least and time_at_least > self.get_time(): | ||
raise ValueError("time_at_least must be in the past") | ||
self.records[key] = {"group_id": group_id, "updated_at": self.get_time()} | ||
|
||
async def aupdate( | ||
self, | ||
keys: Sequence[str], | ||
*, | ||
group_ids: Optional[Sequence[Optional[str]]] = None, | ||
time_at_least: Optional[float] = None, | ||
) -> None: | ||
self.update(keys, group_ids=group_ids, time_at_least=time_at_least) | ||
|
||
def exists(self, keys: Sequence[str]) -> List[bool]: | ||
return [key in self.records for key in keys] | ||
|
||
async def aexists(self, keys: Sequence[str]) -> List[bool]: | ||
return self.exists(keys) | ||
|
||
def list_keys( | ||
self, | ||
*, | ||
before: Optional[float] = None, | ||
after: Optional[float] = None, | ||
group_ids: Optional[Sequence[str]] = None, | ||
limit: Optional[int] = None, | ||
) -> List[str]: | ||
result = [] | ||
for key, data in self.records.items(): | ||
if before and data["updated_at"] >= before: | ||
continue | ||
if after and data["updated_at"] <= after: | ||
continue | ||
if group_ids and data["group_id"] not in group_ids: | ||
continue | ||
result.append(key) | ||
if limit: | ||
return result[:limit] | ||
return result | ||
|
||
async def alist_keys( | ||
self, | ||
*, | ||
before: Optional[float] = None, | ||
after: Optional[float] = None, | ||
group_ids: Optional[Sequence[str]] = None, | ||
limit: Optional[int] = None, | ||
) -> List[str]: | ||
return self.list_keys( | ||
before=before, after=after, group_ids=group_ids, limit=limit | ||
) | ||
|
||
def delete_keys(self, keys: Sequence[str]) -> None: | ||
for key in keys: | ||
if key in self.records: | ||
del self.records[key] | ||
|
||
async def adelete_keys(self, keys: Sequence[str]) -> None: | ||
self.delete_keys(keys) |
4 changes: 2 additions & 2 deletions
4
...nit_tests/indexes/test_hashed_document.py → ...it_tests/indexing/test_hashed_document.py
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be worth keeping some form of the |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i know this is just a migration but a comment of what this is doing would be great