Thursday, May 21, 2026

4.2 - 3 Demo Tree of Thoughts

 

4.2 - 3 Tree of Thoughts







 Tree of Thought Prompting

Objective:

To perform tree-of-thought prompting using LangChain and the OpenAI API, guiding the model to explore multiple reasoning paths, evaluate possibilities, and converge on the most effective solution.


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:

  1. Set up the OpenAI API key

  2. Define a function to get completion

  3. Define your prompts


Step 1: Set up the OpenAI API key

  • Import the required libraries and set up the OpenAI API key

  • The os library is used to interact with the operating system, and openai is used to communicate with the OpenAI API.

import os

from openai import OpenAI

client = OpenAI()

Step 2: Define a function to get completion

  • Define a helper function to send user prompts and retrieve responses from the model

  • Call the client.chat.completions.create method to get a response from the model

  • The temperature parameter is set to 0 for deterministic (non-random) responses

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 prompts that guide the model to explore multiple reasoning branches or “thought paths”

  • Call get_completion to get a response from the AI model

  • Print both the prompt and the AI-generated response

# Example 1: Maximizing area with given fencing

prompt = """

Solve the problem: A farmer has 100 meters of fencing and wants to enclose the maximum area for his rectangular field. What should the dimensions be?

Let's think about this in a few ways:

1. If the field is a square, each side would be 100 / 4 = 25 meters. The area would be 25 * 25 = 625 square meters.

2. What if the field is not a square? Let's try a 4:1 ratio. The lengths would be 40 and 10 meters. The area would be 40 * 10 = 400 square meters.

3. Are there any other ratios that might give a larger area than a square or a 4:1 rectangle?

Considering these options and reason out on own and then output the best dimensions for the maximum area :

"""

response = get_completion(prompt)

print("AI Response:")

print(response)


One-sentence definition: Tree of Thoughts prompting works by breaking a complex problem down into distinct, intermediate steps ("thoughts") so the model can explore multiple alternative paths, evaluate its progress, and backtrack if it hits a dead end.

Keywords in this prompt driving Tree of Thoughts:

  1. "think about this in a few ways" — This is the branching instruction, forcing the model to generate multiple alternative paths (a square vs. a 4:1 rectangle) instead of a single linear answer.

  2. "1. / 2. / 3." — These act as the structural nodes or "thought" milestones where the model maps out and calculates the viability of each distinct shape.

  3. "Considering these options and reason out on own" — This triggers the self-evaluation phase, telling the LLM to actively compare the results ($625\text{ m}^2$ vs. $400\text{ m}^2$) to judge which branch is successful.

  4. "output the best dimensions" — This acts as the final path selection, instructing the system to prune away the inefficient rectangular branches and commit to the optimal square configuration.



AI Response:

You get the maximum area when the rectangle is a square: 25 m by 25 m, area 625 m².


Reason (several quick ways):


1) Algebra/parabola: Let sides be x and y with 2(x+y)=100, so y = 50 − x. Area A(x) = x(50 − x) = −x² + 50x, a downward parabola whose vertex is at x = −b/(2a) = 25. So A is maximized at x = 25, y = 25, area = 25·25 = 625.


2) AM–GM: For fixed sum x + y = 50, arithmetic mean ≥ geometric mean gives

( (x+y)/2 )² ≥ xy, so (25)² ≥ xy, hence xy ≤ 625 with equality when x = y = 25.


(If you allowed any shape, the circle would give an even larger area for the same perimeter; but among rectangles the square is best.)


# Example 2: Analyzing a legal case with multiple reasoning branches

