in_memory.py — langchain Source File
Architecture documentation for in_memory.py, a python file in the langchain codebase. 11 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 16d9b59c_7ced_1b5f_7142_710269a670f7["in_memory.py"] 7aaf52d4_ee88_411e_980e_bc4beeeb30ad["operator"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> 7aaf52d4_ee88_411e_980e_bc4beeeb30ad 8dfa0cac_d802_3ccd_f710_43a5e70da3a5["uuid"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> 8dfa0cac_d802_3ccd_f710_43a5e70da3a5 cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> 91721f45_4909_e489_8c1f_084f8bd87145 b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7 f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> f3bc7443_c889_119d_0744_aacc3620d8d2 c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> c554676d_b731_47b2_a98f_c1c2d537c0aa a8fcc022_dc2b_29f4_4e44_b5c75941d529["langchain_core.indexing"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> a8fcc022_dc2b_29f4_4e44_b5c75941d529 c1c8dd57_1ebb_bad3_6e65_32e0aa3099ae["langchain_core.indexing.base"] 16d9b59c_7ced_1b5f_7142_710269a670f7 --> c1c8dd57_1ebb_bad3_6e65_32e0aa3099ae style 16d9b59c_7ced_1b5f_7142_710269a670f7 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""In memory document index."""
import operator
import uuid
from collections.abc import Sequence
from typing import Any, cast
from pydantic import Field
from typing_extensions import override
from langchain_core._api import beta
from langchain_core.callbacks import CallbackManagerForRetrieverRun
from langchain_core.documents import Document
from langchain_core.indexing import UpsertResponse
from langchain_core.indexing.base import DeleteResponse, DocumentIndex
@beta(message="Introduced in version 0.2.29. Underlying abstraction subject to change.")
class InMemoryDocumentIndex(DocumentIndex):
"""In memory document index.
This is an in-memory document index that stores documents in a dictionary.
It provides a simple search API that returns documents by the number of
counts the given query appears in the document.
"""
store: dict[str, Document] = Field(default_factory=dict)
top_k: int = 4
@override
def upsert(self, items: Sequence[Document], /, **kwargs: Any) -> UpsertResponse:
"""Upsert documents into the index.
Args:
items: Sequence of documents to add to the index.
**kwargs: Additional keyword arguments.
Returns:
A response object that contains the list of IDs that were
successfully added or updated in the index and the list of IDs that
failed to be added or updated.
"""
ok_ids = []
for item in items:
if item.id is None:
id_ = str(uuid.uuid4())
item_ = item.model_copy()
item_.id = id_
else:
item_ = item
id_ = item.id
self.store[id_] = item_
ok_ids.append(cast("str", item_.id))
return UpsertResponse(succeeded=ok_ids, failed=[])
@override
def delete(self, ids: list[str] | None = None, **kwargs: Any) -> DeleteResponse:
"""Delete by IDs.
Args:
ids: List of IDs to delete.
Raises:
ValueError: If IDs is None.
Returns:
A response object that contains the list of IDs that were successfully
deleted and the list of IDs that failed to be deleted.
"""
if ids is None:
msg = "IDs must be provided for deletion"
raise ValueError(msg)
ok_ids = []
for id_ in ids:
if id_ in self.store:
del self.store[id_]
ok_ids.append(id_)
return DeleteResponse(
succeeded=ok_ids, num_deleted=len(ok_ids), num_failed=0, failed=[]
)
@override
def get(self, ids: Sequence[str], /, **kwargs: Any) -> list[Document]:
return [self.store[id_] for id_ in ids if id_ in self.store]
@override
def _get_relevant_documents(
self, query: str, *, run_manager: CallbackManagerForRetrieverRun
) -> list[Document]:
counts_by_doc = []
for document in self.store.values():
count = document.page_content.count(query)
counts_by_doc.append((document, count))
counts_by_doc.sort(key=operator.itemgetter(1), reverse=True)
return [doc.model_copy() for doc, count in counts_by_doc[: self.top_k]]
Domain
Subdomains
Classes
Dependencies
- collections.abc
- langchain_core._api
- langchain_core.callbacks
- langchain_core.documents
- langchain_core.indexing
- langchain_core.indexing.base
- operator
- pydantic
- typing
- typing_extensions
- uuid
Source
Frequently Asked Questions
What does in_memory.py do?
in_memory.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What does in_memory.py depend on?
in_memory.py imports 11 module(s): collections.abc, langchain_core._api, langchain_core.callbacks, langchain_core.documents, langchain_core.indexing, langchain_core.indexing.base, operator, pydantic, and 3 more.
Where is in_memory.py in the architecture?
in_memory.py is located at libs/core/langchain_core/indexing/in_memory.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/core/langchain_core/indexing).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free