Spaces:
Runtime error
Runtime error
Update utils/optimizer.py
Browse files- utils/optimizer.py +113 -96
utils/optimizer.py
CHANGED
|
@@ -100,6 +100,18 @@ class ContentOptimizer:
|
|
| 100 |
"}}\n"
|
| 101 |
)
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
def optimize_content(self, content: str, analyze_only: bool = False,
|
| 104 |
include_keywords: bool = True, optimization_type: str = "seo") -> Dict[str, Any]:
|
| 105 |
"""
|
|
@@ -127,10 +139,8 @@ class ContentOptimizer:
|
|
| 127 |
def _standard_optimization(self, content: str, analyze_only: bool, include_keywords: bool) -> Dict[str, Any]:
|
| 128 |
"""Standard content optimization using enhancement prompt"""
|
| 129 |
try:
|
| 130 |
-
# Modify prompt based on options
|
| 131 |
-
prompt_text = self.enhancement_prompt
|
| 132 |
-
|
| 133 |
if analyze_only:
|
|
|
|
| 134 |
prompt_text = prompt_text.replace(
|
| 135 |
"Rewrite the text to improve:",
|
| 136 |
"Analyze the text for potential improvements in:"
|
|
@@ -138,48 +148,44 @@ class ContentOptimizer:
|
|
| 138 |
'"optimized_text": "..."',
|
| 139 |
'"optimization_suggestions": ["suggestion 1", "suggestion 2"]'
|
| 140 |
)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
])
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
return parsed_result
|
| 172 |
-
|
| 173 |
-
except Exception as e:
|
| 174 |
-
return {'error': f"Standard optimization failed: {str(e)}"}
|
| 175 |
-
|
| 176 |
def _seo_style_optimization(self, content: str, analyze_only: bool) -> Dict[str, Any]:
|
| 177 |
"""SEO-focused optimization for AI search engines"""
|
| 178 |
try:
|
| 179 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 180 |
-
|
| 181 |
-
|
| 182 |
])
|
|
|
|
|
|
|
| 183 |
|
| 184 |
chain = prompt_template | self.llm
|
| 185 |
result = chain.invoke({})
|
|
@@ -205,9 +211,11 @@ class ContentOptimizer:
|
|
| 205 |
formatted_prompt = self.competitive_analysis_prompt.format(content=content[:5000])
|
| 206 |
|
| 207 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 208 |
-
|
| 209 |
-
|
| 210 |
])
|
|
|
|
|
|
|
| 211 |
|
| 212 |
chain = prompt_template | self.llm
|
| 213 |
result = chain.invoke({})
|
|
@@ -278,22 +286,25 @@ class ContentOptimizer:
|
|
| 278 |
try:
|
| 279 |
custom_prompt = f"""You are optimizing content for AI systems. {variation_prompts[i]}.
|
| 280 |
|
| 281 |
-
Original content: {content[:4000]}
|
| 282 |
|
| 283 |
-
Provide the optimized variation in JSON format:
|
| 284 |
-
```json
|
| 285 |
-
{{
|
| 286 |
-
"variation_type": "conversational/authoritative/structured",
|
| 287 |
-
"optimized_content": "the rewritten content...",
|
| 288 |
-
"key_changes": ["change 1", "change 2"],
|
| 289 |
-
"target_use_case": "description of ideal use case"
|
| 290 |
-
}}
|
| 291 |
-
```
|
|
|
|
| 292 |
|
| 293 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 294 |
-
(
|
| 295 |
-
("
|
| 296 |
])
|
|
|
|
|
|
|
| 297 |
|
| 298 |
chain = prompt_template | self.llm
|
| 299 |
result = chain.invoke({})
|
|
@@ -388,31 +399,34 @@ Provide the optimized variation in JSON format:
|
|
| 388 |
try:
|
| 389 |
entity_prompt = """Extract key entities, topics, and concepts from this content for AI optimization.
|
| 390 |
|
| 391 |
-
Content: {content}
|
| 392 |
|
| 393 |
-
Identify:
|
| 394 |
-
1. Named entities (people, places, organizations)
|
| 395 |
-
2. Key concepts and topics
|
| 396 |
-
3. Technical terms and jargon
|
| 397 |
-
4. Potential semantic keywords
|
| 398 |
-
5. Question-answer opportunities
|
| 399 |
|
| 400 |
-
Format as JSON:
|
| 401 |
-
```json
|
| 402 |
-
{{
|
| 403 |
-
"named_entities": ["entity1", "entity2"],
|
| 404 |
-
"key_topics": ["topic1", "topic2"],
|
| 405 |
-
"technical_terms": ["term1", "term2"],
|
| 406 |
-
"semantic_keywords": ["keyword1", "keyword2"],
|
| 407 |
-
"question_opportunities": ["What is...", "How does..."],
|
| 408 |
-
"entity_relationships": ["relationship descriptions"]
|
| 409 |
-
}}
|
| 410 |
-
```
|
|
|
|
| 411 |
|
| 412 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 413 |
-
(
|
| 414 |
-
("
|
| 415 |
])
|
|
|
|
|
|
|
| 416 |
|
| 417 |
chain = prompt_template | self.llm
|
| 418 |
result = chain.invoke({})
|
|
@@ -436,33 +450,36 @@ Format as JSON:
|
|
| 436 |
try:
|
| 437 |
voice_prompt = """Optimize this content for voice search and conversational AI systems.
|
| 438 |
|
| 439 |
-
Focus on:
|
| 440 |
-
1. Natural language patterns
|
| 441 |
-
2. Question-based structure
|
| 442 |
-
3. Conversational tone
|
| 443 |
-
4. Clear, direct answers
|
| 444 |
-
5. Featured snippet optimization
|
| 445 |
|
| 446 |
-
Original content: {content}
|
| 447 |
|
| 448 |
-
Provide optimization in JSON:
|
| 449 |
-
```json
|
| 450 |
-
{{
|
| 451 |
-
"voice_optimized_content": "conversational version...",
|
| 452 |
-
"question_answer_pairs": [
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
],
|
| 456 |
-
"featured_snippet_candidates": ["snippet 1", "snippet 2"],
|
| 457 |
-
"natural_language_improvements": ["improvement 1", "improvement 2"],
|
| 458 |
-
"conversational_score": 8.5
|
| 459 |
-
}}
|
| 460 |
-
```
|
|
|
|
| 461 |
|
| 462 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 463 |
-
(
|
| 464 |
-
("
|
| 465 |
])
|
|
|
|
|
|
|
| 466 |
|
| 467 |
chain = prompt_template | self.llm
|
| 468 |
result = chain.invoke({})
|
|
|
|
| 100 |
"}}\n"
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Dedicated prompt for rewriting/optimizing content
|
| 104 |
+
self.optimization_rewrite_prompt = (
|
| 105 |
+
"You are an expert AI content optimizer. Rewrite the provided text to maximize clarity, logical structure, and suitability for LLM-based search and conversational AI. "
|
| 106 |
+
"Your rewritten version should be more precise, well-organized, and easier for AI systems to extract answers from. "
|
| 107 |
+
"Return your output in the following JSON format:\n"
|
| 108 |
+
"```json\n"
|
| 109 |
+
"{{\n"
|
| 110 |
+
" \"optimized_text\": \"...your rewritten content here...\"\n"
|
| 111 |
+
"}}\n"
|
| 112 |
+
"```"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
def optimize_content(self, content: str, analyze_only: bool = False,
|
| 116 |
include_keywords: bool = True, optimization_type: str = "seo") -> Dict[str, Any]:
|
| 117 |
"""
|
|
|
|
| 139 |
def _standard_optimization(self, content: str, analyze_only: bool, include_keywords: bool) -> Dict[str, Any]:
|
| 140 |
"""Standard content optimization using enhancement prompt"""
|
| 141 |
try:
|
|
|
|
|
|
|
|
|
|
| 142 |
if analyze_only:
|
| 143 |
+
prompt_text = self.enhancement_prompt
|
| 144 |
prompt_text = prompt_text.replace(
|
| 145 |
"Rewrite the text to improve:",
|
| 146 |
"Analyze the text for potential improvements in:"
|
|
|
|
| 148 |
'"optimized_text": "..."',
|
| 149 |
'"optimization_suggestions": ["suggestion 1", "suggestion 2"]'
|
| 150 |
)
|
| 151 |
+
if not include_keywords:
|
| 152 |
+
prompt_text = prompt_text.replace(
|
| 153 |
+
'"keywords": ["example", "installation", "setup"],',
|
| 154 |
+
''
|
| 155 |
+
)
|
| 156 |
+
else:
|
| 157 |
+
# Use dedicated rewrite prompt for optimization
|
| 158 |
+
prompt_text = self.optimization_rewrite_prompt
|
| 159 |
+
|
| 160 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 161 |
+
SystemMessagePromptTemplate.from_template(prompt_text),
|
| 162 |
+
HumanMessagePromptTemplate.from_template(content[:6000])
|
| 163 |
+
])
|
| 164 |
+
|
| 165 |
+
chain = prompt_template | self.llm
|
| 166 |
+
result = chain.invoke({})
|
| 167 |
+
|
| 168 |
+
result_content = result.content if hasattr(result, 'content') else str(result)
|
| 169 |
+
parsed_result = self._parse_optimization_result(result_content)
|
| 170 |
+
|
| 171 |
+
parsed_result.update({
|
| 172 |
+
'optimization_type': 'standard',
|
| 173 |
+
'analyze_only': analyze_only,
|
| 174 |
+
'original_length': len(content),
|
| 175 |
+
'original_word_count': len(content.split())
|
| 176 |
+
})
|
| 177 |
+
|
| 178 |
+
return parsed_result
|
| 179 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
def _seo_style_optimization(self, content: str, analyze_only: bool) -> Dict[str, Any]:
|
| 181 |
"""SEO-focused optimization for AI search engines"""
|
| 182 |
try:
|
| 183 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 184 |
+
SystemMessagePromptTemplate.from_template(self.seo_style_prompt),
|
| 185 |
+
HumanMessagePromptTemplate.from_template(f"Optimize this content for AI search engines:\n\n{content[:6000]}")
|
| 186 |
])
|
| 187 |
+
# ("system", self.seo_style_prompt),
|
| 188 |
+
# ("user", f"Optimize this content for AI search engines:\n\n{content[:6000]}")
|
| 189 |
|
| 190 |
chain = prompt_template | self.llm
|
| 191 |
result = chain.invoke({})
|
|
|
|
| 211 |
formatted_prompt = self.competitive_analysis_prompt.format(content=content[:5000])
|
| 212 |
|
| 213 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 214 |
+
SystemMessagePromptTemplate.from_template(formatted_prompt),
|
| 215 |
+
HumanMessagePromptTemplate.from_template("Perform the competitive analysis and provide optimization recommendations.")
|
| 216 |
])
|
| 217 |
+
# ("system", formatted_prompt),
|
| 218 |
+
# ("user", "Perform the competitive analysis and provide optimization recommendations.")
|
| 219 |
|
| 220 |
chain = prompt_template | self.llm
|
| 221 |
result = chain.invoke({})
|
|
|
|
| 286 |
try:
|
| 287 |
custom_prompt = f"""You are optimizing content for AI systems. {variation_prompts[i]}.
|
| 288 |
|
| 289 |
+
Original content: {content[:4000]}
|
| 290 |
|
| 291 |
+
Provide the optimized variation in JSON format:
|
| 292 |
+
```json
|
| 293 |
+
{{
|
| 294 |
+
"variation_type": "conversational/authoritative/structured",
|
| 295 |
+
"optimized_content": "the rewritten content...",
|
| 296 |
+
"key_changes": ["change 1", "change 2"],
|
| 297 |
+
"target_use_case": "description of ideal use case"
|
| 298 |
+
}}
|
| 299 |
+
```
|
| 300 |
+
"""
|
| 301 |
|
| 302 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 303 |
+
SystemMessagePromptTemplate.from_template(custom_prompt),
|
| 304 |
+
HumanMessagePromptTemplate.from_template("Generate the variation.")
|
| 305 |
])
|
| 306 |
+
# ("system", custom_prompt),
|
| 307 |
+
# ("user", "Generate the variation.")
|
| 308 |
|
| 309 |
chain = prompt_template | self.llm
|
| 310 |
result = chain.invoke({})
|
|
|
|
| 399 |
try:
|
| 400 |
entity_prompt = """Extract key entities, topics, and concepts from this content for AI optimization.
|
| 401 |
|
| 402 |
+
Content: {content}
|
| 403 |
|
| 404 |
+
Identify:
|
| 405 |
+
1. Named entities (people, places, organizations)
|
| 406 |
+
2. Key concepts and topics
|
| 407 |
+
3. Technical terms and jargon
|
| 408 |
+
4. Potential semantic keywords
|
| 409 |
+
5. Question-answer opportunities
|
| 410 |
|
| 411 |
+
Format as JSON:
|
| 412 |
+
```json
|
| 413 |
+
{{
|
| 414 |
+
"named_entities": ["entity1", "entity2"],
|
| 415 |
+
"key_topics": ["topic1", "topic2"],
|
| 416 |
+
"technical_terms": ["term1", "term2"],
|
| 417 |
+
"semantic_keywords": ["keyword1", "keyword2"],
|
| 418 |
+
"question_opportunities": ["What is...", "How does..."],
|
| 419 |
+
"entity_relationships": ["relationship descriptions"]
|
| 420 |
+
}}
|
| 421 |
+
```
|
| 422 |
+
"""
|
| 423 |
|
| 424 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 425 |
+
SystemMessagePromptTemplate.from_template(entity_prompt.format(content=content[:5000])),
|
| 426 |
+
HumanMessagePromptTemplate.from_template("Extract the entities and topics.")
|
| 427 |
])
|
| 428 |
+
# ("system", entity_prompt.format(content=content[:5000])),
|
| 429 |
+
# ("user", "Extract the entities and topics.")
|
| 430 |
|
| 431 |
chain = prompt_template | self.llm
|
| 432 |
result = chain.invoke({})
|
|
|
|
| 450 |
try:
|
| 451 |
voice_prompt = """Optimize this content for voice search and conversational AI systems.
|
| 452 |
|
| 453 |
+
Focus on:
|
| 454 |
+
1. Natural language patterns
|
| 455 |
+
2. Question-based structure
|
| 456 |
+
3. Conversational tone
|
| 457 |
+
4. Clear, direct answers
|
| 458 |
+
5. Featured snippet optimization
|
| 459 |
|
| 460 |
+
Original content: {content}
|
| 461 |
|
| 462 |
+
Provide optimization in JSON:
|
| 463 |
+
```json
|
| 464 |
+
{{
|
| 465 |
+
"voice_optimized_content": "conversational version...",
|
| 466 |
+
"question_answer_pairs": [
|
| 467 |
+
{{"question": "What is...", "answer": "Direct answer..."}},
|
| 468 |
+
{{"question": "How does...", "answer": "Step by step..."}}
|
| 469 |
+
],
|
| 470 |
+
"featured_snippet_candidates": ["snippet 1", "snippet 2"],
|
| 471 |
+
"natural_language_improvements": ["improvement 1", "improvement 2"],
|
| 472 |
+
"conversational_score": 8.5
|
| 473 |
+
}}
|
| 474 |
+
```
|
| 475 |
+
"""
|
| 476 |
|
| 477 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 478 |
+
SystemMessagePromptTemplate.from_template(voice_prompt.format(content=content[:4000])),
|
| 479 |
+
HumanMessagePromptTemplate.from_template("Optimize for voice search.")
|
| 480 |
])
|
| 481 |
+
# ("system", voice_prompt.format(content=content[:4000])),
|
| 482 |
+
# ("user", "Optimize for voice search.")
|
| 483 |
|
| 484 |
chain = prompt_template | self.llm
|
| 485 |
result = chain.invoke({})
|