Mastering Intent Detection & Slot Filling: Proven Strategies and Code Samples
This article shares reusable AI development techniques for intent detection and slot filling, comparing four solution tiers—from simple prompt engineering to advanced RAG‑enhanced architectures—complete with practical code snippets, performance trade‑offs, and guidance on selecting the optimal approach for reliable conversational agents.
Overview
The article provides AI developers with reusable technical paths and experience references to avoid common pitfalls when building intelligent dialogue systems.
Key Concepts
Intent Detection
Accurately determines the user's semantic purpose by mapping input to predefined intent categories (e.g., "weather query", "restaurant reservation"). Incorrect intent detection leads to a dialogue flow that diverges from the user's real needs.
Slot Filling
Extracts structured key information such as time, location, and quantity from the sentence. In a food‑ordering scenario, essential slots include "dish name" and "delivery address".
Both steps form a complete semantic parsing chain that directly impacts dialogue state accuracy.
Solution A – Prompt‑Engineering (Basic)
Uses a single model node with carefully designed prompts to perform intent detection and slot filling.
Prompt engineering consists of three key modules:
Define intents and slots with precise names, data types, and value ranges.
Provide Few‑Shot examples and Chain‑of‑Thought (CoT) for each intent to guide the model.
Specify output format (e.g., JSON) for downstream processing.
Prompt Key Parts
(1) Define Intent Slots
### 技能. 旅行相关的意图识别
你的任务是根据技能1理解的最新提问,在以下意图类型中进行分类,并抽取相应的参数:
1. 交通出行
- 描述: 用户需要网约车、地铁、出租车等服务。
- 参数: 交通方式, 位置意图, 出发地, 目的地位置类型, 地铁线路, 标签
2. 美食导购
- 描述: 用户咨询餐饮相关信息。
- 参数: 问题类型, 出发地, 标签(2) Few‑Shot + CoT
#### case1
最新提问:我要打车去杭州野生动物园
<回答>
```JSON
{"意图类型":"交通出行","参数列表":{"交通方式":"网约车","位置意图":"实际位置","目标位置":"杭州野生动物园"}}
```(3) Output Format
{"意图类型":"<意图类型>","参数列表":{"实体参数1":"取值1","实体参数N":"取值N"}}Solution B – Separate Intent & Slot Nodes (Intermediate)
Decouples intent detection and slot extraction into independent nodes, reducing prompt length and improving clarity. Each intent has a dedicated slot‑extraction node, simplifying maintenance and scaling.
Advantages: clear architecture, easier updates, lower risk of cross‑intent interference. Drawbacks: increased number of model calls, higher latency.
Solution C – RAG‑Enhanced Intent Retrieval (Advanced)
Introduces a pre‑retrieval step using Retrieval‑Augmented Generation (RAG) to fetch similar queries from a knowledge base before invoking the LLM. This improves handling of domain‑specific or paraphrased queries.
Key workflow:
Upload extensive intent classification data to a knowledge base.
When a user query arrives, use RAG to retrieve the most similar case.
Feed the retrieved case as a prompt to the LLM for final intent and slot prediction.
Recommended models: cost‑effective LLMs such as qwen‑turbo or qwen‑plus.
Intent Knowledge‑Base Construction
Steps include defining intent categories, collecting seed queries (30‑50 per intent), and using LLMs to generate paraphrases (Few‑Shot + CoT). Example Python code for generating queries:
from datetime import datetime
from openai import OpenAI
base_url = "xxxxx"
api_key = "xxxxx"
client = OpenAI(base_url=base_url, api_key=api_key)
if __name__ == "__main__":
now = datetime.now()
intent = "查线路途经站点"
desc = "咨询某条公交线路或者某些公交线路路过哪些站点的问题"
file_path = f"./data/{intent}.txt"
with open(file_path,'r',encoding='utf-8') as file:
data_raw = file.readlines()
data_raw = [d.strip() for d in data_raw]
data_pre = data_raw[:100]
prefix = "我们是一个提供上海公交查询服务的一个应用平台,用户会在线上对我们进行提问.不同的问题,对应不同的意图.请你模拟线上真实用户,生成10条意图为<{}>的不同用户指令或提问,此意图含义为<{}>,生成时风格要口语化、简洁.避免礼貌用语."
styles = ["下面给你一些例子:","你的提问风格应该是直接命令式的,直接了当,用词简短,可省略部分信息,不超过12个字,下面给你一些例子:","你的提问风格应该是询问式的,用户以疑问句的形式提出问题,语言简短.不超过15个字,下面给你一些例子:","你的提问风格应该是描述性的,提问时提供一些背景信息或需求描述,下面给你一些例子:"]
input_data = []
prefix = prefix.format(intent, desc)
for style in styles:
for i in range(len(data_pre)-2):
prompt = prefix + style + data_pre[i] + ',' + data_pre[i+1] + ',' + data_pre[i+2]
input_data.append(prompt)
input_data = input_data[:100]
print(len(input_data))
for item in input_data:
completion = client.chat.completions.create(
model="Bailing-4.0-80B-64K-Chat-20250108",
messages=[{"role":"user","content": item}]
)
result = completion.choices[0].message.content
print(result)Solution D – Combined Nodes + Upgraded RAG (High‑End)
Addresses multi‑turn dialogue challenges by merging intent and slot nodes while enhancing the pre‑retrieval layer. The system concatenates historical dialogue with the current query, retrieves the most relevant case via RAG, and feeds it to the LLM.
Key steps:
Assemble history and latest query into a single string.
Perform semantic retrieval to obtain the most similar case.
Use the retrieved case to guide LLM processing, avoiding prompt explosion.
def main(params: dict, context: dict) -> dict:
history = params.get('history', [])
query = params.get('query', '')
query_message = f'最新提问[{query}]'
history_message = ''
for item in history:
role = item.get('role')
text = item.get('content').get('text')
if role == 'user':
history_message = f'{history_message}{role}:{text};'
if len(history_message) > 0:
history_message = f'历史对话[{history_message}]'
message = f'{history_message}{query_message}'
return messageResult example:
历史对话[user:我要打车;user:我要去外滩;]最新提问[我在陆家嘴]Comparative Evaluation
Four solutions were evaluated on a Shanghai Metro chatbot with 13 intents and 443 test cases. Metrics such as accuracy, latency, and maintenance cost were compared (charts omitted for brevity).
Final Thoughts
All four solutions have practical value; the optimal choice depends on scenario requirements, client expectations, and resource constraints. Continuous iteration, knowledge‑base enrichment, and careful intent‑slot management are essential for building reliable conversational AI systems.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
