File size: 1,138 Bytes
0e38162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Summarization tool: calls LLM to produce a concise summary of any text block.
"""
from tools.base_tool import BaseTool, ToolResult
from services.llm_service import LLMService
from utils.logger import get_logger

logger = get_logger("summarize_tool")

SYSTEM = (
    "You are a precise scientific summarizer. "
    "Return a concise, factual summary in 3-5 sentences. "
    "Preserve key findings, methods, and numbers."
)


class SummarizeTool(BaseTool):
    name = "summarize"
    description = "Produce a concise LLM summary of a given text block."

    def __init__(self, llm: LLMService):
        self.llm = llm

    async def run(self, text: str, max_words: int = 150) -> ToolResult:
        if not text or not text.strip():
            return self._err("Empty text provided to summarize tool.")
        prompt = f"Summarize the following text in at most {max_words} words:\n\n{text}"
        try:
            summary = await self.llm.complete(SYSTEM, prompt)
            return self._ok(summary.strip())
        except Exception as e:
            logger.error(f"Summarize tool failed: {e}")
            return self._err(str(e))