3.4 Understand Intent Mapping
1. Add Intent Mapping Prompty
Let's copy over the intent_mapping.prompty
prompt template into our assets folder.
| cp src.sample/api/assets/intent_mapping.prompty src/api/assets/.
|
2. Run Docs Retrieval Script
Before we dive into the details of that file, let's first run the document retrieval script and see what happens. Here's an example with the question we discussed earlier.
| python get_product_documents.py --query "I need a new tent for 4 people, what would you recommend?"
|
Response may look like this |
---|
| 🧠 Intent mapping: {
"intent": "The user is looking for recommendations for a tent suitable for 4 people.",
"search_query": "best tents for 4 people"
}
|
The script output shows that it extracted the user intent and formulated a search query from it that related to a product ("best tents for 4 people") - and can be answered by the search index.
3. Intent Mapping In Action
The intent mapping is achieved using a Prompty asset - a YAML file that consists of frontmatter (prompt metdata) and content (prompt template)
- Frontmatter defines model configuration and prompt inputs
- Template defines prompt structure, context and instructions
Looking into the details of the prompt template, we can see it employs a few-shot learning technique where it attempts to teach the AI to execute a specific task (extract intent) by providing it with examples of inputs and expected responses.
Click to expand and view Intent Mapping Prompty
src/api/assets/intent_mapping.prompty |
---|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 | ---
name: Chat Prompt
description: A prompty that extract users query intent based on the current_query and chat_history of the conversation
model:
api: chat
configuration:
azure_deployment: gpt-4o
inputs:
conversation:
type: array
---
system:
# Instructions
- You are an AI assistant reading a current user query and chat_history.
- Given the chat_history, and current user's query, infer the user's intent expressed in the current user query.
- Once you infer the intent, respond with a search query that can be used to retrieve relevant documents for the current user's query based on the intent
- Be specific in what the user is asking about, but disregard parts of the chat history that are not relevant to the user's intent.
- Provide responses in json format
# Examples
Example 1:
With a conversation like below:
```
- user: are the trailwalker shoes waterproof?
- assistant: Yes, the TrailWalker Hiking Shoes are waterproof. They are designed with a durable and waterproof construction to withstand various terrains and weather conditions.
- user: how much do they cost?
```
Respond with:
{
"intent": "The user wants to know how much the Trailwalker Hiking Shoes cost.",
"search_query": "price of Trailwalker Hiking Shoes"
}
Example 2:
With a conversation like below:
```
- user: are the trailwalker shoes waterproof?
- assistant: Yes, the TrailWalker Hiking Shoes are waterproof. They are designed with a durable and waterproof construction to withstand various terrains and weather conditions.
- user: how much do they cost?
- assistant: The TrailWalker Hiking Shoes are priced at $110.
- user: do you have waterproof tents?
- assistant: Yes, we have waterproof tents available. Can you please provide more information about the type or size of tent you are looking for?
- user: which is your most waterproof tent?
- assistant: Our most waterproof tent is the Alpine Explorer Tent. It is designed with a waterproof material and has a rainfly with a waterproof rating of 3000mm. This tent provides reliable protection against rain and moisture.
- user: how much does it cost?
```
Respond with:
{
"intent": "The user would like to know how much the Alpine Explorer Tent costs.",
"search_query": "price of Alpine Explorer Tent"
}
user:
Return the search query for the messages in the following conversation:
{{#conversation}}
- {{role}}: {{content}}
{{/conversation}}
|