Files
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

424 lines
14 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "781a9b75",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/usecases/10q_sub_question.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "64971178-177d-48ff-9c11-201f3306a6b6",
"metadata": {},
"source": [
"# 10Q Analysis\n",
"In this demo, we explore answering complex queries by decomposing them into simpler sub-queries."
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "036aacde",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aef714be",
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-llms-openai"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24e05dc7",
"metadata": {},
"outputs": [],
"source": [
"!pip install llama-index"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3edf4c98-de44-4d0b-ac7a-7273255a21cf",
"metadata": {},
"outputs": [],
"source": [
"import nest_asyncio\n",
"\n",
"nest_asyncio.apply()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae95482c-516d-41ce-8394-db949ed58716",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import SimpleDirectoryReader, VectorStoreIndex\n",
"from llama_index.core.response.pprint_utils import pprint_response\n",
"from llama_index.llms.openai import OpenAI\n",
"\n",
"from llama_index.core.tools import QueryEngineTool, ToolMetadata\n",
"from llama_index.core.query_engine import SubQuestionQueryEngine"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "f2ffdb6c-b7df-4649-84d5-2b4ce32ad8a9",
"metadata": {},
"source": [
"## Configure LLM service"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ca737492",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"OPENAI_API_KEY\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96edd29c-15b6-44f9-bae0-8a1f97e07d17",
"metadata": {},
"outputs": [],
"source": [
"from llama_index.core import Settings\n",
"\n",
"Settings.llm = OpenAI(temperature=0.2, model=\"gpt-3.5-turbo\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "6a56b61d",
"metadata": {},
"source": [
"## Download Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "00466ae7",
"metadata": {},
"outputs": [],
"source": [
"!mkdir -p 'data/10q/'\n",
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_march_2022.pdf' -O 'data/10q/uber_10q_march_2022.pdf'\n",
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_june_2022.pdf' -O 'data/10q/uber_10q_june_2022.pdf'\n",
"!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10q/uber_10q_sept_2022.pdf' -O 'data/10q/uber_10q_sept_2022.pdf'"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e034ed70-9d74-4ded-9797-13f2cc5c5ac8",
"metadata": {},
"source": [
"## Load data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "133fc28d-65a1-4a1f-8ed0-d6fb3d4e160c",
"metadata": {},
"outputs": [],
"source": [
"march_2022 = SimpleDirectoryReader(\n",
" input_files=[\"./data/10q/uber_10q_march_2022.pdf\"]\n",
").load_data()\n",
"june_2022 = SimpleDirectoryReader(\n",
" input_files=[\"./data/10q/uber_10q_june_2022.pdf\"]\n",
").load_data()\n",
"sept_2022 = SimpleDirectoryReader(\n",
" input_files=[\"./data/10q/uber_10q_sept_2022.pdf\"]\n",
").load_data()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "210ec44c-f559-48cb-9217-1efaca5e5362",
"metadata": {},
"source": [
"# Build indices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3748ac62-42ab-4f4c-b40b-40313a44b80d",
"metadata": {},
"outputs": [],
"source": [
"march_index = VectorStoreIndex.from_documents(march_2022)\n",
"june_index = VectorStoreIndex.from_documents(june_2022)\n",
"sept_index = VectorStoreIndex.from_documents(sept_2022)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "04fd6587-db6d-4782-83ad-ffc2fcd0b3a1",
"metadata": {},
"source": [
"## Build query engines"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a2e4e609-fa2c-432f-ab93-c1607be350a1",
"metadata": {},
"outputs": [],
"source": [
"march_engine = march_index.as_query_engine(similarity_top_k=3)\n",
"june_engine = june_index.as_query_engine(similarity_top_k=3)\n",
"sept_engine = sept_index.as_query_engine(similarity_top_k=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fccbd2c-e43f-4ec6-98d3-bdc3d97bc6cc",
"metadata": {},
"outputs": [],
"source": [
"query_engine_tools = [\n",
" QueryEngineTool(\n",
" query_engine=sept_engine,\n",
" metadata=ToolMetadata(\n",
" name=\"sept_22\",\n",
" description=(\n",
" \"Provides information about Uber quarterly financials ending\"\n",
" \" September 2022\"\n",
" ),\n",
" ),\n",
" ),\n",
" QueryEngineTool(\n",
" query_engine=june_engine,\n",
" metadata=ToolMetadata(\n",
" name=\"june_22\",\n",
" description=(\n",
" \"Provides information about Uber quarterly financials ending\"\n",
" \" June 2022\"\n",
" ),\n",
" ),\n",
" ),\n",
" QueryEngineTool(\n",
" query_engine=march_engine,\n",
" metadata=ToolMetadata(\n",
" name=\"march_22\",\n",
" description=(\n",
" \"Provides information about Uber quarterly financials ending\"\n",
" \" March 2022\"\n",
" ),\n",
" ),\n",
" ),\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b63f755-dda4-49c8-8a00-917d3687e2c1",
"metadata": {},
"outputs": [],
"source": [
"s_engine = SubQuestionQueryEngine.from_defaults(\n",
" query_engine_tools=query_engine_tools\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "0888e0b1-bcc8-4ea0-8162-f4a3eea97475",
"metadata": {},
"source": [
"## Run queries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0e45eb4-39f5-459f-8d57-89cf4d80bc00",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generated 2 sub questions.\n",
"\u001b[36;1m\u001b[1;3m[sept_22] Q: What is the revenue growth of Uber for the quarter ending September 2022\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m[sept_22] A: compared to the same period in 2021?\n",
"\n",
"The revenue growth of Uber for the quarter ending September 2022 compared to the same period in 2021 is 72%.\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] Q: What is the revenue growth of Uber for the quarter ending June 2022\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] A: compared to the same period in 2021?\n",
"\n",
"The revenue growth of Uber for the quarter ending June 2022 compared to the same period in 2021 is 105%.\n",
"\u001b[0m"
]
}
],
"source": [
"response = s_engine.query(\n",
" \"Analyze Uber revenue growth over the latest two quarter filings\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55c67ff8-7a7f-4032-bef8-373f0c272fd2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Uber's revenue growth over the latest two quarter filings has been strong, with a 72% increase for the quarter ending September 2022 compared to the same period in 2021, and a 105% increase for the quarter ending June 2022 compared to the same period in 2021.\n"
]
}
],
"source": [
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0ff55b9b-2b1f-4921-bd5d-5ab04b957b30",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generated 3 sub questions.\n",
"\u001b[36;1m\u001b[1;3m[sept_22] Q: What is the macro environment in September 2022\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m[sept_22] A: \n",
"The macro environment in September 2022 is one of recovery from the impacts of the COVID-19 pandemic, with increases in Mobility Trip volumes, a $1.3 billion increase in Freight Gross Bookings resulting from the acquisition of Transplace, a $1.1 billion increase in Mobility revenue due to business model changes in the UK, and a $164 million increase in Delivery revenue due to an increase in certain Courier payments and incentives. Additionally, there was a $2.2 billion net increase in Mobility revenue due to business model changes in the UK and an accrual made for the resolution of historical claims in the UK relating to the classification of drivers, and a $751 million increase in Delivery revenue due to an increase in certain Courier payments and incentives.\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] Q: What is the macro environment in June 2022\n",
"\u001b[0m\u001b[33;1m\u001b[1;3m[june_22] A: \n",
"In June 2022, the macro environment is characterized by the continued impact of COVID-19 restrictions on global demand, the adoption of new accounting standards, and the potential for shifts in consumer travel patterns due to health concerns.\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3m[march_22] Q: What is the macro environment in March 2022\n",
"\u001b[0m\u001b[38;5;200m\u001b[1;3m[march_22] A: \n",
"The macro environment in March 2022 is uncertain, as the effects of the COVID-19 pandemic and the actions taken to mitigate it are still being felt. Travel restrictions, business restrictions, school closures, and limitations on social or public gatherings may still be in place in some regions, and the demand for services may still be affected.\n",
"\u001b[0m"
]
}
],
"source": [
"response = s_engine.query(\n",
" \"Analyze change in macro environment over the 3 quarters\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "feccbe75-0005-40de-93aa-9ed7c192dd50",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"The macro environment has seen a significant change over the three quarters from March 2022 to September 2022. In March 2022, the macro environment was uncertain due to the effects of the COVID-19 pandemic and the actions taken to mitigate it. By June 2022, the macro environment was characterized by the continued impact of COVID-19 restrictions on global demand, the adoption of new accounting standards, and the potential for shifts in consumer travel patterns due to health concerns. By September 2022, the macro environment had shifted to one of recovery from the impacts of the COVID-19 pandemic, with increases in Mobility Trip volumes, a $1.3 billion increase in Freight Gross Bookings resulting from the acquisition of Transplace, a $1.1 billion increase in Mobility revenue due to business model changes in the UK, and a $164 million increase in Delivery revenue due to an increase in certain Courier payments and incentives. Additionally, there was a $2.2 billion net increase in Mobility revenue due to business model changes in the UK and an accrual made for the resolution of historical claims in the UK relating to the classification of drivers, and a $751 million increase in Delivery revenue due to an increase in certain Courier payments and incentives.\n"
]
}
],
"source": [
"print(response)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a20ac08f-5859-4433-9ed8-5c315a2e050b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generated 1 sub questions.\n",
"\u001b[36;1m\u001b[1;3m[sept_22] Q: How much cash did Uber have in September 2022\n",
"\u001b[0m\u001b[36;1m\u001b[1;3m[sept_22] A: \n",
"Uber had $4,865 million in cash in September 2022.\n",
"\u001b[0m"
]
}
],
"source": [
"response = s_engine.query(\"How much cash did Uber have in sept 2022\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf67b5f4-a470-4deb-b2d8-fe752e86e170",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Uber had $4,865 million in cash in September 2022.\n"
]
}
],
"source": [
"print(response)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py38",
"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"
}
},
"nbformat": 4,
"nbformat_minor": 5
}