LogoLogo
API ReferenceGitHubSlackService StatusLogin
v3.8.16
v3.8.16
  • Deep Lake Docs
  • Vector Store Quickstart
  • Deep Learning Quickstart
  • Storage & Credentials
    • Storage Options
    • User Authentication
    • Storing Deep Lake Data in Your Own Cloud
      • Microsoft Azure
        • Provisioning Federated Credentials
        • Enabling CORS
      • Amazon Web Services
        • Provisioning Role-Based Access
        • Enabling CORS
  • List of ML Datasets
  • 🏢High-Performance Features
    • Introduction
    • Performant Dataloader
    • Tensor Query Language (TQL)
      • TQL Syntax
      • Sampling Datasets
    • Deep Memory
      • How it Works
    • Index for ANN Search
      • Caching and Optimization
    • Managed Tensor Database
      • REST API
      • Migrating Datasets to the Tensor Database
  • 📚EXAMPLE CODE
    • Getting Started
      • Vector Store
        • Step 1: Hello World
        • Step 2: Creating Deep Lake Vector Stores
        • Step 3: Performing Search in Vector Stores
        • Step 4: Customizing Vector Stores
      • Deep Learning
        • Step 1: Hello World
        • Step 2: Creating Deep Lake Datasets
        • Step 3: Understanding Compression
        • Step 4: Accessing and Updating Data
        • Step 5: Visualizing Datasets
        • Step 6: Using Activeloop Storage
        • Step 7: Connecting Deep Lake Datasets to ML Frameworks
        • Step 8: Parallel Computing
        • Step 9: Dataset Version Control
        • Step 10: Dataset Filtering
    • Tutorials (w Colab)
      • Vector Store Tutorials
        • Vector Search Options
          • Deep Lake Vector Store API
          • REST API
          • LangChain API
        • Image Similarity Search
        • Deep Lake Vector Store in LangChain
        • Deep Lake Vector Store in LlamaIndex
        • Improving Search Accuracy using Deep Memory
      • Deep Learning Tutorials
        • Creating Datasets
          • Creating Complex Datasets
          • Creating Object Detection Datasets
          • Creating Time-Series Datasets
          • Creating Datasets with Sequences
          • Creating Video Datasets
        • Training Models
          • Splitting Datasets for Training
          • Training an Image Classification Model in PyTorch
          • Training Models Using MMDetection
          • Training Models Using PyTorch Lightning
          • Training on AWS SageMaker
          • Training an Object Detection and Segmentation Model in PyTorch
        • Updating Datasets
        • Data Processing Using Parallel Computing
      • Concurrent Writes
        • Concurrency Using Zookeeper Locks
    • Playbooks
      • Querying, Training and Editing Datasets with Data Lineage
      • Evaluating Model Performance
      • Training Reproducibility Using Deep Lake and Weights & Biases
      • Working with Videos
    • Low-Level API Summary
  • 🔬Technical Details
    • Best Practices
      • Creating Datasets at Scale
      • Training Models at Scale
      • Storage Synchronization and "with" Context
      • Restoring Corrupted Datasets
      • Concurrent Writes
    • Data Layout
    • Version Control and Querying
    • Dataset Visualization
    • Tensor Relationships
    • Visualizer Integration
    • Shuffling in dataloaders
    • How to Contribute
Powered by GitBook
On this page
  • How to Execute Vector Search Using Deep Lake in LangChain
  • Vector Similarity Search
  • Vector Search in an LLM Context
  • Vector Search Using the Managed Tensor Database

Was this helpful?

  1. EXAMPLE CODE
  2. Tutorials (w Colab)
  3. Vector Store Tutorials
  4. Vector Search Options

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())

Vector Similarity Search

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.

prompt = 'What are the first programs he tried writing?'

query_docs = db.similarity_search(query = prompt)

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

What I Worked On

February 2021

Before college the two main things I worked on, outside of school, were writing and programming. I didn't write essays. I wrote what beginning writers were supposed to write then, and probably still are: short stories. My stories were awful. They had hardly any plot, just characters with strong feelings, which I imagined made them deep.

The first programs I tried writing were on the IBM 1401 that our school district used for what was then called "data processing." This was in 9th grade, so I was 13 or 14. The school district's 1401 happened to be in the basement of our junior high school, and my friend Rich Draves and I got permission to use it. It was like a mini Bond villain's lair down there, with all these alien-looking machines — CPU, disk drives, printer, card reader — sitting up on a raised floor under bright fluorescent lights.

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.

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

qa.run(prompt)

'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

# db = DeepLake(dataset_path = "hub://<org_id>/<dataset_name>", 
#               runtime = {"tensor_db": True},
#               embedding = embedding_function
#              )
PreviousREST APINextImage Similarity Search

Was this helpful?

For Vector Stores in the , 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, :

📚
Managed Tensor Database
they can be migrated using these steps