The SummarizationTool provides intelligent text summarization capabilities using Large Language Models (LLMs). It enables mindX agents to condense large amounts of text into concise summaries while maintaining key information and context.
File: tools/summarization_tool.py
Class: SummarizationTool
Version: 1.0.0
Status: ✅ Active
class SummarizationTool(BaseTool):
- llm_handler: LLMHandlerInterface - LLM for text generation
- config: Config - Configuration object
- logger: Logger - For operation logging
from tools.summarization_tool import SummarizationTool
from llm.llm_interface import LLMHandlerInterface
tool = SummarizationTool(
config=config,
llm_handler=llm_handler
)
Summarize text
summary = await tool.execute(
text_to_summarize="Long text content...",
max_summary_words=150
)
# With topic context and custom format
summary = await tool.execute(
text_to_summarize="Technical documentation...",
topic_context="Python async programming",
max_summary_words=200,
output_format="bullet_points",
temperature=0.3,
custom_instructions="Focus on key concepts and examples"
)
text_to_summarize (str): The text content to be summarizedtopic_context (str): Context about the topic for better summarizationmax_summary_words (int): Maximum words for summary (default: 150)output_format (str): "paragraph" or "bullet_points" (default: "paragraph")temperature (float): LLM generation temperature (default: from config)custom_instructions (str): Additional instructions for the LLMReturns a coherent paragraph summary:
The text discusses the implementation of async programming in Python,
focusing on asyncio and coroutines. Key concepts include event loops,
awaitables, and concurrent execution patterns...
Returns a bulleted list:
- Async programming enables concurrent execution in Python
asyncio provides the event loop and coroutine framework
Key concepts include awaitables and async/await syntax
Best practices involve proper error handling and resource management
For very long texts:
max_input_chars = self.config.get("tools.summarization.max_input_chars", 30000)
if len(text_to_summarize) > max_input_chars:
text_to_summarize = (
text_to_summarize[:max_input_chars//2] +
f"\n... (TEXT TRUNCATED DUE TO LENGTH) ...\n" +
text_to_summarize[-(max_input_chars//2):]
)
Incorporates topic context for better summaries:
if topic:
prompt_lines.append(f"The text is about: {topic}.")
Supports multiple output formats:
Estimates token requirements:
max_tokens_for_summary = int(max_summary_words * 2.5)
tools.summarization.max_input_chars: 30000 # Max input text length
tools.summarization.llm.temperature: 0.2 # Default temperature
# In agent plan
plan = [
{
"action": "summarize_text",
"text_to_summarize": long_text,
"max_summary_words": 200,
"output_format": "bullet_points"
}
]
The SummarizationTool can be used with:
summary = await tool.execute(
text_to_summarize=documentation_text,
topic_context="API documentation",
max_summary_words=100
)
summary = await tool.execute(
text_to_summarize=article_text,
output_format="bullet_points",
max_summary_words=50
)
summary = await tool.execute(
text_to_summarize=technical_doc,
topic_context="system architecture",
custom_instructions="Focus on technical details and implementation",
temperature=0.1 # Lower temperature for more factual summaries
)
llm.llm_interface.LLMHandlerInterface: LLM handlercore.bdi_agent.BaseTool: Base tool classutils.config.Config: Configuration managementprompt_lines = [
"You are an expert summarization AI...",
f"The text is about: {topic}.",
f"The summary should be approximately {max_words} words or less.",
"Focus on extracting the most critical information...",
"\nText to Summarize:\n---BEGIN TEXT---",
text,
"---END TEXT---\n\nConcise Summary:"
]
try:
summary_result = await self.llm_handler.generate_text(...)
if summary_result and not summary_result.lower().startswith("error:"):
return summary_result.strip()
else:
return f"Error: LLM failed to generate summary - {summary_result}"
except Exception as e:
return f"Error: Exception during summarization - {type(e).__name__}: {e}"