mirror of
https://github.com/run-llama/llama_docs_bot.git
synced 2026-06-30 21:27:56 -04:00
update custom node postprocessor
This commit is contained in:
+88
-483
@@ -19,7 +19,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -34,9 +34,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/Users/loganmarkewich/llama_docs_bot/venv/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
||||
" from .autonotebook import tqdm as notebook_tqdm\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from llama_index import ServiceContext, set_global_service_context\n",
|
||||
"from llama_index.llms import OpenAI\n",
|
||||
@@ -72,7 +81,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -138,7 +147,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -205,7 +214,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 46,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -218,27 +227,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
|
||||
"To disable this warning, you can either:\n",
|
||||
"\t- Avoid using `tokenizers` before the fork if possible\n",
|
||||
"\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!rm -rf data_*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 48,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -251,7 +249,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -262,7 +260,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 50,
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -272,14 +270,14 @@
|
||||
"\n",
|
||||
"--- Hierarchical ---\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"You can directly build and configure a query engine from an index in 1 line of code:\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
@@ -289,12 +287,12 @@
|
||||
" verbose=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"> Note: While the high-level API optimizes for ease-of-use, it does *NOT* expose full range of configurability.See **Response Modes** for a full list of response modes and what they do.File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"> Note: While the high-level API optimizes for ease-of-use, it does *NOT* expose full range of configurability.See **Response Modes** for a full list of response modes and what they do.File Name: ./docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
@@ -306,161 +304,14 @@
|
||||
"response_modes.md\n",
|
||||
"streaming.md\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/Low-Level Composition API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"You can use the low-level composition API if you need more granular control.Concretely speaking, you would explicitly construct a `QueryEngine` object instead of calling `index.as_query_engine(...)`.> Note: You may need to look at API references or example notebooks.File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/Low-Level Composition API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"from llama_index import (\n",
|
||||
" VectorStoreIndex,\n",
|
||||
" get_response_synthesizer,\n",
|
||||
")\n",
|
||||
"from llama_index.retrievers import VectorIndexRetriever\n",
|
||||
"from llama_index.query_engine import RetrieverQueryEngine\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/build index\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"index = VectorStoreIndex.from_documents(documents)\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/configure retriever\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"retriever = VectorIndexRetriever(\n",
|
||||
" index=index, \n",
|
||||
" similarity_top_k=2,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/configure response synthesizer\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"response_synthesizer = get_response_synthesizer(\n",
|
||||
" response_mode=\"tree_summarize\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/assemble query engine\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_engine = RetrieverQueryEngine(\n",
|
||||
" retriever=retriever,\n",
|
||||
" response_synthesizer=response_synthesizer,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/query\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"response = query_engine.query(\"What did the author do growing up?\")print(response)\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/Streaming\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"To enable streaming, you simply need to pass in a `streaming=True` flag\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/Streaming\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_engine = index.as_query_engine(\n",
|
||||
" streaming=True,\n",
|
||||
")\n",
|
||||
"streaming_response = query_engine.query(\n",
|
||||
" \"What did the author do growing up?\", \n",
|
||||
")\n",
|
||||
"streaming_response.print_response_stream()\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Query Engine/Streaming\n",
|
||||
"\n",
|
||||
"* Read the full streaming guide\n",
|
||||
"* See an end-to-end example\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/response_synthesizers/modules.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Module Guide\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Detailed inputs/outputs for each response synthesizer are found below.File Name: ../docs/core_modules/query_modules/response_synthesizers/modules.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Module Guide/API Example\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"The following shows the setup for utilizing all kwargs.- `response_mode` specifies which response synthesizer to use\n",
|
||||
"- `service_context` defines the LLM and related settings for synthesis\n",
|
||||
"- `text_qa_template` and `refine_template` are the prompts used at various stages\n",
|
||||
"- `use_async` is used for only the `tree_summarize` response mode right now, to asynchronously build the summary tree\n",
|
||||
"- `streaming` configures whether to return a streaming response object or not\n",
|
||||
"\n",
|
||||
"In the `synthesize`/`asyntheszie` functions, you can optionally provide additional source nodes, which will be added to the `response.source_nodes` list.\n",
|
||||
"You can use the low-level composition API if you need more granular control.Concretely speaking, you would explicitly construct a `QueryEngine` object instead of calling `index.as_query_engine(.)`.> Note: You may need to look at API references or example notebooks.File Name: ./docs/core_modules/query_modules/query_engine/usage_pattern.\n",
|
||||
"---\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/gpt-4\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"caption: Examples\n",
|
||||
"maxdepth: 1\n",
|
||||
"---\n",
|
||||
"/examples/query_transformations/HyDEQueryTransformDemo.ipynb\n",
|
||||
"/examples/query_transformations/SimpleIndexDemo-multistep.ipynb\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/modules.md\n",
|
||||
"Content Type: code\n",
|
||||
"Header Path: Module Guides/Basic\n",
|
||||
"\n",
|
||||
"```{toctree}\n",
|
||||
"---\n",
|
||||
"maxdepth: 1\n",
|
||||
"---\n",
|
||||
"Retriever Query Engine </examples/query_engine/CustomRetrievers.ipynb>\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/modules.md\n",
|
||||
"Content Type: code\n",
|
||||
"Header Path: Module Guides/Structured & Semi-Structured Data\n",
|
||||
"\n",
|
||||
"```{toctree}\n",
|
||||
"---\n",
|
||||
"maxdepth: 1\n",
|
||||
"---\n",
|
||||
"/examples/query_engine/json_query_engine.ipynb\n",
|
||||
"/examples/query_engine/pandas_query_engine.ipynb\n",
|
||||
"/examples/query_engine/knowledge_graph_query_engine.ipynb\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/modules.md\n",
|
||||
"Content Type: code\n",
|
||||
"Header Path: Module Guides/Advanced\n",
|
||||
"\n",
|
||||
"```{toctree}\n",
|
||||
"---\n",
|
||||
"maxdepth: 1\n",
|
||||
"---\n",
|
||||
"/examples/query_engine/RouterQueryEngine.ipynb\n",
|
||||
"/examples/query_engine/RetrieverRouterQueryEngine.ipynb\n",
|
||||
"/examples/query_engine/JointQASummary.ipynb\n",
|
||||
"/examples/query_engine/sub_question_query_engine.ipynb\n",
|
||||
"/examples/query_transformations/SimpleIndexDemo-multistep.ipynb\n",
|
||||
"/examples/query_engine/SQLRouterQueryEngine.ipynb\n",
|
||||
"ipynb\n",
|
||||
"/examples/query_engine/SQLAutoVectorQueryEngine.ipynb\n",
|
||||
"/examples/query_engine/SQLJoinQueryEngine.ipynb\n",
|
||||
"/examples/index_structs/struct_indices/duckdb_sql_query.ipynb\n",
|
||||
@@ -471,7 +322,7 @@
|
||||
"/examples/query_engine/pdf_tables/recursive_retriever.ipynb\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/modules.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/modules.md\n",
|
||||
"Content Type: code\n",
|
||||
"Header Path: Module Guides/Experimental\n",
|
||||
"\n",
|
||||
@@ -482,7 +333,7 @@
|
||||
"/examples/query_engine/flare_query_engine.ipynb\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/response_modes.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/response_modes.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Response Modes\n",
|
||||
"\n",
|
||||
@@ -492,91 +343,8 @@
|
||||
" many `Node` text chunks that can fit within the maximum prompt size.If there are \n",
|
||||
" too many chunks to stuff in one prompt, \"create and refine\" an answer by going through\n",
|
||||
" multiple prompts.- `tree_summarize`: Given a set of `Node` objects and the query, recursively construct a tree \n",
|
||||
" and return the root node as the response.Good for summarization purposes.- `no_text`: Only runs the retriever to fetch the nodes that would have been sent to the LLM, \n",
|
||||
" without actually sending them.Then can be inspected by checking `response.source_nodes`.The response object is covered in more detail in Section 5.- `accumulate`: Given a set of `Node` objects and the query, apply the query to each `Node` text\n",
|
||||
" chunk while accumulating the responses into an array.Returns a concatenated string of all\n",
|
||||
" responses.Good for when you need to run the same query separately against each text\n",
|
||||
" chunk.See Response Synthesizer to learn more.File Name: ../docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Concept\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Query engine is a generic interface that allows you to ask question over your data.A query engine takes in a natural language query, and returns a rich response.It is most often (but not always) built on one or many Indices via Retrievers.You can compose multiple query engines to achieve more advanced capability.File Name: ../docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Concept\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"If you want to have a conversation with your data (multiple back-and-forth instead of a single question & answer), take a look at Chat Engine\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Usage Pattern\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Get started with:\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Usage Pattern\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_engine = index.as_query_engine()\n",
|
||||
"response = query_engine.query(\"Who is Paul Graham.\")File Name: ../docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Usage Pattern\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"To stream response:\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Usage Pattern\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_engine = index.as_query_engine(streaming=True)\n",
|
||||
"streaming_response = query_engine.query(\"Who is Paul Graham.\")\n",
|
||||
" and return the root node as the response.Good for summarization purposes.\n",
|
||||
"---\n",
|
||||
"Here's a corresponding example code snippet over a composed graph.\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/llm_predictor_chatgpt corresponds to the ChatGPT LLM interface\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"from llama_index.indices.query.query_transform.base import DecomposeQueryTransform\n",
|
||||
"decompose_transform = DecomposeQueryTransform(\n",
|
||||
" llm_predictor_chatgpt, verbose=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/initialize indexes and graph\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"...\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/configure retrievers\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"vector_query_engine = vector_index.as_query_engine()\n",
|
||||
"vector_query_engine = TransformQueryEngine(\n",
|
||||
" vector_query_engine, \n",
|
||||
" query_transform=decompose_transform\n",
|
||||
" transform_extra_info={'index_summary': vector_index.index_struct.summary}\n",
|
||||
")\n",
|
||||
"custom_query_engines = {\n",
|
||||
" vector_index.index_id: vector_query_engine\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/query\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_str = (\n",
|
||||
" \"Compare and contrast the airports in Seattle, Houston, and Toronto. \"\n",
|
||||
")\n",
|
||||
"query_engine = graph.as_query_engine(custom_query_engines=custom_query_engines)\n",
|
||||
"response = query_engine.query(query_str)\n",
|
||||
@@ -586,53 +354,68 @@
|
||||
"Header Path: Query Transformations/query\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Check out our example notebook for a full walkthrough.\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Check out our example notebook for a full walkthrough.File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/Multi-Step Query Transformations\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Multi-step query transformations are a generalization on top of existing single-step query transformation approaches.\n",
|
||||
"\n",
|
||||
"Given an initial, complex query, the query is transformed and executed against an index. The response is retrieved from the query. \n",
|
||||
"Given the response (along with prior responses) and the query, followup questions may be asked against the index as well. This technique allows a query to be run against a single knowledge source until that query has satisfied all questions.\n",
|
||||
"\n",
|
||||
"An example image is shown below.\n",
|
||||
"\n",
|
||||
"!\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Here's a corresponding example code snippet.File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Multi-step query transformations are a generalization on top of existing single-step query transformation approaches.Given an initial, complex query, the query is transformed and executed against an index.The response is retrieved from the query.Given the response (along with prior responses) and the query, followup questions may be asked against the index as well.This technique allows a query to be run against a single knowledge source until that query has satisfied all questions.An example image is shown below.!\n",
|
||||
"---\n",
|
||||
"/docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/Multi-Step Query Transformations\n",
|
||||
"Header Path: Query Transformations/run query with HyDE query transform\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"from llama_index.indices.query.query_transform.base import StepDecomposeQueryTransform\n",
|
||||
"query_str = \"what did paul graham do after going to RISD\"\n",
|
||||
"hyde = HyDEQueryTransform(include_original=True)\n",
|
||||
"query_engine = index.as_query_engine()\n",
|
||||
"query_engine = TransformQueryEngine(query_engine, query_transform=hyde)\n",
|
||||
"response = query_engine.query(query_str)\n",
|
||||
"print(response)\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/gpt-4\n",
|
||||
"Header Path: Query Transformations/run query with HyDE query transform\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"step_decompose_transform = StepDecomposeQueryTransform(\n",
|
||||
" llm_predictor, verbose=True\n",
|
||||
")\n",
|
||||
"Check out our example notebook for a full walkthrough.File Name: ./docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/Single-Step Query Decomposition\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Some recent approaches (e.g.self-ask, ReAct) have suggested that LLM's \n",
|
||||
"perform better at answering complex questions when they break the question into smaller steps.We have found that this is true for queries that require knowledge augmentation as well.If your query is complex, different parts of your knowledge base may answer different \"subqueries\" around the overall query.Our single-step query decomposition feature transforms a **complicated** question into a simpler one over the data collection to help provide a sub-answer to the original question.This is especially helpful over a composed graph.Within a composed graph, a query can be routed to multiple subindexes, each representing a subset of the overall knowledge corpus.Query decomposition allows us to transform the query into a more suitable question over any given index.An example image is shown below.!\n",
|
||||
"---\n",
|
||||
"- `no_text`: Only runs the retriever to fetch the nodes that would have been sent to the LLM, \n",
|
||||
" without actually sending them.Then can be inspected by checking `response.source_nodes`.The response object is covered in more detail in Section 5.- `accumulate`: Given a set of `Node` objects and the query, apply the query to each `Node` text\n",
|
||||
" chunk while accumulating the responses into an array.Returns a concatenated string of all\n",
|
||||
" responses.Good for when you need to run the same query separately against each text\n",
|
||||
" chunk.See Response Synthesizer to learn more.File Name: ./docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Concept\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Query engine is a generic interface that allows you to ask question over your data.A query engine takes in a natural language query, and returns a rich response.It is most often (but not always) built on one or many Indices via Retrievers.You can compose multiple query engines to achieve more advanced capability.File Name: ./docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Concept\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"If you want to have a conversation with your data (multiple back-and-forth instead of a single question & answer), take a look at Chat Engine\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Usage Pattern\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Get started with:\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/root.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Engine/Usage Pattern\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_engine = index.as_query_engine()\n",
|
||||
"query_engine = MultiStepQueryEngine(query_engine, query_transform=step_decompose_transform)\n",
|
||||
"\n",
|
||||
"response = query_engine.query(\n",
|
||||
" \"Who was in the first batch of the accelerator program the author started?\",\n",
|
||||
")\n",
|
||||
"print(str(response))\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/gpt-4\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Check out our example notebook for a full walkthrough.\n",
|
||||
"response = query_engine.query(\"Who is Paul Graham.\n",
|
||||
"---\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
@@ -661,179 +444,7 @@
|
||||
"```python\n",
|
||||
"chat_engine = index.as_chat_engine()\n",
|
||||
"streaming_response = chat_engine.stream_chat(\"Tell me a joke.\n",
|
||||
"---\n",
|
||||
"@abstractmethod\n",
|
||||
" def postprocess_nodes(\n",
|
||||
" self, nodes: List[NodeWithScore], query_bundle: Optional[QueryBundle]\n",
|
||||
" ) -> List[NodeWithScore]:\n",
|
||||
" \"\"\"Postprocess nodes.\"\"\"File Name: ../docs/core_modules/query_modules/node_postprocessors/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Custom Node PostProcessor\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"A dummy node-postprocessor can be implemented in just a few lines of code:\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/node_postprocessors/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Custom Node PostProcessor\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"from llama_index import QueryBundle\n",
|
||||
"from llama_index.indices.postprocessor.base import BaseNodePostprocessor\n",
|
||||
"from llama_index.schema import NodeWithScore\n",
|
||||
"\n",
|
||||
"class DummyNodePostprocessor:\n",
|
||||
"\n",
|
||||
" def postprocess_nodes(\n",
|
||||
" self, nodes: List[NodeWithScore], query_bundle: Optional[QueryBundle]\n",
|
||||
" ) -> List[NodeWithScore]:\n",
|
||||
" \n",
|
||||
" # subtracts 1 from the score\n",
|
||||
" for n in nodes:\n",
|
||||
" n.score -= 1\n",
|
||||
"\n",
|
||||
" return nodes\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"LlamaIndex allows you to perform *query transformations* over your index structures.Query transformations are modules that will convert a query into another query.They can be **single-step**, as in the transformation is run once before the query is executed against an index.They can also be **multi-step**, as in: \n",
|
||||
"1.The query is transformed, executed against an index, \n",
|
||||
"2.The response is retrieved.3.Subsequent queries are transformed/executed in a sequential fashion.We list some of our query transformations in more detail below.File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/Use Cases\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Query transformations have multiple use cases:\n",
|
||||
"- Transforming an initial query into a form that can be more easily embedded (e.g.HyDE)\n",
|
||||
"- Transforming an initial query into a subquestion that can be more easily answered from the data (single-step query decomposition)\n",
|
||||
"- Breaking an initial query into multiple subquestions that can be more easily answered on their own.(multi-step query decomposition)\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/HyDE (Hypothetical Document Embeddings)\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"HyDE is a technique where given a natural language query, a hypothetical document/answer is generated first.This hypothetical document is then used for embedding lookup rather than the raw query.To use HyDE, an example code snippet is shown below.File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/HyDE (Hypothetical Document Embeddings)\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"from llama_index import VectorStoreIndex, SimpleDirectoryReader\n",
|
||||
"from llama_index.indices.query.query_transform.base import HyDEQueryTransform\n",
|
||||
"from llama_index.query_engine.transform_query_engine import TransformQueryEngine\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/load documents, build index\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"documents = SimpleDirectoryReader('../paul_graham_essay/data').load_data()\n",
|
||||
"index = VectorStoreIndex(documents)\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/run query with HyDE query transform\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"query_str = \"what did paul graham do after going to RISD\"\n",
|
||||
"hyde = HyDEQueryTransform(include_original=True)\n",
|
||||
"query_engine = index.as_query_engine()\n",
|
||||
"query_engine = TransformQueryEngine(query_engine, query_transform=hyde)\n",
|
||||
"response = query_engine.query(query_str)\n",
|
||||
"print(response)\n",
|
||||
"\n",
|
||||
"File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/run query with HyDE query transform\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Check out our example notebook for a full walkthrough.File Name: ../docs/core_modules/query_modules/query_engine/advanced/query_transformations.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Query Transformations/Single-Step Query Decomposition\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"Some recent approaches (e.g.self-ask, ReAct) have suggested that LLM's \n",
|
||||
"perform better at answering complex questions when they break the question into smaller steps.We have found that this is true for queries that require knowledge augmentation as well.If your query is complex, different parts of your knowledge base may answer different \"subqueries\" around the overall query.Our single-step query decomposition feature transforms a **complicated** question into a simpler one over the data collection to help provide a sub-answer to the original question.This is especially helpful over a composed graph.Within a composed graph, a query can be routed to multiple subindexes, each representing a subset of the overall knowledge corpus.Query decomposition allows us to transform the query into a more suitable question over any given index.An example image is shown below.!\n",
|
||||
"---\n",
|
||||
"File Name: ./docs/core_modules/query_modules/chat_engines/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Chat Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"You can directly build and configure a chat engine from an index in 1 line of code:\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/chat_engines/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Chat Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"chat_engine = index.as_chat_engine(\n",
|
||||
" chat_mode='condense_question', \n",
|
||||
" verbose=True\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/chat_engines/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Chat Engine/High-Level API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"> Note: you can access different chat engines by specifying the `chat_mode` as a kwarg.`condense_question` corresponds to `CondenseQuestionChatEngine`, `react` corresponds to `ReActChatEngine`.> Note: While the high-level API optimizes for ease-of-use, it does *NOT* expose full range of configurability.File Name: ./docs/core_modules/query_modules/chat_engines/usage_pattern.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Usage Pattern/Configuring a Chat Engine/Low-Level Composition API\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"You can use the low-level composition API if you need more granular control.Concretely speaking, you would explicitly construct `ChatEngine` object instead of calling `index.as_chat_engine(.)`.> Note: You may need to look at API references or example notebooks.Here's an example where we configure the following:\n",
|
||||
"* configure the condense question prompt, \n",
|
||||
"* initialize the conversation with some existing history,\n",
|
||||
"* print verbose debug message.File Name: ./docs/core_modules/query_modules/chat_engines/usage_pattern.\n",
|
||||
"---\n",
|
||||
"similarity_top_k=1\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/streaming.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Streaming/Setup\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"If you are using the low-level API to compose the query engine,\n",
|
||||
"pass `streaming=True` when constructing the `Response Synthesizer`:\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/streaming.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Streaming/Setup\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"from llama_index import get_response_synthesizer\n",
|
||||
"synth = get_response_synthesizer(streaming=True, .)\n",
|
||||
"query_engine = RetrieverQueryEngine(response_synthesizer=synth, .)\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/streaming.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Streaming/Streaming Response\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"After properly configuring both the LLM and the query engine,\n",
|
||||
"calling `query` now returns a `StreamingResponse` object.File Name: ./docs/core_modules/query_modules/query_engine/streaming.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Streaming/Streaming Response\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"streaming_response = query_engine.query(\n",
|
||||
" \"What did the author do growing up?\", \n",
|
||||
")\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/streaming.md\n",
|
||||
"Content Type: text\n",
|
||||
"Header Path: Streaming/Streaming Response\n",
|
||||
"Links:\n",
|
||||
"\n",
|
||||
"The response is returned immediately when the LLM call *starts*, without having to wait for the full completion.> Note: In the case where the query engine makes multiple LLM calls, only the last LLM call will be streamed and the response is returned when the last LLM call starts.You can obtain a `Generator` from the streaming response and iterate over the tokens as they arrive:\n",
|
||||
"\n",
|
||||
"File Name: ./docs/core_modules/query_modules/query_engine/streaming.\n",
|
||||
"Total length: 6612\n"
|
||||
"Total length: 2365\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -852,7 +463,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -979,26 +590,20 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing import Callable, Optional\n",
|
||||
"\n",
|
||||
"from llama_index.bridge.pydantic import Field, PrivateAttr\n",
|
||||
"from llama_index.indices.postprocessor.types import BaseNodePostprocessor\n",
|
||||
"from llama_index.utils import globals_helper\n",
|
||||
"from llama_index.schema import MetadataMode\n",
|
||||
"\n",
|
||||
"class LimitRetrievedNodesLength(BaseNodePostprocessor):\n",
|
||||
" limit: int = 3000\n",
|
||||
" _tokenizer: Callable = PrivateAttr()\n",
|
||||
"class LimitRetrievedNodesLength:\n",
|
||||
"\n",
|
||||
" def __init__(self, limit: int = 3000, tokenizer: Optional[Callable] = None):\n",
|
||||
" self._tokenizer = tokenizer or globals_helper.tokenizer\n",
|
||||
" super().__init__(\n",
|
||||
" limit=limit,\n",
|
||||
" )\n",
|
||||
" self.limit = limit\n",
|
||||
"\n",
|
||||
" def postprocess_nodes(self, nodes, query_bundle):\n",
|
||||
" included_nodes = []\n",
|
||||
@@ -1015,7 +620,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 55,
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -1035,7 +640,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -1049,14 +654,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Total length: 2365\n"
|
||||
"Total length: 2971\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
{"graph_dict": {}}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -1,19 +1,13 @@
|
||||
from typing import Callable, Optional
|
||||
|
||||
from llama_index.bridge.pydantic import PrivateAttr
|
||||
from llama_index.indices.postprocessor.types import BaseNodePostprocessor
|
||||
from llama_index.utils import globals_helper
|
||||
from llama_index.schema import MetadataMode
|
||||
|
||||
class LimitRetrievedNodesLength(BaseNodePostprocessor):
|
||||
limit: int = 3000
|
||||
_tokenizer: Callable = PrivateAttr()
|
||||
class LimitRetrievedNodesLength:
|
||||
|
||||
def __init__(self, limit: int = 3000, tokenizer: Optional[Callable] = None):
|
||||
self._tokenizer = tokenizer or globals_helper.tokenizer
|
||||
super().__init__(
|
||||
limit=limit,
|
||||
)
|
||||
self.limit = limit
|
||||
|
||||
def postprocess_nodes(self, nodes, query_bundle):
|
||||
included_nodes = []
|
||||
|
||||
Reference in New Issue
Block a user