LangChain API

Vector Search using Deep Lake in LangChain

How to Execute Vector Search Using Deep Lake in LangChain

This tutorial requires installation of:

!pip3 install langchain deeplake openai tiktoken

Let's load the same vector store used in the Quickstart and run embeddings search based on a user prompt using the LangChain API.

from langchain.vectorstores import DeepLake
from langchain.chains import RetrievalQA
from langchain.llms import OpenAIChat
from langchain.embeddings.openai import OpenAIEmbeddings
import os

os.environ['OPENAI_API_KEY'] = <OPENAI_API_KEY>

vector_store_path = 'hub://activeloop/paul_graham_essay'

embedding_function = OpenAIEmbeddings(model = 'text-embedding-ada-002')

# Re-load the vector store
db = DeepLake(dataset_path = vector_store_path, 
              embedding = embedding_function, 
              read_only = True)

qa = RetrievalQA.from_chain_type(llm=OpenAIChat(model = 'gpt-3.5-turbo'), 
                                 chain_type = 'stuff', 
                                 retriever = db.as_retriever())

Let's run a similarity search on Paul Graham's essay based on a query we want to answer. The query is embedded and a similarity search is performed against the stored embeddings, with execution taking place on the client.

If we print the first document using query_docs[0].page_content, it appears to be relevant to the query:

Vector Search in an LLM Context

We can directly use LangChain to run a Q&A using an LLM and answer the question about Paul Graham's essay. Internally, this API performs an embedding search to find the most relevant data to feeds them into the LLM context.

'The first programs he tried writing were on the IBM 1401 that his school district used for "data processing" in 9th grade.'

Vector Search Using the Managed Tensor Database

For Vector Stores in the Managed Tensor Database, queries will automatically execute on the database (instead of the client). Vector Stores are created in the Managed Tensor Database by specifying vector_store_path = hub://org_id/dataset_name and runtime = {"tensor_db": True} during Vector Store creation.

If Vector Stores are not in the Managed Tensor Database, they can be migrated using these steps:

Was this helpful?