MBilal-72 commited on
Commit
8662a42
·
verified ·
1 Parent(s): 501c2b9

comment extra functions and optimized prompt

Browse files
Files changed (1) hide show
  1. 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
- 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
  # 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 = "standard") -> Dict[str, Any]:
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
- 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),
@@ -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
- # ("system", self.seo_style_prompt),
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
- Optimize multiple pieces of content in batch
241
 
242
- Args:
243
- content_list (List[str]): List of content pieces to optimize
244
- optimization_type (str): Type of optimization to apply
245
-
246
- Returns:
247
- List[Dict]: List of optimization results
248
- """
249
- results = []
250
 
251
- for i, content in enumerate(content_list):
252
- try:
253
- result = self.optimize_content(
254
- content,
255
- optimization_type=optimization_type
256
- )
257
- result['batch_index'] = i
258
- results.append(result)
259
 
260
- except Exception as e:
261
- results.append({
262
- 'batch_index': i,
263
- 'error': f"Batch optimization failed: {str(e)}"
264
- })
265
 
266
- return results
267
 
268
- def generate_content_variations(self, content: str, num_variations: int = 3) -> List[Dict[str, Any]]:
269
- """
270
- Generate multiple optimized variations of the same content
271
 
272
- Args:
273
- content (str): Original content
274
- num_variations (int): Number of variations to generate
275
-
276
- Returns:
277
- List[Dict]: List of content variations with analysis
278
- """
279
- variations = []
280
 
281
- variation_prompts = [
282
- "Create a more conversational version optimized for AI chat responses",
283
- "Create a more authoritative version optimized for citations",
284
- "Create a more structured version optimized for question-answering"
285
- ]
286
 
287
- for i in range(min(num_variations, len(variation_prompts))):
288
- try:
289
- custom_prompt = f"""You are optimizing content for AI systems. {variation_prompts[i]}.
290
 
291
- Original content: {content[:4000]}
292
 
293
- Provide the optimized variation in JSON format:
294
- ```json
295
- {{
296
- "variation_type": "conversational/authoritative/structured",
297
- "optimized_content": "the rewritten content...",
298
- "key_changes": ["change 1", "change 2"],
299
- "target_use_case": "description of ideal use case"
300
- }}
301
- ```
302
- """
303
 
304
- prompt_template = ChatPromptTemplate.from_messages([
305
- SystemMessagePromptTemplate.from_template(custom_prompt),
306
- HumanMessagePromptTemplate.from_template("Generate the variation.")
307
- ])
308
- # ("system", custom_prompt),
309
- # ("user", "Generate the variation.")
310
 
311
- chain = prompt_template | self.llm
312
- result = chain.invoke({})
313
 
314
- result_content = result.content if hasattr(result, 'content') else str(result)
315
- parsed_result = self._parse_optimization_result(result_content)
316
 
317
- parsed_result.update({
318
- 'variation_index': i,
319
- 'variation_prompt': variation_prompts[i]
320
- })
321
 
322
- variations.append(parsed_result)
323
 
324
- except Exception as e:
325
- variations.append({
326
- 'variation_index': i,
327
- 'error': f"Variation generation failed: {str(e)}"
328
- })
329
 
330
- return variations
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
- Extract key entities and topics for optimization
394
 
395
- Args:
396
- content (str): Content to analyze
397
-
398
- Returns:
399
- Dict: Extracted entities and topics
400
- """
401
- try:
402
- entity_prompt = """Extract key entities, topics, and concepts from this content for AI optimization.
403
 
404
- Content: {content}
405
 
406
- Identify:
407
- 1. Named entities (people, places, organizations)
408
- 2. Key concepts and topics
409
- 3. Technical terms and jargon
410
- 4. Potential semantic keywords
411
- 5. Question-answer opportunities
412
 
413
- Format as JSON:
414
- ```json
415
- {{
416
- "named_entities": ["entity1", "entity2"],
417
- "key_topics": ["topic1", "topic2"],
418
- "technical_terms": ["term1", "term2"],
419
- "semantic_keywords": ["keyword1", "keyword2"],
420
- "question_opportunities": ["What is...", "How does..."],
421
- "entity_relationships": ["relationship descriptions"]
422
- }}
423
- ```
424
- """
425
-
426
- prompt_template = ChatPromptTemplate.from_messages([
427
- SystemMessagePromptTemplate.from_template(entity_prompt.format(content=content[:5000])),
428
- HumanMessagePromptTemplate.from_template("Extract the entities and topics.")
429
- ])
430
- # ("system", entity_prompt.format(content=content[:5000])),
431
- # ("user", "Extract the entities and topics.")
432
-
433
- chain = prompt_template | self.llm
434
- result = chain.invoke({})
435
-
436
- result_content = result.content if hasattr(result, 'content') else str(result)
437
- return self._parse_optimization_result(result_content)
438
-
439
- except Exception as e:
440
- return {'error': f"Entity extraction failed: {str(e)}"}
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
- voice_prompt = """Optimize this content for voice search and conversational AI systems.
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.")