如何将含意图与训练短语的CSV转为Rasa兼容nlu.md,或先建CSV再转换?
Alright, let's tackle these two Rasa NLU conversion challenges head-on. I’ve built similar workflows for clients before, so I’ll walk you through practical, reusable steps with examples you can adapt to your needs.
1. Convert a Specific CSV (Intent + Training Phrases) to Rasa's nlu.md
First, let’s align on a common "specific format" CSV structure (since you didn’t provide exact specs, I’ll use a widely adopted setup). We’ll cover two common CSV variants: one phrase per row, and multiple phrases per intent row.
Step 1: Define Your CSV Structure
Variant 1: Single Training Phrase Per Row
Save this as nlu_single_phrase.csv:
intent,training_phrase greet,hi there greet,hello! greet,good morning ask_weather,what's the weather today? ask_weather,is it going to rain tomorrow?
Variant 2: Multiple Phrases Per Intent Row
Use a separator (like ;) to group phrases for the same intent. Save as nlu_multi_phrase.csv:
intent,training_phrases greet,hi there;hello!;good morning ask_weather,what's the weather today?;is it going to rain tomorrow?
Step 2: Python Script for Conversion
This script handles both variants—no need to rewrite code for different CSV formats:
import csv def csv_to_rasa_nlu(csv_path, output_md_path, multi_phrase_sep=None): """ Convert intent-training phrase CSV to Rasa-compatible nlu.md :param csv_path: Path to your input CSV file :param output_md_path: Path to save the final nlu.md :param multi_phrase_sep: Separator for multiple phrases in one cell (e.g., ';') """ intent_map = {} # Read and parse CSV with open(csv_path, 'r', encoding='utf-8') as csv_file: reader = csv.DictReader(csv_file) for row in reader: intent = row['intent'].strip() phrase_raw = row['training_phrases' if multi_phrase_sep else 'training_phrase'].strip() # Split phrases if using multi-phrase format phrases = [p.strip() for p in phrase_raw.split(multi_phrase_sep)] if multi_phrase_sep else [phrase_raw] phrases = [p for p in phrases if p] # Remove empty strings # Build intent-phrase mapping if intent not in intent_map: intent_map[intent] = [] intent_map[intent].extend(phrases) # Write to nlu.md with open(output_md_path, 'w', encoding='utf-8') as md_file: md_file.write("# NLU Training Data\n\n") for intent, phrases in intent_map.items(): md_file.write(f"## intent:{intent}\n") for phrase in phrases: md_file.write(f"- {phrase}\n") md_file.write("\n") # Usage for single-phrase CSV csv_to_rasa_nlu('nlu_single_phrase.csv', 'nlu.md') # Usage for multi-phrase CSV (uncomment below) # csv_to_rasa_nlu('nlu_multi_phrase.csv', 'nlu.md', multi_phrase_sep=';')
Step 3: Verify the Output
Your nlu.md will match Rasa’s required format exactly:
# NLU Training Data ## intent:greet - hi there - hello! - good morning ## intent:ask_weather - what's the weather today? - is it going to rain tomorrow?
2. Design a Multi-Intent CSV & Convert to nlu.md
Rasa supports multi-intents using the + separator (e.g., greet+ask_weather). Let’s design a CSV that handles both multi-intent labels and grouped training phrases.
Step 1: Multi-Intent CSV Structure
We’ll stick to two columns, but allow intent to be a combined label and training_phrases to hold multiple relevant sentences. Save as nlu_multi_intent.csv:
intent,training_phrases greet,hi there;hello!;good morning ask_weather,what's the weather today?;is it going to rain tomorrow? greet+ask_weather,hi, what's the weather today?;hello, is it going to rain tomorrow?;good morning, can you tell me the forecast? order_pizza+order_drink,i want a pepperoni pizza and a coke;can i get a margherita pizza plus a sprite?
Step 2: Convert with the Same Script
Reuse the script from part 1—just pass the multi-phrase separator:
csv_to_rasa_nlu('nlu_multi_intent.csv', 'nlu_multi_intent.md', multi_phrase_sep=';')
Step 3: Final nlu_multi_intent.md Output
The file will correctly format multi-intents for Rasa:
# NLU Training Data ## intent:greet - hi there - hello! - good morning ## intent:ask_weather - what's the weather today? - is it going to rain tomorrow? ## intent:greet+ask_weather - hi, what's the weather today? - hello, is it going to rain tomorrow? - good morning, can you tell me the forecast? ## intent:order_pizza+order_drink - i want a pepperoni pizza and a coke - can i get a margherita pizza plus a sprite?
Quick Tips for Multi-Intent in Rasa
- Ensure your pipeline in
config.ymlsupports multi-intents (most standard pipelines do, but double-check if you’re using a custom setup) - Reference combined intents directly in your stories/rules (e.g.,
intent:greet+ask_weather) to handle them in conversations
内容的提问来源于stack exchange,提问作者Lohith Arcot

