Find answers from the community

Updated 3 months ago

Is there a way to get metadata when I am using chat engine ?, say response.metadata , but

Is there a way to get metadata when I am using chat engine ?, say response.metadata , but I am getting empty list. Basically I want to get from which document / filename that response comes from
k
W
i
7 comments
Please stand by @ihshan :) as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Yes, there is a way to get metadata when using the chat engine in LlamaIndex. The metadata can be accessed using response.metadata. This metadata often includes information about the source document or filename from which the response comes.

However, if you're getting an empty list, it might be due to the specific configuration of your chat engine or the nature of the query. It's also possible that the response doesn't have any associated metadata.

Here are some examples of how metadata is accessed in different contexts:

  1. In the Knowledge Graph RAG Query Engine, metadata is accessed as follows:
Plain Text
import pprint

pp = pprint.PrettyPrinter()
pp.pprint(response.metadata)

Source ()

  1. In the Fuzzy Citation Query Engine, metadata is accessed and used to get the exact character location of the response from the original document:
Plain Text
for chunk_info in response.metadata.values():
    start_char_idx = chunk_info["start_char_idx"]
    end_char_idx = chunk_info["end_char_idx"]

    node = chunk_info["node"]
    node_start_char_idx = node.start_char_idx
    node_end_char_idx = node.end_char_idx

    # using the node start and end char idx, we can offset the
    # citation chunk to locate the citation in the
    document_start_char_idx = start_char_idx + node_start_char_idx
    document_end_char_idx = document_start_char_idx + (end_char_idx - start_char_idx)
    text = documents[0].text[document_start_char_idx:document_end_char_idx]

    print(text)
    print(node.metadata)
    print("----------------")

Source ()
  1. In the Vector Stores example, metadata is accessed and printed as follows:
Plain Text
print(response)
print("metadata -", response.metadata)

Source ()

If you're still having trouble, it might be helpful to review the documentation or examples related to the specific chat engine you're using.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
How have you implemented your chat engine. Chat engine do return the source nodes if found for any of your queries.
Yes, I implemented in this way

response = Index.as_chat_engine(chat_mode= 'context').chat("What is the revenue"?)

But when I run this line, it throws me empty

response.metadata()

How can I get citation that a specific info from a particular document
You'll get metadata associated with each node used for generating the response.

They are found inside the response object. and not directly with response.metadata
Plain Text
response = Index.as_chat_engine(chat_mode= 'context').chat("What is the revenue"?)

for source in response.source_nodes:
   print(source.node.extra_info)
Thanks a lot. I have implemented CitationQueryEngine before. It provides "Text...[1] and [2]" (response text)
  1. Doc1
  2. Doc2
Is there a way to achieve the same way by using chat engine ? with he response.source_nodes, I cannot figure out where should I put the citation in the response text
Add a reply
Sign up and join the conversation on Discord