Find answers from the community

Updated 3 months ago

how do i pass text_qa_template to Composable Graph as_query_engine?

how do i pass text_qa_template to Composable Graph as_query_engine?
k
R
12 comments
Please stand by @Raz as I search the knowledge base. We are experiencing especially high traffic right now and might take longer than usual. :wave:
----------------------
To pass a text_qa_template to the as_query_engine method of a ComposableGraph, you first need to define a custom prompt using the Prompt class from llama_index. This custom prompt will be used as your text_qa_template. Here's an example of how to do this:

Plain Text
from llama_index import Prompt, VectorStoreIndex, SimpleDirectoryReader

# load documents
documents = SimpleDirectoryReader('data').load_data()

# define custom Prompt
TEMPLATE_STR = (
    "We have provided context information below. \n"
    "---------------------\n"
    "{context_str}"
    "\n---------------------\n"
    "Given this information, please answer the question: {query_str}\n"
)
QA_TEMPLATE = Prompt(TEMPLATE_STR)

# Build index 
index = VectorStoreIndex.from_documents(documents)

# Configure query engine
query_engine = index.as_query_engine(text_qa_template=QA_TEMPLATE)

# Execute query
response = query_engine.query("What did the author do growing up?")
print(response)

(Source: Custom Prompts ())

However, the as_query_engine method for a ComposableGraph does not directly accept a text_qa_template parameter. Instead, you can define custom query engines for its sub-indices, which can include the text_qa_template:

Plain Text
from llama_index.query_engine.graph_query_engine import ComposableGraphQueryEngine

# Assuming 'graph' is your ComposableGraph object
custom_query_engines = {
    graph.root_id: graph.root_index.as_query_engine(text_qa_template=QA_TEMPLATE)
}

query_engine = graph.as_query_engine(custom_query_engines=custom_query_engines)

(Source: Graph Query Engine ())
Please note that the actual implementation may vary depending on your specific use case and the structure of your ComposableGraph.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai how do i get the source from the result of graph.as_query_engine query?
Let me check the knowledge base for you. One moment! :books:
----------------------
To get the source from the result of a query executed with graph.as_query_engine, you can use the get_formatted_sources method on the response object. Here's an example:

Plain Text
# Assuming 'query_engine' is your ComposableGraphQueryEngine object
response = query_engine.query("Your query here")

# Get source of response
sources = response.get_formatted_sources()
print(sources)


This will print the sources of the response. The sources are the documents or data from which the response was generated.

This method is demonstrated in multiple extracts, such as in the ComposableIndices-Weaviate () and ComposableIndices-Prior () examples.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai how do I do a response.source_nodes?
Let me check the knowledge base for you. One moment! :books:
----------------------
The response.source_nodes attribute is used to access the source nodes from the response of a query. Here's an example of how to use it:

Plain Text
# Assuming 'query_engine' is your QueryEngine object
response = query_engine.query("Your query here")

# Get source nodes of response
source_nodes = response.source_nodes
print(source_nodes)


This will print the source nodes of the response. The source nodes are the nodes in the knowledge base from which the response was generated.

This usage is demonstrated in multiple extracts, such as in the CustomRetrievers () and CitationQueryEngine () examples.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai how do i get the filename of the source_nodes?
Let me check the knowledge base for you. One moment! :books:
----------------------
To get the filename from the source nodes, you need to access the metadata attribute of each source node. The filename is stored in the metadata dictionary if it was set during the creation of the document. Here's an example of how to do this:

Plain Text
# Assuming 'response' is the response from your query
for source_node in response.source_nodes:
    filename = source_node.node.metadata.get('filename')
    print(filename)


This will print the filename of each source node in the response. Please note that this assumes that the filename was set in the metadata dictionary during the creation of the document. If it wasn't, you will need to set it manually. Here's an example of how to do this:

Plain Text
from llama_index import Document

# Create a document with filename in metadata
document = Document(
    text='text', 
    metadata={
        'filename': '<doc_file_name>', 
        'category': '<category>'
    }
)


This usage is demonstrated in the Customizing Documents () guide.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord