ak3ra commited on
Commit
8f8aff5
·
1 Parent(s): 5322617

Create helpers.py

Browse files
Files changed (1) hide show
  1. helpers.py +40 -0
helpers.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+ from llama_index.core import Response
3
+
4
+ def process_response(response: Response) -> Dict[str, Any]:
5
+ source_nodes = response.source_nodes
6
+ sources = {}
7
+ for i, node in enumerate(source_nodes, 1):
8
+ source = format_source(node.metadata)
9
+ if source not in sources.values():
10
+ sources[i] = source
11
+
12
+ markdown_text = response.response + "\n\n### Sources\n\n"
13
+ raw_text = response.response + "\n\nSources:\n"
14
+
15
+ for i, source in sources.items():
16
+ markdown_text += f"{i}. {source}\n"
17
+ raw_text += f"[{i}] {source}\n"
18
+
19
+ return {"markdown": markdown_text, "raw": raw_text, "sources": sources}
20
+
21
+ def format_source(metadata: Dict[str, Any]) -> str:
22
+ authors = metadata.get('authors', 'Unknown Author')
23
+ year = metadata.get('year', 'n.d.')
24
+ title = metadata.get('title', 'Untitled')
25
+
26
+ author_list = authors.split(',')
27
+ if len(author_list) > 2:
28
+ formatted_authors = f"{author_list[0].strip()} et al."
29
+ elif len(author_list) == 2:
30
+ formatted_authors = f"{author_list[0].strip()} and {author_list[1].strip()}"
31
+ else:
32
+ formatted_authors = author_list[0].strip()
33
+
34
+ year = 'n.d.' if year is None or year == 'None' else str(year)
35
+
36
+ max_title_length = 250
37
+ if len(title) > max_title_length:
38
+ title = title[:max_title_length] + '...'
39
+
40
+ return f"{formatted_authors} ({year}). {title}"