Stage 4
This commit is contained in:
@@ -6,8 +6,9 @@ from typing import Dict, List, Any
|
||||
from django.db import transaction
|
||||
from igny8_core.ai.base import BaseAIFunction
|
||||
from igny8_core.modules.planner.models import Keywords, Clusters
|
||||
from igny8_core.modules.system.utils import get_prompt_value
|
||||
from igny8_core.ai.ai_core import AICore
|
||||
from igny8_core.ai.prompts import PromptRegistry
|
||||
from igny8_core.ai.settings import get_model_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -90,20 +91,18 @@ class AutoClusterFunction(BaseAIFunction):
|
||||
}
|
||||
|
||||
def build_prompt(self, data: Dict, account=None) -> str:
|
||||
"""Build clustering prompt"""
|
||||
"""Build clustering prompt using registry"""
|
||||
keyword_data = data['keyword_data']
|
||||
sector_id = data.get('sector_id')
|
||||
|
||||
# Get prompt template
|
||||
prompt_template = get_prompt_value(account, 'clustering')
|
||||
|
||||
# Format keywords
|
||||
keywords_text = '\n'.join([
|
||||
f"- {kw['keyword']} (Volume: {kw['volume']}, Difficulty: {kw['difficulty']}, Intent: {kw['intent']})"
|
||||
for kw in keyword_data
|
||||
])
|
||||
|
||||
prompt = prompt_template.replace('[IGNY8_KEYWORDS]', keywords_text)
|
||||
# Build context
|
||||
context = {'KEYWORDS': keywords_text}
|
||||
|
||||
# Add sector context if available
|
||||
if sector_id:
|
||||
@@ -111,14 +110,20 @@ class AutoClusterFunction(BaseAIFunction):
|
||||
from igny8_core.auth.models import Sector
|
||||
sector = Sector.objects.get(id=sector_id)
|
||||
if sector:
|
||||
prompt += f"\n\nNote: These keywords are for the '{sector.name}' sector."
|
||||
context['SECTOR'] = sector.name
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Get prompt from registry
|
||||
prompt = PromptRegistry.get_prompt(
|
||||
function_name='auto_cluster',
|
||||
account=account,
|
||||
context=context
|
||||
)
|
||||
|
||||
# IMPORTANT: When using JSON mode, OpenAI requires explicit JSON instruction
|
||||
# The prompt template already includes "Format the output as a JSON object"
|
||||
# but we need to ensure it's explicit for JSON mode compliance
|
||||
# Check if prompt already explicitly requests JSON (case-insensitive)
|
||||
prompt_lower = prompt.lower()
|
||||
has_json_request = (
|
||||
'json' in prompt_lower and
|
||||
|
||||
@@ -8,9 +8,10 @@ from typing import Dict, List, Any
|
||||
from django.db import transaction
|
||||
from igny8_core.ai.base import BaseAIFunction
|
||||
from igny8_core.modules.writer.models import Tasks
|
||||
from igny8_core.modules.system.utils import get_prompt_value, get_default_prompt
|
||||
from igny8_core.ai.ai_core import AICore
|
||||
from igny8_core.ai.validators import validate_tasks_exist
|
||||
from igny8_core.ai.prompts import PromptRegistry
|
||||
from igny8_core.ai.settings import get_model_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -72,7 +73,7 @@ class GenerateContentFunction(BaseAIFunction):
|
||||
return tasks
|
||||
|
||||
def build_prompt(self, data: Any, account=None) -> str:
|
||||
"""Build content generation prompt for a single task"""
|
||||
"""Build content generation prompt for a single task using registry"""
|
||||
if isinstance(data, list):
|
||||
# For now, handle single task (will be called per task)
|
||||
if not data:
|
||||
@@ -81,10 +82,7 @@ class GenerateContentFunction(BaseAIFunction):
|
||||
else:
|
||||
task = data
|
||||
|
||||
# Get prompt template
|
||||
prompt_template = get_prompt_value(account or task.account, 'content_generation')
|
||||
if not prompt_template:
|
||||
prompt_template = get_default_prompt('content_generation')
|
||||
account = account or task.account
|
||||
|
||||
# Build idea data string
|
||||
idea_data = f"Title: {task.title or 'Untitled'}\n"
|
||||
@@ -132,10 +130,17 @@ class GenerateContentFunction(BaseAIFunction):
|
||||
if not keywords_data and task.idea:
|
||||
keywords_data = task.idea.target_keywords or ''
|
||||
|
||||
# Replace placeholders
|
||||
prompt = prompt_template.replace('[IGNY8_IDEA]', idea_data)
|
||||
prompt = prompt.replace('[IGNY8_CLUSTER]', cluster_data)
|
||||
prompt = prompt.replace('[IGNY8_KEYWORDS]', keywords_data)
|
||||
# Get prompt from registry with context
|
||||
prompt = PromptRegistry.get_prompt(
|
||||
function_name='generate_content',
|
||||
account=account,
|
||||
task=task,
|
||||
context={
|
||||
'IDEA': idea_data,
|
||||
'CLUSTER': cluster_data,
|
||||
'KEYWORDS': keywords_data,
|
||||
}
|
||||
)
|
||||
|
||||
return prompt
|
||||
|
||||
@@ -228,11 +233,17 @@ def generate_content_core(task_ids: List[int], account_id: int = None, progress_
|
||||
# Build prompt for this task
|
||||
prompt = fn.build_prompt([task], account)
|
||||
|
||||
# Get model config from settings
|
||||
model_config = get_model_config('generate_content')
|
||||
|
||||
# Call AI using centralized request handler
|
||||
ai_core = AICore(account=account)
|
||||
result = ai_core.run_ai_request(
|
||||
prompt=prompt,
|
||||
max_tokens=4000,
|
||||
model=model_config.get('model'),
|
||||
max_tokens=model_config.get('max_tokens'),
|
||||
temperature=model_config.get('temperature'),
|
||||
response_format=model_config.get('response_format'),
|
||||
function_name='generate_content'
|
||||
)
|
||||
|
||||
|
||||
@@ -8,10 +8,11 @@ from typing import Dict, List, Any
|
||||
from django.db import transaction
|
||||
from igny8_core.ai.base import BaseAIFunction
|
||||
from igny8_core.modules.planner.models import Clusters, ContentIdeas
|
||||
from igny8_core.modules.system.utils import get_prompt_value
|
||||
from igny8_core.ai.ai_core import AICore
|
||||
from igny8_core.ai.validators import validate_cluster_exists, validate_cluster_limits
|
||||
from igny8_core.ai.tracker import ConsoleStepTracker
|
||||
from igny8_core.ai.prompts import PromptRegistry
|
||||
from igny8_core.ai.settings import get_model_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -96,11 +97,9 @@ class GenerateIdeasFunction(BaseAIFunction):
|
||||
}
|
||||
|
||||
def build_prompt(self, data: Dict, account=None) -> str:
|
||||
"""Build ideas generation prompt"""
|
||||
"""Build ideas generation prompt using registry"""
|
||||
cluster_data = data['cluster_data']
|
||||
|
||||
# Get prompt template
|
||||
prompt_template = get_prompt_value(account or data.get('account'), 'ideas')
|
||||
account = account or data.get('account')
|
||||
|
||||
# Format clusters text
|
||||
clusters_text = '\n'.join([
|
||||
@@ -114,9 +113,15 @@ class GenerateIdeasFunction(BaseAIFunction):
|
||||
for c in cluster_data
|
||||
])
|
||||
|
||||
# Replace placeholders
|
||||
prompt = prompt_template.replace('[IGNY8_CLUSTERS]', clusters_text)
|
||||
prompt = prompt.replace('[IGNY8_CLUSTER_KEYWORDS]', cluster_keywords_text)
|
||||
# Get prompt from registry with context
|
||||
prompt = PromptRegistry.get_prompt(
|
||||
function_name='generate_ideas',
|
||||
account=account,
|
||||
context={
|
||||
'CLUSTERS': clusters_text,
|
||||
'CLUSTER_KEYWORDS': cluster_keywords_text,
|
||||
}
|
||||
)
|
||||
|
||||
return prompt
|
||||
|
||||
@@ -231,11 +236,17 @@ def generate_ideas_core(cluster_id: int, account_id: int = None, progress_callba
|
||||
tracker.prep("Building prompt...")
|
||||
prompt = fn.build_prompt(data, account)
|
||||
|
||||
# Get model config from settings
|
||||
model_config = get_model_config('generate_ideas')
|
||||
|
||||
# Call AI using centralized request handler
|
||||
ai_core = AICore(account=account)
|
||||
result = ai_core.run_ai_request(
|
||||
prompt=prompt,
|
||||
max_tokens=4000,
|
||||
model=model_config.get('model'),
|
||||
max_tokens=model_config.get('max_tokens'),
|
||||
temperature=model_config.get('temperature'),
|
||||
response_format=model_config.get('response_format'),
|
||||
function_name='generate_ideas',
|
||||
tracker=tracker
|
||||
)
|
||||
|
||||
@@ -7,9 +7,10 @@ from typing import Dict, List, Any
|
||||
from django.db import transaction
|
||||
from igny8_core.ai.base import BaseAIFunction
|
||||
from igny8_core.modules.writer.models import Tasks, Images
|
||||
from igny8_core.modules.system.utils import get_prompt_value, get_default_prompt
|
||||
from igny8_core.ai.ai_core import AICore
|
||||
from igny8_core.ai.validators import validate_tasks_exist
|
||||
from igny8_core.ai.prompts import PromptRegistry
|
||||
from igny8_core.ai.settings import get_model_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -107,23 +108,29 @@ class GenerateImagesFunction(BaseAIFunction):
|
||||
|
||||
# Use AI to extract image prompts
|
||||
ai_core = AICore(account=account or data.get('account'))
|
||||
account_obj = account or data.get('account')
|
||||
|
||||
# Get prompt template
|
||||
prompt_template = get_prompt_value(account or data.get('account'), 'image_prompt_extraction')
|
||||
if not prompt_template:
|
||||
prompt_template = get_default_prompt('image_prompt_extraction')
|
||||
|
||||
# Format prompt
|
||||
prompt = prompt_template.format(
|
||||
title=task.title,
|
||||
content=task.content[:5000], # Limit content length
|
||||
max_images=max_images
|
||||
# Get prompt from registry
|
||||
prompt = PromptRegistry.get_prompt(
|
||||
function_name='extract_image_prompts',
|
||||
account=account_obj,
|
||||
context={
|
||||
'title': task.title,
|
||||
'content': task.content[:5000], # Limit content length
|
||||
'max_images': max_images
|
||||
}
|
||||
)
|
||||
|
||||
# Get model config
|
||||
model_config = get_model_config('extract_image_prompts')
|
||||
|
||||
# Call AI to extract prompts using centralized request handler
|
||||
result = ai_core.run_ai_request(
|
||||
prompt=prompt,
|
||||
max_tokens=1000,
|
||||
model=model_config.get('model'),
|
||||
max_tokens=model_config.get('max_tokens'),
|
||||
temperature=model_config.get('temperature'),
|
||||
response_format=model_config.get('response_format'),
|
||||
function_name='extract_image_prompts'
|
||||
)
|
||||
|
||||
@@ -214,14 +221,9 @@ def generate_images_core(task_ids: List[int], account_id: int = None, progress_c
|
||||
data = fn.prepare(payload, account)
|
||||
tasks = data['tasks']
|
||||
|
||||
# Get prompts
|
||||
image_prompt_template = get_prompt_value(account, 'image_prompt_template')
|
||||
if not image_prompt_template:
|
||||
image_prompt_template = get_default_prompt('image_prompt_template')
|
||||
|
||||
negative_prompt = get_prompt_value(account, 'negative_prompt')
|
||||
if not negative_prompt:
|
||||
negative_prompt = get_default_prompt('negative_prompt')
|
||||
# Get prompts from registry
|
||||
image_prompt_template = PromptRegistry.get_image_prompt_template(account)
|
||||
negative_prompt = PromptRegistry.get_negative_prompt(account)
|
||||
|
||||
ai_core = AICore(account=account)
|
||||
images_created = 0
|
||||
|
||||
Reference in New Issue
Block a user