You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rasa中实现对话启动阶段统一返回欢迎消息(解决规则与故事冲突问题)

Hey there! Let's tackle this Rasa problem you're facing: you want to send a welcome message no matter what the user sends to kick off a conversation, but your initial rule clashed with existing stories, and you can't define intents for every possible user input. Here are a few practical, actionable solutions:

1. Use a Wildcard Rule + Adjust Policy Priority

This is the simplest approach—we'll create a rule that matches any user intent when the conversation starts, then set the rule policy to take priority over stories.

First, update your rules file with this wildcard rule:

rule: Greet on Conversation Start
conversation_start: true
steps:
  - intent: *  # Matches ANY user intent (including nlu_fallback for unrecognized inputs)
  - action: utter_greet

Next, fix the rule-story conflict by boosting the RulePolicy priority in your config.yml. Rules will now take precedence over memorized stories:

policies:
  - name: RulePolicy
    priority: 2  # Higher than MemoizationPolicy's default priority of 1
  - name: MemoizationPolicy
  - name: TEDPolicy

This way, the first message from the user—whether it's a product price question, random text, or unrecognized input—will trigger the welcome message first.

2. Custom Action for First Message Handling

If you want more control (like sending the greeting and processing the user's request in one step), use a custom action. This avoids rule conflicts entirely and lets you handle edge cases easily.

First, add this custom action to your actions.py:

from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher

class ActionHandleFirstMessage(Action):
    def name(self) -> Text:
        return "action_handle_first_message"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        # Send the welcome message first
        dispatcher.utter_message(response="utter_greet")
        
        # Now process the user's actual request
        latest_intent = tracker.latest_message["intent"]["name"]
        if latest_intent == "product_price":
            dispatcher.utter_message(response="utter_product_price")
        # Add more intent-handling logic here for other use cases
        # elif latest_intent == "some_other_intent":
        #     dispatcher.utter_message(response="utter_some_response")
        
        return []

Then update your rule to trigger this custom action for any starting intent:

rule: Handle First User Message
conversation_start: true
steps:
  - intent: *
  - action: action_handle_first_message

3. Auto-Greet on Session Start (Before User Messages)

If you want to send the welcome message as soon as the conversation starts (before the user even types anything), use a custom ActionSessionStart:

Add this to actions.py:

from rasa_sdk.actions.action import ActionSessionStart
from rasa_sdk.events import SessionStarted

class CustomSessionStart(ActionSessionStart):
    def name(self) -> Text:
        return "action_session_start"

    async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
        # Send welcome message immediately when session starts
        dispatcher.utter_message(response="utter_greet")
        
        # Run the default session start logic
        events = await super().run(dispatcher, tracker, domain)
        return events

Make sure to register this action in your domain.yml under actions:, and it will fire automatically when a new conversation begins.

Quick Note on Rule-Story Conflicts

The core issue with your original rule was that it only matched one specific intent (product_price), and your existing stories probably had a flow where that intent triggered a different action. Using the wildcard intent: * combined with a higher rule priority ensures your welcome message rule takes precedence every time a conversation starts.

内容的提问来源于stack exchange,提问作者LLB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 09:09:07