prompt = """

Let's analyze the legal case by considering multiple precedents and possible outcomes.

1. Precedent 1: A similar case where the plaintiff won.

   - Branch A: The court found that the defendant was negligent.

       - Sub-branch A1: The plaintiff was awarded damages due to clear evidence of negligence.

       - Sub-branch A2: The court ruled in favor of the plaintiff due to the defendant's breach of duty.

2. Precedent 2: A similar case where the defendant won.

   - Branch B: The court found no negligence on the defendant's part.

       - Sub-branch B1: The plaintiff failed to provide sufficient evidence.

       - Sub-branch B2: The court ruled that the plaintiff assumed the risk.

3. Precedent 3: A case with a mixed outcome.

   - Branch C: The court found both parties partially at fault.

       - Sub-branch C1: Damages were reduced based on the plaintiff's contributory negligence.

       - Sub-branch C2: The court ruled that both parties shared liability, resulting in a split decision.

4. Based on the facts of the current case, consider the most likely outcome.

"""

response = get_completion(prompt)

print(response)


The specific keywords making this legal prompt function as a Tree of Thoughts (ToT) framework are:

  1. "multiple precedents and possible outcomes" — This is the initial branching instruction, telling the model to break the problem space into distinct, parallel legal arguments instead of a single linear conclusion.

  2. "Precedent 1 / 2 / 3" — These act as the primary root nodes of the reasoning tree, categorizing the three completely different legal directions the case could take.

  3. "Branch A / B / C" and "Sub-branch A1 / A2 / B1..." — These are the deep hierarchical nodes. They force the model to explore deep logical paths and look ahead at the granular legal justifications within each precedent.

  4. "consider the most likely outcome" — This is the evaluation and pruning mechanism. It instructs the model to compare the sub-branches against the current case facts, throw out the irrelevant precedents (pruning the wrong branches), and commit to the most legally sound final path.



I can do that — but I don’t yet have the facts of the current case, so I can’t sensibly pick the single “most likely” outcome. Below I’ll (1) summarize how the three precedents map to fact patterns and legal findings, (2) list the key factual and legal issues that push a case toward each precedent, and (3) give a simple decision-framework you can apply to your case. If you give me the specific facts (and the governing jurisdiction), I’ll apply this framework and tell you which precedent is most analogous and why.


1) How each precedent aligns with fact patterns and findings

- Precedent 1 (plaintiff won)

  - Typical findings: duty existed, defendant breached standard of care, breach caused plaintiff’s loss, damages proven.

  - Strong indicators: contemporaneous documentation or credible witness testimony of the breach; expert testimony showing departure from accepted practice; clear causal link from defendant’s act/omission to plaintiff’s injury; measurable damages.

  - Outcome variants:

    - A1: Clear, undisputed evidence of negligence — full damages awarded.

    - A2: Court frames the result as a breach of duty even if facts were mixed — plaintiff still wins.


- Precedent 2 (defendant won)

  - Typical findings: no actionable negligence or plaintiff’s case fails on proof; alternative defenses succeed (assumption of risk, consent, compliance with standard of care).

  - Strong indicators: lack of direct evidence tying defendant to a breach; credible proof that plaintiff voluntarily faced a known risk; defendant complied with industry standards or statutory requirements; intervening cause.

  - Outcome variants:

    - B1: Plaintiff’s proof is insufficient on elements (duty, breach, causation, damages).

    - B2: Plaintiff knew and accepted the risk (assumption of risk) or other affirmative defense negates liability.


- Precedent 3 (mixed outcome)

  - Typical findings: both sides share fault (comparative or contributory negligence, depending on jurisdiction), or the court apportions liability.

  - Strong indicators: evidence that the plaintiff’s own conduct contributed materially to the harm; both parties deviated from ideal behavior; factual disputes about primary causation but clear shared causation.

  - Outcome variants:

    - C1: Plaintiff’s recoverable damages reduced because of their contributory negligence (percentage reduction).

    - C2: Court allocates specific shares of liability and splits damages accordingly.


2) Key factual and legal issues that push the case to each outcome

- Factors favoring a plaintiff win (Precedent 1)

  - Clear, contemporaneous evidence of defendant’s breach (photos, logs, video, admissions).

  - Strong expert testimony establishing standard of care and breach.

  - Direct causal chain from breach to the harm with little intervening cause.

  - No credible argument that plaintiff knowingly assumed the risk.

  - Statutory/regulatory violation by defendant (negligence per se).


