-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummaries.py
More file actions
88 lines (72 loc) · 2.8 KB
/
summaries.py
File metadata and controls
88 lines (72 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from prompt_library import summaries, general_paper_summary
import asyncio
async def make_summary(llm, elements):
"""
Input - elements -> TUPLES (list element with [0] - title, [1] - text; llm
Output - LLM-generated summaries for 1 paragraph
"""
print(f"Making summary for {elements[0]}")
user_prompt = f"""
Given the paragraph title and paragraph text.
Return the consise summary of paragraph text.
# Paragpraph Title:
{elements[0]}
# Paragraph Text:
{elements[1]}
Return the summary of the paragraph text only.
"""
messages = [("system", summaries), ("human", user_prompt)]
try:
# Use ainvoke for async LLM calls
res = await llm.ainvoke(messages)
summary = res.content
except Exception as e:
print(f"Warning: Error generating summary for {elements[0]}: {e}")
summary = elements[1] # Use original text as fallback
return summary
async def process_paper_paragraphs_parallel(llm, paper):
"""
Process all paragraphs of a paper in parallel.
"""
paper_tuples = paper["Tuples"]
if not paper_tuples:
print(f"No tuples found in paper: {paper['title']}")
return []
# Process all paragraphs in parallel
tasks = []
for tuple_data in paper_tuples:
task = make_summary(llm, tuple_data)
tasks.append(task)
summaries = await asyncio.gather(*tasks)
# Return the summaries as tuples (title, text, summary)
result_tuples = []
for i, summary in enumerate(summaries):
result_tuples.append([paper_tuples[i][0], paper_tuples[i][1], summary])
return result_tuples
async def create_general_summary(llm, paper):
"""
Create a comprehensive general summary of the paper using section summaries.
"""
print(f"Creating general summary for: {paper['title'][:50]}...")
# Get section summaries
section_summaries = paper.get("section_summaries", [])
if not section_summaries:
print(f"No section summaries found for paper: {paper['title']}")
return ""
# Build the prompt with paper information
general_summary_prompt = f"""
Paper Title: {paper['title']}
Abstract: {paper.get('Abstract', 'No abstract available')}
Section Summaries:
"""
# Add each section summary
for section in section_summaries:
if len(section) >= 3: # [title, text, summary]
general_summary_prompt += f"\n\n**{section[0]}:**\n{section[2]}"
messages = [("system", general_paper_summary), ("human", general_summary_prompt)]
try:
res = await llm.ainvoke(messages)
return res.content
except Exception as e:
print(f"Error creating general summary for {paper['title']}: {e}")
return ""