Sentence Window With Langchain
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.
We add the wider window text information to each chunk using the write_wider_window function. We can see the metadata of the document contains a wider_text field, which is the wider window text information.
(Document(page_content='Short-term memory: I would consider all the in-context learning (See Prompt Engineering) as utilizing short-term memory of the model to learn.\nLong-term memory: This provides the agent with the capability to retain and recall (infinite) information over extended periods, often by leveraging an external vector store and fast retrieval.\n\n\nTool use\n\nThe agent learns to call external APIs for extra information that is missing from the model weights (often hard to change after pre-training), including current information, code execution capability, access to proprietary information sources and more.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/', 'start_index': 979, 'end_index': 1579, 'wider_text': 't System Overview#\nIn a LLM-powered autonomous agent system, LLM functions as the agent’s brain, complemented by several key components:\n\nPlanning\n\nSubgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks.\nReflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps, thereby improving the quality of final results.\n\n\nMemory\n\nShort-term memory: I would consider all the in-context learning (See Prompt Engineering) as utilizing short-term memory of the model to learn.\nLong-term memory: This provides the agent with the capability to retain and recall (infinite) information over extended periods, often by leveraging an external vector store and fast retrieval.\n\n\nTool use\n\nThe agent learns to call external APIs for extra information that is missing from the model weights (often hard to change after pre-training), including current information, code execution capability, access to proprietary information sources and more.\n\n\n\n\nFig. 1. Overview of a LLM-powered autonomous agent system.\nComponent One: Planning#\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\nTask Decomposition#\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms '}),
, 63) Build the chain
We load the docs into milvus vectorstore, and build a milvus retriever.
Define the vanilla RAG chain.
Define the sentence window chain.
Test the chain
[vanilla_result]: The context provided does not list different types of Artificial Neural Network (ANN) algorithms. However, it does mention different algorithms used for Approximate Nearest Neighbors (ANN) search, which are LSH (Locality-Sensitive Hashing), ANNOY (Approximate Nearest Neighbors Oh Yeah), FAISS (Facebook AI Similarity Search), and ScaNN (Scalable Nearest Neighbors). [sentence_window_result]: The different types of Approximate Nearest Neighbors (ANN) algorithms mentioned in the context are: 1. LSH (Locality-Sensitive Hashing): This algorithm uses a hashing function to map similar input items to the same buckets with high probability. 2. ANNOY (Approximate Nearest Neighbors Oh Yeah): This algorithm uses 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. HNSW (Hierarchical Navigable Small World): This algorithm is inspired by the idea of small world networks where most nodes can be reached by any other nodes within a small number of steps. 4. FAISS (Facebook AI Similarity Search): This algorithm 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. 5. 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.
We can see that the sentence window results include HNSW, but the vanilla results do not. This is because the context window of the sentence window is wider, so it can contain more relevant content fragments to reduce the loss of information.