Documentsdify
Automatic Rule Generation
Automatic Rule Generation
Type
Topic
Status
Published
Created
Jul 8, 2026
Updated
Jul 8, 2026
Created by
Dosu Bot
Updated by
Dosu Bot

Automatic Rule Generation#

Automatic rule generation lets users describe an app's purpose in plain text and have an LLM produce a complete prompt, input variables, and an opening statement for a basic (non-workflow) Dify app. It surfaces in the app configuration UI as the "Auto generate" button, opening the GetAutomaticRes modal.


Entry Points#

LayerLocation
UI modalget-automatic-res.tsx
Frontend API callsweb/service/debug.tsgenerateBasicAppFirstTimeRule & generateRule
Template pre-filluseGenerateRuleTemplate in web/service/use-apps.ts
Backend routePOST /rule-generateRuleGenerateApi
Backend logicLLMGenerator.generate_rule_config() in api/core/llm_generator/llm_generator.py

How It Works#

1. UI — GetAutomaticRes component#

The GetAutomaticRes dialog is the main user-facing surface. Key behaviors:

  • Model selection: reads from useAutoGenModel (auto-gen-model-storage.ts), which persists the chosen model to localStorage under the key auto-gen-model. Falls back to the tenant's default text-generation model .
  • Instruction pre-fill: on mount, fetches a starter instruction template from POST /instruction-generate/template via useGenerateRuleTemplate .
  • Template shortcuts: in isBasicMode, nine preset use-cases (Python debugger, translator, meeting takeaways, etc.) appear as quick-fill chips .
  • Generation path: when isBasicMode || !currentPrompt, onGenerate calls generateBasicAppFirstTimeRule; otherwise it calls generateRule for prompt-improvement on existing workflow nodes.
  • Versioning: results are stored session-wide via useGenData, allowing the user to browse and compare multiple generated versions before applying.
  • Apply confirmation: selecting "Apply" triggers an AlertDialog overwrite confirmation before calling onFinished(current) .

2. Frontend service layer#

generateBasicAppFirstTimeRule issues POST /rule-generate with body { instruction, model_config, no_variable }.

generateRule issues POST /instruction-generate — used when an existing prompt is present (workflow node or legacy app improvement).

The response type GenRes carries modified (the generated/revised prompt), plus optional variables[] and opening_statement that are only populated by rule-generate.

3. Backend — RuleGenerateApi and LLMGenerator#

RuleGenerateApi.post() validates the request as RuleGeneratePayload (instruction, model_config_data, no_variable) and delegates to LLMGenerator.generate_rule_config().

generate_rule_config() has two code paths :

no_variable=True (1 LLM call — used by basic app first-time generation):
Formats WORKFLOW_RULE_CONFIG_PROMPT_GENERATE_TEMPLATE with the user's instruction and invokes the LLM once. The response text becomes rule_config["prompt"].

no_variable=False (3 sequential LLM calls — used when variable extraction is needed):

  1. Prompt generation — uses RULE_CONFIG_PROMPT_GENERATE_TEMPLATErule_config["prompt"]
  2. Variable extraction — uses RULE_CONFIG_PARAMETER_GENERATE_TEMPLATE on the generated prompt; extracts variable names with re.findall(r'"\s*([^"]+)\s*"', ...)rule_config["variables"]
  3. Opening statement — uses RULE_CONFIG_STATEMENT_GENERATE_TEMPLATErule_config["opening_statement"]

Prompt templates live in api/core/llm_generator/prompts.py. Output parsing helpers are in api/core/llm_generator/output_parser/rule_config_generator.py.


FilePurpose
automatic/All UI sub-components for the generation modal (result.tsx, instruction-editor.tsx, version-selector.tsx, etc.)
api/core/llm_generator/llm_generator.pyCore LLM generation logic for rule/prompt/code/workflow generation
api/controllers/console/app/generator.pyFlask routes for all generator endpoints
api/core/llm_generator/prompts.pyRaw prompt templates used by all generator methods
api/core/llm_generator/entities.pyPydantic request/response models (RuleGeneratePayload, etc.)
Automatic Rule Generation | Dosu