logger.warning(f"Content summarization model type is {self.model_type} but KNOWLEDGE_SUMMARY_BASE_URL is not set. Summarization will be disabled.")
self.enabled=False
def_get_agent(self)->Optional[Agent]:
"""Lazy initialization of the summarization agent"""
ifnotself.enabled:
returnNone
ifself._agentisNone:
try:
model=create_model(
model_type=self.model_type,
api_key=self.api_key,
model_name=self.model_name,
max_tokens=self.max_tokens,
base_url=self.base_url
)
self._agent=Agent(
name="Content Summarizer",
model=model,
instructions="""You are an expert content summarizer for a knowledge base system.
Your task is to summarize web page content while:
1. **PRESERVING ALL URLs** - This is CRITICAL. Every single URL mentioned in the content MUST be kept in the summary:
- Product URLs
- Reference URLs
- Image URLs
- Documentation URLs
- Any links mentioned in the text
DO NOT remove, shorten, or modify ANY URLs. Keep them exactly as they appear.
2. Preserving ALL important information needed to answer user questions:
- Product names, descriptions, and details
- Pricing and availability information
- Technical specifications and features
- Contact information and addresses
- Instructions and procedures
- Important facts and data points
3. Reducing only redundant and verbose text:
- Remove excessive marketing language
- Eliminate repetitive phrases
- Condense wordy explanations
- Remove filler content
4. Maintaining the original context and structure so users can:
- Find specific products or information
- Access the original URLs for more details
- Get accurate answers to their questions
REMEMBER: URLs are the most critical element to preserve. Users need these links to access products and resources. Never remove or modify URLs under any circumstances.
Format your response as a concise summary that maintains all URLs and critical information.""",
markdown=False,
debug_mode=settings.ENVIRONMENT=="development"
)
logger.info(f"Initialized content summarizer with model: {self.model_type}/{self.model_name}")
exceptExceptionase:
logger.error(f"Failed to initialize content summarizer: {str(e)}")
self.enabled=False
returnNone
returnself._agent
defsummarize(self,content:str,url:str)->str:
"""
Summarize content while preserving important information and URLs.
Args:
content: The original content to summarize
url: The source URL for reference
Returns:
Summarized content or original content if summarization fails/disabled
"""
# If summarization is disabled, return original content
ifnotself.enabled:
returncontent
# If content is already short, don't summarize
iflen(content)<1000:
logger.debug(f"Content too short to summarize ({len(content)} chars), keeping original")
returncontent
try:
agent=self._get_agent()
ifnotagent:
logger.warning("Summarization agent not available, using original content")
returncontent
# Create the summarization prompt
prompt=f"""Summarize the following web page content from {url}:
Content:
{content}
CRITICAL REQUIREMENTS:
- **KEEP EVERY SINGLE URL EXACTLY AS IT APPEARS** - Do not remove, shorten, or modify any URLs
- Preserve all product URLs, reference links, and resource URLs
- Keep all important facts, product details, pricing, and specifications
- Maintain context for answering user questions
- Only reduce redundant/verbose text, never remove URLs or important information"""
logger.info(f"Summarizing content from {url} ({len(content)} chars)")