Content ITV PRO
This is Itvedant Content department
Prompt Engineering II : Reasoning & Structured Output
Business Scenario
Welcome!
Previously, you improved a vague prompt using task, limits, format, audience and compared zero-shot and few-shot prompting on real queries, explored how role prompting changes responses, created a Kart system prompt with rules to fix known v1 problems and upgraded SmartCart v1
Today, your manager has assigned you a task to upgrade SmartCart AI to v2 by improving how it compares products, returns recommendations in a fixed format for product cards, and remembers previous messages so shoppers can refer to earlier recommendations. You will test these improvements to make Kart more helpful and reliable for shoppers.
Git Pull
git pull origin branchNamePre-Lab Preparation
Dataset :
Topic : Prompt Engineering II : Reasoning & Structured Output
1) Chain of Thought Prompting
2) Output Formatting
3) Conversational Memory
Task 1: Reload & Continue in Lab 11
!pip install -q -r requirements.txt
import os
import json
import importlib
from google.colab import userdata
os.environ["GROQ_API_KEY"] = userdata.get("GROQ_API_KEY")
import smartcart_v1
importlib.reload(smartcart_v1) # force Python to re-read the updated file
from smartcart_v1 import llm, SYSTEM_PROMPT
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
def ask(prompt):
"""Send one prompt and return the reply text."""
return llm.invoke(prompt).content
print(SYSTEM_PROMPT[:60]) # quick check that it loadedTask 2 : Chain of Thought Prompting
Ask a Value question directly with CoT
1
question = (
"Pack A: 1 kg basmati rice for Rs. 120. "
"Pack B: 5 kg basmati rice for Rs. 560 with 10% off. "
"Which pack is better value per kg?"
)
direct = ask(question + " Give only the answer, no explanation.")
cot = ask(question + " Think step by step: work out the price per kg "
"of each pack, compare them, then give your answer.")
print("=== DIRECT ===\n", direct)
print("\n=== CHAIN OF THOUGHT ===\n", cot)Output
COMPARE_STEPS = """Think step by step:
Step 1: List what the shopper needs.
Step 2: Check each product against those needs.
Step 3: Give a one-line verdict."""
compare = ("I need a phone mainly for photos, budget Rs. 15,000. "
"Compare Redmi Note 13 5G and Samsung Galaxy M35.\n" + COMPARE_STEPS)
print(llm.invoke([SystemMessage(content=SYSTEM_PROMPT),
HumanMessage(content=compare)]).content)Use a CoT template for Product Comparision
2
Output
Task 3: Output Formatting - Why Text is Not Enough
text_reply = ask("Recommend 2 gym earbuds under Rs. 2,000 as JSON with keys "
"name and price_rs.")
print(text_reply)
try:
data = json.loads(text_reply)
print("\nParsed OK:", data)
except json.JSONDecodeError as error:
print("\nCould not read as JSON:", error)Output
Task 4: Structured Output with JSON Mode & an Output Parser
from langchain_groq import ChatGroq
from langchain_core.output_parsers import JsonOutputParser
json_llm = ChatGroq(
model="qwen/qwen3.8-27b",
temperature=0.3, # lower = steadier format
max_tokens=600,
model_kwargs={"response_format": {"type": "json_object"}}, # JSON mode on
reasoning_effort="none",)
chain = json_llm | JsonOutputParser() # model output -> parser
CARD_FORMAT = """Reply in JSON with this exact shape:
{"products": [{"name": "text", "price_rs": number,
"category": "Electronics or Grocery or Apparel",
"reason": "one short sentence"}]}"""
data = chain.invoke("Recommend 3 gym earbuds under Rs. 2,000.\n" + CARD_FORMAT)
print(type(data))
print(data)Output
Create a JSON Mode model & a chain
1
BUDGET = 2000
for product in data["products"]:
within = "Yes" if product["price_rs"] <= BUDGET else "NO"
print("+-------------------------------------------")
print("| " + product["name"])
print(f"| Rs. {product['price_rs']:,} | {product['category']}")
print("| " + product["reason"])
print(f"| Within budget: {within}")
print("+-------------------------------------------")Output
Use a data like app would print products cards and check budget
2
Task 5 : Add Conversational Memory
history = [] # grows by 2 messages every turn
MEMORY_SIZE = 10 # send only the last 10 messages (5 turns)
def chat_with_memory(text):
"""Send the new message plus recent history; save both sides."""
messages = ([SystemMessage(content=SYSTEM_PROMPT)]
+ history[-MEMORY_SIZE:]
+ [HumanMessage(content=text)])
reply = llm.invoke(messages).content
history.append(HumanMessage(content=text))
history.append(AIMessage(content=reply))
return reply
print("Kart:", chat_with_memory("running shoes for women under 3000"), "\n")
print("Kart:", chat_with_memory("make the second one cheaper"), "\n")
print("Messages in memory:", len(history))Output
Task 6 : Build SmartCart v2
%%writefile smartcart_v2.py
import os
import json
from langchain_groq import ChatGroq
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from smartcart_v1_chat import check_input, EXIT_WORDS, FRIENDLY_ERROR
if not os.environ.get("GROQ_API_KEY"):
raise SystemExit("GROQ_API_KEY not found. Add it to Colab Secrets (Lab 9, Task 2).")
llm = ChatGroq(
model="qwen/qwen3.8-27b",
temperature=0.3,
max_tokens=800,
model_kwargs={"response_format": {"type": "json_object"}}, # JSON mode
reasoning_effort="none",)
SYSTEM_PROMPT = """You are Kart, SmartCart's friendly shopping assistant for shoppers in India.
Rules:
1. Only help with shopping. For anything else, politely say you can only help with shopping.
2. Recommend up to 3 products. Prices are in rupees.
3. Reply in the same style as the shopper: English stays English, Hinglish stays Hinglish.
4. Never make up SmartCart store policies. Say: "Please check the Help page in the SmartCart app."
5. Use the earlier conversation to understand words like "it", "that one" or "the second one".
6. When the shopper compares products, think step by step:Save the Script
1
%%writefile smartcart_v2.py
import os
import json
from langchain_groq import ChatGroq
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from smartcart_v1_chat import check_input, EXIT_WORDS, FRIENDLY_ERROR
if not os.environ.get("GROQ_API_KEY"):
raise SystemExit("GROQ_API_KEY not found. Add it to Colab Secrets (Lab 9, Task 2).")
llm = ChatGroq(
model="qwen/qwen3.8-27b",
temperature=0.3,
max_tokens=800,
model_kwargs={"response_format": {"type": "json_object"}}, # JSON mode
reasoning_effort="none",)
SYSTEM_PROMPT = """You are Kart, SmartCart's friendly shopping assistant for shoppers in India.
Rules:
1. Only help with shopping. For anything else, politely say you can only help with shopping.
2. Recommend up to 3 products. Prices are in rupees.
3. Reply in the same style as the shopper: English stays English, Hinglish stays Hinglish.
4. Never make up SmartCart store policies. Say: "Please check the Help page in the SmartCart app."
5. Use the earlier conversation to understand words like "it", "that one" or "the second one".
6. When the shopper compares products, think step by step:
Step 1 list what the shopper needs, Step 2 check each product, Step 3 give a verdict.
Always reply in JSON with exactly these keys:
{"steps": ["reasoning steps, or an empty list if this is not a comparison"],
"reply": "a short friendly message, under 60 words",
"products": [{"name": "text", "price_rs": number,
"category": "Electronics or Grocery or Apparel",
"reason": "one short sentence"}]}"""
MEMORY_SIZE = 10
history = []
def ask_smartcart_v2(text):
"""Send the message with memory; return the reply as a dictionary."""
messages = ([SystemMessage(content=SYSTEM_PROMPT)]
+ history[-MEMORY_SIZE:]
+ [HumanMessage(content=text)])
response = llm.invoke(messages)
data = json.loads(response.content) # JSON text -> dictionary
history.append(HumanMessage(content=text))
history.append(AIMessage(content=response.content))
return data
def show(data):
"""Print the reasoning steps, the message and the product cards."""
for step in data.get("steps", []):
print(" (thinking)", step)
print("Kart:", data.get("reply", ""))
for p in data.get("products", []):
print(f" [{p['category']}] {p['name']} – Rs. {p['price_rs']:,} – {p['reason']}")%%writefile smartcart_v2.py
import os
import json
from langchain_groq import ChatGroq
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from smartcart_v1_chat import check_input, EXIT_WORDS, FRIENDLY_ERROR
if not os.environ.get("GROQ_API_KEY"):
raise SystemExit("GROQ_API_KEY not found. Add it to Colab Secrets (Lab 9, Task 2).")
llm = ChatGroq(
model="qwen/qwen3.8-27b",
temperature=0.3,
max_tokens=800,
model_kwargs={"response_format": {"type": "json_object"}}, # JSON mode
reasoning_effort="none",)
SYSTEM_PROMPT = """You are Kart, SmartCart's friendly shopping assistant for shoppers in India.
Rules:
1. Only help with shopping. For anything else, politely say you can only help with shopping.
2. Recommend up to 3 products. Prices are in rupees.
3. Reply in the same style as the shopper: English stays English, Hinglish stays Hinglish.
4. Never make up SmartCart store policies. Say: "Please check the Help page in the SmartCart app."
5. Use the earlier conversation to understand words like "it", "that one" or "the second one".
6. When the shopper compares products, think step by step:
Step 1 list what the shopper needs, Step 2 check each product, Step 3 give a verdict.
Always reply in JSON with exactly these keys:
{"steps": ["reasoning steps, or an empty list if this is not a comparison"],
"reply": "a short friendly message, under 60 words",
"products": [{"name": "text", "price_rs": number,
"category": "Electronics or Grocery or Apparel",
"reason": "one short sentence"}]}"""
MEMORY_SIZE = 10
history = []
def ask_smartcart_v2(text):
"""Send the message with memory; return the reply as a dictionary."""
messages = ([SystemMessage(content=SYSTEM_PROMPT)]
+ history[-MEMORY_SIZE:]
+ [HumanMessage(content=text)])
response = llm.invoke(messages)
data = json.loads(response.content) # JSON text -> dictionary
history.append(HumanMessage(content=text))
history.append(AIMessage(content=response.content))
return data
def show(data):
"""Print the reasoning steps, the message and the product cards."""
for step in data.get("steps", []):
print(" (thinking)", step)
print("Kart:", data.get("reply", ""))
for p in data.get("products", []):
print(f" [{p['category']}] {p['name']} – Rs. {p['price_rs']:,} – {p['reason']}")
def chat():
"""Run SmartCart AI v2 until the shopper types an exit word."""
print("SmartCart AI v2 -- type 'quit' to exit, 'new' to start a new conversation")
while True:
user_input = input("\nYou: ")
command = user_input.strip().lower()
if command in EXIT_WORDS:
print("Kart: Thanks for shopping with SmartCart. Bye!")
break
if command == "new":
history.clear()
print("Kart: Okay, starting fresh! What are you shopping for?")
continue
cleaned, problem = check_input(user_input)
if problem:
print("Kart:", problem)
continue
try:
show(ask_smartcart_v2(cleaned))
except json.JSONDecodeError:
print("Kart: Sorry, something went wrong with my answer. Please ask again.")
except Exception as error:
print("[log]", type(error).__name__, "-", str(error)[:200])
print("Kart:", FRIENDLY_ERROR)
if __name__ == "__main__":
chat()Run it, try a request, a follow up & a comparision
2
%run smartcart_v2.pyOutput
Task 7 : Final Test v1 vs v2
import pandas as pd
import smartcart_v2
tests = [
{"id": "T1", "input": "gym earbuds under Rs. 2,000"},
{"id": "T6", "input": "make the second one cheaper"},
{"id": "T7", "input": "who won the cricket match yesterday"},
{"id": "T8", "input": "What is SmartCart's return policy for opened electronics?"},
{"id": "T9", "input": "Compare boAt Airdopes 141 and Noise Buds VS104 for the gym"},
{"id": "T10", "input": "office ke liye formal shirt chahiye under 1500 rupees"},]
smartcart_v2.history.clear()
for case in tests:
data = smartcart_v2.ask_smartcart_v2(case["input"])
case["steps"] = len(data.get("steps", []))
case["products"] = len(data.get("products", []))
case["reply"] = data.get("reply", "")
results = pd.DataFrame(tests)
results.to_csv("smartcart_v2_test_results.csv", index=False)
print(results[["id", "steps", "products", "reply"]].to_string(max_colwidth=50))Output
Great job!
You have successfully used Chain of Thought prompting for product and price comparisons, enforced valid JSON and JsonOutputParser in a LangChain, converted JSON responses into product cards, and checked budgets with one line of code. You also added memory using the last 10 messages and built SmartCart AI v2, successfully passing all tests that v1 failed.
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Topic : LLM Access - Running Models Locally
1) Running LLM Locally
2) Open-weight vs API Models
3) Swapping the provider
By Content ITV