Welcome to the Chain-of-Thought (CoT) Demo
In this demo application, I showcase the power of Chain-of-Thought (CoT) prompting in a real-world scenario using a simulated restaurant database. My goal is to demonstrate how CoT can make complex decision-making processes more transparent and explainable across various domains.
What Is This Demo About?
I've built a simulated restaurant environment to show how CoT generates detailed, human-like explanations for tasks such as menu recommendations and ingredient sourcing. While this is just a demo and not a real restaurant, it provides a comprehensive and interactive way for users to explore the potential of CoT technology.
Key Features
- Simulated Restaurant Database: Explore a rich collection of dishes and ingredients.
- Chain-of-Thought Explanations: Understand how CoT provides step-by-step reasoning for recommendations.
- Interactive Experience: Engage with the system and receive personalized menu suggestions.
- Mood and Language Detection: The system detects the mood and language of your query to enhance interaction.
- Translation: Feedback and recommendations are provided in your language.
How Does CoT Power Menu Recommendations?
Understanding Your Query
When you ask for a dish or make a menu inquiry, CoT starts by analyzing your query to determine your intent—whether you're asking for a specific dish or searching based on certain preferences.
Intent Analysis
The system sends your query to OpenAI, which interprets the intent, detects the mood, and recognizes the language you're using. This initial step helps narrow down what you're looking for without immediately generating recommendations, saving on processing power.
const intentAnalysisResponse = await callOpenAI([
{
role: "system",
content: "You are an AI assistant that understands user queries about food and restaurant orders.
Analyze the user's input and provide a brief summary of their intent, mood, and language."
},
{
role: "user",
content: query,
}
], 150);
Generating Recommendations
Once your intent is understood, CoT requests dish recommendations from a predefined menu. The response includes:
- A list of suggested dishes, each with a unique ID, name, and reason for the recommendation.
- Whether an exact match to your query was found.
- A friendly feedback message, translated into your language.
const recommendationRequestSettings = getRecommendationRequestSettings(
language, mood, intent, intentReasoning, intentThought
);
const menuRecommendationsResponse = await callOpenAI([
recommendationRequestSettings
], 400);
Personalized Suggestions
After receiving the recommendations, the system pulls more details from the database, such as related dishes or items within the same category.
Final Output
The final output combines all the insights gained from the CoT reasoning process:
- Recommendations: A list of dishes suggested based on the user's intent and mood.
- Exact Match: A boolean value indicating whether a specific dish was found in the database.
- Feedback: A friendly response that introduces the recommendations, tailored to the user's language.
- Reasoning and Thought: Detailed explanations that provide the underlying rationale for the recommendations, enhancing the transparency of the AI's decision-making process.
Defining the CoT Structure
In the openAIModelRules and getMenuRecommendationRules objects, the rules for utilizing Chain-of-Thought (CoT) are explicitly defined. The following components are incorporated:
- Intent: The user's intent is identified and summarized, providing context for the interaction.
- Mood: Emotional cues from the user's input are extracted and analyzed to tailor the response.
- Language: The language of the user's input is determined, enabling personalized feedback.
- Reasoning: A detailed rationale for the AI's conclusions is provided, demonstrating the thought process behind the recommendations.
- Thought: This is an elaboration on the reasoning, offering deeper insights into the AI's considerations and decision-making process.
Working Example
For instance, if a user enters the query: "Got anything to go with the NFL", the Chain of Thought (CoT) structure might look like this:
intent: "Looking for food or snacks for NFL game watching.",
mood: "Excitement for NFL games.",
language: "English",
reasoning: "The user is inquiring about food options likely related to watching NFL games, suggesting a social or casual atmosphere where snacks or meals are typically enjoyed while watching sports.",
thought: "Football games often bring a festive mood where people enjoy finger foods, appetizers, or themed dishes. The user's casual language indicates they might be searching for simple yet enjoyable food pairings for their game day experience."
OpenAI Model Rules
const openAIModelRules = {
role: "You are an AI assistant that understands user queries about food and restaurant orders.",
task: "Analyze the user's input and provide a brief summary of their intent or request, the language of the user input, and include your chain-of-thought reasoning.",
errorHandling: {
misspelling: "If the user misspells a dish, correct the spelling while preserving the meaning of the query.",
mood: "Detect and extract any mood from the query, such as 'it's hot outside' or 'I feel hungry.'"
},
restrictions: {
recommend: "Do not make recommendations yet."
},
responseFormat: {
structure: {
intent: "A 'intent' string indicating the user's intent.",
mood: "A 'mood' string indicating the detected mood.",
language: "A 'language' string indicating the detected language.",
reasoning: "A 'reasoning' string containing your chain-of-thought.",
thought: "Elaborate more on why you choose this reasoning."
}
}
};
Menu Recommendation Rules
function getMenuRecommendationRules(language: string, mood: string, intent: string, reasoning: string, thought: string) {
return {
role: "You are a helpful and friendly restaurant waitress.",
task: "Recommend dishes from the following menu.",
menuPlaceholder: JSON.stringify(menuData.menu),
user: {
mood: mood,
intent: intent,
language: language
},
cot: {
reasoning: reasoning,
thought: thought
},
recommendations: {
count: "Always return 3 to 5 items.",
exactMatch: "If the exact dish is not found, suggest similar alternatives for the user's intent.",
},
responseFormat: {
structure: {
recommendations: "A 'recommendations' array of objects, each with 'id', 'name', and 'reason' fields only.",
exactMatch: "A 'exactMatch' boolean indicating if the exact dish was found.",
feedback: "A 'feedback' friendly, waitress-like response introducing the recommendations."
},
restrictions: {
prices: "Do not return prices.",
markdown: "Do not include any markdown formatting.",
translation: "Translate the reason and feedback to the user's language."
}
}
};
}
Conclusion
This structured approach showcases the power of Chain-of-Thought prompting in AI, providing not just outputs but also a clear understanding of how those outputs were derived. This makes interactions with the AI more engaging and informative.
All these steps happen seamlessly when you click "Get Recommendations" in the demo, providing you with an engaging and informative experience.