Jinja prompt template

Hi all, I was trying to create a branching prompt template. But for some reason, I could not make it work. I would be very grateful if someone could tell me why it is wrong. from langchain_openai import ChatOpenAI

from langchain_core.prompts import PromptTemplate, ChatPromptTemplate

from langchain_core.output_parsers import StrOutputParser

from langchain_core.runnables import RunnableParallel, RunnablePassthrough

from dotenv import load_dotenv

load_dotenv()

llm = ChatOpenAI(model=“gpt-4”, temperature=0)

intent_prompt = PromptTemplate(

input_variables=\["query"\],

template="""

Classify the intent of this query: {query}

Categories: weather, news, search, database, unknown.

Just return the category.

"""

)

intent_chain = intent_prompt | llm | StrOutputParser()

followup_prompt = ChatPromptTemplate.from_template(

template="""

Category: {category}

{% if constraint %}

Please ensure to follow this rule: {constraint}

{% endif %}

Write a follow-up question for clarification.

"""

)

followup_chain = followup_prompt | llm | StrOutputParser()

pipeline = (

RunnableParallel(

    category=intent_chain,

    constraint=RunnablePassthrough(),

)

| followup_chain

)

output = pipeline.invoke({“query”: “Tell me something interesting about AI.”, “constraint”: “Keep it beginner-friendly.”})

print(output)

PromptTemplate and ChatPromptTemplate are different. From what i gather (I’m new to this as well), PromptTemplate will generate a single prompt, whereas ChatPromptTemplate will generate messages for a chat input.

PromptTemplate.from_template takes a template_format parameter, which you can set to jinja2. ChatPromptTemplate.from_template doesn’t take that param. You should use ChatPromptTemplate.from_messages, as it does take the template_format param.

Keep in mind ChatPromptTemplate.from_messages takes a list of messages either strings, tuples like (‘user’, ‘plz help’) or actual Message instances, such as HumanMessage. You should pass your templates as strings or tuples, for jinja formatting to apply. Otherwise, for Message instances, it will assume they are already formatted.