Formatting Outputs for ChatPrompt Templates(one)

https://python.langchain.com.cn/docs/modules/model_io/prompts/prompt_templates/format_output

The chat_prompt variable in LangChain is built by combining message templates (system messages, human messages, etc.) into a structured ChatPromptTemplate. Let's break down how it's constructed, using the exact example from the original source (translating English to French).

Step 1: Import Required Tools

First, import the necessary classes from LangChain to create chat prompts:

python 复制代码
from langchain.prompts.chat import (
    ChatPromptTemplate,          # To combine message templates
    SystemMessagePromptTemplate, # For system messages (AI's role)
    HumanMessagePromptTemplate   # For human/user messages (input)
)

Step 2: Define Message Templates

A chat_prompt typically includes two key parts:

  • A system message: Tells the AI its role/instructions.
  • A human message: The user's input (with placeholders for dynamic content).
Create the System Message Template

This defines the AI's task (e.g., "translate English to French"):

python 复制代码
# Template string for the system message
system_template = "You are a helpful assistant that translates {input_language} to {output_language}."

# Convert the string to a SystemMessagePromptTemplate
system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)
  • {input_language} and {output_language} are placeholders (we'll fill them later).
Create the Human Message Template

This defines the user's input (the text to translate):

python 复制代码
# Template string for the human message
human_template = "{text}"  # {text} is a placeholder for the user's text

# Convert the string to a HumanMessagePromptTemplate
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

Step 3: Combine Templates into chat_prompt

Use ChatPromptTemplate.from_messages() to merge the system and human message templates into a single chat_prompt:

python 复制代码
# Combine the two message templates into a ChatPromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([
    system_message_prompt,  # First: system instructions
    human_message_prompt    # Second: user input
])

Final Result: What chat_prompt Contains

The chat_prompt variable now holds a structured prompt that:

  1. Includes the system's role (translation task).
  2. Includes a placeholder for the user's text.
  3. Can be filled with actual values (e.g., input_language="English", text="I love programming") later using .format() or .format_prompt().

This exact structure matches the original source---no changes to code or logic. The chat_prompt is simply a container for combining message templates to guide the AI's behavior.

相关推荐
phltxy17 分钟前
LangGraph智能租房助手实践
大数据·人工智能·python·深度学习·语言模型·langchain
Esaka_Forever1 小时前
LangChain RunnableSequence 获取中间输出的几种方案(替代SequentialChain获取中间结果)
langchain
早点睡啊Y3 小时前
深入学LangChain官方文档(二十二):Frontend 高级形态——Headless Tools、Time Travel 与 Generative UI
ui·langchain·状态模式
大郭鹏宇11 小时前
基于 LangGraph 构建智能分诊系统(一):项目概述与环境搭建
大数据·人工智能·microsoft·langchain
玉鸯13 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
chaors16 小时前
DeepRearchSystem 0x00:初识
langchain·llm·ai编程
BerryS3N20 小时前
深度解析 LangChain V1.3:架构演进、核心源码剖析与企业级应用落地指南
架构·langchain
大郭鹏宇1 天前
基于 LangGraph 构建智能分诊系统(三):FastAPI 接口与 Gradio WebUI 实战
大数据·网络·数据库·人工智能·langchain
BraveWang1 天前
【LangChain 1.x】11、长期记忆|Store 跨会话持久化与向量搜索
langchain
思绪漂移2 天前
工作日报 / 周报 Agent——发芽板块(含prompt和应用示例)
prompt·agent