Find answers from the community

Updated 2 years ago

I have constructed an index using GPTPineconeIndex. I have also queried it for response an

@kapa.ai I have constructed an index using GPTPineconeIndex. I have also queried it for response and received a response. How do I get the sources from the response?
k
L
5 comments
Please stand by @LawSnap as I search the knowledge base. We are experiencing especially high traffic right now and might take longer than usual. :wave:
----------------------
To get the sources from the response after querying a GPTPineconeIndex, you can use the source_nodes attribute or the get_formatted_sources() method of the Response object. Here's how to do it:

Plain Text
# Assuming you have a response object after querying the index
response = index.query("<query_str>")

# Get sources
sources = response.source_nodes

# Get formatted sources
formatted_sources = response.get_formatted_sources()


You can find more information about the Response object in the documentation (https://gpt-index.readthedocs.io/en/latest/reference/response.html).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai when I do that I get an error message
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/llama_index/data_structs/node_v2.py:144: UserWarning: .source_text is deprecated, use .node.get_text() instead
warnings.warn(".source_text is deprecated, use .node.get_text() instead")
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/llama_index/data_structs/node_v2.py:139: UserWarning: .doc_id is deprecated, use .node.ref_doc_id instead
warnings.warn(".doc_id is deprecated, use .node.ref_doc_id instead")
Let me check the knowledge base for you again. One moment! :books:
----------------------
It seems you are encountering deprecation warnings when trying to access the source_text and doc_id attributes of the SourceNode objects. To resolve these warnings, you should use the recommended methods instead:

  1. Replace .source_text with .node.get_text()
  2. Replace .doc_id with .node.ref_doc_id
Here's an example of how to get the sources and their document IDs using the recommended methods:

Plain Text
# Assuming you have a response object after querying the index
response = index.query("<query_str>")

# Get sources
sources = response.source_nodes

# Iterate through the sources and get the source text and document ID
for source in sources:
    source_text = source.node.get_text()
    doc_id = source.node.ref_doc_id
    print(f"Source Text: {source_text}\nDocument ID: {doc_id}\n")


Please note that the code above is based on the information provided in the warning messages and not directly from the information extracts.
----------------------
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