AI / LangGraph LangChain Interview questions
What are PromptTemplates in LangChain?
PromptTemplates are objects that format dynamic inputs into the correct structure before passing them to a model. Instead of building prompt strings with f-strings scattered across your codebase, templates give you reusable, testable, versionable prompt construction with named variables.
There are two main types:
- PromptTemplate — produces a plain text string. Best for LLMs (non-chat models):
from langchain_core.prompts import PromptTemplate
pt = PromptTemplate.from_template("Summarise this in {n} sentences: {text}")
print(pt.format(n=2, text="LangChain is..."))
- ChatPromptTemplate — produces a list of typed messages. Best for Chat Models:
from langchain_core.prompts import ChatPromptTemplate
chat_pt = ChatPromptTemplate.from_messages([
("system", "You are a {role}."),
("human", "{user_input}"),
])
messages = chat_pt.format_messages(role="poet", user_input="Write about the sea.")
MessagesPlaceholder is used inside a ChatPromptTemplate to insert a variable-length list of messages — useful for injecting conversation history. partial() lets you pre-fill some variables while leaving others to be filled at call time, which is handy for re-usable templates across different contexts.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
