----------------------
To parse the response object of LlamaIndex and obtain the document ids, the source text, and the similarity scores of those sources with the query, you can use the
response.source_nodes
attribute of the response object. This attribute contains a list of nodes that contributed to the response. Each node in this list is an instance of the
NodeWithScore
class, which includes the document id, the source text, and the similarity score among other attributes.
Here is an example of how you can parse the response:
response = query_engine.query("<query_str>")
# get sources
source_nodes = response.source_nodes
# iterate over the source nodes
for node in source_nodes:
# get the document id
doc_id = node.node.doc_id
# get the source text
text = node.node.text
# get the similarity score
similarity = node.score
print(f"Document ID: {doc_id}, Text: {text}, Similarity: {similarity}")
In this example,
query_engine
is an instance of a query engine, and
<query_str>
is the query string.
Please note that the
NodeWithScore
class is part of the
llama_index.data_structs.node
module, and the
query
method is part of the
QueryEngine
class in the
llama_index.query_engine
module.
For more information, you can refer to the
LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:
----------------------