InMemoryVectorStore Class — langchain Architecture
Architecture documentation for the InMemoryVectorStore class in in_memory.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD aca2549c_644c_9cf1_03c9_ee00daa0cf81["InMemoryVectorStore"] 9d2a2799_754f_4de7_e4e6_081d8ea620e0["VectorStore"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|extends| 9d2a2799_754f_4de7_e4e6_081d8ea620e0 eb1e92dc_3a47_90f7_13c1_755c92499dc6["in_memory.py"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|defined in| eb1e92dc_3a47_90f7_13c1_755c92499dc6 a1922a1a_912b_609d_d433_8a2532f2a72d["__init__()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| a1922a1a_912b_609d_d433_8a2532f2a72d d14ed12a_5076_d4b8_cbe0_5bd2bef5a2dd["embeddings()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| d14ed12a_5076_d4b8_cbe0_5bd2bef5a2dd 65ea77d3_d5cd_2930_3ebe_0b4059b5d282["delete()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| 65ea77d3_d5cd_2930_3ebe_0b4059b5d282 a5151b07_8782_9466_29b2_6df15bd1c297["adelete()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| a5151b07_8782_9466_29b2_6df15bd1c297 557a1ecd_355d_48cb_2a6e_9b9305511155["add_documents()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| 557a1ecd_355d_48cb_2a6e_9b9305511155 fb3be94a_25d7_d4ae_d705_fd7f03009c39["aadd_documents()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| fb3be94a_25d7_d4ae_d705_fd7f03009c39 cbda0446_eb8e_ed27_3028_57b7f98d103c["get_by_ids()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| cbda0446_eb8e_ed27_3028_57b7f98d103c bf59d277_fba5_2eac_22c6_532bd2afc538["aget_by_ids()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| bf59d277_fba5_2eac_22c6_532bd2afc538 9bec299a_baf3_fdf5_93a1_256035671565["_similarity_search_with_score_by_vector()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| 9bec299a_baf3_fdf5_93a1_256035671565 1570a5e6_2ce6_e0a2_369f_cbe0aee465ce["similarity_search_with_score_by_vector()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| 1570a5e6_2ce6_e0a2_369f_cbe0aee465ce 49567762_9dfd_9b31_5408_4b2976edc7da["similarity_search_with_score()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| 49567762_9dfd_9b31_5408_4b2976edc7da c22130e3_6028_83c3_978e_cce8c6aabc6b["asimilarity_search_with_score()"] aca2549c_644c_9cf1_03c9_ee00daa0cf81 -->|method| c22130e3_6028_83c3_978e_cce8c6aabc6b
Relationship Graph
Source Code
libs/core/langchain_core/vectorstores/in_memory.py lines 34–546
class InMemoryVectorStore(VectorStore):
"""In-memory vector store implementation.
Uses a dictionary, and computes cosine similarity for search using numpy.
Setup:
Install `langchain-core`.
```bash
pip install -U langchain-core
```
Key init args — indexing params:
* embedding_function: Embeddings
Embedding function to use.
Instantiate:
```python
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = InMemoryVectorStore(OpenAIEmbeddings())
```
Add Documents:
```python
from langchain_core.documents import Document
document_1 = Document(id="1", page_content="foo", metadata={"baz": "bar"})
document_2 = Document(id="2", page_content="thud", metadata={"bar": "baz"})
document_3 = Document(id="3", page_content="i will be deleted :(")
documents = [document_1, document_2, document_3]
vector_store.add_documents(documents=documents)
```
Inspect documents:
```python
top_n = 10
for index, (id, doc) in enumerate(vector_store.store.items()):
if index < top_n:
# docs have keys 'id', 'vector', 'text', 'metadata'
print(f"{id}: {doc['text']}")
else:
break
```
Delete Documents:
```python
vector_store.delete(ids=["3"])
```
Search:
```python
results = vector_store.similarity_search(query="thud", k=1)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
```
```txt
* thud [{'bar': 'baz'}]
```
Search with filter:
```python
def _filter_function(doc: Document) -> bool:
return doc.metadata.get("bar") == "baz"
results = vector_store.similarity_search(
query="thud", k=1, filter=_filter_function
)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
```
```txt
* thud [{'bar': 'baz'}]
```
Extends
Source
Frequently Asked Questions
What is the InMemoryVectorStore class?
InMemoryVectorStore is a class in the langchain codebase, defined in libs/core/langchain_core/vectorstores/in_memory.py.
Where is InMemoryVectorStore defined?
InMemoryVectorStore is defined in libs/core/langchain_core/vectorstores/in_memory.py at line 34.
What does InMemoryVectorStore extend?
InMemoryVectorStore extends VectorStore.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free