Spaces:
Runtime error
Runtime error
File size: 3,205 Bytes
4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 20eb62a 73f33e3 20eb62a 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 4a25546 73f33e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 79 |
"""
Content Optimization Module
Enhances content for better performance in AI/LLM systems and GEO scoring.
"""
import json
import re
from typing import Dict, Any, List, Optional
from langchain.prompts import ChatPromptTemplate
class ContentOptimizer:
"""Main class for optimizing content for AI and GEO performance."""
def __init__(self, llm):
self.llm = llm
self.setup_prompts()
def setup_prompts(self):
"""Initialize system prompts for GEO evaluation and content optimization."""
self.geo_analysis_prompt = ChatPromptTemplate.from_template("""
You are a Generative Engine Optimization (GEO) specialist. Analyze the provided content for effectiveness in AI-powered search and LLM systems.
Evaluate the content on a scale of 1–10 for each criterion:
1. **AI Search Visibility**: How likely is this content to appear in AI-generated responses?
2. **Query Intent Matching**: Does it clearly match common user queries?
3. **Clarity**: Is the language clear, direct, and easy to understand?
4. **Structuredness**: How logically organized and well-formatted is the content?
5. **Completeness**: Does it fully answer potential questions?
6. **Use of Keywords**: Are relevant keywords included naturally?
7. **Trust & Factuality**: Is the content accurate, trustworthy, and verifiable?
8. **Engagement**: Does the content encourage interaction or further exploration?
9. **Readability**: Is the tone appropriate for the intended audience?
10. **LLM Friendliness**: How well can LLMs use this content for QA or generation?
Return scores, reasoning, and improvement suggestions.
""")
self.optimization_prompt = ChatPromptTemplate.from_template("""
You are an AI Content Enhancement Specialist. Improve the following content to maximize effectiveness in LLMs and AI search engines. Ensure clarity, keyword relevance, structured formatting, and natural language.
Original Content:
-----------------
{content}
Optimized Output (Clear, Structured, and LLM-Friendly):
-----------------
""")
def analyze_geo(self, content: str) -> Dict[str, Any]:
"""Analyze GEO performance of given content."""
prompt = self.geo_analysis_prompt.format_messages(content=content)
response = self.llm(prompt)
return self.parse_response(response)
def optimize_content(self, content: str) -> str:
"""Return optimized version of input content."""
prompt = self.optimization_prompt.format_messages(content=content)
response = self.llm(prompt)
return self.extract_optimized_content(response)
@staticmethod
def parse_response(response: Any) -> Dict[str, Any]:
"""Parse structured response from LLM."""
try:
if isinstance(response, str):
return json.loads(response)
return response
except json.JSONDecodeError:
return {"error": "Invalid JSON response from LLM", "raw": str(response)}
@staticmethod
def extract_optimized_content(response: Any) -> str:
"""Extract optimized content from LLM response."""
if isinstance(response, str):
return response.strip()
return str(response)
|