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.

相关推荐
g32308634 小时前
Langchain mcp 可视化界面
langchain·mcp
AI_小站4 小时前
6个GitHub爆火的免费大模型教程,助你快速进阶AI编程
人工智能·langchain·github·知识图谱·agent·llama·rag
Traving Yu13 小时前
Prompt提示词工程
人工智能·prompt
茉莉玫瑰花茶14 小时前
LangChain 核心组件 [ 2 ]
java·数据库·langchain
hrhcode16 小时前
【LangGraph】四.持久化:保存和恢复执行状态
python·ai·langchain·agent·langgraph
码点滴16 小时前
私有 Gateway 接入企业 IM:从消息路由到多租户隔离——Hermes Agent 工程实战
人工智能·架构·gateway·prompt·智能体·hermes
JaydenAI17 小时前
[MCP在LangChain中的应用-03]在Session构建的上下文中与MCP Server交互
python·langchain·ai编程·ai agent·mcp·fastmcp
weisian15118 小时前
进阶篇-LangChain篇-20--从零构建企业大脑:RAG系统全流程实战
开发语言·langchain·rag·实战编码
tangweiguo0305198718 小时前
LangGraph 入门:多智能体工作流实战(阿里云百炼)
人工智能·python·langchain
2301_8152795218 小时前
实战分享LangChain WebUI 部署智能客服:从零搭建到生产环境优化
人工智能·langchain