Find answers from the community

Updated 2 months ago

Links

Hello, I was wondering what's the recommended way to list reference links (links to source material) along with a response while using chat_engine?

My knowledge base is basically a cloned github repo containing markdown files from my documentation and blog. What I ideally want to do is along with the response show the hyperlinks to the documents that were referred to build that response.

I couldn't find any guides or examples that does this.
L
s
15 comments
Each response has a source_nodes attribute. And each source node has metadata

So basically, you'll need to set some metadata on your input documents to properly retrieve it from the source nodes.

response.source_nodes[0].node.metadata
thanks, so IIUC, the filename can be set as metadata, and then I can write the custom logic to convert the filename to the hyperlink I'm after
I seem to run into

Plain Text
AttributeError: 'Document' object has no attribute 'file_metadata'
Did you follow the same though?

Plain Text
from llama_index import SimpleDirectoryReader
filename_fn = lambda filename: {'file_name': filename}

# automatically sets the metadata of each document according to filename_fn
documents = SimpleDirectoryReader('./data', file_metadata=filename_fn).load_data()
print(documents[0].metadata['file_name'])
yeah, let me try again in a simpler sample app..
Works for me locally πŸ€”

Plain Text
>>> from llama_index import SimpleDirectoryReader
>>> name_fn = lambda filename: {'file_name': filename}
>>> filename_fn = lambda filename: {'file_name': filename}
>>> documents = SimpleDirectoryReader('./docs/examples/data/paul_graham', file_metadata=filename_fn).load_data()
>>> print(documents[0].metadata['file_name'])
docs/examples/data/paul_graham/paul_graham_essay.txt
>>> 
Got it working, thanks !
one last Q for the day πŸ™‚

So in the response

Plain Text
 response = chat_engine.chat(prompt)


can it also return the metadata?
I guess I'm asking during runtime how do I access the relevant filename for the question that was asked
Yea, exact same as I linked above

response.source_nodes[0].node.metadata

Although source nodes can be an empty list if the chat engine is an agent and didn't choose to use any tools
Got it! And it works. Thanks for being awesome.

The support I got within minutes of hopping on here has been outstanding. Keep up the great work πŸ”₯
:dotsCATJAM: :dotsHARDSTYLE:
Add a reply
Sign up and join the conversation on Discord