@@ -85,16 +85,31 @@ def question_answer(retriever, question):
8585 "question" : lambda input : input ["question" ]}
8686 ) | {
8787 "answer" : rag_chain_from_docs ,
88- "Source" : lambda input : list (set (doc .metadata ['source' ] for doc in input ["documents" ])),
88+ "Source" : lambda input : [
89+ {
90+ 'url' : doc .metadata ['source' ],
91+ 'title' : doc .metadata .get ('title' , 'Unknown Title' ),
92+ 'authors' : doc .metadata .get ('authors' , 'Unknown Authors' )
93+ }
94+ for doc in input ["documents" ]
95+ ],
8996 }
9097 response = rag_chain_with_source .invoke (question )
9198
9299 answer = response ['answer' ].content + '<br>' + '<br>' + '<b>Reference:</b>' + '<br>'
93100
94- # Generate links with numbers, assuming the list items are URLs
95- links = [f'<a href="{ url } ">paper { i + 1 } </a>' for i , url in enumerate (response ['Source' ])]
101+ # Generate links with paper titles and authors instead of numbers
102+ seen_urls = set ()
103+ unique_sources = []
104+ for source in response ['Source' ]:
105+ if source ['url' ] not in seen_urls :
106+ seen_urls .add (source ['url' ])
107+ unique_sources .append (source )
96108
97- # Join the links into a single string separated by commas
109+ links = [f'<a href="{ source ["url" ]} ">{ source ["title" ]} </a> ({ source ["authors" ]} )'
110+ for source in unique_sources ]
111+
112+ # Join the links into a single string separated by line breaks
98113 formatted_links = '<br>' .join (links )
99114
100115 # Append the formatted links to the answer
0 commit comments