Query Routing With Langchain
image-searchvector-databasesemantic-searchmilvusembeddingsunstructured-dataquestion-answeringLLMmilvus-bootcampdeep-learningimage-recognitionimage-classificationaudio-searchPythonbootcampragadvanced_ragNLP
Export
Google Colab preparation[optional]
This is an optional step, if you want to run this notebook on Google Colab.
[ ]
[ ]
[ ]
Please prepare you OPENAI_API_KEY in your environment variables.

If you are running this notebook on Google Colab, you have to restart this session by Cmd/Ctrl + M, then press . to make the environment take effect.
[ ]
Get started

Prepare the data
We use the Langchain WebBaseLoader to load documents from blog sources and split them into chunks using the RecursiveCharacterTextSplitter.
[1]
Build the chain
We load the docs into milvus vectorstore, and build a milvus retriever.
[2]
Build a router chain, and try to invoke it. It can return a string that classifies whether the query is decomposable.
[3]
'Decomposable\nReason: The question can be decomposed into two sub-questions: "How can I use Milvus?" and "What is Zilliz?".'
Define the vanilla RAG chain.
[4]
Define the sub query chain.
[5]
Define a route function.
[6]
Let's define the full chain.
[7]
Test the chain
[8]
invoke sub_query_chain... sub_queries: ['What are the different types of memory?', 'What are the different types of ANN algorithms?'] The different types of memory include: 1. Sensory Memory: This is the earliest stage of memory, providing the ability to retain impressions of sensory information after the original stimuli have ended. It typically lasts for up to a few seconds. Subcategories include iconic memory (visual), echoic memory (auditory), and haptic memory (touch). 2. Short-Term Memory (STM) or Working Memory: It stores information that we are currently aware of and needed to carry out complex cognitive tasks such as learning and reasoning. Short-term memory is believed to have the capacity of about 7 items and lasts for 20-30 seconds. 3. Long-Term Memory (LTM): Long-term memory can store information for a remarkably long time, ranging from a few days to decades, with an essentially unlimited storage capacity. There are two subtypes of LTM: - Explicit / declarative memory: This is memory of facts and events, and refers to those memories that can be consciously recalled. - Implicit / procedural memory: This type of memory is unconscious and involves skills and routines that are performed automatically. The different types of Approximate Nearest Neighbors (ANN) algorithms include: 1. LSH (Locality-Sensitive Hashing): It introduces a hashing function such that similar input items are mapped to the same buckets with high probability. 2. ANNOY (Approximate Nearest Neighbors Oh Yeah): The core data structure are random projection trees, a set of binary trees where each non-leaf node represents a hyperplane splitting the input space into half and each leaf stores one data point. 3. FAISS (Facebook AI Similarity Search): It operates on the assumption that in high dimensional space, distances between nodes follow a Gaussian distribution and thus there should exist clustering of data points. 4. ScaNN (Scalable Nearest Neighbors): The main innovation in ScaNN is anisotropic vector quantization. It quantizes a data point to such that the inner product is as similar to the original distance as possible, instead of picking the closet quantization centroid points.
[9]
The different types of memory include: 1. Sensory Memory: This is the earliest stage of memory, retaining impressions of sensory information for up to a few seconds. It includes iconic (visual), echoic (auditory), and haptic (touch) memory. 2. Short-Term Memory (STM) or Working Memory: This stores information that we are currently aware of and needed for complex cognitive tasks. It has a capacity of about 7 items and lasts for 20-30 seconds. 3. Long-Term Memory (LTM): This can store information for a remarkably long time, ranging from a few days to decades, with an essentially unlimited storage capacity. It includes explicit/declarative memory (facts and events that can be consciously recalled) and implicit/procedural memory (skills and routines performed automatically). As for ANN algorithms for fast Maximum Inner Product Search (MIPS), the common choice is the approximate nearest neighbors (ANN) algorithm. This algorithm returns approximately the top k nearest neighbors, trading off a little accuracy for a significant speedup.
[10]
invoke vanilla_rag_chain... The different types of memory include: 1. Sensory Memory: This is the earliest stage of memory, retaining impressions of sensory information for up to a few seconds. It includes iconic (visual), echoic (auditory), and haptic (touch) memory. 2. Short-Term Memory (STM) or Working Memory: This type of memory stores information that we are currently aware of and is needed for complex cognitive tasks. It has a capacity of about 7 items and lasts for 20-30 seconds. 3. Long-Term Memory (LTM): This type of memory can store information for a remarkably long time, ranging from a few days to decades, with an essentially unlimited storage capacity. It has two subtypes: - Explicit / declarative memory: This is memory of facts and events that can be consciously recalled. It includes episodic memory (events and experiences) and semantic memory (facts and concepts). - Implicit / procedural memory: This type of memory is unconscious and involves skills and routines that are performed automatically. 4. Memory Stream: This is a long-term memory module that records a comprehensive list of an agent’s experiences in natural language. These types of memory work together to help us acquire, store, retain, and retrieve information.
[11]
The different types of memory include: 1. Sensory Memory: This is the earliest stage of memory, retaining impressions of sensory information for up to a few seconds. It includes iconic memory (visual), echoic memory (auditory), and haptic memory (touch). 2. Short-Term Memory (STM) or Working Memory: This type of memory stores information that we are currently aware of and is needed for complex cognitive tasks. It has a capacity of about 7 items and lasts for 20-30 seconds. 3. Long-Term Memory (LTM): This type of memory can store information for a remarkably long time, ranging from a few days to decades, with an essentially unlimited storage capacity. It has two subtypes: - Explicit / declarative memory: This is memory of facts and events that can be consciously recalled. It includes episodic memory (events and experiences) and semantic memory (facts and concepts). - Implicit / procedural memory: This type of memory is unconscious and involves skills and routines that are performed automatically. 4. Memory Stream: This is a long-term memory module that records a comprehensive list of agents’ experiences in natural language. Each of these types of memory plays a different role in how we process, store, and recall information.