- Factors favoring a defendant win (Precedent 2)

  - Weak or circumstantial evidence, gaps in causation, or credibility problems with plaintiff’s witnesses.

  - Plaintiff had actual knowledge of the risk and voluntarily proceeded.

  - Defendant complied with objective standards or industry practice.

  - An intervening, superseding cause breaks causation.

  - Procedural problems (statute of limitations, failure to preserve evidence).


- Factors favoring shared liability (Precedent 3)

  - Evidence that both sides acted unreasonably or negligently.

  - Conflicting accounts that both could plausibly have caused the harm.

  - Jurisdiction applies comparative negligence, enabling a partial recovery rather than complete bar.

  - Damages are divisible or can be apportioned.


3) Quick decision framework you can apply to your facts

Ask these questions in order; the first decisive answer points to the likely branch:


A. Is there proof of duty, breach, causation, and quantum of damages by a preponderance of evidence?

   - Yes → Precedent 1 (plaintiff likely prevails; A1/A2 depending on clarity).

   - No → go to B.


B. Did plaintiff knowingly assume the risk or consent to the activity that caused the harm?

   - Yes → Precedent 2 (B2).

   - No → go to C.


C. Is plaintiff’s evidence simply too weak on one or more elements (gaps, credibility issues)?

   - Yes → Precedent 2 (B1).

   - No → go to D.


D. Is there evidence that plaintiff’s conduct contributed to the harm?

   - Yes → Precedent 3 (C1/C2 depending on degree and jurisdictional rules).

   - No → If none of the above applies, reassess for statutory defenses or unique facts; may still fall under Precedent 1 if re-examined.


4) Jurisdictional rule that changes outcomes

- If your jurisdiction follows pure comparative negligence, the plaintiff can recover reduced damages even if mostly at fault (favours partial recovery).

- If it follows contributory negligence (rare), any fault by plaintiff can bar recovery (favors defendants).

- Assumption of risk, indemnity clauses, and statutory immunity can be dispositive defenses in many places.


5) Evidence and strategic steps that matter most

- Preserve contemporaneous evidence (photos, logs, surveillance).

- Obtain expert reports early to establish standard of care and causation.

- Find witnesses who can corroborate timeline and actions.

- Gather any contractual waivers, warnings given to plaintiff, or compliance certifications for defendant.

- Identify any statutory or regulatory violations by defendant (negligence per se).


If you provide the specific facts (what happened, who did what when, what physical evidence and witnesses exist, the damages, and which jurisdiction applies), I will apply the above framework and say which precedent is the closest match and the most likely outcome, with reasoning and practical steps to strengthen the position you want.


# Example 3: Solving an analytical reasoning problem through multiple potential strategies

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.

Think step-by-step, and at each step, explore multiple possible ways to proceed.

Evaluate which options are promising and continue expanding those paths.

Discard any unhelpful paths. After exploring the reasoning tree, provide the best solution.

