Spaces:
Runtime error
Runtime error
comment extra functions and optimized prompt
Browse files- utils/optimizer.py +138 -181
utils/optimizer.py
CHANGED
|
@@ -39,8 +39,8 @@ class ContentOptimizer:
|
|
| 39 |
" \"structuredness\": 7.0,\n"
|
| 40 |
" \"answerability\": 9.0\n"
|
| 41 |
" }},\n"
|
| 42 |
-
" \"keywords\": [\"example\", \"installation\", \"setup\"],\n"
|
| 43 |
-
" \"optimized_text\": \"...\"\n"
|
| 44 |
"}}\n"
|
| 45 |
"```"
|
| 46 |
)
|
|
@@ -78,27 +78,8 @@ class ContentOptimizer:
|
|
| 78 |
)
|
| 79 |
|
| 80 |
# Competitive content analysis prompt
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
"Original Content: {content}\n"
|
| 84 |
-
"Analyze against these AI search factors:\n"
|
| 85 |
-
"- Entity recognition and linking\n"
|
| 86 |
-
"- Question coverage completeness\n"
|
| 87 |
-
"- Factual statement clarity\n"
|
| 88 |
-
"- Conversational flow\n"
|
| 89 |
-
"- Semantic relationship mapping\n\n"
|
| 90 |
-
"Provide competitive analysis in JSON format with specific recommendations:\n"
|
| 91 |
-
"{{\n"
|
| 92 |
-
" \"competitive_analysis\": {{\n"
|
| 93 |
-
" \"entity_gaps\": [\"gap1\", \"gap2\"],\n"
|
| 94 |
-
" \"question_coverage\": \"summary of coverage\",\n"
|
| 95 |
-
" \"factual_clarity\": \"assessment\",\n"
|
| 96 |
-
" \"conversational_flow\": \"assessment\",\n"
|
| 97 |
-
" \"semantic_relationships\": [\"relationship1\", \"relationship2\"]\n"
|
| 98 |
-
" }},\n"
|
| 99 |
-
" \"recommendations\": [\"recommendation 1\", \"recommendation 2\"]\n"
|
| 100 |
-
"}}\n"
|
| 101 |
-
)
|
| 102 |
|
| 103 |
# Dedicated prompt for rewriting/optimizing content
|
| 104 |
self.optimization_rewrite_prompt = (
|
|
@@ -113,7 +94,7 @@ class ContentOptimizer:
|
|
| 113 |
)
|
| 114 |
|
| 115 |
def optimize_content(self, content: str, analyze_only: bool = False,
|
| 116 |
-
include_keywords: bool = True, optimization_type: str = "
|
| 117 |
"""
|
| 118 |
Main content optimization function
|
| 119 |
Args:
|
|
@@ -148,14 +129,15 @@ class ContentOptimizer:
|
|
| 148 |
'"optimized_text": "..."',
|
| 149 |
'"optimization_suggestions": ["suggestion 1", "suggestion 2"]'
|
| 150 |
)
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 161 |
SystemMessagePromptTemplate.from_template(prompt_text),
|
|
@@ -186,8 +168,7 @@ class ContentOptimizer:
|
|
| 186 |
SystemMessagePromptTemplate.from_template(self.seo_style_prompt),
|
| 187 |
HumanMessagePromptTemplate.from_template(f"Optimize this content for AI search engines:\n\n{content[:6000]}")
|
| 188 |
])
|
| 189 |
-
|
| 190 |
-
# ("user", f"Optimize this content for AI search engines:\n\n{content[:6000]}")
|
| 191 |
|
| 192 |
chain = prompt_template | self.llm
|
| 193 |
result = chain.invoke({})
|
|
@@ -235,99 +216,99 @@ class ContentOptimizer:
|
|
| 235 |
except Exception as e:
|
| 236 |
return {'error': f"Competitive optimization failed: {str(e)}"}
|
| 237 |
|
| 238 |
-
def batch_optimize_content(self, content_list: List[str], optimization_type: str = "standard") -> List[Dict[str, Any]]:
|
| 239 |
-
|
| 240 |
-
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
-
|
| 267 |
|
| 268 |
-
def generate_content_variations(self, content: str, num_variations: int = 3) -> List[Dict[str, Any]]:
|
| 269 |
-
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
|
| 291 |
-
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
|
| 322 |
-
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
|
| 330 |
-
|
| 331 |
|
| 332 |
def analyze_content_readability(self, content: str) -> Dict[str, Any]:
|
| 333 |
"""
|
|
@@ -388,56 +369,56 @@ class ContentOptimizer:
|
|
| 388 |
except Exception as e:
|
| 389 |
return {'error': f"Readability analysis failed: {str(e)}"}
|
| 390 |
|
| 391 |
-
def extract_key_entities(self, content: str) -> Dict[str, Any]:
|
| 392 |
-
|
| 393 |
-
|
| 394 |
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
|
| 404 |
-
|
| 405 |
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
|
| 442 |
def optimize_for_voice_search(self, content: str) -> Dict[str, Any]:
|
| 443 |
"""
|
|
@@ -450,32 +431,8 @@ class ContentOptimizer:
|
|
| 450 |
Dict: Voice search optimization results
|
| 451 |
"""
|
| 452 |
try:
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
Focus on:
|
| 456 |
-
1. Natural language patterns
|
| 457 |
-
2. Question-based structure
|
| 458 |
-
3. Conversational tone
|
| 459 |
-
4. Clear, direct answers
|
| 460 |
-
5. Featured snippet optimization
|
| 461 |
|
| 462 |
-
Original content: {content}
|
| 463 |
-
|
| 464 |
-
Provide optimization in JSON:
|
| 465 |
-
```json
|
| 466 |
-
{{
|
| 467 |
-
"voice_optimized_content": "conversational version...",
|
| 468 |
-
"question_answer_pairs": [
|
| 469 |
-
{{"question": "What is...", "answer": "Direct answer..."}},
|
| 470 |
-
{{"question": "How does...", "answer": "Step by step..."}}
|
| 471 |
-
],
|
| 472 |
-
"featured_snippet_candidates": ["snippet 1", "snippet 2"],
|
| 473 |
-
"natural_language_improvements": ["improvement 1", "improvement 2"],
|
| 474 |
-
"conversational_score": 8.5
|
| 475 |
-
}}
|
| 476 |
-
```
|
| 477 |
-
"""
|
| 478 |
-
|
| 479 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 480 |
SystemMessagePromptTemplate.from_template(voice_prompt.format(content=content[:4000])),
|
| 481 |
HumanMessagePromptTemplate.from_template("Optimize for voice search.")
|
|
|
|
| 39 |
" \"structuredness\": 7.0,\n"
|
| 40 |
" \"answerability\": 9.0\n"
|
| 41 |
" }},\n"
|
| 42 |
+
" \"keywords\": [\"example\", \"installation\", \"setup\"],\n,"
|
| 43 |
+
" \"optimized_text\": \"...\"\n,"
|
| 44 |
"}}\n"
|
| 45 |
"```"
|
| 46 |
)
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
# Competitive content analysis prompt
|
| 81 |
+
self.competitive_analysis_prompt = ("Analyze the following content for AI search optimization gaps in entities, questions, clarity, flow, and semantic links. Return JSON with gaps and actionable recommendations.\nContent: {content}")
|
| 82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Dedicated prompt for rewriting/optimizing content
|
| 85 |
self.optimization_rewrite_prompt = (
|
|
|
|
| 94 |
)
|
| 95 |
|
| 96 |
def optimize_content(self, content: str, analyze_only: bool = False,
|
| 97 |
+
include_keywords: bool = True, optimization_type: str = "seo") -> Dict[str, Any]:
|
| 98 |
"""
|
| 99 |
Main content optimization function
|
| 100 |
Args:
|
|
|
|
| 129 |
'"optimized_text": "..."',
|
| 130 |
'"optimization_suggestions": ["suggestion 1", "suggestion 2"]'
|
| 131 |
)
|
| 132 |
+
# else:
|
| 133 |
+
# # Use dedicated rewrite prompt for optimization
|
| 134 |
+
# prompt_text = self.optimization_rewrite_prompt
|
| 135 |
+
|
| 136 |
+
if not include_keywords:
|
| 137 |
+
prompt_text = prompt_text.replace(
|
| 138 |
+
'"keywords": ["example", "installation", "setup"],',
|
| 139 |
+
''
|
| 140 |
+
)
|
| 141 |
|
| 142 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 143 |
SystemMessagePromptTemplate.from_template(prompt_text),
|
|
|
|
| 168 |
SystemMessagePromptTemplate.from_template(self.seo_style_prompt),
|
| 169 |
HumanMessagePromptTemplate.from_template(f"Optimize this content for AI search engines:\n\n{content[:6000]}")
|
| 170 |
])
|
| 171 |
+
|
|
|
|
| 172 |
|
| 173 |
chain = prompt_template | self.llm
|
| 174 |
result = chain.invoke({})
|
|
|
|
| 216 |
except Exception as e:
|
| 217 |
return {'error': f"Competitive optimization failed: {str(e)}"}
|
| 218 |
|
| 219 |
+
# def batch_optimize_content(self, content_list: List[str], optimization_type: str = "standard") -> List[Dict[str, Any]]:
|
| 220 |
+
# """
|
| 221 |
+
# Optimize multiple pieces of content in batch
|
| 222 |
|
| 223 |
+
# Args:
|
| 224 |
+
# content_list (List[str]): List of content pieces to optimize
|
| 225 |
+
# optimization_type (str): Type of optimization to apply
|
| 226 |
+
|
| 227 |
+
# Returns:
|
| 228 |
+
# List[Dict]: List of optimization results
|
| 229 |
+
# """
|
| 230 |
+
# results = []
|
| 231 |
|
| 232 |
+
# for i, content in enumerate(content_list):
|
| 233 |
+
# try:
|
| 234 |
+
# result = self.optimize_content(
|
| 235 |
+
# content,
|
| 236 |
+
# optimization_type=optimization_type
|
| 237 |
+
# )
|
| 238 |
+
# result['batch_index'] = i
|
| 239 |
+
# results.append(result)
|
| 240 |
|
| 241 |
+
# except Exception as e:
|
| 242 |
+
# results.append({
|
| 243 |
+
# 'batch_index': i,
|
| 244 |
+
# 'error': f"Batch optimization failed: {str(e)}"
|
| 245 |
+
# })
|
| 246 |
|
| 247 |
+
# return results
|
| 248 |
|
| 249 |
+
# def generate_content_variations(self, content: str, num_variations: int = 3) -> List[Dict[str, Any]]:
|
| 250 |
+
# """
|
| 251 |
+
# Generate multiple optimized variations of the same content
|
| 252 |
|
| 253 |
+
# Args:
|
| 254 |
+
# content (str): Original content
|
| 255 |
+
# num_variations (int): Number of variations to generate
|
| 256 |
+
|
| 257 |
+
# Returns:
|
| 258 |
+
# List[Dict]: List of content variations with analysis
|
| 259 |
+
# """
|
| 260 |
+
# variations = []
|
| 261 |
|
| 262 |
+
# variation_prompts = [
|
| 263 |
+
# "Create a more conversational version optimized for AI chat responses",
|
| 264 |
+
# "Create a more authoritative version optimized for citations",
|
| 265 |
+
# "Create a more structured version optimized for question-answering"
|
| 266 |
+
# ]
|
| 267 |
|
| 268 |
+
# for i in range(min(num_variations, len(variation_prompts))):
|
| 269 |
+
# try:
|
| 270 |
+
# custom_prompt = f"""You are optimizing content for AI systems. {variation_prompts[i]}.
|
| 271 |
|
| 272 |
+
# Original content: {content[:4000]}
|
| 273 |
|
| 274 |
+
# Provide the optimized variation in JSON format:
|
| 275 |
+
# ```json
|
| 276 |
+
# {{
|
| 277 |
+
# "variation_type": "conversational/authoritative/structured",
|
| 278 |
+
# "optimized_content": "the rewritten content...",
|
| 279 |
+
# "key_changes": ["change 1", "change 2"],
|
| 280 |
+
# "target_use_case": "description of ideal use case"
|
| 281 |
+
# }}
|
| 282 |
+
# ```
|
| 283 |
+
# """
|
| 284 |
|
| 285 |
+
# prompt_template = ChatPromptTemplate.from_messages([
|
| 286 |
+
# SystemMessagePromptTemplate.from_template(custom_prompt),
|
| 287 |
+
# HumanMessagePromptTemplate.from_template("Generate the variation.")
|
| 288 |
+
# ])
|
| 289 |
+
# # ("system", custom_prompt),
|
| 290 |
+
# # ("user", "Generate the variation.")
|
| 291 |
|
| 292 |
+
# chain = prompt_template | self.llm
|
| 293 |
+
# result = chain.invoke({})
|
| 294 |
|
| 295 |
+
# result_content = result.content if hasattr(result, 'content') else str(result)
|
| 296 |
+
# parsed_result = self._parse_optimization_result(result_content)
|
| 297 |
|
| 298 |
+
# parsed_result.update({
|
| 299 |
+
# 'variation_index': i,
|
| 300 |
+
# 'variation_prompt': variation_prompts[i]
|
| 301 |
+
# })
|
| 302 |
|
| 303 |
+
# variations.append(parsed_result)
|
| 304 |
|
| 305 |
+
# except Exception as e:
|
| 306 |
+
# variations.append({
|
| 307 |
+
# 'variation_index': i,
|
| 308 |
+
# 'error': f"Variation generation failed: {str(e)}"
|
| 309 |
+
# })
|
| 310 |
|
| 311 |
+
# return variations
|
| 312 |
|
| 313 |
def analyze_content_readability(self, content: str) -> Dict[str, Any]:
|
| 314 |
"""
|
|
|
|
| 369 |
except Exception as e:
|
| 370 |
return {'error': f"Readability analysis failed: {str(e)}"}
|
| 371 |
|
| 372 |
+
# def extract_key_entities(self, content: str) -> Dict[str, Any]:
|
| 373 |
+
# """
|
| 374 |
+
# Extract key entities and topics for optimization
|
| 375 |
|
| 376 |
+
# Args:
|
| 377 |
+
# content (str): Content to analyze
|
| 378 |
+
|
| 379 |
+
# Returns:
|
| 380 |
+
# Dict: Extracted entities and topics
|
| 381 |
+
# """
|
| 382 |
+
# try:
|
| 383 |
+
# entity_prompt = """Extract key entities, topics, and concepts from this content for AI optimization.
|
| 384 |
|
| 385 |
+
# Content: {content}
|
| 386 |
|
| 387 |
+
# Identify:
|
| 388 |
+
# 1. Named entities (people, places, organizations)
|
| 389 |
+
# 2. Key concepts and topics
|
| 390 |
+
# 3. Technical terms and jargon
|
| 391 |
+
# 4. Potential semantic keywords
|
| 392 |
+
# 5. Question-answer opportunities
|
| 393 |
|
| 394 |
+
# Format as JSON:
|
| 395 |
+
# ```json
|
| 396 |
+
# {{
|
| 397 |
+
# "named_entities": ["entity1", "entity2"],
|
| 398 |
+
# "key_topics": ["topic1", "topic2"],
|
| 399 |
+
# "technical_terms": ["term1", "term2"],
|
| 400 |
+
# "semantic_keywords": ["keyword1", "keyword2"],
|
| 401 |
+
# "question_opportunities": ["What is...", "How does..."],
|
| 402 |
+
# "entity_relationships": ["relationship descriptions"]
|
| 403 |
+
# }}
|
| 404 |
+
# ```
|
| 405 |
+
# """
|
| 406 |
+
|
| 407 |
+
# prompt_template = ChatPromptTemplate.from_messages([
|
| 408 |
+
# SystemMessagePromptTemplate.from_template(entity_prompt.format(content=content[:5000])),
|
| 409 |
+
# HumanMessagePromptTemplate.from_template("Extract the entities and topics.")
|
| 410 |
+
# ])
|
| 411 |
+
# # ("system", entity_prompt.format(content=content[:5000])),
|
| 412 |
+
# # ("user", "Extract the entities and topics.")
|
| 413 |
+
|
| 414 |
+
# chain = prompt_template | self.llm
|
| 415 |
+
# result = chain.invoke({})
|
| 416 |
+
|
| 417 |
+
# result_content = result.content if hasattr(result, 'content') else str(result)
|
| 418 |
+
# return self._parse_optimization_result(result_content)
|
| 419 |
+
|
| 420 |
+
# except Exception as e:
|
| 421 |
+
# return {'error': f"Entity extraction failed: {str(e)}"}
|
| 422 |
|
| 423 |
def optimize_for_voice_search(self, content: str) -> Dict[str, Any]:
|
| 424 |
"""
|
|
|
|
| 431 |
Dict: Voice search optimization results
|
| 432 |
"""
|
| 433 |
try:
|
| 434 |
+
self.voice_prompt = ("Optimize the following content for voice search and conversational AI by improving natural language flow, question-based structure, tone, and featured snippet potential. Return JSON with improved content, Q&A pairs, snippet candidates, and a conversational score.\nContent: {content}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 437 |
SystemMessagePromptTemplate.from_template(voice_prompt.format(content=content[:4000])),
|
| 438 |
HumanMessagePromptTemplate.from_template("Optimize for voice search.")
|