Generate the final answer text with source attributions and hardware info.
Expects call_id
and has_gpu
in kwargs.
Source code in aiagents4pharma/talk2scholars/tools/pdf/utils/answer_formatter.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 | def format_answer(
question: str,
chunks: List[Any],
llm: Any,
articles: Dict[str, Any],
config: Any,
**kwargs: Any,
) -> str:
"""
Generate the final answer text with source attributions and hardware info.
Expects `call_id` and `has_gpu` in kwargs.
"""
result = generate_answer(question, chunks, llm, config)
answer = result.get("output_text", "No answer generated.")
# Get unique paper titles for source attribution
titles: Dict[str, str] = {}
for pid in result.get("papers_used", []):
if pid in articles:
titles[pid] = articles[pid].get("Title", "Unknown paper")
# Format sources
if titles:
srcs = "\n\nSources:\n" + "\n".join(f"- {t}" for t in titles.values())
else:
srcs = ""
# Extract logging metadata
call_id = kwargs.get("call_id", "<no-call-id>")
has_gpu = kwargs.get("has_gpu", False)
hardware_info = "GPU-accelerated" if has_gpu else "CPU-processed"
# Log final statistics with hardware info
logger.info(
"%s: Generated answer using %d chunks from %d papers (%s)",
call_id,
len(chunks),
len(titles),
hardware_info,
)
# Add subtle hardware info to logs but not to user output
logger.debug(
"%s: Answer generation completed with %s processing",
call_id,
hardware_info,
)
return f"{answer}{srcs}"
|