mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-21 02:19:31 +00:00
[docs]: vector store integration pages (#24858)
Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
@@ -72,21 +72,130 @@ def _len_check_if_sized(x: Any, y: Any, x_name: str, y_name: str) -> None:
|
||||
|
||||
|
||||
class FAISS(VectorStore):
|
||||
"""`Meta Faiss` vector store.
|
||||
"""FAISS vector store integration.
|
||||
|
||||
To use, you must have the ``faiss`` python package installed.
|
||||
Setup:
|
||||
Install ``langchain_community`` and ``faiss-cpu`` python packages.
|
||||
|
||||
Example:
|
||||
.. code-block:: bash
|
||||
|
||||
pip install -qU langchain_community faiss-cpu
|
||||
|
||||
Key init args — indexing params:
|
||||
embedding_function: Embeddings
|
||||
Embedding function to use.
|
||||
|
||||
Key init args — client params:
|
||||
index: Any
|
||||
FAISS index to use.
|
||||
docstore: Docstore
|
||||
Docstore to use.
|
||||
index_to_docstore_id: Dict[int, str]
|
||||
Mapping of index to docstore id.
|
||||
|
||||
Instantiate:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_community.embeddings.openai import OpenAIEmbeddings
|
||||
import faiss
|
||||
from langchain_community.vectorstores import FAISS
|
||||
from langchain_community.docstore.in_memory import InMemoryDocstore
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
|
||||
embeddings = OpenAIEmbeddings()
|
||||
texts = ["FAISS is an important library", "LangChain supports FAISS"]
|
||||
faiss = FAISS.from_texts(texts, embeddings)
|
||||
index = faiss.IndexFlatL2(len(OpenAIEmbeddings().embed_query("hello world")))
|
||||
|
||||
"""
|
||||
vector_store = FAISS(
|
||||
embedding_function=OpenAIEmbeddings(),
|
||||
index=index,
|
||||
docstore= InMemoryDocstore(),
|
||||
index_to_docstore_id={}
|
||||
)
|
||||
|
||||
Add Documents:
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_core.documents import Document
|
||||
|
||||
document_1 = Document(page_content="foo", metadata={"baz": "bar"})
|
||||
document_2 = Document(page_content="thud", metadata={"bar": "baz"})
|
||||
document_3 = Document(page_content="i will be deleted :(")
|
||||
|
||||
documents = [document_1, document_2, document_3]
|
||||
ids = ["1", "2", "3"]
|
||||
vector_store.add_documents(documents=documents, ids=ids)
|
||||
|
||||
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:: python
|
||||
|
||||
* thud [{'bar': 'baz'}]
|
||||
|
||||
Search with filter:
|
||||
.. code-block:: python
|
||||
|
||||
results = vector_store.similarity_search(query="thud",k=1,filter={"bar": "baz"})
|
||||
for doc in results:
|
||||
print(f"* {doc.page_content} [{doc.metadata}]")
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
* 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:: python
|
||||
|
||||
* [SIM=0.335304] foo [{'baz': 'bar'}]
|
||||
|
||||
Async:
|
||||
.. code-block:: python
|
||||
|
||||
# add documents
|
||||
# await vector_store.aadd_documents(documents=documents, ids=ids)
|
||||
|
||||
# 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:: python
|
||||
|
||||
* [SIM=0.335304] 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:: python
|
||||
|
||||
[Document(metadata={'bar': 'baz'}, page_content='thud')]
|
||||
|
||||
""" # noqa: E501
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
Reference in New Issue
Block a user