-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarizer.py
More file actions
49 lines (39 loc) · 2.06 KB
/
summarizer.py
File metadata and controls
49 lines (39 loc) · 2.06 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
from openai import OpenAI
from typing import List
from logger_config import get_logger
logger = get_logger(__name__)
class ChapterSummarizer:
def __init__(self, api_key: str, model: str, prompt: str):
logger.info(f"Initializing ChapterSummarizer with model: {model}")
self.client = OpenAI(api_key=api_key)
self.model = model
self.prompt = prompt
logger.info(f"Prompt length: {len(prompt)} characters")
def summarize_chunks(self, chunks: List[str]) -> str:
logger.info(f"Starting summarization of {len(chunks)} chunks")
summaries = []
for idx, chunk in enumerate(chunks):
logger.info(f"Processing chunk {idx + 1}/{len(chunks)}")
logger.info(f"Chunk {idx + 1} length: {len(chunk)} characters")
try:
response = self.client.chat.completions.create(
model=self.model,
messages=[
{"role": "system", "content": "You are a system design expert."},
{
"role": "user",
"content": f"{self.prompt}\n\nChunk {idx+1}:\n{chunk}"
}
]
)
summary = response.choices[0].message.content
summaries.append(summary)
logger.info(f"Successfully summarized chunk {idx + 1}")
logger.info(f"Summary {idx + 1} length: {len(summary)} characters")
logger.info(f"Tokens used - Prompt: {response.usage.prompt_tokens}, Completion: {response.usage.completion_tokens}, Total: {response.usage.total_tokens}")
except Exception as e:
logger.error(f"Failed to summarize chunk {idx + 1}", exc_info=True)
raise
total_summary_length = sum(len(s) for s in summaries)
logger.info(f"All chunks summarized successfully. Total summary length: {total_summary_length} characters")
return "\n\n".join(summaries)