Python / Python Modern Generative AI and Agents Interview Questions
How do you manage, version, and reuse prompts in production LLM applications?
In production systems, prompts are first-class assets — they evolve through experimentation, need version control, and may be shared across teams. Hard-coding prompts in application code makes them difficult to update without deployment. Several strategies improve prompt management.
# ── Approach 1: LangChain Hub (versioned, shareable prompt registry)
from langchain import hub
# Pull a community prompt by handle (owner/prompt-name:commit-hash)
rag_prompt = hub.pull('rlm/rag-prompt')
print(rag_prompt.messages[0].prompt.template)
# ── Approach 2: PromptTemplate with variables
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate
from langchain_core.prompts import FewShotChatMessagePromptTemplate
# Parameterised template
qa_template = PromptTemplate.from_template(
'You are an expert in {domain}. Answer the following question concisely.\n\n'
'Question: {question}\n'
'Answer:'
)
formatted = qa_template.format(domain='astrophysics', question='What is a black hole?')
# ── Few-shot template
examples = [
{'input': 'happy', 'output': 'sad'},
{'input': 'tall', 'output': 'short'},
{'input': 'energetic','output': 'lethargic'},
]
example_prompt = ChatPromptTemplate.from_messages([
('human', '{input}'),
('ai', '{output}'),
])
few_shot_prompt = FewShotChatMessagePromptTemplate(
example_prompt=example_prompt,
examples=examples,
)
final_prompt = ChatPromptTemplate.from_messages([
('system', 'Give the antonym of each word.'),
few_shot_prompt,
('human', '{word}'),
])
print(final_prompt.invoke({'word': 'joyful'}).to_messages())
# ── Approach 3: LangSmith for prompt tracing and experimentation
# Set env vars: LANGCHAIN_API_KEY, LANGCHAIN_TRACING_V2=true
# Every chain invocation is automatically logged to LangSmith dashboard
# enabling side-by-side comparison of prompt versions
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...