"""

response = get_completion(prompt)

print("AI Response:")

print(response)


The specific keywords making this classic 12-ball puzzle function as a Tree of Thoughts (ToT) framework are:

  1. "explore multiple possible ways to proceed" — This is the core branching instruction, commanding the model to split its thinking into alternative pathways (e.g., weighing 4 vs 4, or 6 vs 6) at every decision point.

  2. "at each step" — This sets up the sequential milestones or nodes, ensuring the model treats the problem as a multi-layered tree structure rather than a single, flat calculation.

  3. "Evaluate which options are promising and continue expanding" — This triggers the lookahead and selection mechanism, forcing the LLM to judge the viability of its current thought branch before investing deeper text generation into it.

  4. "Discard any unhelpful paths" — This is the explicit pruning instruction. It forces the model to drop dead ends, while the overall search structure allows it to backtrack to a previous valid weighing strategy if a chosen path fails to isolate the odd ball.


AI Response:

High-level idea and choice of first weighing

- With 3 weighings a balance scale has at most 3^3 = 27 distinct outcome sequences, which is enough to distinguish 12 × 2 = 24 possibilities (which ball, heavy or light). So a solution exists.

- A good first move is to split the 12 into two groups of 4 (4 vs 4). That gives a clean 3-way branch: left heavy, right heavy, or balance. If the first weighing balances, the odd ball is among the 4 that were not weighed; if it doesn’t balance, the odd ball is among the 8 on the scale and we also know whether the left group is heavier or lighter than the right. This symmetry makes it possible to finish in two more weighings.


Label the balls 1..12.


Weighing 1: 1+2+3+4 vs 5+6+7+8


There are three branches. I’ll expand each, evaluate options for weighing 2, prune, and give weighing 3 that settles it.


Branch A — W1 balances

- Conclusion after W1: balls 1–8 are normal. The odd ball is among {9,10,11,12}.

- Candidate types: each of these could be heavy or light (8 possibilities).


Weighing 2 (good option): 1+2+3 (known normal) vs 9+10+11 (three suspects)

- Why this is promising: placing known normals against three suspects isolates whether 9–11 contain the odd ball and whether it’s heavy or light; if this balances, the odd is 12 and third weighing is trivial.


Outcomes of W2:

1) W2 balances → odd = 12. Do W3: weigh 12 vs 1 (a normal). If 12 heavier → 12 is heavy; if lighter → 12 is light.

2) W2 tips right (9–11 side heavy) → because 1–3 are normal, one of 9–11 is heavy. Do W3: weigh 9 vs 10.

   - If 9 = 10 → 11 is heavy.

   - If 9 heavier than 10 → 9 is heavy.

   - If 9 lighter than 10 → 10 is heavy.

3) W2 tips left (1–3 side heavy) → because 1–3 are normal, one of 9–11 is light. Do W3: weigh 9 vs 10.

   - If 9 = 10 → 11 is light.

   - If 9 lighter than 10 → 9 is light.

   - If 9 heavier than 10 → 10 is light.


Branch B — W1: left side heavier (1–4 side heavier than 5–8)

- Conclusion after W1: odd ball is among {1,2,3,4,5,6,7,8} and is either one of 1–4 heavy or one of 5–8 light. Balls 9–12 are normal (useful reference balls).

- We need a second weighing that separates the 8 possibilities into small groups resolvable in the last weighing. A good and standard choice is:


Weighing 2: 1+2+5 (left) vs 3+6+9 (right). (9 is known normal because W1 implicated only 1–8.)


Why this is promising: it gives three small outcome-sets that can each be resolved by a single last weighing.


Evaluate W2 outcomes:

- W2 balances → the balls on the scale (1,2,3,5,6,9) are all normal. From the W1 deduction (left heavy), remaining candidates are 4 (heavy), 7 (light), 8 (light). Three possibilities resolved by one weighing.

   - Weighing 3: compare 7 vs 8.

     - If 7 = 8 → 4 is heavy.

     - If 7 lighter → 7 is light.

     - If 8 lighter → 8 is light.

- W2 tips left (1+2+5 heavier than 3+6+9) → from earlier logic the possibilities consistent with W1(left heavy) are: 1 heavy, 2 heavy, or 6 light. (Check: 5 is on left but a light 5 would make left lighter, so 5 light is inconsistent; 3 heavy would make right heavy, inconsistent.)

   - Weighing 3: compare 1 vs 2.

     - If 1 = 2 → 6 is light.

     - If 1 heavier than 2 → 1 is heavy.

     - If 1 lighter than 2 → 2 is heavy.

- W2 tips right (1+2+5 lighter than 3+6+9) → possibilities: 3 heavy or 5 light.

   - Weighing 3: compare 3 vs 9 (9 known normal).

     - If 3 = 9 → 5 is light.

     - If 3 heavier than 9 → 3 is heavy.


Branch C — W1: right side heavier (1–4 lighter than 5–8)

- This is symmetric to Branch B by swapping the roles of the two sides. Conclusion: odd ball among 1–8; either 1–4 is light or 5–8 is heavy. Balls 9–12 are normal.

- Use the symmetric second weighing: 5+6+1 (left) vs 7+2+9 (right) — or, more simply, reuse the same pattern but swapping labels. One convenient way: just swap labels in the plan for Branch B. Concretely, do W2: 5+6+1 vs 7+2+9 (or construct the mirror of the prior W2). Following the same logical mapping you will get three small candidate sets; weigh appropriately to distinguish them in W3. (Equivalently, you can apply the exact mirror of the branching and third weigh described in Branch B.)


Summary of a full concrete, easy-to-follow plan (compact):

1) W1: weigh {1,2,3,4} vs {5,6,7,8}.

   - If balance → go to Branch A steps below.

   - If left heavier → go to Branch B steps below.

   - If right heavier → do the mirror of Branch B.


Branch A (W1 balanced):

2) W2: weigh {1,2,3} vs {9,10,11}.

   - If balance → W3: 12 vs 1 → decides heavy/light.

   - If right heavier → W3: 9 vs 10 → decide which of 9,10,11 is heavy.

   - If left heavier → W3: 9 vs 10 → decide which of 9,10,11 is light.


Branch B (W1 left heavy):

2) W2: weigh {1,2,5} vs {3,6,9} (9 is normal).

   - If balance → W3: 7 vs 8 (resolves 4 heavy or 7/8 light).

   - If left heavier → W3: 1 vs 2 (resolves 1 heavy, 2 heavy, or 6 light).

   - If right heavier → W3: 3 vs 9 (resolves 3 heavy or 5 light).


Branch C (W1 right heavy):

- Mirror Branch B: interchange the roles of the two sides (or implement symmetric weighing), and you can distinguish the cases in the same manner.


This plan covers all 24 possibilities and determines both which ball is odd and whether it is heavier or lighter within three weighings.


# Example 4: Planning using a reasoning tree for everyday decisions

prompt = """

