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.

相关推荐
hackchen8 小时前
LangChain 学习第 3 课 - 输出解析器(Output Parser)
数据库·学习·langchain
LMT流氓兔9 小时前
第四单元——第17课:让回答边生成、边显示
服务器·数据库·人工智能·python·oracle·langchain
BD_Marathon9 小时前
OpenAI库的基础使用
langchain
云和数据.ChenGuang10 小时前
LangChain 实战指南:从入门到构建智能应用
开发语言·人工智能·python·langchain
风雨中的小七11 小时前
和AI一起搞事情#9. Jev不就是个 JSON 分类器?我劝你把这篇看完再说
prompt
ctlover1 天前
Langchain 消息与提示词模板
langchain
一千柯橘1 天前
通过 langchain 开发一个很 small 的 agent 来了解基本流程 - py 版本
langchain
YIAN1 天前
入门篇:LangGraph 入门实战:从零搭建最简单的 RAG 问答系统
javascript·langchain
YIAN1 天前
高级篇:进阶多跳 RAG:子问题拆解 + 循环检索 + 智能规划,搞定复杂链式问题
javascript·langchain
YIAN1 天前
前置篇:手把手用 Milvus + LangChain 搭建小说向量知识库,《天龙八部》EPUB 一键入库
javascript·langchain