β€‹πŸ€– The AI's Inner Monologue: Unlocking Self-Reflection with Chain-of-Thought

SaifSaif
4 min read

Have you ever wondered if an AI can truly think? πŸ€” It turns out, it can! A thinking model can reason by talking to itself through a fascinating process of self-reflection.

The magic behind this capability is a powerful technique known as CoT (Chain-of-Thought).

What is Chain-of-Thought? 🧠

Think of Chain-of-Thought as a prompting strategy that creates an internal dialogue for the AI. It uses various prompt roles (like system, user, and assistant) to build a cohesive thinking process.

This process enables the Large Language Model (LLM) to ask itself crucial questions at every step. It's like watching a detective solve a case by thinking out loud! πŸ•΅οΈβ€β™‚οΈ This allows the model to fine-tune its reasoning and ultimately produce a far more nuanced and accurate output than it would otherwise.

This can be seen more practically by reviewing a code snippet that intends to establish a CoT.

For example:

async function main() {

// These API calls are stateless (Chain Of Thought)

const SYSTEM_PROMPT = `

You are an AI assistant who works on START, THINK, EVALUATE, and OUTPUT format.

For a given user query, first think and break down the problem into sub-problems.

You should always keep thinking and analyzing before giving the actual output.

Also, before outputting the final result, you must double-check if everything is correct.

Rules:

- Strictly follow the output JSON format.

- Always follow the sequence: START, THINK, EVALUATE, and OUTPUT.

- After every THINK, there is going to be an EVALUATE step performed by someone else, and you need to wait for it.

- Always perform only one step at a time and wait for the next step.

- Always make sure to do multiple steps of thinking before giving the final output.

Output JSON Format:

{ "step": "START | THINK | EVALUATE | OUTPUT", "content": "string" }

Example:

User: Can you solve 3 + 4 * 10 - 4 * 3

ASSISTANT: { "step": "START", "content": "The user wants me to solve the math problem 3 + 4 * 10 - 4 * 3" }

ASSISTANT: { "step": "THINK", "content": "This is a typical math problem where I should use the BODMAS/PEMDAS rule for calculation." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "Let's break down the problem step-by-step." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "As per BODMAS, I'll solve all multiplications first." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "So, first I need to solve 4 * 10, which is 40." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "Great, now the equation looks like 3 + 40 - 4 * 3." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "Now, I see one more multiplication: 4 * 3 = 12." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "Perfect, now the equation is 3 + 40 - 12." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "Now that multiplications are done, let's do the addition and subtraction." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "So, 3 + 40 = 43." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "The new equation is 43 - 12, which equals 31." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "THINK", "content": "Excellent, all steps are complete and the final result is 31." }

ASSISTANT: { "step": "EVALUATE", "content": "Alright, looking good." }

ASSISTANT: { "step": "OUTPUT", "content": "3 + 4 * 10 - 4 * 3 = 31" }

`;

const messages = [

{

role: 'system',

content: SYSTEM_PROMPT,

},

{

role: 'user',

content: 'Write a code in JS to find a prime number as fast as possible',

},

];

}

This setup allows the model to answer the actual question (the user prompt) by first understanding its instructions (the system prompt) and then "thinking" through the problem using its own assistant role prompts. This results in a contextually superior and well-reasoned response.

Final Thoughts ✨

And there you have it! By diving into the Chain-of-Thought prompting method, we can see how it's used to get smarter responses. It’s all about enabling any model to self-reflect, essentially giving it the power to think before it speaks.

0
Subscribe to my newsletter

Read articles from Saif directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Saif
Saif