130 lines
5.5 KiB
Python
130 lines
5.5 KiB
Python
"""
|
|
Task Planning Prompt Generation
|
|
Handles prompt templates and extraction functions for task planning phase.
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
from typing import Dict, Any, List
|
|
from modules.datamodels.datamodelChat import PromptBundle, PromptPlaceholder
|
|
from modules.workflows.processing.shared.placeholderFactory import (
|
|
extractUserPrompt,
|
|
extractAvailableDocumentsSummary,
|
|
extractWorkflowHistory,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def generateTaskPlanningPrompt(services, context: Any) -> PromptBundle:
|
|
"""Define placeholders first, then the template; return PromptBundle."""
|
|
placeholders: List[PromptPlaceholder] = [
|
|
PromptPlaceholder(label="USER_PROMPT", content=extractUserPrompt(context), summaryAllowed=False),
|
|
PromptPlaceholder(label="AVAILABLE_DOCUMENTS_SUMMARY", content=extractAvailableDocumentsSummary(services, context), summaryAllowed=True),
|
|
PromptPlaceholder(label="WORKFLOW_HISTORY", content=extractWorkflowHistory(services, context), summaryAllowed=True),
|
|
]
|
|
|
|
template = """# Task Planning
|
|
|
|
Break down user requests into logical, executable task steps.
|
|
|
|
**IMPORTANT**: If the user asks for ONE complete business objective, create ONLY ONE task that accomplishes the entire objective. Do NOT split it into multiple micro-tasks.
|
|
|
|
## 📋 Context
|
|
|
|
### User Request
|
|
{{KEY:USER_PROMPT}}
|
|
|
|
### Available Documents
|
|
{{KEY:AVAILABLE_DOCUMENTS_SUMMARY}}
|
|
|
|
### Previous Workflow Rounds
|
|
{{KEY:WORKFLOW_HISTORY}}
|
|
|
|
## 📝 Task Planning Rules
|
|
|
|
### Strategic Task Grouping
|
|
- **GROUP RELATED ACTIONS** - Combine all actions for the same business topic into ONE task
|
|
- **ONE TOPIC PER TASK** - Each task should handle one complete business objective
|
|
- **HIGH-LEVEL FOCUS** - Plan strategic outcomes, not implementation steps
|
|
- **AVOID MICRO-TASKS** - Don't create separate tasks for each small action
|
|
- **CRITICAL**: If the user asks for ONE thing (like "analyse document list and produce summary"), create ONLY ONE task that does the complete job
|
|
|
|
### Task Grouping Examples
|
|
- **Research + Analysis + Report** → ONE task: "Web research report"
|
|
- **Data Collection + Processing + Visualization** → ONE task: "Collect and present data"
|
|
- **Document splitting** (analyze + extract + create files) → ONE task: "Split document into separate files"
|
|
- **Different topics** (email + flowers) → SEPARATE tasks: "Send formal email..." + "Order flowers from Fleurop for delivery to 123 Main St, include card message"
|
|
|
|
### Common Single-Task Scenarios
|
|
- **"Split document into sections"** → ONE task: "Split document into separate files"
|
|
- **"Extract data and create report"** → ONE task: "Extract data and create report"
|
|
- **"Analyze and summarize document"** → ONE task: "Analyze and summarize document"
|
|
- **"Convert file to different format"** → ONE task: "Convert file to different format"
|
|
|
|
### Retry Handling
|
|
- **If retry request**: Analyze previous rounds to understand what failed
|
|
- **Learn from mistakes**: Improve the plan based on previous failures
|
|
|
|
## 📊 Required JSON Structure
|
|
|
|
```json
|
|
{
|
|
"overview": "Brief description of the overall plan",
|
|
"userMessage": "User-friendly message explaining the task plan (use the user's detected language from intent)",
|
|
"tasks": [
|
|
{
|
|
"id": "task_1",
|
|
"objective": "Clear business objective focusing on what to deliver",
|
|
"dependencies": ["task_0"],
|
|
"success_criteria": ["measurable criteria 1", "measurable criteria 2"],
|
|
"estimated_complexity": "low|medium|high",
|
|
"userMessage": "What this task will accomplish"
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
## 🎯 Task Structure Guidelines
|
|
|
|
### Task ID Format
|
|
- Use sequential numbering: `task_1`, `task_2`, `task_3`
|
|
- Keep IDs simple and clear
|
|
|
|
### Objective Writing
|
|
- **Be VERY SPECIFIC** - Include exact details needed for action planning
|
|
- **Include all requirements** - recipient, attachments, format, recipients, etc.
|
|
- **State the complete deliverable** - What exactly will be produced
|
|
- **Include context and constraints** - When, where, how, with what
|
|
- **Make it actionable** - Clear enough to plan specific actions
|
|
|
|
### Specific Objective Examples
|
|
- **Good**: "Send formal email to ceo and board of directors with annual report as attachment"
|
|
- **Bad**: "Handle email communication"
|
|
- **Good**: "Order flowers from Fleurop for delivery to 123 Main St, include card message 'Happy Birthday', deliver on March 15th"
|
|
- **Bad**: "Order flowers"
|
|
|
|
### Action Planning Requirements
|
|
- **Include all necessary details** - The objective must contain everything needed to plan actions
|
|
- **Specify recipients and destinations** - Who should receive what
|
|
- **Include file names and formats** - What documents to use/create
|
|
- **State timing and deadlines** - When things need to be done
|
|
- **Include context and constraints** - Any special requirements or limitations
|
|
|
|
### Success Criteria
|
|
- **Make them measurable** - specific, quantifiable outcomes
|
|
- **Focus on deliverables** - what the user will receive
|
|
- **Keep criteria realistic** - achievable within the task scope
|
|
- **Include all related actions** - success means completing the entire business objective
|
|
- **Be specific about requirements** - Include exact details like recipients, formats, deadlines
|
|
- **State clear completion criteria** - How to know the task is fully done
|
|
|
|
### Complexity Estimation
|
|
- **Low**: Simple, single-action tasks (1-2 actions)
|
|
- **Medium**: Multi-action tasks for one topic (3-5 actions)
|
|
- **High**: Complex strategic tasks (6+ actions)
|
|
|
|
## 🚀 Response Format
|
|
Return ONLY the JSON object."""
|
|
|
|
return PromptBundle(prompt=template, placeholders=placeholders)
|