← Back to Cookbook
SubQuestionQueryEngine
Details
File: third_party/LlamaIndex/SubQuestionQueryEngine.ipynb
Type: Jupyter Notebook
Use Cases: Agents, RAG
Integrations: Llamaindex
Content
Notebook content (JSON format):
{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "a3b1829d", "metadata": {}, "source": [ "<a href=\"https://colab.research.google.com/github/mistralai/cookbook/blob/main/third_party/LlamaIndex/SubQuestionQueryEngine.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" ] }, { "cell_type": "markdown", "id": "02c3900a-6024-42ec-af62-af1001fd4af5", "metadata": {}, "source": [ "# Sub-Question Query Engine with Mistral AI and LlamaIndex \n", "\n", "A `VectorStoreIndex` is adept at addressing queries that pertain to specific contexts within a single document or a collection of documents. However, user queries in real-world scenarios can be intricate, often requiring the retrieval of context from multiple documents to provide an answer. In such situations, a straightforward VectorStoreIndex might not suffice. Instead, breaking down the complex user queries into sub-queries can yield more accurate responses.\n", "\n", "In this notebook, we will explore how the `SubQuestionQueryEngine` can be leveraged to tackle complex queries by generating and addressing sub-queries." ] }, { "cell_type": "markdown", "id": "b451e7df-78eb-457e-ae3d-d84af7fef8e3", "metadata": {}, "source": [ "### Installation" ] }, { "cell_type": "code", "execution_count": null, "id": "ad1e167f-2332-4be0-844e-0fcb0e97d663", "metadata": {}, "outputs": [], "source": [ "!pip install llama-index\n", "!pip install llama-index-llms-mistralai\n", "!pip install llama-index-embeddings-mistralai" ] }, { "cell_type": "markdown", "id": "fa002b8e-4a22-4033-ab71-0ade6065d19a", "metadata": {}, "source": [ "### Setup API Key" ] }, { "cell_type": "code", "execution_count": 1, "id": "e2447175-3cdf-4c41-8846-d678fed72e3a", "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['MISTRAL_API_KEY'] = '<YOUR MISTRAL API KEY>'" ] }, { "cell_type": "markdown", "id": "71dd4757-05e6-4357-a4de-681634d9a55b", "metadata": {}, "source": [ "### Set LLM and Embedding Model" ] }, { "cell_type": "code", "execution_count": 2, "id": "6572ec1c-bbbc-4d15-86a3-a7ecf72360e1", "metadata": {}, "outputs": [], "source": [ "import nest_asyncio\n", "\n", "nest_asyncio.apply()" ] }, { "cell_type": "code", "execution_count": 3, "id": "a1ae8238-f53e-4e6c-8448-08c9ab3894a4", "metadata": {}, "outputs": [], "source": [ "from llama_index.core import SimpleDirectoryReader, VectorStoreIndex\n", "from llama_index.llms.mistralai import MistralAI\n", "from llama_index.embeddings.mistralai import MistralAIEmbedding\n", "from llama_index.core import Settings\n", "\n", "from llama_index.core.tools import QueryEngineTool, ToolMetadata\n", "from llama_index.core.query_engine import SubQuestionQueryEngine" ] }, { "cell_type": "code", "execution_count": 4, "id": "d5ac41d1-2223-4577-a533-cbf1949607c8", "metadata": {}, "outputs": [], "source": [ "llm = MistralAI(model='mistral-large')\n", "embed_model = MistralAIEmbedding()\n", "\n", "Settings.llm = llm\n", "Settings.embed_model = embed_model\n", "Settings.chunk_size = 512" ] }, { "cell_type": "markdown", "id": "9b675f52-6e6b-4493-ba68-b03121d1c965", "metadata": {}, "source": [ "### Logging" ] }, { "cell_type": "code", "execution_count": 5, "id": "1bac3309-c613-4f47-b192-c0dd3f6ecac8", "metadata": {}, "outputs": [], "source": [ "# NOTE: This is ONLY necessary in jupyter notebook.\n", "# Details: Jupyter runs an event-loop behind the scenes.\n", "# This results in nested event-loops when we start an event-loop to make async queries.\n", "# This is normally not allowed, we use nest_asyncio to allow it for convenience.\n", "import nest_asyncio\n", "\n", "nest_asyncio.apply()\n", "\n", "import logging\n", "import sys\n", "\n", "# Set up the root logger\n", "logger = logging.getLogger()\n", "logger.setLevel(logging.INFO) # Set logger level to INFO\n", "\n", "# Clear out any existing handlers\n", "logger.handlers = []\n", "\n", "# Set up the StreamHandler to output to sys.stdout (Colab's output)\n", "handler = logging.StreamHandler(sys.stdout)\n", "handler.setLevel(logging.INFO) # Set handler level to INFO\n", "\n", "# Add the handler to the logger\n", "logger.addHandler(handler)\n", "\n", "from IPython.display import display, HTML" ] }, { "cell_type": "markdown", "id": "96d5cbc1-d469-4872-ab79-049ff5234819", "metadata": {}, "source": [ "### Download Data\n", "\n", "We will use `Uber, Lyft 10K SEC Filings` and `Paul Graham Essay Document`." ] }, { "cell_type": "code", "execution_count": 6, "id": "6f2f70e2-7a18-417e-82c0-158c784a265e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2024-04-03 21:42:52-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/uber_2021.pdf\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.110.133, 185.199.109.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 1880483 (1.8M) [application/octet-stream]\n", "Saving to: ‘./uber_2021.pdf’\n", "\n", "./uber_2021.pdf 100%[===================>] 1.79M --.-KB/s in 0.05s \n", "\n", "2024-04-03 21:42:52 (36.2 MB/s) - ‘./uber_2021.pdf’ saved [1880483/1880483]\n", "\n", "--2024-04-03 21:42:52-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/lyft_2021.pdf\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 1440303 (1.4M) [application/octet-stream]\n", "Saving to: ‘./lyft_2021.pdf’\n", "\n", "./lyft_2021.pdf 100%[===================>] 1.37M --.-KB/s in 0.05s \n", "\n", "2024-04-03 21:42:53 (26.7 MB/s) - ‘./lyft_2021.pdf’ saved [1440303/1440303]\n", "\n", "--2024-04-03 21:42:53-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt\n", "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.111.133, 185.199.110.133, ...\n", "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 75042 (73K) [text/plain]\n", "Saving to: ‘./paul_graham_essay.txt’\n", "\n", "./paul_graham_essay 100%[===================>] 73.28K --.-KB/s in 0.01s \n", "\n", "2024-04-03 21:42:54 (5.76 MB/s) - ‘./paul_graham_essay.txt’ saved [75042/75042]\n", "\n" ] } ], "source": [ "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/uber_2021.pdf' -O './uber_2021.pdf'\n", "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/lyft_2021.pdf' -O './lyft_2021.pdf'\n", "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O './paul_graham_essay.txt'" ] }, { "cell_type": "markdown", "id": "8bde9293-51cf-4406-b628-a97891bc281b", "metadata": {}, "source": [ "### Load Data" ] }, { "cell_type": "code", "execution_count": 8, "id": "7f93584f-b19d-4595-a2e6-f6124be7ff2b", "metadata": {}, "outputs": [], "source": [ "# Uber docs\n", "uber_docs = SimpleDirectoryReader(input_files=[\"./uber_2021.pdf\"]).load_data()\n", "\n", "# Lyft docs\n", "lyft_docs = SimpleDirectoryReader(input_files=[\"./lyft_2021.pdf\"]).load_data()\n", "\n", "# Paul Graham Essay \n", "paul_graham_docs = SimpleDirectoryReader(input_files=[\"./paul_graham_essay.txt\"]).load_data()" ] }, { "cell_type": "markdown", "id": "459d163f-f15f-4363-a657-857e288093eb", "metadata": {}, "source": [ "### Index and Query Engine creation" ] }, { "cell_type": "code", "execution_count": 9, "id": "69dbbbdc-f0f3-408a-9a2c-dd65a6db9cb4", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n" ] } ], "source": [ "# Index on uber docs\n", "uber_vector_index = VectorStoreIndex.from_documents(uber_docs)\n", "\n", "# Index on lyft docs\n", "lyft_vector_index = VectorStoreIndex.from_documents(lyft_docs)\n", "\n", "# Index on Paul Graham docs\n", "paul_graham_vector_index = VectorStoreIndex.from_documents(paul_graham_docs)" ] }, { "cell_type": "code", "execution_count": 10, "id": "14bb6f0b-5ccd-4ed0-8227-6d488d4e5adf", "metadata": {}, "outputs": [], "source": [ "# Query Engine over Index with uber docs\n", "uber_vector_query_engine = uber_vector_index.as_query_engine(similarity_top_k = 5)\n", "\n", "# Query Engine over Index with lyft docs\n", "lyft_vector_query_engine = lyft_vector_index.as_query_engine(similarity_top_k = 5)\n", "\n", "# Query Engine over Index with Paul Graham Essay\n", "paul_graham_vector_query_engine = paul_graham_vector_index.as_query_engine(similarity_top_k = 5)" ] }, { "cell_type": "markdown", "id": "8df11259-e0ab-496d-8b0b-cb26adfb6ef4", "metadata": {}, "source": [ "### Create Tools" ] }, { "cell_type": "code", "execution_count": 11, "id": "8ffe1664-bc36-416a-a1b0-ded7d4468508", "metadata": {}, "outputs": [], "source": [ "query_engine_tools = [\n", " QueryEngineTool(\n", " query_engine=uber_vector_query_engine,\n", " metadata=ToolMetadata(\n", " name=\"uber_vector_query_engine\",\n", " description=(\n", " \"Provides information about Uber financials for year 2021.\"\n", " ),\n", " ),\n", " ),\n", " QueryEngineTool(\n", " query_engine=lyft_vector_query_engine,\n", " metadata=ToolMetadata(\n", " name=\"lyft_vector_query_engine\",\n", " description=(\n", " \"Provides information about Lyft financials for year 2021.\"\n", " ),\n", " ),\n", " ),\n", " QueryEngineTool(\n", " query_engine=paul_graham_vector_query_engine,\n", " metadata=ToolMetadata(\n", " name=\"paul_graham_vector_query_engine\",\n", " description=(\n", " \"Provides information about paul graham.\"\n", " ),\n", " ),\n", " ),\n", "]" ] }, { "cell_type": "markdown", "id": "65f8b9d0-b1c1-4bf4-8f78-1d67d984bc7c", "metadata": {}, "source": [ "### Create SubQuestion Query Engine" ] }, { "cell_type": "code", "execution_count": 12, "id": "1ae0a39e-c14b-4c90-a443-8ad80e5ccd71", "metadata": {}, "outputs": [], "source": [ "sub_question_query_engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=query_engine_tools)" ] }, { "cell_type": "markdown", "id": "5d382576-949d-43ce-8572-7d78e8215602", "metadata": {}, "source": [ "### Querying\n", "\n", "Here you can see the sub-queries created to answer complex user-query which has multiple questions." ] }, { "cell_type": "markdown", "id": "1109d5ec-48fd-4457-ab1a-64948fa8d913", "metadata": {}, "source": [ "#### Query related to Uber and Lyft docs.\n", "\n", "Creates two sub-queries related to Uber and Lyft." ] }, { "cell_type": "code", "execution_count": 13, "id": "186cc0d1-5c2f-422d-9324-749306a049aa", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "Generated 2 sub questions.\n", "\u001b[1;3;38;2;237;90;200m[uber_vector_query_engine] Q: What is the revenue of Uber for year 2021\n", "\u001b[0m\u001b[1;3;38;2;90;149;237m[lyft_vector_query_engine] Q: What is the revenue of Lyft for year 2021\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;90;149;237m[lyft_vector_query_engine] A: The revenue for Lyft in 2021 was $3,208,323 (in thousands).\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;237;90;200m[uber_vector_query_engine] A: The total revenue for Uber in the year 2021 was $17,455 million. This includes revenue from various offerings such as Mobility, Delivery, Freight, and All Other revenue streams. The Mobility revenue was $6,953 million, Delivery revenue was $8,362 million, Freight revenue was $2,132 million, and All Other revenue was $8 million.\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n" ] }, { "data": { "text/html": [ "<p style=\"font-size:20px\">In 2021, Uber's total revenue was significantly higher than Lyft's. Uber generated $17,455 million from various services like Mobility, Delivery, Freight, and other revenue streams. On the other hand, Lyft's revenue for the same year was $3,208,323 in thousands, which translates to $3,208.323 million. Therefore, Uber's revenue was approximately five times that of Lyft in 2021.</p>" ], "text/plain": [ "<IPython.core.display.HTML object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "response = sub_question_query_engine.query(\"Compare the revenue of uber and lyft?\")\n", "display(HTML(f'<p style=\"font-size:20px\">{response.response}</p>'))" ] }, { "cell_type": "markdown", "id": "b3806f9b-ada8-43d0-8276-4b0a2a1c8835", "metadata": {}, "source": [ "#### Query related to Uber and Paul Graham Essay\n", "\n", "Creates two sub-queries related to Uber and Paul Graham Essay." ] }, { "cell_type": "code", "execution_count": 14, "id": "dd6c321b-2c97-4701-82a1-53455307adc2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "Generated 2 sub questions.\n", "\u001b[1;3;38;2;237;90;200m[uber_vector_query_engine] Q: What is the revenue of Uber for the year 2021\n", "\u001b[0m\u001b[1;3;38;2;90;149;237m[paul_graham_vector_query_engine] Q: Why did Paul Graham start Y Combinator\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;237;90;200m[uber_vector_query_engine] A: The revenue for Uber in the year 2021 was $17,455 million.\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;90;149;237m[paul_graham_vector_query_engine] A: Paul Graham started Y Combinator as a result of a convergence of three thoughts: his desire to stop procrastinating about angel investing, his wish to collaborate with Robert and Trevor on projects, and his frustration with VCs who took too long to make decisions. He decided to start his own investment firm, where he would fund it, his wife Jessica could quit her job and work for it, and they could get Robert and Trevor as partners. The aim was to make seed investments and help startups in the same way they had been helped when they were starting out.\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n" ] }, { "data": { "text/html": [ "<p style=\"font-size:20px\">Uber's revenue for the year 2021 was reported to be $17,455 million. As for the reason behind Paul Graham starting Y Combinator, it was a result of three main factors. Firstly, he wanted to stop delaying his plans for angel investing. Secondly, he wished to collaborate with Robert and Trevor on projects. Lastly, he was frustrated with venture capitalists who took too long to make decisions. Therefore, he decided to start his own investment firm, where he would fund it, his wife Jessica could work for it, and they could bring Robert and Trevor on board as partners. The goal was to make seed investments and assist startups in the same way they had been helped when they were starting out.</p>" ], "text/plain": [ "<IPython.core.display.HTML object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "response = sub_question_query_engine.query(\"What is the revenue of uber and why did paul graham start YC?\")\n", "display(HTML(f'<p style=\"font-size:20px\">{response.response}</p>'))" ] }, { "cell_type": "markdown", "id": "03ed65c4-3977-424c-b438-26834f1db9c7", "metadata": {}, "source": [ "#### Query Related to Uber, Lyft and Paul Graham Essay.\n", "\n", "Creates sub-queries related to Uber, Lyft and Paul Graham Essay." ] }, { "cell_type": "code", "execution_count": 15, "id": "09239a33-93ec-4a59-9572-b95b21bcb329", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "HTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "Generated 3 sub questions.\n", "\u001b[1;3;38;2;237;90;200m[uber_vector_query_engine] Q: What is the revenue of Uber for year 2021\n", "\u001b[0m\u001b[1;3;38;2;90;149;237m[lyft_vector_query_engine] Q: What is the revenue of Lyft for year 2021\n", "\u001b[0m\u001b[1;3;38;2;11;159;203m[paul_graham_vector_query_engine] Q: Why did Paul Graham start Y Combinator\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/embeddings \"HTTP/1.1 200 OK\"\n", "HTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;90;149;237m[lyft_vector_query_engine] A: The revenue for Lyft in 2021 was $3,208,323 (in thousands).\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;237;90;200m[uber_vector_query_engine] A: The total revenue for Uber in the year 2021 was $17,455 million. This includes revenue from various offerings such as Mobility, Delivery, Freight, and All Other revenue streams. The Mobility revenue was $6,953 million, Delivery revenue was $8,362 million, Freight revenue was $2,132 million, and All Other revenue was $8 million.\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n", "\u001b[1;3;38;2;11;159;203m[paul_graham_vector_query_engine] A: Paul Graham started Y Combinator as a result of a convergence of three thoughts: his desire to stop procrastinating about angel investing, his wish to collaborate with Robert and Trevor on projects, and his frustration with VCs who took too long to make decisions. He decided to start his own investment firm, where he would fund it, his wife Jessica could quit her job and work for it, and they could get Robert and Trevor as partners. The aim was to make seed investments and help startups in the same way they had been helped when they were starting out.\n", "\u001b[0mHTTP Request: POST https://api.mistral.ai/v1/chat/completions \"HTTP/1.1 200 OK\"\n" ] }, { "data": { "text/html": [ "<p style=\"font-size:20px\">In 2021, Uber's total revenue was significantly higher than Lyft's. Uber generated a total of $17,455 million from various offerings like Mobility, Delivery, Freight, and other revenue streams. On the other hand, Lyft's revenue for the same year was $3,208,323 (in thousands).\n", "\n", "Paul Graham started Y Combinator due to a combination of factors. Firstly, he wanted to stop delaying his plans to become an angel investor. Secondly, he wished to collaborate with Robert and Trevor on projects. Lastly, he was frustrated with the slow decision-making process of venture capitalists. These factors led him to establish his own investment firm, where he could make seed investments and assist startups in a manner similar to how he had been helped when starting out.</p>" ], "text/plain": [ "<IPython.core.display.HTML object>" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "response = sub_question_query_engine.query(\"Compare revenue of uber with lyft and why did paul graham start YC?\")\n", "display(HTML(f'<p style=\"font-size:20px\">{response.response}</p>'))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" }, "vscode": { "interpreter": { "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e" } } }, "nbformat": 4, "nbformat_minor": 5 }