Find answers from the community

Updated 5 months ago

Getting retrieved nodes for a given query in chat engine

At a glance

The community member is asking how to get the retrieved nodes for a given query in the chat engine from the Llama Index library. The comments provide two solutions:

1. Use the VectorIndexRetriever to retrieve the nodes, then pass them to the ContextChatEngine.

2. Directly access the source_nodes attribute of the response from the chat_engine.chat() method, which contains all the retrieved nodes.

The community member confirms that the second solution, using print(response.source_nodes), is the answer they were looking for.

Useful resources
How I can get the retrieved nodes for a given query in chat engine(https://docs.llamaindex.ai/en/stable/examples/chat_engine/chat_engine_context/). Any hint of the steps invovled is appreciated.
Attachment
image.png
t
W
J
3 comments
Try this

Plain Text
index = VectorStoreIndex.from_vector_store(vector_store=qdrant_vector_storage)
 retriever = VectorIndexRetriever(
  index=index,
  similarity_top_k=150
)
retrived_nodes = retriever.retrieve("your_query")
chat_engine = ContextChatEngine(
  retriever=retriever,
  chat_history=[],
  system_prompt=self.PROMPT_TEMPLATE
)
response = chat_engine.chat("your_query")
This will work but you can get source nodes in the similar way that you were getting in query_engine inchat_engine as well.


https://github.com/run-llama/llama_index/blob/main/llama-index-core%2Fllama_index%2Fcore%2Fchat_engine%2Fcontext.py#L223


So something like this

Plain Text
chat_engine= index.as_chat_engine(...)

response= chat_engine.chat("query")

#print source nodes
print(response.source_nodes)


Source nodes contain all the retrieved nodes
Turns out, all i was looking for this command "print(response.source_nodes)" thanks!
Add a reply
Sign up and join the conversation on Discord