Skip to content

Commit 0e52651

Browse files
authored
docs[patch]: Update retrieval and embeddings docs (#5429)
* Update retrieval and embeddings docs * Remove silly rule
1 parent 54ae3f1 commit 0e52651

22 files changed

+321
-361
lines changed

docs/core_docs/.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ docs/how_to/output_parser_structured.md
9595
docs/how_to/output_parser_structured.mdx
9696
docs/how_to/output_parser_json.md
9797
docs/how_to/output_parser_json.mdx
98+
docs/how_to/multiple_queries.md
99+
docs/how_to/multiple_queries.mdx
98100
docs/how_to/logprobs.md
99101
docs/how_to/logprobs.mdx
100102
docs/how_to/graph_semantic.md
@@ -138,6 +140,4 @@ docs/how_to/binding.mdx
138140
docs/how_to/assign.md
139141
docs/how_to/assign.mdx
140142
docs/how_to/agent_executor.md
141-
docs/how_to/agent_executor.mdx
142-
docs/how_to/MultiQueryRetriever.md
143-
docs/how_to/MultiQueryRetriever.mdx
143+
docs/how_to/agent_executor.mdx

docs/core_docs/docs/concepts.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,10 @@ const retriever = vectorstore.asRetriever();
449449

450450
### Retrievers
451451

452-
A retriever is an interface that returns documents given an unstructured query.
453-
It is more general than a vector store.
452+
A retriever is an interface that returns relevant documents given an unstructured query.
453+
They are more general than a vector store.
454454
A retriever does not need to be able to store documents, only to return (or retrieve) them.
455-
Retrievers can be created from vectorstores, but are also broad enough to include [Exa search](/docs/integrations/retrievers/exa/)(web search) and [Amazon Kendra](/docs/integrations/retrievers/kendra-retriever/).
455+
Retrievers can be created from vector stores, but are also broad enough to include [Exa search](/docs/integrations/retrievers/exa/)(web search) and [Amazon Kendra](/docs/integrations/retrievers/kendra-retriever/).
456456

457457
Retrievers accept a string query as input and return an array of Document's as output.
458458

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import CodeBlock from "@theme/CodeBlock";
22
import InMemoryExample from "@examples/embeddings/cache_backed_in_memory.ts";
3-
import ConvexExample from "@examples/embeddings/convex/cache_backed_convex.ts";
43
import RedisExample from "@examples/embeddings/cache_backed_redis.ts";
54

65
# How to cache embedding results
76

7+
:::info Prerequisites
8+
9+
This guide assumes familiarity with the following concepts:
10+
11+
- [Embeddings](/docs/concepts/#embedding-models)
12+
13+
:::
14+
815
Embeddings can be stored or temporarily cached to avoid needing to recompute them.
916

1017
Caching embeddings can be done using a `CacheBackedEmbeddings` instance.
@@ -15,13 +22,13 @@ The text is hashed and the hash is used as the key in the cache.
1522

1623
The main supported way to initialized a `CacheBackedEmbeddings` is the `fromBytesStore` static method. This takes in the following parameters:
1724

18-
- `underlying_embedder`: The embedder to use for embedding.
19-
- `document_embedding_cache`: The cache to use for storing document embeddings.
20-
- `namespace`: (optional, defaults to "") The namespace to use for document cache. This namespace is used to avoid collisions with other caches. For example, set it to the name of the embedding model used.
25+
- `underlyingEmbeddings`: The embeddings model to use.
26+
- `documentEmbeddingCache`: The cache to use for storing document embeddings.
27+
- `namespace`: (optional, defaults to "") The namespace to use for document cache. This namespace is used to avoid collisions with other caches. For example, you could set it to the name of the embedding model used.
2128

2229
**Attention:** Be sure to set the namespace parameter to avoid collisions of the same text embedded using different embeddings models.
2330

24-
## Usage, in-memory
31+
## In-memory
2532

2633
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
2734

@@ -36,47 +43,7 @@ Do not use this cache if you need to actually store the embeddings for an extend
3643

3744
<CodeBlock language="typescript">{InMemoryExample}</CodeBlock>
3845

39-
## Usage, Convex
40-
41-
Here's an example with a [Convex](https://convex.dev/) as a cache.
42-
43-
### Create project
44-
45-
Get a working [Convex](https://docs.convex.dev/) project set up, for example by using:
46-
47-
```bash
48-
npm create convex@latest
49-
```
50-
51-
### Add database accessors
52-
53-
Add query and mutation helpers to `convex/langchain/db.ts`:
54-
55-
```ts title="convex/langchain/db.ts"
56-
export * from "langchain/util/convex";
57-
```
58-
59-
### Configure your schema
60-
61-
Set up your schema (for indexing):
62-
63-
```ts title="convex/schema.ts"
64-
import { defineSchema, defineTable } from "convex/server";
65-
import { v } from "convex/values";
66-
67-
export default defineSchema({
68-
cache: defineTable({
69-
key: v.string(),
70-
value: v.any(),
71-
}).index("byKey", ["key"]),
72-
});
73-
```
74-
75-
### Example
76-
77-
<CodeBlock language="typescript">{ConvexExample}</CodeBlock>
78-
79-
## Usage, Redis
46+
## Redis
8047

8148
Here's an example with a Redis cache.
8249

@@ -87,3 +54,9 @@ npm install ioredis
8754
```
8855

8956
<CodeBlock language="typescript">{RedisExample}</CodeBlock>
57+
58+
## Next steps
59+
60+
You've now learned how to use caching to avoid recomputing embeddings.
61+
62+
Next, check out the [full tutorial on retrieval-augmented generation](/docs/tutorials/rag).

docs/core_docs/docs/how_to/contextual_compression.mdx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
---
2-
hide_table_of_contents: true
3-
---
4-
51
# How to do retrieval with contextual compression
62

3+
:::info Prerequisites
4+
5+
This guide assumes familiarity with the following concepts:
6+
7+
- [Retrievers](/docs/concepts/#retrievers)
8+
- [Retrieval-augmented generation (RAG)](/docs/tutorials/rag)
9+
10+
:::
11+
712
One challenge with retrieval is that usually you don't know the specific queries your document storage system will face when you ingest data into the system. This means that the information most relevant to a query may be buried in a document with a lot of irrelevant text. Passing that full document through your application can lead to more expensive LLM calls and poorer responses.
813

914
Contextual compression is meant to fix this. The idea is simple: instead of immediately returning retrieved documents as-is, you can compress them using the context of the given query, so that only the relevant information is returned. “Compressing” here refers to both compressing the contents of an individual document and filtering out documents wholesale.
@@ -58,3 +63,10 @@ This skips the need to add documents to a vector store to perform similarity sea
5863
import DocumentCompressorPipelineExample from "@examples/retrievers/document_compressor_pipeline.ts";
5964

6065
<CodeBlock language="typescript">{DocumentCompressorPipelineExample}</CodeBlock>
66+
67+
## Next steps
68+
69+
You've now learned a few ways to use contextual compression to remove bad data from your results.
70+
71+
See the individual sections for deeper dives on specific retrievers, the [broader tutorial on RAG](/docs/tutorials/rag), or this section to learn how to
72+
[create your own custom retriever over any data source](/docs/modules/data_connection/retrievers/custom).

docs/core_docs/docs/how_to/custom_retriever.mdx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
---
2-
hide_table_of_contents: true
3-
sidebar_position: 0
4-
---
5-
61
# How to write a custom retriever class
72

8-
To create your own retriever, you need to extend the [`BaseRetriever` class](https://api.js.langchain.com/classes/langchain_core_retrievers.BaseRetriever.html)
9-
and implement a `_getRelevantDocuments` method that takes a `string` as its first parameter and an optional `runManager` for tracing.
10-
This method should return an array of `Document`s fetched from some source. This process can involve calls to a database or to the web using `fetch`.
11-
Note the underscore before `_getRelevantDocuments()` - the base class wraps the non-prefixed version in order to automatically handle tracing of the original call.
3+
:::info Prerequisites
4+
5+
This guide assumes familiarity with the following concepts:
6+
7+
- [Retrievers](/docs/concepts/#retrievers)
8+
9+
:::
10+
11+
To create your own retriever, you need to extend the [`BaseRetriever`](https://api.js.langchain.com/classes/langchain_core_retrievers.BaseRetriever.html) class
12+
and implement a `_getRelevantDocuments` method that takes a `string` as its first parameter (and an optional `runManager` for tracing).
13+
This method should return an array of `Document`s fetched from some source. This process can involve calls to a database, to the web using `fetch`, or any other source.
14+
Note the underscore before `_getRelevantDocuments()`. The base class wraps the non-prefixed version in order to automatically handle tracing of the original call.
1215

1316
Here's an example of a custom retriever that returns static documents:
1417

@@ -70,3 +73,9 @@ await retriever.invoke("LangChain docs");
7073
}
7174
]
7275
```
76+
77+
## Next steps
78+
79+
You've now seen an example of implementing your own custom retriever.
80+
81+
Next, check out the individual sections for deeper dives on specific retrievers, or the [broader tutorial on RAG](/docs/tutorials/rag).

docs/core_docs/docs/how_to/embed_text.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ sidebar_position: 2
88
Head to [Integrations](/docs/integrations/text_embedding) for documentation on built-in integrations with text embedding providers.
99
:::
1010

11-
The Embeddings class is a class designed for interfacing with text embedding models. There are lots of embedding model providers (OpenAI, Cohere, Hugging Face, etc) - this class is designed to provide a standard interface for all of them.
11+
:::info Prerequisites
12+
13+
This guide assumes familiarity with the following concepts:
14+
15+
- [Embeddings](/docs/concepts/#embedding-models)
16+
17+
:::
1218

1319
Embeddings create a vector representation of a piece of text. This is useful because it means we can think about text in the vector space, and do things like semantic search where we look for pieces of text that are most similar in the vector space.
1420

1521
The base Embeddings class in LangChain exposes two methods: one for embedding documents and one for embedding a query. The former takes as input multiple texts, while the latter takes a single text. The reason for having these as two separate methods is that some embedding providers have different embedding methods for documents (to be searched over) vs queries (the search query itself).
1622

1723
## Get started
1824

19-
Embeddings can be used to create a numerical representation of textual data. This numerical representation is useful because it can be used to find similar documents.
20-
2125
Below is an example of how to use the OpenAI embeddings. Embeddings occasionally have different embedding methods for queries versus documents, so the embedding class exposes a `embedQuery` and `embedDocuments` method.
2226

2327
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
@@ -77,3 +81,9 @@ const documentRes = await embeddings.embedDocuments(["Hello world", "Bye bye"]);
7781
]
7882
*/
7983
```
84+
85+
## Next steps
86+
87+
You've now learned how to use embeddings models with queries and text.
88+
89+
Next, check out how to [avoid excessively recomputing embeddings with caching](/docs/how_to/caching_embeddings), or the [full tutorial on retrieval-augmented generation](/docs/tutorials/rag).

docs/core_docs/docs/how_to/index.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ Embedding Models take a piece of text and create a numerical representation of i
127127

128128
Vector stores are databases that can efficiently store and retrieve embeddings.
129129

130-
- [How to: use a vector store to retrieve data](/docs/how_to/vectorstores)
130+
- [How to: create and query vector stores](/docs/how_to/vectorstores)
131131

132132
### Retrievers
133133

134134
Retrievers are responsible for taking a query and returning relevant documents.
135135

136136
- [How to: use a vector store to retrieve data](/docs/how_to/vectorstore_retriever)
137-
- [How to: generate multiple queries to retrieve data for](/docs/how_to/MultiQueryRetriever)
137+
- [How to: generate multiple queries to retrieve data for](/docs/how_to/multiple_queries)
138138
- [How to: use contextual compression to compress the data retrieved](/docs/how_to/contextual_compression)
139139
- [How to: write a custom retriever class](/docs/how_to/custom_retriever)
140140
- [How to: add similarity scores to retriever results](/docs/how_to/add_scores_retriever)
@@ -144,7 +144,7 @@ Retrievers are responsible for taking a query and returning relevant documents.
144144
- [How to: retrieve the whole document for a chunk](/docs/how_to/parent_document_retriever)
145145
- [How to: generate metadata filters](/docs/how_to/self_query)
146146
- [How to: create a time-weighted retriever](/docs/how_to/time_weighted_vectorstore)
147-
- [How to: use a Matryoshka retriever](/docs/how_to/matryoshka_retriever)
147+
- [How to: reduce retrieval latency](/docs/how_to/reduce_retrieval_latency)
148148

149149
### Indexing
150150

docs/core_docs/docs/how_to/multi_vector.mdx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
---
2-
hide_table_of_contents: true
3-
---
4-
51
# How to generate multiple embeddings per document
62

7-
It can often be beneficial to store multiple vectors per document.
8-
LangChain has a base MultiVectorRetriever which makes querying this type of setup easier!
3+
:::info Prerequisites
4+
5+
This guide assumes familiarity with the following concepts:
6+
7+
- [Retrievers](/docs/concepts/#retrievers)
8+
- [Text splitters](/docs/concepts/#text-splitters)
9+
- [Retrieval-augmented generation (RAG)](/docs/tutorials/rag)
10+
11+
:::
12+
13+
Embedding different representations of an original document, then returning the original document when any of the representations result in a search hit, can allow you to
14+
tune and improve your retrieval performance. LangChain has a base [`MultiVectorRetriever`](https://api.js.langchain.com/classes/langchain_retrievers_multi_vector.MultiVectorRetriever.html) designed to do just this!
915

1016
A lot of the complexity lies in how to create the multiple vectors per document.
11-
This notebook covers some of the common ways to create those vectors and use the MultiVectorRetriever.
17+
This guide covers some of the common ways to create those vectors and use the `MultiVectorRetriever`.
1218

1319
Some methods to create multiple vectors per document include:
1420

15-
- smaller chunks: split a document into smaller chunks, and embed those (e.g. the [ParentDocumentRetriever](/docs/modules/data_connection/retrievers/parent-document-retriever))
21+
- smaller chunks: split a document into smaller chunks, and embed those (e.g. the [`ParentDocumentRetriever`](/docs/modules/data_connection/retrievers/parent-document-retriever))
1622
- summary: create a summary for each document, embed that along with (or instead of) the document
1723
- hypothetical questions: create hypothetical questions that each document would be appropriate to answer, embed those along with (or instead of) the document
1824

@@ -54,3 +60,10 @@ These questions can then be embedded and used to retrieve the original document:
5460
import HypotheticalExample from "@examples/retrievers/multi_vector_hypothetical.ts";
5561

5662
<CodeBlock language="typescript">{HypotheticalExample}</CodeBlock>
63+
64+
## Next steps
65+
66+
You've now learned a few ways to generate multiple embeddings per document.
67+
68+
Next, check out the individual sections for deeper dives on specific retrievers, the [broader tutorial on RAG](/docs/tutorials/rag), or this section to learn how to
69+
[create your own custom retriever over any data source](/docs/modules/data_connection/retrievers/custom).

0 commit comments

Comments
 (0)