Spaces:
Runtime error
Runtime error
Update utils/optimizer.py
Browse files- utils/optimizer.py +61 -46
utils/optimizer.py
CHANGED
|
@@ -6,7 +6,7 @@ Enhances content for better AI/LLM performance and GEO scores
|
|
| 6 |
import json
|
| 7 |
import re
|
| 8 |
from typing import Dict, Any, List, Optional
|
| 9 |
-
from langchain.prompts import ChatPromptTemplate
|
| 10 |
|
| 11 |
|
| 12 |
class ContentOptimizer:
|
|
@@ -18,7 +18,7 @@ class ContentOptimizer:
|
|
| 18 |
|
| 19 |
def setup_prompts(self):
|
| 20 |
"""Initialize optimization prompts"""
|
| 21 |
-
|
| 22 |
# Main content enhancement prompt
|
| 23 |
self.enhancement_prompt = (
|
| 24 |
"You are an AI Content Enhancement Specialist. Your purpose is to optimize user-provided text to maximize its effectiveness for large language models (LLMs) in search, question-answering, and conversational AI systems."
|
|
@@ -44,49 +44,62 @@ class ContentOptimizer:
|
|
| 44 |
"}}"
|
| 45 |
"```"
|
| 46 |
)
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# Competitive content analysis prompt
|
| 79 |
-
self.competitive_analysis_prompt =( "Compare this content against best practices for AI search optimization. Identify gaps and opportunities."
|
| 80 |
-
"Original Content: {content}"
|
| 81 |
-
"Analyze against these AI search factors:"
|
| 82 |
-
"- Entity recognition and linking"
|
| 83 |
-
"- Question coverage completeness"
|
| 84 |
-
"- Factual statement clarity"
|
| 85 |
-
"- Conversational flow"
|
| 86 |
-
"- Semantic relationship mapping"
|
| 87 |
-
|
| 88 |
-
"Provide competitive analysis in JSON format with specific recommendations."
|
| 89 |
-
)
|
| 90 |
def optimize_content(self, content: str, analyze_only: bool = False,
|
| 91 |
include_keywords: bool = True, optimization_type: str = "standard") -> Dict[str, Any]:
|
| 92 |
"""
|
|
@@ -134,9 +147,11 @@ class ContentOptimizer:
|
|
| 134 |
|
| 135 |
# Create and run chain
|
| 136 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 137 |
-
|
| 138 |
-
(
|
| 139 |
])
|
|
|
|
|
|
|
| 140 |
|
| 141 |
chain = prompt_template | self.llm
|
| 142 |
result = chain.invoke({})
|
|
|
|
| 6 |
import json
|
| 7 |
import re
|
| 8 |
from typing import Dict, Any, List, Optional
|
| 9 |
+
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 10 |
|
| 11 |
|
| 12 |
class ContentOptimizer:
|
|
|
|
| 18 |
|
| 19 |
def setup_prompts(self):
|
| 20 |
"""Initialize optimization prompts"""
|
| 21 |
+
|
| 22 |
# Main content enhancement prompt
|
| 23 |
self.enhancement_prompt = (
|
| 24 |
"You are an AI Content Enhancement Specialist. Your purpose is to optimize user-provided text to maximize its effectiveness for large language models (LLMs) in search, question-answering, and conversational AI systems."
|
|
|
|
| 44 |
"}}"
|
| 45 |
"```"
|
| 46 |
)
|
| 47 |
+
|
| 48 |
+
# SEO-style optimization prompt
|
| 49 |
+
self.seo_style_prompt = (
|
| 50 |
+
"You are an AI-first SEO specialist. Optimize this content for AI search engines and LLM systems. "
|
| 51 |
+
"Focus on:\n"
|
| 52 |
+
"1. Semantic keyword optimization\n"
|
| 53 |
+
"2. Question-answer format enhancement\n"
|
| 54 |
+
"3. Factual accuracy and authority signals\n"
|
| 55 |
+
"4. Conversational readiness\n"
|
| 56 |
+
"5. Citation-worthy structure\n"
|
| 57 |
+
"Provide analysis and optimization in JSON:\n"
|
| 58 |
+
"```json\n"
|
| 59 |
+
"{{\n"
|
| 60 |
+
" \"seo_analysis\": {{\n"
|
| 61 |
+
" \"keyword_density\": \"analysis of current keywords\",\n"
|
| 62 |
+
" \"semantic_gaps\": [\"missing semantic terms\"],\n"
|
| 63 |
+
" \"readability_score\": 8.5,\n"
|
| 64 |
+
" \"authority_signals\": [\"credentials\", \"citations\"]\n"
|
| 65 |
+
" }},\n"
|
| 66 |
+
" \"optimized_content\": {{\n"
|
| 67 |
+
" \"title_suggestions\": [\"optimized title 1\", \"optimized title 2\"],\n"
|
| 68 |
+
" \"meta_description\": \"AI-optimized meta description\",\n"
|
| 69 |
+
" \"enhanced_content\": \"full optimized content...\",\n"
|
| 70 |
+
" \"structured_data_suggestions\": [\"schema markup recommendations\"]\n"
|
| 71 |
+
" }},\n"
|
| 72 |
+
" \"improvement_summary\": {{\n"
|
| 73 |
+
" \"changes_made\": [\"change 1\", \"change 2\"],\n"
|
| 74 |
+
" \"expected_impact\": \"description of expected improvements\"\n"
|
| 75 |
+
" }}\n"
|
| 76 |
+
"}}\n"
|
| 77 |
+
"```"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
# Competitive content analysis prompt
|
| 81 |
+
self.competitive_analysis_prompt = (
|
| 82 |
+
"Compare this content against best practices for AI search optimization. Identify gaps and opportunities.\n"
|
| 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 |
def optimize_content(self, content: str, analyze_only: bool = False,
|
| 104 |
include_keywords: bool = True, optimization_type: str = "standard") -> Dict[str, Any]:
|
| 105 |
"""
|
|
|
|
| 147 |
|
| 148 |
# Create and run chain
|
| 149 |
prompt_template = ChatPromptTemplate.from_messages([
|
| 150 |
+
SystemMessagePromptTemplate.from_template(prompt_text),
|
| 151 |
+
HumanMessagePromptTemplate.from_template(content[:6000]) # Limit content length
|
| 152 |
])
|
| 153 |
+
# ("system", prompt_text),
|
| 154 |
+
# ("user", content[:6000]) # Limit content length
|
| 155 |
|
| 156 |
chain = prompt_template | self.llm
|
| 157 |
result = chain.invoke({})
|