Prompt Templates¶
Prompt Templates bridge user input and Large Language Models (LLMs). They organize raw input and related parameters into structured instructions that models can reliably follow, improving response quality and consistency in specific contexts. In RAG scenarios, Prompt Templates are especially important: well-designed templates can effectively combine retrieved knowledge with user questions and significantly improve answer quality.
Below is an introduction to some commonly used template components:
For more component usage details, see Langchain Prompt Templates.
Installation¶
The packages related to Prompt Templates are provided by langchain-core, which is a dependency of langchain.
After installing the trpc-python-agent framework, the related dependencies are automatically installed, so no further installation is required.
PromptTemplate (StringPromptTemplate)¶
Usage¶
- Create a
PromptTemplateobject
PromptTemplate is used to format a single string, typically for simple inputs. It concatenates retrieval results with user questions through {context} and {query} placeholders.
from langchain_core.prompts import PromptTemplate
prompt = PromptTemplate.from_template(
"Please answer the user's question based on the following retrieved context.\n"
"Context: {context}\n"
"Question: {query}\n"
"Answer:"
)
- Construct a
LangchainKnowledgeobject based on this prompt object
from trpc_agent_sdk.server.knowledge.langchain_knowledge import LangchainKnowledge
rag = LangchainKnowledge(
prompt_template=prompt,
document_loader=text_loader,
document_transformer=text_splitter,
embedder=embedder,
vectorstore=vectorstore,
)
Reference¶
ChatPromptTemplate¶
Usage¶
- Create a
ChatPromptTemplateobject
ChatPromptTemplate is used to format a list of messages, composed of a list of templates. It supports system / user role separation, allowing the model to clearly distinguish between system instructions and user input.
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate([
("system", "You are a knowledge Q&A assistant. Please answer questions based on the provided context. If the context does not contain relevant information, please state so honestly."),
("user", "Context: {context}\n\nQuestion: {query}"),
])
- Construct a
LangchainKnowledgeobject based on this prompt object
from trpc_agent_sdk.server.knowledge.langchain_knowledge import LangchainKnowledge
rag = LangchainKnowledge(
prompt_template=prompt,
document_loader=text_loader,
document_transformer=text_splitter,
embedder=embedder,
vectorstore=vectorstore,
)
Reference¶
MessagesPlaceholder¶
Usage¶
- Create a prompt object containing
MessagesPlaceholder
MessagesPlaceholder is used to insert a list of messages at a specific position, suitable for multi-turn conversation scenarios that require preserving conversation history.
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
prompt = ChatPromptTemplate([
("system", "You are a knowledge Q&A assistant. Please answer questions based on the provided context and conversation history."),
MessagesPlaceholder("chat_history"),
("user", "Context: {context}\n\nQuestion: {query}"),
])
MessagesPlaceholder("chat_history") will be replaced with the actual conversation history message list at invocation time:
from langchain_core.messages import HumanMessage, AIMessage
prompt.invoke({
"chat_history": [
HumanMessage(content="What is artificial intelligence?"),
AIMessage(content="Artificial intelligence is a branch of computer science..."),
],
"context": "Deep learning is a subfield of machine learning...",
"query": "What is the relationship between deep learning and machine learning?",
})
- Construct a
LangchainKnowledgeobject based on this prompt object
from trpc_agent_sdk.server.knowledge.langchain_knowledge import LangchainKnowledge
rag = LangchainKnowledge(
prompt_template=prompt,
document_loader=text_loader,
document_transformer=text_splitter,
embedder=embedder,
vectorstore=vectorstore,
)
Reference¶
Complete Example¶
For a complete example, see: examples/knowledge_with_prompt_template/run_agent.py