dify-workflow.md 4.9 KB

Dify Chatflow: 数字能量解读工作流

Overview

A Dify Chatflow that accepts user phone numbers + questions, assembles chart context, and generates AI readings.

Workflow Design

Nodes

1. Start Node (Input)

  • query: string — user's natural language question (e.g., "我的职业发展如何?")
  • phone: string — 11-digit phone number
  • chart_data: json — pre-calculated triangle data from mini-program (if provided)
  • user_id: string — WeChat OpenID

2. Python Node: context_builder

Node ID: context_builder
Purpose: Assemble chart data into structured context for LLM

def main(query: str, phone: str, chart_data: dict, user_id: str) -> dict:
    import json

    # Build position descriptions
    positions = {
        "A": "命宫/事业", "B": "福德宫", "C": "父母宫",
        "D": "迁移宫", "E": "财帛宫/财富",
        "F": "夫妻宫/感情", "G": "兄弟宫", "N": "交友宫",
        "H": "官禄宫", "I": "田宅宫",
        "J": "疾厄宫", "M": "子女宫", "L": "仆役宫",
        "K": "财库", "O": "整体/综合"
    }

    if chart_data:
        context = "## 数字能量分析数据\n\n"
        context += "### 各宫位数字\n\n"
        context += "| 宫位 | 数字 | 含义 |\n"
        context += "|------|------|------|\n"

        for key, label in positions.items():
            num = chart_data.get(key, "—")
            context += f"| {key}({label}) | {num} | |\n"

        # Add primary positions
        primary = ["A", "E", "F", "O"]
        context += "\n### 主星能量\n\n"
        for p in primary:
            if p in chart_data:
                context += f"- {p}({positions[p]}): {chart_data[p]}\n"

        # Add combinations
        pairs = [("A","E"),("A","F"),("E","O"),("F","O"),("A","O")]
        context += "\n### 重要数字组合\n\n"
        for a, b in pairs:
            if a in chart_data and b in chart_data:
                context += f"- {a}({chart_data[a]}) + {b}({chart_data[b]}) = {(chart_data[a]+chart_data[b])%10}\n"

        context += "\n### 手机号\n"
        context += f"- {phone}\n"
    else:
        context = "未提供图表数据。"

    # Identify question intent
    intents = {
        "职业": "career", "事业": "career", "工作": "career",
        "财运": "wealth", "财": "wealth", "赚钱": "wealth",
        "感情": "relationship", "爱情": "relationship", "婚姻": "relationship",
        "健康": "health", "身体": "health",
        "家庭": "family", "家人": "family"
    }

    intent = "general"
    for keyword, it in intents.items():
        if keyword in query:
            intent = it
            break

    return {
        "context": context,
        "intent": intent,
        "formatted": f"用户问题:{query}\n\n分析焦点:{intent}\n\n{context}"
    }

3. Knowledge Retrieval Node (Optional)

Retrieves from Digital Energy knowledge base if configured.

4. LLM Node: reading_generator

  • Model: GPT-4o / Claude 3.5 Sonnet
  • Context: System prompt + context from Python node
  • Temperature: 0.7

System Prompt:

你是资深数字能量学专家。你的任务是基于用户手机号的数字能量三角图数据,提供专业、准确、有价值的解读。

## 核心原则
1. 只基于用户提供的数字进行分析,不凭空编造
2. 保持客观,不制造焦虑或过度承诺
3. 分析应该既有专业术语又通俗易懂
4. 给出建设性建议而非宿命论结论

## 解读框架
1. 主星分析:A(事业)、E(财富)、F(感情)、O(综合)
2. 数字组合:关键配对的含义
3. 具体领域分析:根据用户问题聚焦(事业/财富/感情/健康/家庭)
4. 建议和指引

## 重要格式
- 使用自然流畅的中文
- 适当分段,有层次感
- 重点数字用加粗或引号标注

User Prompt Template:

{{formatted}}

5. End Node (Output)

  • answer: string — AI response text
  • intent: string — detected intent category

Dify API Integration

Endpoint

POST /v1/workflows/run
Authorization: Bearer {api_key}
Content-Type: application/json

Request Body

{
  "inputs": {
    "query": "我的事业运势如何?",
    "phone": "13800138000",
    "chart_data": {"A": 3, "B": 5, ...},
    "user_id": "wx_user_001"
  },
  "response_mode": "blocking",
  "user": "wx_user_001"
}

Response

{
  "workflow_run_id": "run_xxx",
  "data": {
    "outputs": {
      "answer": "根据您的数字能量分析...",
      "intent": "career"
    },
    "status": "succeeded"
  }
}

Backend Integration (Java)

See DifyService.java for Spring Boot client implementation.

Deployment

  1. Create a new Chatflow in Dify console
  2. Add nodes in order: Start → Python → LLM → End
  3. Copy the Python code into the Python node
  4. Configure LLM node with system prompt above
  5. Set Start node input schema
  6. Deploy as API service
  7. Copy API key to application.yml