Let's plan a weekend trip by considering multiple options.

1. Option 1: Go to the mountains.

   - Branch A: If the weather is good in the mountains.

       - Sub-branch A1: You can go hiking.

       - Sub-branch A2: You can visit a nearby lake.

   - Branch B: If the weather is bad in the mountains.

       - Sub-branch B1: You will stay in a cabin and relax.

       - Sub-branch B2: You can explore local museums.

2. Option 2: Go to the beach.

   - Branch C: If the weather is sunny at the beach.

       - Sub-branch C1: You can swim in the ocean.

       - Sub-branch C2: You can sunbathe and play beach volleyball.

   - Branch D: If the weather is cloudy or rainy at the beach.

       - Sub-branch D1: You will visit indoor attractions like an aquarium.

       - Sub-branch D2: You can go shopping in beachside stores.

3. Considering all these factors, decide the best option for your weekend trip.

What should i do for my trip to Goa in month of august

"""

response = get_completion(prompt)

print(response)


The specific keywords making Example 4 function as a Tree of Thoughts (ToT) framework are:

  • "considering multiple options" — This is the core branching instruction, commanding the model to split its thinking into alternative destination pathways (e.g., Option 1: Mountains or Option 2: Beach) right from the start.

  • "Branch A / Branch B / Branch C / Branch D" — This sets up the conditional reasoning nodes, establishing the explicit tree structure where the model must evaluate different environmental changes like good weather versus bad weather.

  • "Sub-branch A1 / A2 / B1 / B2 / C1 / C2 / D1 / D2" — This triggers the deeper multi-layered expansion mechanism, forcing the LLM to drill down into specific activity choices within each weather condition rather than keeping the plan broad.

  • "Considering all these factors, decide the best option" — This is the explicit evaluation and selection instruction. It forces the model to review every single active pathway it just explored, weigh the pros and cons of each branch, and select the single most optimal trip solution based on those steps.


Short answer: Yes — go to Goa in August, but plan for monsoon weather. Focus on monsoon-friendly activities (waterfalls, spice plantations, heritage, spas, river cruises, indoor attractions) rather than beach swimming. If you insist on beach time, be prepared for rough seas and limited swimming.


Why go in August

- Pros: dramatic green scenery, spectacular waterfalls (peak flow), fewer tourists, lower prices, cooler temperatures, lively monsoon culture/food.  

- Cons: frequent rain, rough sea currents (NOT safe for swimming), occasional road/transport disruption.


Top recommended activities for August

- Dudhsagar Falls (jeep/trek when conditions permit) — best in monsoon but check safety/permits.  

- Spice plantation tour (Ponda) — great in wet season, with local food.  

- Old Goa churches and museums (Basilica of Bom Jesus, Se Cathedral, Goa State Museum).  

- Panjim/Fontainhas walking tour (Portuguese Quarter, cafés, street photography).  

- Mandovi river cruise (evening cruises and river casinos are good rainy-night options).  

- Ayurveda, yoga, spa treatments and cooking classes — ideal for rainy days.  

- Waterfall/river hikes and forest walks (guided only; avoid during heavy downpours).  

- Food: seafood restaurants, local monsoon snacks (misal, bhaji-puri, hot chai & pakoras).  

- Markets/shops: local handicrafts, flea markets (check schedules — off-season can mean reduced days).


Safety and practical tips

- Do NOT swim if the sea is rough; obey beach flags and lifeguards. Monsoon currents are dangerous.  

- Check local forecasts and road conditions daily; hire local guides for treks/jeep safaris.  

- Book accommodations with flexible cancellation and good drainage (flooding can happen).  

- Bring rain gear: waterproof jacket, quick-dry clothes, waterproof footwear or sandals with grip, small umbrella, dry bags for electronics.  

- Carry insect repellent and basic meds; roads/trails get slippery — wear good shoes.  

- If planning Dudhsagar, check if the trail/rail access is open — sometimes closed in heavy rains.


Where to stay (depending on vibe)

- North Goa (Baga/Calangute/Anjuna): lively nightlife, restaurants — better if you want cafes/casinos/indoor entertainment.  

- Panjim/Fontainhas: central, cultural, good for heritage walks and river cruises.  

- South Goa (Palolem/Agonda): quieter, scenic, better for nature and relaxation — but check access in heavy rains.


Sample 2-day weekend itineraries


A) Monsoon-friendly weekend (recommended for August)

Day 1: Morning arrive → check in → visit a spice plantation and have lunch. Afternoon/early evening: Panjim/Fontainhas walk + café. Night: Mandovi river cruise or casino.  

Day 2: Morning: Dudhsagar jeep/short guided trek (if open & safe) or nearby waterfalls/forest walk. Afternoon: spa/Ayurveda session and local seafood dinner. Depart evening.


B) If you get a sunny spell

Day 1: Early morning beach walk (Calangute/Baga or Palolem) and breakfast → water sport/safe swimming in supervised sections → lunch. Afternoon: Old Goa churches + Dona Paula viewpoint. Night: nightlife in North Goa or quiet beach shack in South Goa.  

Day 2: Boat trip to nearby islands or a long beach day + sunset at a beach shack, then depart.


Final recommendation

If you want lush scenery, waterfalls, culture, good food, and lower prices — definitely go to Goa in August and plan for monsoon activities. If your primary aim is safe beach swimming and full beach-party atmosphere, pick another month (Nov–Feb or Mar–May). 


If you tell me which kind of vibe you prefer (relaxation/spa, adventure/waterfalls, nightlife, or beach sports), how many days you have, and whether you prefer North or South Goa, I can give a tailored day-by-day plan and specific hotel/restaurant suggestions.


Analyze the output generated by the model. If the results are not satisfactory, refine the prompts and run the code again to explore improved reasoning paths.

Conclusion:

By following these steps, you have successfully implemented tree-of-thought prompting using LangChain and the OpenAI API. This approach allows the model to explore multiple reasoning branches, evaluate alternative solutions, and converge on the most promising outcome, making it highly effective for analytical, decision-making, and problem-solving tasks.


No comments:

Post a Comment