Skip to content

Commit 173e1c7

Browse files
Addressed comments
1 parent 3df16b8 commit 173e1c7

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed
-2.44 KB
Loading
11.2 KB
Loading

tutorial/markdown/python/python-langchain-pdf-chat-query/python-langchain-pdf-chat-query.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
22
# frontmatter
3-
path: "/tutorial-python-langchain-pdf-chat-query"
3+
path: "/tutorial-python-langchain-pdf-chat-with-hyperscale-or-composite-vector-index"
4+
alt_paths: ["/tutorial-python-langchain-pdf-chat-with-hyperscale-vector-index", "/tutorial-python-langchain-pdf-chat-with-composite-vector-index"]
45
# 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
78
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.
1112
content_type: tutorial
1213
filter: sdk
1314
technology:
@@ -28,6 +29,8 @@ length: 45 Mins
2829

2930
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.
3031

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+
3134
This tutorial will demonstrate how to -
3235

3336
- 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
132135

133136
The `index_description` parameter controls how Couchbase optimizes vector storage and search performance:
134137

135-
**Format:** `'IVF[<centroids>],{PQ|SQ}<settings>'`
138+
**Format:** `IVF[<centroids>],{PQ|SQ}<settings>`
136139

137140
**Centroids (IVF - Inverted File):**
138141
- Controls how the dataset is subdivided for faster searches
@@ -152,7 +155,7 @@ The `index_description` parameter controls how Couchbase optimizes vector storag
152155

153156
For detailed configuration options, see the [Quantization & Centroid Settings](https://docs.couchbase.com/server/current/vector-index/hyperscale-vector-index.html#algo_settings).
154157

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.
156159
157160
### Setup Environment Config
158161

@@ -177,9 +180,9 @@ LOGIN_PASSWORD = "<password to access the streamlit app>"
177180
178181
> For this tutorial, `DB_BUCKET = pdf-chat`, `DB_SCOPE = _default`, `DB_COLLECTION = _default`.
179182
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.
181184
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.
183186
184187
### Running the Application
185188

@@ -272,15 +275,15 @@ LangChain is a powerful library that simplifies the process of building applicat
272275

273276
In the PDF Chat app, LangChain is used for several tasks:
274277

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.
278281
- **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.
282285

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.
284287

285288
## Let us Understand the Flow
286289

@@ -310,8 +313,7 @@ def connect_to_couchbase(connection_string, db_username, db_password):
310313

311314
auth = PasswordAuthenticator(db_username, db_password)
312315
options = ClusterOptions(auth)
313-
connect_string = connection_string
314-
cluster = Cluster(connect_string, options)
316+
cluster = Cluster(connection_string, options)
315317

316318
# Wait until the cluster is ready for use.
317319
cluster.wait_until_ready(timedelta(seconds=5))
@@ -328,6 +330,8 @@ We will also initialize Couchbase vector store with Couchbase bucket info. First
328330
We will define the bucket, scope, and collection names from [Environment Variables](#setup-environment-config).
329331

330332
```python
333+
from langchain_couchbase.vectorstores import DistanceStrategy
334+
331335
# Use OpenAI Embeddings
332336
embedding = OpenAIEmbeddings()
333337

@@ -367,6 +371,8 @@ def get_vector_store(
367371
return vector_store
368372
```
369373

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+
370376
## Uploading And Ingesting PDF
371377

372378
`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"):
392398

393399
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.
394400

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
396402

397403
```python
398404
def save_to_vector_store(uploaded_file, vector_store):
@@ -409,7 +415,7 @@ def save_to_vector_store(uploaded_file, vector_store):
409415

410416
### Split Documents
411417

412-
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):
413419

414420
- chunk_size: 1500: This parameter specifies that each chunk should contain approximately 1500 characters.
415421
- 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.
@@ -426,7 +432,7 @@ doc_pages = text_splitter.split_documents(docs)
426432

427433
### Add Documents to Vector Store
428434

429-
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.
430436

431437
```python
432438
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
457463

458464
### Create Retriever Chain
459465

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.
461467

462468
```python
463469
retriever = vector_store.as_retriever()
@@ -535,7 +541,7 @@ This section creates an interactive chat interface where users can ask questions
535541
- Create a placeholder for streaming the assistant's response.
536542
- Use the chain.invoke(question) method to generate the response from the RAG chain.
537543
- 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.
539545
- Add the final assistant's response to the chat history.
540546

541547
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

Comments
 (0)