DocumentSummaryIndex
automates (generates a summary for a document, uses the summary to select which document should be used to answer a query, sends the whole selected document(s) to the LLM)from llama_index.core.prompts import PromptTemplate # customize as needed DEFAULT_TREE_SUMMARIZE_TMPL = ( "Context information from multiple sources is below.\n" "---------------------\n" "{context_str}\n" "---------------------\n" "Given the information from multiple sources and not prior knowledge, " "answer the query.\n" "Query: {query_str}\n" "Answer: " ) DEFAULT_TREE_SUMMARIZE_PROMPT = PromptTemplate( DEFAULT_TREE_SUMMARIZE_TMPL, ) from llama_index.core.response_synthesizer import TreeSummarize summarizer = TreeSummarize(llm=llm, use_async=True, summary_template=DEFAULT_TREE_SUMMARIZE_PROMPT) # would do this for every document/text chunk response = summarizer.get_response("query_str", ["text1"]) from llama_index.core.schema import TextNode # here I only have one node node = TextNode(text=resposne) index = VectorStoreIndex(nodes=[node])
core.response.Response
in LlamaIndex. However, you can refer to the LlamaIndex documentation () for more detailed information.DocumentSummaryIndex
in LlamaIndex is a specialized index that extracts a summary from each document and stores that summary, as well as all nodes corresponding to the document. It is particularly useful for summarizing large amounts of text from various documents.DocumentSummaryIndex
is built using the from_documents
method. This method takes in a list of documents, an instance of a language model (llm
), a list of transformations (like a SentenceSplitter
), a response_synthesizer
, and a boolean flag to show progress. The response_synthesizer
is configured with the response_mode
set to "tree_summarize"
. This mode is used to generate a summary of the text in the documents. The from_documents
method processes each document, applies the transformations, and uses the language model to generate a summary for each document. The summaries and corresponding nodes are then stored in the index.response_synthesizer = get_response_synthesizer( response_mode="tree_summarize", use_async=True ) doc_summary_index = DocumentSummaryIndex.from_documents( city_docs, llm=chatgpt, transformations=[splitter], response_synthesizer=response_synthesizer, show_progress=True, )
DocumentSummaryIndex
, you can use the retrieve
method of a DocumentSummaryIndexEmbeddingRetriever
instance. This method takes a query string and returns the nodes corresponding to the documents whose summaries are relevant to the query. The retrieved nodes can then be used to generate a response.retriever = DocumentSummaryIndexEmbeddingRetriever( doc_summary_index, # similarity_top_k=1, ) retrieved_nodes = retriever.retrieve("What are the sports teams in Toronto?")
RetrieverQueryEngine
instance. This query engine takes a retriever
and a response_synthesizer
as arguments. The query
method of the query engine takes a query string and returns a response synthesized from the retrieved nodes.response_synthesizer = get_response_synthesizer(response_mode="tree_summarize") query_engine = RetrieverQueryEngine( retriever=retriever, response_synthesizer=response_synthesizer, ) response = query_engine.query("What are the sports teams in Toronto?") print(response)
response_synthesizer
plays a crucial role in both building the DocumentSummaryIndex
and generating responses from it. It is used to generate summaries of the documents during the index building process, and to synthesize responses from the retrieved nodes during the query process.