Spaces:
Runtime error
Runtime error
| """ | |
| 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) | |
| 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)} | |
| def extract_optimized_content(response: Any) -> str: | |
| """Extract optimized content from LLM response.""" | |
| if isinstance(response, str): | |
| return response.strip() | |
| return str(response) | |