4.2 - 1 Demo - Chain of Thoughts
Chain of Thought Prompting
Objective:
To perform chain-of-thought prompting using LangChain and the OpenAI API, enabling the model to reason step-by-step through complex problems for improved logical, analytical, and problem-solving responses.
Note:
Before running any demo, ensure that the requirements.txt file is installed. This file contains all the required dependencies for all demos and guided practices under Building LLM Applications.
If the dependencies were already installed earlier (after creating the virtual environment), there is no need to install them again. You can directly proceed with running the demo.
Refer to Lesson_01 Demo_01_Zero_Shot_Prompting.ipynb Step 1 for creating a virtual environment and installing the requirements.txt
Ensure you select the right kernel Python (myenv) while running the demos
Steps to perform:
Set up the OpenAI API key
Define a function to get completion
Define your prompts
Analyze the output
Step 1: Set up the OpenAI API key
Import the required libraries
The os library is used for interacting with the operating system, and openai is used to communicate with the OpenAI API.
Initialize the OpenAI client
import os
from openai import OpenAI
client = OpenAI()
Step 2: Define a function to get completion
Construct a message using the user’s prompt and send it to the OpenAI API
This approach ensures consistency and accuracy across reasoning-based tasks.
def get_completion(prompt, model="gpt-5-mini"):
response = client.chat.completions.create(
model=model,
messages=[
{"role": "user", "content": prompt}
],
max_completion_tokens=5000
)
return response.choices[0].message.content
Step 3: Define your prompts
Provide a series of prompts that guide the model through a chain of thought
Call the get_completion to get a response from the AI model
Print both the prompt and the AI-generated response
prompts = [
"Imagine you are a detective trying to solve a mystery.",
"You arrive at the crime scene and start looking for clues.",
"You find a strange object at the crime scene. What is it?",
"How does this object relate to the crime?",
"Who do you think is the suspect and why?"
]
for prompt in prompts:
response = get_completion(prompt)
print(f"Prompt: {prompt}")
print(f"Response: {response}")
print()
Prompt: Imagine you are a detective trying to solve a mystery.
Response: The lamp light throws the books’ spines into hard, parallel shadows. I push my thumb along the edge of the desk and find the ring of dried blood—fresh enough that someone wiped at it and didn’t finish. The door was locked from the inside; the window latched; footprints in the carpet lead nowhere. On the floor, a king from a chess set lies face-down, a dark smear on its back. The master’s pocket watch is gone.
I am the detective. I breathe, catalog what’s visible, and start building possibilities.
Initial observations
- Locked room, no obvious forced entry: either the killer exited through a hidden route, the door was locked by someone inside after the attack, or the scene was staged.
- Blood smears and a knocked-over chess piece suggest a struggle.
- Missing pocket watch could be motive (theft) or misdirection.
- No signs of a firearm discharge; smell of almonds? (possible cyanide) — check for odor.
- Time of death unclear; body temperature and rigor to be determined.
Working hypotheses (not mutually exclusive)
- Murder staged as suicide.
- Intruder slipped in and out via hidden passage or duplicate key.
- Assistant/close person committed the crime over a quarrel or inheritance.
- Poisoning followed by staging.
Immediate next steps (what I’ll do right now)
1. Secure the scene (no one else in/out).
2. Photograph and document the exact positions of items and body.
3. Check body for wounds, needle marks, residue; estimate time of death.
4. Tag and bag the chess piece and any other small objects, search for trace DNA/fingerprints.
5. Search the room for hidden compartments, service entrances, or disturbed books.
6. Interview the person who found the body, plus household staff and family—note alibis and behavior.
7. Check house keys, watch serial numbers, and nearby CCTV or street cameras.
8. Call for a forensic technician to run toxin screens and lift prints.
Questions I want answered now
- Who found the body and exactly when?
- What was the relationship of the deceased to the household staff and visitors?
- Was anything else reported missing?
- Are there recent threats, disputes, or insurance/inheritance issues?
- Any surveillance or visitors in the neighborhood at the relevant time?
How you can help me
- Give me the case you want solved (real, fictional, a classic puzzle).
- Or choose which step I should take next in this scene: examine the body; search the study; question the butler; follow up on the missing watch; test the chess piece for prints/blood; interview family members.
Tell me what you want: I’ll follow the evidence, form and test theories, and—if you like—reveal the culprit when the clues point clearly.
Prompt: You arrive at the crime scene and start looking for clues.
Response: Do you want me to:
- Continue this as a fictional, first‑person investigation scene (story/roleplay), or
- Give practical, professional guidance and a checklist for processing a crime scene, or
- Produce a short list of likely clues and what they can indicate?
Below is a concise, high‑level checklist and the common types of clues (safe, non‑actionable overview). Tell me which option you prefer and I’ll continue accordingly.
Immediate high‑level steps when you arrive
- Ensure personal safety; don PPE (gloves, shoe covers, mask) to avoid contamination.
- If anyone is injured, render or call for medical aid immediately.
- Secure and isolate the scene: establish a perimeter and control entry/exit to preserve evidence.
- Call for additional resources (forensics team, photographers, investigators) as needed.
- Conduct a preliminary survey to get an overview without touching anything; note obvious evidence and hazards.
- Document the scene in place: take wide and close photos, and make rough sketches/notes with positions and measurements.
- Plan a systematic search pattern (grid, spiral, zone) appropriate to the scene to ensure coverage.
- Collect and package physical evidence using proper procedures to avoid cross‑contamination; label everything and start chain‑of‑custody documentation.
- Canvass for witnesses and nearby surveillance; record statements and preserve any video.
- Log and transport evidence to the lab; continue investigative followup (forensic analyses, interviews).
Common types of clues and what they suggest
- Blood and bloodstain patterns: location, volume, directionality; can indicate actions, positions, and sequence (handled by trained analysts).
- Fingerprints and latent prints: point to who touched surfaces.
- Footwear/tire impressions: can indicate movement, direction, and make/model.
- Fibers, hair, trace materials: link people, places, or objects.
- Weapons, shell casings, tool marks: identify means and potential matching to suspect items.
- Digital evidence (phones, CCTV, computers): location, timeline, communications.
- Biological samples (saliva, semen, tissue): identity and contact; handled by lab.
- Chemical residues (flammables, drugs): motive or activity at scene.
- Scene disturbances and broken objects: signs of struggle or staging.
Notes and cautions
- Preserve chain of custody and detailed documentation—these are critical for evidence admissibility.
- Avoid giving or following detailed, technical instructions that could be misused to avoid detection.
- If this is a real scene and not a fictional exercise, contact local law enforcement/forensic professionals and do not interfere.
Tell me which direction you want (fictional scene, detailed checklist/procedures, or a focused list of clues to look for) and I’ll proceed.
Prompt: You find a strange object at the crime scene. What is it?
Response: I can help — but I need more detail. First: are you at a real scene (safety/law-enforcement situation) or is this for a story/game/brainstorming? If it’s real, do not touch it and call the appropriate authorities (police/forensics/bomb squad) immediately. If this is fictional or you can describe it, tell me shape, size, color, markings, material, where it was found and what else was nearby.
While you give more info, here’s a practical way to think about and handle a “strange object” and the common possibilities so you can describe it more precisely.
Immediate responder priorities (do not handle unless trained)
- Safety first: evacuate/cordon the area if the object looks hazardous (smoke, wires, leaking fluid, strong odor) or if you have any suspicion of an explosive/chemical device.
- Do not power on or plug in electronics. Don’t open containers. Don’t taste or sniff.
- Photograph the object in place from multiple angles and note measurements and position relative to other evidence.
- Notify law enforcement/forensics/bomb squad/HAZMAT as appropriate.
Key descriptive details that help identification
- Size and shape (e.g., coin-sized disk, cylinder 10 cm long, box 20×15×10 cm)
- Material (metal, plastic, glass, fabric, organic)
- Color and markings (labels, serial numbers, logos, handwriting, stains)
- Components (wires, batteries, circuit boards, switches, needles, residue)
- Smell (chemical, solvent, fuel, no smell)
- Contents or openings (sealed, screw-top, holes)
- Context (on victim, in vehicle, in trash can, in mailbox)
- Any visible biological traces (blood, hair, tissue) or powders/liquids
Common possibilities and indicators
- Weapon or weapon component: blood, impact marks, obvious blade or barrel.
- Drug paraphernalia: small glass pipes, syringes, baggies with residue, burnt areas, white powders.
- Electronic/data device: USB drives, SD cards, phones, small circuit boards; may have LEDs, ports, labels.
- Tracking device: small sealed box with antenna, battery, often taped to underside of vehicle.
- Improvised explosive device (IED): batteries, wiring, switches, timers, unknown powder or liquid — treat as suspicious and evacuate.
- Personal item (keys, jewelry, ID): obvious markings, names, brand logos.
- Biological/medical device: vials, syringes, implanted medical devices (tags, serials).
Forensic handling basics (for trained personnel)
- Photograph/measure before moving. Use gloves and avoid contaminating surfaces.
- Package dry trace evidence in paper (not plastic). Seal wet/biological evidence in breathable packaging.
- Collect swabs for DNA from relevant surfaces; lift latent prints if appropriate.
- For electronics, do not charge or power. Place in anti-static container or Faraday bag to preserve data.
- Record chain of custody every time the object changes hands.
If you want me to identify it, describe it now or upload a photo (if your interface supports images). If this is a real scene and you’re unsure or worried, call emergency services — do not handle it yourself.
Prompt: How does this object relate to the crime?
Response: I don’t have details yet — what object and what kind of crime? With that I can be specific. Meanwhile, here are the common ways an object can relate to a crime and the practical steps/tests used to establish those links.
Possible relationships between an object and a crime
- Instrument of the crime: the object was used to commit the offense (weapon, tool for forced entry, vehicle).
- Transfer/trace evidence carrier: the object bears biological or physical traces (blood, hair, fibers, fingerprints) from victim or perpetrator.
- Possession or concealment: the object was owned, carried, or hidden by a suspect (stolen goods, contraband).
- Location/time indicator: the object ties a person to a place or timeframe (a phone with location data, a receipt, CCTV footage).
- Identification/ownership: serial numbers, registration, purchase records link an item to a person or source.
- Exculpatory evidence: the object can support innocence (alibi corroboration, third‑party DNA).
- Secondary/contextual evidence: the object provides circumstantial context (clothing showing pattern of events, a note suggesting motive).
Analyses and tests commonly used
- Latent fingerprint examination — links handling to an individual.
- DNA analysis (swabs for blood, saliva, skin cells) — can identify or exclude persons.
- Blood or bodily-fluid testing and bloodstain pattern analysis — establishes presence and actions.
- Ballistics and tool‑mark analysis — links bullets, casings, or tool marks to a specific weapon or tool.
- Gunshot residue (GSR) testing — indicates recent firing of a gun.
- Trace evidence (fibers, hair, glass, paint, soil) — can connect people, objects, and locations.
- Chemical or toxicology tests — for drugs, accelerants, poisons.
- Digital forensics — recovers metadata, timestamps, location, communications from phones/computers.
- Serial number tracing and purchase/registration records — tie object to buyer/owner.
- CCTV and witness correlation — places object/suspect at scene or time.
Practical evidence‑handling considerations
- Document everything: photos (with scale), notes on where/how found, time and environment.
- Avoid contamination: wear gloves, minimize handling, keep separate packaging for different items.
- Maintain chain of custody: who collected, when, transfers — essential for admissibility.
- If you’re not an investigator, contact police or a qualified lab rather than doing invasive tests yourself.
Next steps I can help with
- If you describe the object and the crime, I can suggest the most relevant forensic analyses and what a positive/negative result would mean.
- If you have specific test results (e.g., “DNA from the object matched X”), I can help interpret strength and limitations of that evidence.
- If you’re deciding whether to turn the item over to police, tell me your situation and I’ll advise.
Tell me what the object is and any context you have, and I’ll be more specific.
Prompt: Who do you think is the suspect and why?
Response: I don’t have any case details yet — could you share the facts you want me to evaluate? If you paste the scenario, timeline, evidence, and who the possible suspects are, I can reason through it.
If you’d like a quick guide for deciding who’s most likely to be the suspect, use this framework:
1. Motive — who benefits or has a reason to do it? (money, revenge, jealousy, covering something up)
2. Opportunity — who had the chance and access at the right time/place?
3. Means — who had the ability, tools, or skills needed?
4. Behavioural indicators — unusual actions, evasiveness, inconsistent statements, sudden changes (e.g., fleeing, destroying evidence).
5. Physical and forensic links — DNA, fingerprints, weapon, surveillance, phone/GPS location.
6. Corroborating witness/account consistency — do multiple independent sources point to the same person?
7. Alibi strength — is an alibi verified or weak/inconsistent?
8. Mobile/financial/digital traces — calls, messages, transactions, searches that align with the incident.
9. Past history — relevant prior offenses or patterns (used cautiously).
Example (short): if Person A had a clear motive (large life insurance), was alone with the victim when the incident happened (opportunity), their phone pinged the scene (digital trace), and their alibi is shaky, they become the primary suspect — unless forensic evidence or reliable witnesses exonerate them.
If you’re asking about a real person or real crime, I can’t make formal accusations — I can only analyze the evidence you provide and point out who looks most likely and what further checks to run. Share the details and I’ll walk through the reasoning.
Step 4: Analyze the output
Chain of thought prompting works by forcing the model to break down a complex problem into a sequential series of intermediate reasoning steps before delivering the final answer.
Here are the exact words that trigger Chain of Thought (CoT) for each example:
Example 1: "let's analyze the sentiment of the review step by step"
Example 2: "let's sort the values of the list step by step"
Example 3: "Explain step-by-step how you can find the odd ball" and "Think through the problem carefully and explain your reasoning in detail before giving the final answer."
Evaluate the model’s reasoning by experimenting with different analytical and logical prompts.
# Example 1: Sentiment analysis with reasoning steps
prompt = """
let's analyze the sentiment of the review step by step
1. Identify the Positive aspect of the review and give a score from 10
2. Identify the Negative aspect of the review and give a score from 10
3. Weight the positive and negative aspect to determine the overall sentiment.
4. provide the final sentiment classification with justification for scores used in above steps.
Review: "The product is very well designed product"
"""
response = get_completion(prompt)
print(response)
Chain of thought prompting works by forcing the model to break down a complex problem into a sequential series of intermediate reasoning steps before delivering the final answer.
This example sets up Chain of Thought (CoT) by utilizing the explicit phrase "let's analyze the sentiment of the review step by step" combined with a numbered sequence of analytical constraints ("1. Identify... 2. Identify... 3. Weight... 4. provide..."). By forcing the model to calculate positive and negative aspects individually before executing a weighted justification, these specific text instructions prevent a rapid, unreasoned snap judgment. Because an LLM calculates the probability of each subsequent token based on everything previously written, this structured text acts as a rolling logical scratchpad, grounding the final classification securely in the model's own displayed reasoning.
1) Positive aspect (+ score out of 10)
- Aspect: Clear praise of design — the reviewer says the product is "very well designed".
- Score: 9/10
- Rationale: Strong, unambiguous positive language. Points not maxed because the statement is brief and lacks specifics.
2) Negative aspect (+ score out of 10)
- Aspect: Minor issues only — grammatical redundancy ("product" repeated) and no detail/context.
- Score: 1/10
- Rationale: No substantive complaint about the product itself; only minor presentation/detail shortcomings.
3) Weighting to determine overall sentiment
- Estimated weights: Positive 90%, Negative 10% (based on presence and strength in the text).
- Combined score = 9 * 0.90 + 1 * 0.10 = 8.2 / 10
4) Final sentiment classification with justification
- Classification: Positive (strongly positive)
- Justification: The review contains an unambiguous compliment about design ("very well designed") with no substantive complaints. The only negatives are negligible (redundant wording and lack of detail), so the overall sentiment is strongly positive, reflected by the combined score of 8.2/10.
# Example 2: Logical sequence reasoning
prompt = """
let's sort the values of the list step by step
1. Start with the unsorted list.
2. Compare each elements and find the smallest value.
3. Place the samllest value in the first position.
4. Repeat the process for all the remaming elements.
5. provide the sorted list.
Sort the list : [3,1,4,6,5,9,2]
"""
response = get_completion(prompt)
print(response)
One-Sentence Explanation
This works because compiling the sorting instructions, the raw array data, and the step-by-step algorithmic constraints into a single prompt string forces the model to use its own generated text as an active tracking scratchpad to compute the final ordered list accurately.
Technical Breakdown
This example sets up Chain of Thought (CoT) by utilizing the explicit phrase "let's sort the values of the list step by step" combined with a numbered sequence of algorithmic rules ("1. Start... 2. Compare... 3. Place... 4. Repeat... 5. provide..."). By forcing the model to visually execute each pass of a selection sort algorithm on the screen before declaring the final result, these specific text instructions prevent the LLM from trying to predict the entire sorted list in a single, unreasoned flash. Because an LLM calculates the probability of each subsequent token based on everything previously written, this structured text acts as a rolling logical scratchpad, anchoring the final sorted array securely in the model's own displayed processing steps.
Initial (unsorted) list:
[3, 1, 4, 6, 5, 9, 2]
Pass 1 (find smallest in whole list → 1). Swap with first position:
[1, 3, 4, 6, 5, 9, 2]
Pass 2 (find smallest in remaining [3,4,6,5,9,2] → 2). Swap with position 2:
[1, 2, 4, 6, 5, 9, 3]
Pass 3 (find smallest in remaining [4,6,5,9,3] → 3). Swap with position 3:
[1, 2, 3, 6, 5, 9, 4]
Pass 4 (find smallest in remaining [6,5,9,4] → 4). Swap with position 4:
[1, 2, 3, 4, 5, 9, 6]
Pass 5 (remaining [5,9,6] → smallest 5, already in place):
[1, 2, 3, 4, 5, 9, 6]
Pass 6 (remaining [9,6] → 6). Swap with position 6:
[1, 2, 3, 4, 5, 6, 9]
Pass 7: one element left — sorted.
Sorted list:
[1, 2, 3, 4, 5, 6, 9]
# Example 3: Analytical problem-solving reasoning
prompt = """
You have 12 identical-looking balls, but one is either heavier or lighter.
You have a balance scale and can only use it three times.
Explain step-by-step how you can find the odd ball and determine whether it is heavier or lighter.
Think through the problem carefully and explain your reasoning in detail before giving the final answer.
"""
response = get_completion(prompt)
print(response)
One-Sentence Explanation
This works because compiling the multi-step weighing rules, the physical logic constraints of the scale, and explicit analytical directives into a single prompt string forces the model to use its own generated text as an active branch-tracking scratchpad to navigate the complex decision tree accurately.
Technical Breakdown
This example sets up Chain of Thought (CoT) by utilizing explicit reasoning directives ("Explain step-by-step how you can find the odd ball...") combined with intense meta-cognitive guidelines ("Think through the problem carefully and explain your reasoning in detail before giving the final answer."). By forcing the model to exhaustively map out and document every branching physical outcome (Scale Balances vs. Scale Tips) of the classic 12-ball riddle before arriving at a final conclusion, these specific text instructions prevent the LLM from generating an erratic, uncalculated guess.
Because an LLM calculates the probability of each subsequent token based on everything previously written, this structured text acts as a rolling logical scratchpad, anchoring the massive conditional logic tree securely in the model's own displayed processing steps.
Label the balls 1 through 12. You are allowed three weighings on a balance scale. Proceed as follows.
Weighing 1
- Weigh 1+2+3+4 against 5+6+7+8.
There are two possible outcomes: the scale balances, or it does not.
Case A — First weighing balances
- Conclusion after weigh 1: none of 1–8 is odd; the odd ball is one of 9,10,11,12.
Weighing 2 (in this branch)
- Weigh 1+2+3 (known good) against 9+10+11.
If weigh 2 balances
- Conclusion: 12 is the odd ball.
- Weighing 3: weigh 12 against any known-good ball (say 1). If 12 is heavier, it’s the heavy odd ball; if lighter, it’s the light odd ball.
If weigh 2 does not balance
- Because 1,2,3 are known good, the imbalance identifies whether one of 9,10,11 is heavy or light:
- If the side with 9–11 goes down, the odd ball is one of 9,10,11 and it is heavier.
- If the side with 9–11 goes up, the odd ball is one of 9,10,11 and it is lighter.
- Weighing 3: weigh 9 against 10.
- If they balance, 11 is the odd ball and its type (heavy or light) is as deduced from weigh 2.
- If they do not balance, the heavier or lighter side (matching the type deduced from weigh 2) tells you which of 9 or 10 is the odd ball and whether it is heavy or light.
Case B — First weighing does not balance
- Without loss of generality, suppose 1+2+3+4 (left pan) is heavier than 5+6+7+8 (right pan). (If the opposite happened, swap the roles in the instructions below.)
- Conclusion after weigh 1: the odd ball is among 1–8; specifically, it is either one of 1,2,3,4 and heavy, or one of 5,6,7,8 and light.
Weighing 2 (in this branch)
- Weigh 1+2+5 (left) against 3+6+9 (right). Note: 9 is known-good because it was not on the scale in weighing 1.
Analyze weigh 2:
- If weigh 2 balances
- Then 1,2,3,5,6 are normal. The odd ball must be either 4 (heavy) or 7 or 8 (light).
- Weighing 3: weigh 7 against 8.
- If they balance, 4 is the odd ball and it must be heavy.
- If they do not balance, the lighter one is the odd ball (and it is light).
- If weigh 2 is unbalanced and the left pan (1+2+5) is heavier
- Then the possibilities consistent with the two weighings are: 1 is heavy, 2 is heavy, or 6 is light.
- Weighing 3: weigh 1 against 2.
- If they balance, 6 is the odd ball and it is light.
- If they do not balance, the heavier one of 1 and 2 is the odd ball and it is heavy.
- If weigh 2 is unbalanced and the right pan (3+6+9) is heavier (i.e., left is lighter)
- Then the possibilities consistent with the two weighings are: 3 is heavy or 5 is light.
- Weighing 3: weigh 3 against a known-good ball (e.g. 9).
- If 3 balances with 9, then 5 is the odd ball and it is light.
- If 3 is heavier than 9, 3 is the odd ball and it is heavy.
That completes all branches. Every possible outcome of the three weighings identifies exactly which ball is odd and whether it is heavier or lighter.
Conclusion
By following these steps, you have successfully used chain-of-thought prompting with LangChain and the OpenAI API. This approach allows the model to reason in structured steps, helping it break down complex problems and produce clear, logical explanations for analytical, numerical, and reasoning-based tasks.
No comments:
Post a Comment