-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_summarizer.py
More file actions
59 lines (50 loc) · 2.56 KB
/
Copy pathdocument_summarizer.py
File metadata and controls
59 lines (50 loc) · 2.56 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
import asyncio
import traceback
import logging
import json
from typing import Sequence
from langchain.schema import Document
from langchain.schema import SystemMessage, HumanMessage
from langchain_openai import ChatOpenAI
class SummaryPrompt:
def __init__(self, summarizations: int, importance: str):
self.importance = importance
self.summarizations = summarizations
def to_prompt_string(self) -> str:
summarization_description = ""
# Adjust the message based on the number of summarizations
if self.summarizations == 1:
summarization_description = "has already been summarized once."
else:
summarization_description = f"has already been summarized {self.summarizations} times."
return (f"Summarize this memory. Keep in mind it's of {self.importance} importance and {summarization_description}.")
class FlexibleDocumentSummarizer:
_llm: ChatOpenAI
verbose: bool
def __init__(self, llm: ChatOpenAI, verbose: bool = False) -> None:
self._llm = llm
self.verbose = verbose
async def _get_single_summary(self, document: Document) -> None:
try:
summary_prompt = SummaryPrompt(
summarizations=document.metadata["summarizations"], importance=document.metadata["importance"])
memory = json.loads(document.page_content)
summary_prompt_str = summary_prompt.to_prompt_string()
user_message = [SystemMessage(
content=summary_prompt_str), HumanMessage(content=memory["user"])]
aida_message = [SystemMessage(
content=summary_prompt_str), HumanMessage(content=memory["AiDA"])]
response = await self._llm.agenerate([user_message, aida_message])
if not response.generations or not response.generations[0] or not response.generations[1]:
raise Exception(
"LLM did not provide a valid summary response.")
# Update the document's page_content in place with the summarized text
document.page_content = json.dumps(
{'user': response.generations[0][0].text, 'AiDA': response.generations[1][0].text})
except Exception as e:
if self.verbose:
logging.warning(
f"FlexibleDocumentSummarizer: _get_single_summary exception e: {e}\n{traceback.format_exc()}")
async def asummarize(self, documents: Sequence[Document]) -> None:
tasks = [self._get_single_summary(document) for document in documents]
await asyncio.gather(*tasks)