登录
原创

Jenius技术分享-ChatPromptTemplate 解析

发布于 2025-06-04 阅读 111
  • AI
  • Agent
  • Jenius
原创

ChatPromptTemplate 解析:从原理到实战

本文基于我们在构建 Jenius 智能体系统中的真实经验,介绍了 LangChain 中 ChatPromptTemplate 的使用技巧和工程实践。Jenius 是我们开发的多轮对话式智能体平台,广泛应用于旅游行程规划、数据分析、知识问答等场景,依赖 LLM 的上下文管理与对话结构构建能力。

深入剖析 LangChain 中用于构造对话式提示词的核心组件——ChatPromptTemplate,并结合多个实战场景带你掌握它的用法。

什么是 ChatPromptTemplate?

在使用大语言模型(LLM)进行聊天交互时,构造合理、结构化的提示词(prompt)至关重要。LangChain 提供了 ChatPromptTemplate 类,它是专为 聊天类模型 设计的 prompt 构建工具。

相比传统的 PromptTemplate 只是生成一段纯文本,ChatPromptTemplate 允许我们以结构化方式构造“多角色对话消息”,包括:

  • 系统提示(system)
  • 用户输入(human)
  • AI 回复(ai)
  • 历史上下文(chat_history)
  • 工具消息(function/tool)

基础组件详解

消息类型(Message)

ChatPromptTemplate 由多条消息组成,每条消息代表对话中的一句话。常见角色包括:

消息类型 说明
SystemMessage 系统指令,用于设定 AI 的角色与风格
HumanMessage 用户输入消息
AIMessage AI 回复消息(可用于多轮上下文)
MessagesPlaceholder 占位符,动态插入历史对话记录
ToolMessage 工具函数调用结果(高级用法)

快速入门:构造一个简单对话模板

from langchain.prompts import ChatPromptTemplate

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("human", "Translate '{text}' to French."),
])

messages = chat_prompt.format_messages(text="I love you")

输出:

[
  SystemMessage(content="You are a helpful assistant."),
  HumanMessage(content="Translate 'I love you' to French.")
]

进阶用法:使用模板对象组合构建

from langchain.prompts import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate
)

system_prompt = SystemMessagePromptTemplate.from_template("You are a helpful assistant.")
human_prompt = HumanMessagePromptTemplate.from_template("What is the capital of {country}?")

chat_prompt = ChatPromptTemplate.from_messages([
    system_prompt,
    human_prompt
])

messages = chat_prompt.format_messages(country="Germany")

多轮对话支持:插入历史消息

在 Jenius 中,所有用户对话都被结构化保存,并通过 MessagesPlaceholder 动态插入提示词中,实现上下文连续的智能问答体验。这种设计显著提升了多轮推理、知识记忆和任务连贯性。

from langchain.prompts import MessagesPlaceholder

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "What is the weather like in {location}?")
])

调用:

from langchain.schema import HumanMessage, AIMessage

history = [
    HumanMessage(content="Hi, who won the world cup in 2018?"),
    AIMessage(content="France won the 2018 FIFA World Cup."),
]

messages = chat_prompt.format_messages(chat_history=history, location="Paris")

与 LLM 结合使用

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI()

response = llm(chat_prompt.format_messages(location="Paris", chat_history=history))
print(response.content)

功能总结表

特性 是否支持
多角色对话构造
支持变量动态填充
插入对话上下文(多轮)
模板组合嵌套构建
与 ChatOpenAI/GPT 模型兼容
支持 Function/Tool 调用消息 ✅(进阶)

使用建议

  • 使用 ChatPromptTemplate 构建提示词时,尽量保持 prompt 可维护、结构化。
  • 结合 MessagesPlaceholder 你可以轻松实现具有“记忆”的聊天系统。

小结

ChatPromptTemplate 是 LangChain 提供的构造多轮对话 prompt 的强大工具。它让开发者能够精准地控制对话内容与上下文信息,特别适合需要保持连续上下文、多轮交互的智能体系统。

掌握了它之后,你就能更高效、更稳定地与 ChatGPT 类模型交互,构建出专业级别的 AI 应用。


如果你对 Jenius的 Agent、工具调用或 RAG 系统也感兴趣,欢迎关注后续的深入文章!

评论区

~

0

0

4

举报