Formatting Outputs for ChatPrompt Templates(two)

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

This guide explains how to use the format method of ChatPrompt (in LangChain) to get outputs in three useful formats. All examples use the same core task: creating a prompt for translating English to French. We'll keep code and outputs exactly as in the original source---no changes.

Key Background First

Before diving into formats, remember: A ChatPrompt typically includes a SystemMessage (tells the AI its role) and a HumanMessage (the user's input). For our examples, the ChatPrompt is set up to translate text from an input_language to an output_language (we'll use English → French).

1. Output as a String

The simplest format: a plain text string that combines the system message and human message. There are two equivalent ways to get this.

Method 1: Use chat_prompt.format()

This directly returns the prompt as a string.

Code (From Original Source)
python 复制代码
# Assume `chat_prompt` is already set up for translation (English → French)
output = chat_prompt.format(
    input_language="English", 
    output_language="French", 
    text="I love programming."
)
print(output)
Output (From Original Source)
复制代码
System: You are a helpful assistant that translates English to French.
Human: I love programming.

Method 2: Use chat_prompt.format_prompt().to_string()

This is a two-step way to get the same string. First, format_prompt() creates a ChatPromptValue (see Section 2), then to_string() converts it to text.

Code (From Original Source)
python 复制代码
output_2 = chat_prompt.format_prompt(
    input_language="English", 
    output_language="French", 
    text="I love programming."
).to_string()

# Check if both outputs are identical (they will be!)
assert output == output_2  # No error means they match

What This Means

Both methods give you a readable text string. Use this if you want to quickly check or share the prompt content.

2. Output as a ChatPromptValue

ChatPromptValue is a special LangChain object that stores the full prompt (with messages). It's not just text---it keeps track of the message types (system vs. human).

Code (From Original Source)

python 复制代码
chat_prompt_value = chat_prompt.format_prompt(
    input_language="English", 
    output_language="French", 
    text="I love programming."
)
print(chat_prompt_value)
Output (From Original Source)
复制代码
ChatPromptValue(messages=[
    SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), 
    HumanMessage(content='I love programming.', additional_kwargs={})
])

What This Means

  • ChatPromptValue has a messages attribute that holds a list of message objects (here: SystemMessage and HumanMessage).
  • Use this if you need to work with the prompt as a structured object (not just text) in LangChain workflows.

3. Output as a List of Message Objects

You can convert the ChatPromptValue into a list of SystemMessage and HumanMessage objects. This list is ready to pass directly to Chat models (e.g., ChatOpenAI), since models accept message objects as input.

Code (From Original Source)

python 复制代码
message_list = chat_prompt.format_prompt(
    input_language="English", 
    output_language="French", 
    text="I love programming."
).to_messages()
print(message_list)
Output (From Original Source)
复制代码
[
    SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), 
    HumanMessage(content='I love programming.', additional_kwargs={})
]

What This Means

  • The list contains actual LangChain message objects (not just text).
  • This is the most useful format for running the prompt with a Chat model ---you can pass message_list directly to the model's predict or generate methods.

Quick Summary of All 3 Formats

Format Type How to Get It Use Case
String chat_prompt.format(...) or format_prompt().to_string() Quick checks/sharing prompt text
ChatPromptValue chat_prompt.format_prompt(...) Working with structured prompt objects
List of Message Objects format_prompt().to_messages() Passing input directly to a Chat model

All code, outputs, and logic match the original source---no extra changes or additions.

相关推荐
Tanjia_kiki6 分钟前
谷歌浏览器中F12编辑并重发请求
开发语言·前端·javascript
Aloudata14 分钟前
语义层 vs MCP 工具层:Agent 调用数据时,语义应该放在哪一层
数据库·数据分析·chatbi·data agent·语义层
zcmodeltech14 分钟前
源网荷储一体化沙盘模型多场景控制系统设计——基于STM32与Modbus RTU的源网荷储、多能互补、冷热电三联供全场景联动方案,服务范围覆盖全国
数据库·人工智能·stm32·单片机·嵌入式硬件
honkun616 分钟前
vue 表格组件 vxe-table 实现拖拽列字段自动生成透视表汇总
前端·javascript·vue.js
数智启示录17 分钟前
PostgreSQL 执行计划实战(第 9 篇):SQL 和索引没变,计划为什么突然慢一百倍
数据库·经验分享·sql·postgresql·面试
新中地GIS开发老师31 分钟前
WebGIS开发入门 | 从 Web 开发到地图开发,差别在哪儿?
前端·vue·webgis
Gent_倪32 分钟前
MySQL分库、分表、分区详解
数据库·mysql
IMPYLH38 分钟前
HTML 的 <p> 元素
前端·javascript·html
额额额对了39 分钟前
Linux 数据库开发实战:使用 C 语言与 SQLite3 构建轻量级终端词典
linux·数据库·oracle
leoZ23140 分钟前
第 4 篇:布局骨架——页面壳、栅格、卡片
前端·javascript·vue.js·opencv·计算机视觉·数据挖掘·语音识别