[docs]: vector store integration pages (#24858)

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Isaac Francisco
2024-08-06 10:20:27 -07:00
committed by GitHub
parent 2c798622cd
commit a72fddbf8d
29 changed files with 5649 additions and 4436 deletions

View File

@@ -67,15 +67,13 @@
"import getpass\n",
"import os\n",
"\n",
"import os\n",
"\n",
"if not os.getenv(\"__MODULE_NAME___API_KEY\"):\n",
" import getpass\n",
" os.environ[\"__MODULE_NAME___API_KEY\"] = getpass.getpass(\"Enter your __ModuleName__ API key: \")"
]
},
{
"cell_type": "markdown",
"id": "7f98392b",
"metadata": {},
"source": [
"If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:"
@@ -84,6 +82,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "e7b6a6e0",
"metadata": {},
"outputs": [],
"source": [
@@ -96,9 +95,16 @@
"id": "93df377e",
"metadata": {},
"source": [
"## Instantiation\n",
"## Initialization\n",
"\n",
"- TODO: Fill out with relevant init params"
"- TODO: Fill out with relevant init params\n",
"\n",
"\n",
"```{=mdx}\n",
"import EmbeddingTabs from \"@theme/EmbeddingTabs\";\n",
"\n",
"<EmbeddingTabs/>\n",
"```"
]
},
{
@@ -112,7 +118,7 @@
"source": [
"from __module_name__.vectorstores import __ModuleName__VectorStore\n",
"\n",
"vector_store = __ModuleName__VectorStore()"
"vector_store = __ModuleName__VectorStore(embeddings=embeddings)"
]
},
{
@@ -146,14 +152,14 @@
" metadata={\"source\": \"https://example.com\"}\n",
")\n",
"\n",
"document_2 = Document(\n",
"document_3 = Document(\n",
" page_content=\"baz\",\n",
" metadata={\"source\": \"https://example.com\"}\n",
")\n",
"\n",
"documents = [document_1, document_2]\n",
"documents = [document_1, document_2, document_3]\n",
"\n",
"vector_store.add_documents(documents=documents,ids=[\"1\",\"2\"])"
"vector_store.add_documents(documents=documents,ids=[\"1\",\"2\",\"3\"])"
]
},
{
@@ -224,7 +230,7 @@
"metadata": {},
"outputs": [],
"source": [
"results = vector_store.similarity_search(query=\"thud\",k=1,filter={\"source\":\"https://example.com\"})\n",
"results = vector_store.similarity_search(query=\"thud\",k=1,filter={\"source\":\"https://another-example.com\"})\n",
"for doc in results:\n",
" print(f\"* {doc.page_content} [{doc.metadata}]\")"
]
@@ -270,7 +276,10 @@
"metadata": {},
"outputs": [],
"source": [
"retriever = vector_store.as_retriever()\n",
"retriever = vector_store.as_retriever(\n",
" search_type=\"mmr\",\n",
" search_kwargs={\"k\": 1}\n",
")\n",
"retriever.invoke(\"thud\")"
]
},
@@ -279,7 +288,15 @@
"id": "901c75dc",
"metadata": {},
"source": [
"Using retriever in a simple RAG chain:"
"## Chain usage\n",
"\n",
"The code below shows how to use the vector store as a retriever in a simple RAG chain:\n",
"\n",
"```{=mdx}\n",
"import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
"\n",
"<ChatModelTabs customVarName=\"llm\" />\n",
"```"
]
},
{

View File

@@ -57,6 +57,7 @@ class __ModuleName__VectorStore(VectorStore):
.. code-block:: python
from __module_name__.vectorstores import __ModuleName__VectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = __ModuleName__VectorStore(
collection_name="foo",
@@ -71,24 +72,19 @@ class __ModuleName__VectorStore(VectorStore):
from langchain_core.documents import Document
document = Document(page_content="foo", metadata={"baz": "bar"})
vector_store.add_documents([document],ids=["1"])
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)
# TODO: Populate with relevant variables.
Update Documents:
.. code-block:: python
updated_document = Document(
page_content="qux",
metadata={"bar": "baz"}
)
vector_store.update_documents(document_id="1",document=updated_document)
Delete Documents:
.. code-block:: python
vector_store.delete(ids=["1"])
vector_store.delete(ids=["3"])
# TODO: Fill out with relevant variables and example output.
Search:
@@ -102,11 +98,23 @@ class __ModuleName__VectorStore(VectorStore):
# TODO: Example output
# TODO: Fill out with relevant variables and example output.
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
# TODO: Example output
# TODO: Fill out with relevant variables and example output.
Search with score:
.. code-block:: python
results = vector_store.similarity_search_with_score(query="thud",k=1)
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}]")
@@ -114,13 +122,35 @@ class __ModuleName__VectorStore(VectorStore):
# TODO: Example output
# TODO: Fill out with relevant variables and example output.
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
# TODO: Example output
# TODO: Fill out with relevant variables and example output.
Use as Retriever:
.. code-block:: python
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={"k": 1, "fetch_k": 10, "lambda_mult": 0.5},
search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.invoke("thud")