"""sequential_chains_demo.py — two LLM steps in sequence
Run with venv active:  python sequential_chains_demo.py
"""
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

load_dotenv()

model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
parser = StrOutputParser()

# Step 1: explain what the tag does
explain_prompt = ChatPromptTemplate.from_messages([
    ("system", "Keep each reply to one short sentence."),
    ("human", "What does the HTML <{tag}> tag do?"),
])
explain_chain = explain_prompt | model | parser

# Step 2: write a beginner tip using the explanation
tip_prompt = ChatPromptTemplate.from_messages([
    ("system", "Keep each reply to one short sentence."),
    ("human", "Tag: <{tag}>. Explanation: {explanation}\nGive one practical tip for beginners."),
])

# Sequential: keep {tag}, run explain_chain, add result as {explanation}
chain = (
    RunnablePassthrough.assign(explanation=explain_chain)
    | tip_prompt
    | model
    | parser
)

tags = ["a", "img"]

for tag in tags:
    tip = chain.invoke({"tag": tag})
    print(f"Tag: <{tag}>")
    print(f"Tip: {tip}\n")