You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# title and description do not need to be added to markdown, start with H2 (##)
5
-
title: Build PDF Chat App With Couchbase Python SDK, LangChain and Vector Search
6
-
short_title: Build PDF Chat App (Vector Search)
6
+
title: Build PDF Chat App with LangChain and Couchbase Hyperscale and Composite Vector Index
7
+
short_title: Build PDF Chat App
7
8
description:
8
-
- Construct a PDF Chat App with LangChain, Couchbase Python SDK, Couchbase Vector Search (Query Service), and Streamlit.
9
-
- Learn to upload PDFs into Couchbase Vector Store with LangChain using Query-based Vector Search.
10
-
- Discover how to use RAG's for context-based Q&A's from PDFs with LLMs.
9
+
- Construct a PDF Chat App with LangChain, Couchbase Python SDK, Query based Vector Store, and Streamlit.
10
+
- Learn to upload PDFs into Couchbase Query based Vector Store with LangChain.
11
+
- Discover how to use RAG for context-based Q&A from PDFs with LLMs.
11
12
content_type: tutorial
12
13
filter: sdk
13
14
technology:
@@ -28,6 +29,8 @@ length: 45 Mins
28
29
29
30
Welcome to this comprehensive guide on constructing an AI-enhanced Chat Application using Couchbase Vector Search. We will create a dynamic chat interface capable of delving into PDF documents to extract and provide summaries, key facts, and answers to your queries. By the end of this tutorial, you'll have a powerful tool at your disposal, transforming the way you interact with and utilize the information contained within PDFs.
30
31
32
+
**This tutorial uses Query Based Vector Search** with Couchbase's Query Service and Hyperscale/Composite Vector Indexes. If you are looking for Vector Search using the Search service (formerly known as Full Text Search), refer to [this tutorial](https://developer.couchbase.com/tutorial-python-langchain-pdf-chat/) instead.
33
+
31
34
This tutorial will demonstrate how to -
32
35
33
36
- Utilize [Couchbase Vector Search](https://www.couchbase.com/products/vector-search/) with Query Service and Hyperscale/Composite Vector Indexes for doing Vector Search.
@@ -132,7 +135,7 @@ For more details on the `create_index()` method, see the [LangChain Couchbase AP
132
135
133
136
The `index_description` parameter controls how Couchbase optimizes vector storage and search performance:
134
137
135
-
**Format:**`'IVF[<centroids>],{PQ|SQ}<settings>'`
138
+
**Format:**`IVF[<centroids>],{PQ|SQ}<settings>`
136
139
137
140
**Centroids (IVF - Inverted File):**
138
141
- Controls how the dataset is subdivided for faster searches
@@ -152,7 +155,7 @@ The `index_description` parameter controls how Couchbase optimizes vector storag
152
155
153
156
For detailed configuration options, see the [Quantization & Centroid Settings](https://docs.couchbase.com/server/current/vector-index/hyperscale-vector-index.html#algo_settings).
154
157
155
-
> **Note:** In Couchbase Vector Search, the distance represents the vector distance between the query and document embeddings. Lower distance indicates higher similarity, while higher distance indicates lower similarity. This demo uses cosine similarity for measuring document relevance.
158
+
> **Note:** In Couchbase Vector Search, the distance represents the vector distance between the query and document embeddings. Lower distance indicates higher similarity, while higher distance indicates lower similarity. This demo uses cosine similarity for measuring document similarity.
156
159
157
160
### Setup Environment Config
158
161
@@ -177,9 +180,9 @@ LOGIN_PASSWORD = "<password to access the streamlit app>"
177
180
178
181
> For this tutorial, `DB_BUCKET = pdf-chat`, `DB_SCOPE = _default`, `DB_COLLECTION = _default`.
179
182
180
-
> Note: Unlike the Search-based approach, this method does NOT require `INDEX_NAME` as vector indexes are optional and automatically used when available.
183
+
> Note: Unlike the [Search service based approach](https://developer.couchbase.com/tutorial-python-langchain-pdf-chat/), this method does NOT require `INDEX_NAME` as vector indexes are optional and automatically used when available.
181
184
182
-
> Login_Password of Streamlit app is a basic password to access the app. You can set the password here and while using the app, password will be required to access the app.
185
+
> `Login_Password` of Streamlit app is a basic password to access the app. You can set the password here and while using the app, password will be required to access the app.
183
186
184
187
### Running the Application
185
188
@@ -272,15 +275,15 @@ LangChain is a powerful library that simplifies the process of building applicat
272
275
273
276
In the PDF Chat app, LangChain is used for several tasks:
274
277
275
-
-**Loading and processing PDF documents**: LangChain's [_PDFLoader_](https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf/) is used to load the PDF files and convert them into text documents.
276
-
-**Text splitting**: LangChain's [_RecursiveCharacterTextSplitter_](https://python.langchain.com/docs/modules/data_connection/document_transformers/recursive_text_splitter/) is used to split the text from the PDF documents into smaller chunks or passages, which are more suitable for embedding and retrieval.
277
-
-**Embedding generation**: LangChain integrates with [various embedding models](https://python.langchain.com/docs/modules/data_connection/text_embedding/), such as OpenAI's embeddings, to convert the text chunks into embeddings.
278
+
-**Loading and processing PDF documents**: LangChain's [_PDFLoader_](https://docs.langchain.com/oss/python/integrations/document_loaders) is used to load the PDF files and convert them into text documents.
279
+
-**Text splitting**: LangChain's [_RecursiveCharacterTextSplitter_](https://docs.langchain.com/oss/python/integrations/splitters) is used to split the text from the PDF documents into smaller chunks or passages, which are more suitable for embedding and retrieval.
280
+
-**Embedding generation**: LangChain integrates with [various embedding models](https://docs.langchain.com/oss/python/integrations/text_embedding), such as OpenAI's embeddings, to convert the text chunks into embeddings.
278
281
-**Vector store integration**: LangChain provides a [_CouchbaseQueryVectorStore_](https://couchbase-ecosystem.github.io/langchain-couchbase/langchain_couchbase.html#module-langchain_couchbase.vectorstores.query_vector_store) class that seamlessly integrates with Couchbase's Vector Search using Query Service, allowing the app to store and search through the embeddings and their corresponding text.
279
-
-**Chains**: LangChain provides various [chains](https://python.langchain.com/docs/modules/chains/) for different requirements. For using RAG concept, we require _Retrieval Chain_ for Retrieval and _Question Answering Chain_ for Generation part. We also add _Prompts_ that guide the language model's behavior and output. These all are combined to form a single chain which gives output from user questions.
280
-
-**Streaming Output**: LangChain supports [streaming](https://python.langchain.com/docs/expression_language/streaming/), allowing the app to stream the generated answer to the client in real-time.
281
-
-**Caching**: LangChain's [caching layer](https://python.langchain.com/docs/modules/model_io/llms/llm_caching/) integrates with Couchbase to cache LLM responses, reducing costs and improving response times.
282
+
-**Chains**: LangChain provides various [chains](https://api.python.langchain.com/en/latest/langchain/chains.html) for different requirements. For using RAG concept, we require _Retrieval Chain_ for Retrieval and _Question Answering Chain_ for Generation part. We also add _Prompts_ that guide the language model's behavior and output. These all are combined to form a single chain which gives output from user questions.
283
+
-**Streaming Output**: LangChain supports [streaming](https://docs.langchain.com/oss/python/langchain/streaming), allowing the app to stream the generated answer to the client in real-time.
284
+
-**Caching**: LangChain's [caching layer](https://langchain-doc.readthedocs.io/en/latest/modules/llms/examples/llm_caching.html) integrates with Couchbase to cache LLM responses, reducing costs and improving response times.
282
285
283
-
By combining Vector Search with Couchbase Query Service, RAG, LLM Caching, and LangChain; the PDF Chat app can efficiently ingest PDF documents, convert their content into searchable embeddings, retrieve relevant information based on user queries and conversation context, cache LLM responses for repeated queries, and generate context-aware and informative responses using large language models. This approach provides users with a powerful and intuitive way to explore and interact with large PDF files.
286
+
By combining Vector Search with Couchbase Query Service, RAG, LLM Caching, and LangChain, the PDF Chat app can efficiently ingest PDF documents, convert their content into searchable embeddings, retrieve relevant information based on user queries and conversation context, cache LLM responses for repeated queries, and generate context-aware and informative responses using large language models. This approach provides users with a powerful and intuitive way to explore and interact with large PDF files.
@@ -328,6 +330,8 @@ We will also initialize Couchbase vector store with Couchbase bucket info. First
328
330
We will define the bucket, scope, and collection names from [Environment Variables](#setup-environment-config).
329
331
330
332
```python
333
+
from langchain_couchbase.vectorstores import DistanceStrategy
334
+
331
335
# Use OpenAI Embeddings
332
336
embedding = OpenAIEmbeddings()
333
337
@@ -367,6 +371,8 @@ def get_vector_store(
367
371
return vector_store
368
372
```
369
373
374
+
> **Note:** The [`DistanceStrategy`](https://couchbase-ecosystem.github.io/langchain-couchbase/langchain_couchbase#couchbase-query-vector-store) enum from `langchain_couchbase.vectorstores` provides different distance metrics for vector similarity: `COSINE`, `EUCLIDEAN_DISTANCE`, and `DOT_PRODUCT`. In this tutorial, we use `COSINE` similarity which measures the cosine of the angle between two vectors.
375
+
370
376
## Uploading And Ingesting PDF
371
377
372
378
`save_to_vector_store` function takes care of uploading the PDF file in vector format to Couchbase Database using CouchbaseQueryVectorStore in LangChain. It splits text into small chunks, generate embeddings for those chunks, and ingest the chunks and their embeddings into a Couchbase vector store. Let's go step by step on how it does.
@@ -392,7 +398,7 @@ with st.form("upload pdf"):
392
398
393
399
This function ensures that the uploaded PDF file is properly handled, loaded, and prepared for storage or processing in the vector store. It first checks if file was actually uploaded. Then the uploaded file is saved to a temporary file in `binary` format.
394
400
395
-
From the temporary file, PDF is loaded in [PyPDFLoader](https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf/) from the LangChain library which loads the PDF into [LangChain Document](https://python.langchain.com/docs/modules/data_connection/document_loaders/) Format
401
+
From the temporary file, PDF is loaded in [PyPDFLoader](https://reference.langchain.com/python/langchain_core/document_loaders/) from the LangChain library which loads the PDF into [LangChain Document](https://reference.langchain.com/python/langchain_core/document_loaders/) Format
This LangChain document array will contain huge individual files which defeats the purpose while retrieval as we want to send more relevant context to LLM. So we will split it into smaller chunks or passages using LangChain's [_RecursiveCharacterTextSplitter_](https://python.langchain.com/docs/modules/data_connection/document_transformers/recursive_text_splitter/):
418
+
This LangChain document array will contain huge individual files which defeats the purpose while retrieval as we want to send more relevant context to LLM. So we will split it into smaller chunks or passages using LangChain's [_RecursiveCharacterTextSplitter_](https://docs.langchain.com/oss/python/integrations/splitters):
413
419
414
420
- chunk_size: 1500: This parameter specifies that each chunk should contain approximately 1500 characters.
415
421
- chunk_overlap: 150: This parameter ensures that there is an overlap of 150 characters between consecutive chunks. This overlap helps maintain context and prevent important information from being split across chunk boundaries.
We will utilize the vector store created at [Initialize OpenAI and Couchbase Vector Store](#initialize-openai-and-couchbase-vector-store). In this we will add the documents using add_documents method of Couchbase vector store. This method will utilize the OpenAI embeddings to create embeddings(vectors) from text and add it to Couchbase documents in the specified collection.
435
+
We will utilize the vector store created at [Initialize OpenAI and Couchbase Vector Store](#initialize-openai-and-couchbase-vector-store). In this we will add the documents using `add_documents` method of Couchbase vector store. This method will utilize the OpenAI embeddings to create embeddings(vectors) from text and add it to Couchbase documents in the specified collection.
430
436
431
437
```python
432
438
vector_store.add_documents(doc_pages)
@@ -457,7 +463,7 @@ We will be using LCEL chains in next few sections and will see how LCEL optimize
457
463
458
464
### Create Retriever Chain
459
465
460
-
We also create the [retriever](https://python.langchain.com/docs/modules/data_connection/retrievers/vectorstore) of the couchbase vector store. This retriever will be used to retrieve the previously added documents which are similar to current query.
466
+
We also create the [retriever](https://docs.langchain.com/oss/python/integrations/retrievers) of the couchbase vector store. This retriever will be used to retrieve the previously added documents which are similar to current query.
461
467
462
468
```python
463
469
retriever = vector_store.as_retriever()
@@ -535,7 +541,7 @@ This section creates an interactive chat interface where users can ask questions
535
541
- Create a placeholder for streaming the assistant's response.
536
542
- Use the chain.invoke(question) method to generate the response from the RAG chain.
537
543
- The response is automatically cached by the CouchbaseCache layer.
538
-
-[Stream](https://python.langchain.com/docs/use_cases/question_answering/streaming/) the response in real-time using the custom `stream_string` function.
544
+
-[Stream](https://docs.langchain.com/oss/python/langchain/streaming) the response in real-time using the custom `stream_string` function.
539
545
- Add the final assistant's response to the chat history.
540
546
541
547
This setup allows users to have a conversational experience, asking questions related to the uploaded PDF, with responses generated by the RAG chain and streamed in real-time. Both the user's questions and the assistant's responses are displayed in the chat interface, along with their respective roles and avatars.
0 commit comments