Skip to content

Commit

Permalink
docs: improve docs for InMemoryVectorStore (#25786)
Browse files Browse the repository at this point in the history
Closes #25775
  • Loading branch information
ccurme authored Aug 28, 2024
1 parent 1023fbc commit 3c784e1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/src/theme/FeatureTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ const FEATURE_TABLES = {
},
{
name: "InMemoryVectorStore",
link: "in_memory",
link: "https://python.langchain.com/v0.2/api_reference/core/vectorstores/langchain_core.vectorstores.in_memory.InMemoryVectorStore.html",
deleteById: true,
filtering: true,
searchByVector: false,
Expand Down
118 changes: 115 additions & 3 deletions libs/core/langchain_core/vectorstores/in_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,122 @@


class InMemoryVectorStore(VectorStore):
"""In-memory implementation of VectorStore using a dictionary.
"""In-memory vector store implementation.
Uses numpy to compute cosine similarity for search.
"""
Uses a dictionary, and computes cosine similarity for search using numpy.
Setup:
Install ``langchain-core``.
.. code-block:: bash
pip install -U langchain-core
Key init args — indexing params:
embedding_function: Embeddings
Embedding function to use.
Instantiate:
.. code-block:: python
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = InMemoryVectorStore(OpenAIEmbeddings())
Add Documents:
.. code-block:: 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)
Delete Documents:
.. code-block:: python
vector_store.delete(ids=["3"])
Search:
.. code-block:: python
results = vector_store.similarity_search(query="thud",k=1)
for doc in results:
print(f"* {doc.page_content} [{doc.metadata}]")
.. code-block:: none
* thud [{'bar': 'baz'}]
Search with filter:
.. code-block:: 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}]")
.. code-block:: none
* thud [{'bar': 'baz'}]
Search with score:
.. code-block:: python
results = vector_store.similarity_search_with_score(
query="qux", k=1
)
for doc, score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
.. code-block:: none
* [SIM=0.832268] foo [{'baz': 'bar'}]
Async:
.. code-block:: python
# add documents
# await vector_store.aadd_documents(documents=documents)
# delete documents
# await vector_store.adelete(ids=["3"])
# search
# results = vector_store.asimilarity_search(query="thud", k=1)
# search with score
results = await vector_store.asimilarity_search_with_score(query="qux", k=1)
for doc,score in results:
print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
.. code-block:: none
* [SIM=0.832268] foo [{'baz': 'bar'}]
Use as Retriever:
.. code-block:: python
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")
.. code-block:: none
[Document(id='2', metadata={'bar': 'baz'}, page_content='thud')]
""" # noqa: E501

def __init__(self, embedding: Embeddings) -> None:
"""Initialize with the given embedding function.
Expand Down

0 comments on commit 3c784e1

Please sign in to comment.