跳至内容
Intermediate

Build a Custom Workflow

Create your own AI workflow with custom stages, prompts, and processing logic.

Prerequisites
  • Familiarity with module development
  • Understanding of Python and XML
  • Completed the Getting Started guide

What We'll Build

In this tutorial, we'll create a "Blog Post Generator" workflow that:

  • Takes a topic and generates a blog post outline
  • Requires approval of the outline
  • Generates the full blog post content
  • Allows review and publishing

Step 1: Create the Module

Create a new module for your workflow.

Bash
# Create module directory
mkdir -p ai_workflow_blog/models ai_workflow_blog/views ai_workflow_blog/data

# Create __init__.py
echo "from . import models" > ai_workflow_blog/__init__.py
echo "from . import blog_workflow" > ai_workflow_blog/models/__init__.py

Manifest File

Python - __manifest__.py
# ai_workflow_blog/__manifest__.py
{
    'name': 'AI Blog Post Generator',
    'version': '19.0.1.0.0',
    'category': 'AI/Workflow',
    'summary': 'Generate blog posts with AI',
    'depends': ['ai_project_workflow'],
    'data': [
        'data/workflow_template.xml',
        'views/blog_views.xml',
    ],
    'installable': True,
}

Step 2: Create the Workflow Model

Create a model that implements the workflow contract.

Python - models/blog_workflow.py
# ai_workflow_blog/models/blog_workflow.py
from odoo import models, fields, api


class BlogWorkflow(models.Model):
    _name = 'blog.workflow'
    _inherit = ['ai.workflow.contract', 'ai.content.workflow.mixin']
    _description = 'Blog Post Workflow'

    # Workflow-specific fields
    topic = fields.Char('Blog Topic', required=True)
    target_audience = fields.Char('Target Audience')
    word_count = fields.Selection([
        ('500', '500 words'),
        ('1000', '1000 words'),
        ('2000', '2000 words'),
    ], default='1000')
    tone = fields.Selection([
        ('professional', 'Professional'),
        ('casual', 'Casual'),
        ('educational', 'Educational'),
    ], default='professional')

    # Output fields
    outline = fields.Text('Generated Outline')
    content = fields.Html('Blog Content')

    def _get_workflow_context(self):
        """Provide context for AI prompts."""
        return {
            'topic': self.topic,
            'target_audience': self.target_audience or 'general',
            'word_count': self.word_count,
            'tone': self.tone,
        }

    def _process_outline_stage(self, stage):
        """Custom processing for outline generation."""
        context = self._get_workflow_context()
        content = self.generate_content(stage.ai_template_id, context)
        self.outline = content
        return True

    def _process_content_stage(self, stage):
        """Custom processing for content generation."""
        context = self._get_workflow_context()
        context['outline'] = self.outline
        content = self.generate_content(stage.ai_template_id, context)
        self.content = content
        return True

Step 3: Define the Workflow Template

Create the workflow template with stages in XML.

XML - data/workflow_template.xml



    
    
        Blog Outline Generator
        You are an expert content strategist.
Create detailed blog post outlines that engage readers.
        Create an outline for a blog post about:
Topic: {{topic}}
Target Audience: {{target_audience}}
Tone: {{tone}}
Target Length: {{word_count}} words

Provide:
1. Compelling title options (3)
2. Introduction hook
3. Main sections with key points
4. Conclusion summary
5. Call-to-action suggestions
        markdown
    

    
        Blog Content Generator
        You are a skilled content writer.
Write engaging blog posts that inform and inspire.
        Write a complete blog post based on this outline:
{{outline}}

Requirements:
- Target audience: {{target_audience}}
- Tone: {{tone}}
- Length: approximately {{word_count}} words
- Include relevant examples and actionable advice
- Format in HTML with proper heading structure
        html
    

    
    
        Blog Post Generator
        blog_generator
        Generate blog posts with AI
        blog.workflow
    

    
    
        Discovery
        
        10
        manual
        True
    

    
        Generate Outline
        
        20
        ai
        
        _process_outline_stage
        True
    

    
        Outline Review
        
        30
        manual
        True
        True
    

    
        Generate Content
        
        40
        ai
        
        _process_content_stage
        True
    

    
        Content Review
        
        50
        manual
        True
        True
    

    
        Complete
        
        100
        True
    

Step 4: Test Your Workflow

Install the module and test the workflow.

Python - RebusAI Shell
# Create a blog workflow instance
blog = env['blog.workflow'].create({
    'name': 'AI in Business',
    'topic': 'How AI is transforming small business operations',
    'target_audience': 'Small business owners',
    'word_count': '1000',
    'tone': 'professional',
})

# Start the workflow
blog.action_start_workflow()

# Check progress
print(f"Current stage: {blog.stage_id.name}")
print(f"State: {blog.ai_state}")

# After outline generation, view the outline
print(blog.outline)

# Approve the outline
blog.action_approve_stage()

# After content generation, view the content
print(blog.content)
Success!

You've created a custom AI workflow. Extend it further by adding portal wizards, webhooks, and custom output handlers.