子问题查询引擎
在本教程中,我们将展示如何使用子问题查询引擎来解决通过多个数据源回答复杂查询的问题。
它首先将复杂查询分解为每个相关数据源的子问题,
然后收集所有中间响应并综合生成最终响应。
如果您在 Colab 上打开这个笔记本,您可能需要安装 LlamaIndex 🦙。
!pip install llama-indeximport os
os.environ["OPENAI_API_KEY"] = "sk-..."
import nest_asyncio
nest_asyncio.apply()from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.core.tools import QueryEngineTool, ToolMetadatafrom llama_index.core.query_engine import SubQuestionQueryEnginefrom llama_index.core.callbacks import CallbackManager, LlamaDebugHandlerfrom llama_index.core import Settings# Using the LlamaDebugHandler to print the trace of the sub questions# captured by the SUB_QUESTION callback event typellama_debug = LlamaDebugHandler(print_trace_on_end=True)callback_manager = CallbackManager([llama_debug])
Settings.callback_manager = callback_manager!mkdir -p 'data/paul_graham/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'Will not apply HSTS. The HSTS database must be a regular and non-world-writable file.ERROR: could not open HSTS store at '/home/loganm/.wget-hsts'. HSTS will be disabled.--2024-01-28 11:27:04-- https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txtResolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 75042 (73K) [text/plain]Saving to: ‘data/paul_graham/paul_graham_essay.txt’
data/paul_graham/pa 100%[===================>] 73.28K --.-KB/s in 0.04s
2024-01-28 11:27:05 (1.73 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]# load datapg_essay = SimpleDirectoryReader(input_dir="./data/paul_graham/").load_data()
# build index and query enginevector_query_engine = VectorStoreIndex.from_documents( pg_essay, use_async=True,).as_query_engine()**********Trace: index_construction |_CBEventType.NODE_PARSING -> 0.112481 seconds |_CBEventType.CHUNKING -> 0.105627 seconds |_CBEventType.EMBEDDING -> 0.959998 seconds**********# setup base query engine as toolquery_engine_tools = [ QueryEngineTool( query_engine=vector_query_engine, metadata=ToolMetadata( name="pg_essay", description="Paul Graham essay on What I Worked On", ), ),]
query_engine = SubQuestionQueryEngine.from_defaults( query_engine_tools=query_engine_tools, use_async=True,)response = query_engine.query( "How was Paul Grahams life different before, during, and after YC?")Generated 3 sub questions.[1;3;38;2;237;90;200m[pg_essay] Q: What did Paul Graham work on before YC?[0m[1;3;38;2;90;149;237m[pg_essay] Q: What did Paul Graham work on during YC?[0m[1;3;38;2;11;159;203m[pg_essay] Q: What did Paul Graham work on after YC?[0m[1;3;38;2;11;159;203m[pg_essay] A: After YC, Paul Graham worked on starting his own investment firm with Jessica.[0m[1;3;38;2;90;149;237m[pg_essay] A: During his time at YC, Paul Graham worked on various projects. He wrote all of YC's internal software in Arc and also worked on Hacker News (HN), which was a news aggregator initially meant for startup founders but later changed to engage intellectual curiosity. Additionally, he wrote essays and worked on helping the startups in the YC program with their problems.[0m[1;3;38;2;237;90;200m[pg_essay] A: Paul Graham worked on writing essays and working on YC before YC.[0m**********Trace: query |_CBEventType.QUERY -> 66.492657 seconds |_CBEventType.LLM -> 2.226621 seconds |_CBEventType.SUB_QUESTION -> 62.387177 seconds |_CBEventType.QUERY -> 62.386864 seconds |_CBEventType.RETRIEVE -> 0.271039 seconds |_CBEventType.EMBEDDING -> 0.269134 seconds |_CBEventType.SYNTHESIZE -> 62.115674 seconds |_CBEventType.TEMPLATING -> 2.8e-05 seconds |_CBEventType.LLM -> 62.108522 seconds |_CBEventType.SUB_QUESTION -> 2.421552 seconds |_CBEventType.QUERY -> 2.421303 seconds |_CBEventType.RETRIEVE -> 0.227773 seconds |_CBEventType.EMBEDDING -> 0.224198 seconds |_CBEventType.SYNTHESIZE -> 2.193355 seconds |_CBEventType.TEMPLATING -> 4.2e-05 seconds |_CBEventType.LLM -> 2.183101 seconds |_CBEventType.SUB_QUESTION -> 1.530997 seconds |_CBEventType.QUERY -> 1.530781 seconds |_CBEventType.RETRIEVE -> 0.25523 seconds |_CBEventType.EMBEDDING -> 0.252898 seconds |_CBEventType.SYNTHESIZE -> 1.275401 seconds |_CBEventType.TEMPLATING -> 3.2e-05 seconds |_CBEventType.LLM -> 1.26685 seconds |_CBEventType.SYNTHESIZE -> 1.877223 seconds |_CBEventType.TEMPLATING -> 1.6e-05 seconds |_CBEventType.LLM -> 1.875031 seconds**********print(response)Paul Graham's life was different before, during, and after YC. Before YC, he focused on writing essays and working on YC. During his time at YC, he worked on various projects, including writing software, developing Hacker News, and providing support to startups in the YC program. After YC, he started his own investment firm with Jessica. These different phases in his life involved different areas of focus and responsibilities.# iterate through sub_question items captured in SUB_QUESTION eventfrom llama_index.core.callbacks import CBEventType, EventPayload
for i, (start_event, end_event) in enumerate( llama_debug.get_event_pairs(CBEventType.SUB_QUESTION)): qa_pair = end_event.payload[EventPayload.SUB_QUESTION] print("Sub Question " + str(i) + ": " + qa_pair.sub_q.sub_question.strip()) print("Answer: " + qa_pair.answer.strip()) print("====================================")Sub Question 0: What did Paul Graham work on before YC?Answer: Paul Graham worked on writing essays and working on YC before YC.====================================Sub Question 1: What did Paul Graham work on during YC?Answer: During his time at YC, Paul Graham worked on various projects. He wrote all of YC's internal software in Arc and also worked on Hacker News (HN), which was a news aggregator initially meant for startup founders but later changed to engage intellectual curiosity. Additionally, he wrote essays and worked on helping the startups in the YC program with their problems.====================================Sub Question 2: What did Paul Graham work on after YC?Answer: After YC, Paul Graham worked on starting his own investment firm with Jessica.====================================