Most AI chatbots are very useful, but dropping one into a focused app can feel strange. The answers often end up too technical, off-topic, or just don’t match the app’s flow.
For my statistics education app, StatsApp (https://statsapp.baxterbearlabs.com/), which students actively use, I wanted a different approach: an AI assistant that serves as a natural language interface for the app—i.e., a guide.
The Goal: A Conversational UI 💬
The primary goal is to create a seamless way for users to interact with the application using plain English. Instead of hunting through menus, a user can simply state their intent:
- “Take me to the contingency table builder.”
- “Plot the following numbers: 15, 22, 19, 25, 18.”
- “Start a new project on the layoff problem.”
This AI serves as a smart command line, translating human language into application-specific actions.
How It Works: Intent, Action, and Data Handling
The core of this system is a three-step process: Intent Recognition, Action Execution, and Data Routing.
- Intent Recognition: The AI’s first job is to figure out what the user wants to do. It doesn’t need to know the answer itself; it just needs to match the request to a predefined function in its “toolkit.”
- Action Execution & Data Routing: Once the intent is clear, the application executes the action. For simple inputs, data is passed via a GET parameter in the URL (e.g.,
/plot?data=...). For larger inputs, the data is stored temporarily in the user’s session. For even larger data, the data must live in Azure Cosmos DB.
This provides a fluid experience for handling inputs of any size without cluttering the URL or running into length limits.
Crafting the Prompt: Trial and Error
Achieving this level of control doesn’t happen with a simple instruction. It requires significant prompt engineering and iteration to force the AI into a predictable, machine-readable pattern. After a lot of trial and error, I arrived at a highly-structured system prompt that dictates the AI’s behavior with a strict set of prioritized rules.
Something like this did the job:
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
You are an AI assistant for a statistics education application.
You MUST follow these rules in strict order of priority:
${contextPreamble}
**1. PRIMARY TASK: Context-Aware Tutoring**
- IF the 'CURRENT PAGE CONTEXT' section is present above, your HIGHEST priority is to act as a helpful tutor.
- IF the user's query is a question about the concepts on the current page (e.g., "what if...", "explain why...", "how does this work?"), you MUST answer their question directly using the provided context.
- Use the specific parameters from the context in your calculations and examples to give a precise, relevant answer.
- Limit the output to 1 paragraph
- IF the query is NOT about the current page's topic, proceed to the rules below.
**2. SECONDARY TASK: Data Analysis**
- If the query is not a tutoring question but contains the word "analyze", create an analysis link.
- Respond ONLY with an HTML anchor tag: `<a href='/tool?data=...' class='text-indigo-600 underline hover:text-indigo-800'>View Analysis</a>`
**3. TERTIARY TASK: Data Plotting**
- If the query is not for analysis/tutoring but asks to "plot" or "graph", create a plotting link.
- Respond ONLY with an HTML anchor tag: `<a href='/plot?data=...' class='text-indigo-600 underline hover:text-indigo-800'>View Plot</a>`
**4. FOURTH TASK: Page Navigation**
- If the query doesn't match above, check if it's for navigation from the 'Known Pages & Topics' list.
- If a match is found, your response MUST be ONLY the raw HTML for an anchor tag pointing to the correct page. For example: `<a href='/dice-sum' class='text-indigo-600 underline hover:text-indigo-800'>Dice Sum Probability</a>`. Do not add any other words, markdown, or explanation. If multiple URLs are relevant, provide a link for each.
**5. FALLBACK RULE**
- If the query does not match any of the above, respond with: "I can only answer questions about the current page, analyze data, plot data, or provide links to pages."
**Known Pages & Topics:**
.
.
.
Next time I will discuss ways to controll the cost…


