Model I/O进阶

目录

  • [1 结构化输出 OutputParser](#1 结构化输出 OutputParser)
    • [1.1 普通 JsonOutputParser(提示约束)](#1.1 普通 JsonOutputParser(提示约束))
    • [1.2 原生结构化输出(厂商接口能力)](#1.2 原生结构化输出(厂商接口能力))
    • [1.3 其他解析器](#1.3 其他解析器)
  • [2 ChatPromptTemplate 提示词模板](#2 ChatPromptTemplate 提示词模板)
  • [3 LCEL 链式表达式(核心 Runnable)](#3 LCEL 链式表达式(核心 Runnable))
    • [3.1 Runnable 统一接口](#3.1 Runnable 统一接口)
    • [3.2 RunnableSequence 串行流水线(最常用)](#3.2 RunnableSequence 串行流水线(最常用))
    • [3.3 RunnableParallel 并行执行](#3.3 RunnableParallel 并行执行)
    • [3.4 其他常用 Runnable](#3.4 其他常用 Runnable)
  • [4 本章小结](#4 本章小结)

1 结构化输出 OutputParser

1.1 普通 JsonOutputParser(提示约束)

流程: 1 用 Pydantic 定义输出结构 2 解析器生成格式要求注入 System 提示 3 LLM 输出后自动转为 Python 对象

python 复制代码
from pydantic import BaseModel,Field
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import JsonOutputParser

class PrimeModel(BaseModel):
    prime: list[int] = Field(description="1000-10000素数列表")
    count: int = Field(description="素数个数")

parser = JsonOutputParser(pydantic_object=PrimeModel)
llm = ChatOpenAI(temperature=0)
prompt = parser.get_format_instructions()
res = llm.invoke([("system",prompt),("user","生成5个1000~10000素数")])
data = parser.invoke(res)
print(type(data))

1.2 原生结构化输出(厂商接口能力)

OpenAI/Gemini 支持with_structured_output,无需手写约束提示:

python 复制代码
class Event(BaseModel):
    name: str
    date: str
    people: list[str]
llm = ChatOpenAI(temperature=0)
new_llm = llm.with_structured_output(Event)
res = new_llm.invoke("Alice周五参加科技展")
print(res.name)

1.3 其他解析器

内置解析器:列表、XML、Markdown、纯文本 StrOutputParser

2 ChatPromptTemplate 提示词模板

支持变量占位,动态拼接对话上下文

python 复制代码
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_messages([
    ("system","你是{product}专业评测师"),
    ("human","分析{aspect1}和{aspect2}优缺点")
])
msg = prompt.invoke({"product":"iPhone16","aspect1":"性能","aspect2":"续航"})

3 LCEL 链式表达式(核心 Runnable)

3.1 Runnable 统一接口

所有组件(提示 / 模型 / 解析器)都拥有invoke/stream/batch/ainvoke,支持管道|拼接

3.2 RunnableSequence 串行流水线(最常用)

提示模板 → LLM → 输出解析

python 复制代码
prompt = ChatPromptTemplate.from_template("讲{topic}段子")
llm = init_chat_model(model="gpt-4o-mini",model_provider="openai")
parser = StrOutputParser()
# LCEL管道
chain = prompt | llm | parser
res = chain.invoke({"topic":"大模型"})
print(res)

3.3 RunnableParallel 并行执行

同时运行多条链路,合并结果

python 复制代码
trans_en = PromptTemplate.from_template("翻译{text}为英文") | llm | StrOutputParser()
trans_kr = PromptTemplate.from_template("翻译{text}为韩文") | llm | StrOutputParser()
# 并行字典写法
map_chain = {"en":trans_en,"kr":trans_kr}
res = map_chain.invoke({"text":"人工智能技术"})
print(res)

3.4 其他常用 Runnable

1 RunnableLambda:普通函数包装进流水线 2 RunnableBranch:if 分支路由 3 RunnablePassthrough:透传原始输入 4 RunnableWithFallbacks:异常降级兜底

4 本章小结

1 OutputParser 实现结构化 JSON 输出,分提示约束、原生两种方案 2 ChatPromptTemplate 实现动态变量提示词 3 LCEL 管道|快速组装业务流程,串行 / 并行是开发主流

相关推荐
sxd200129 分钟前
Vmware和multicast
linux·windows·vmware·无线桥接·multicast
执子星海3 小时前
Registry Win32 API详解
c++·windows·microsoft·visualstudio·微软
defrag2573 小时前
Win9x下用Win32程序调用Win16函数
windows
me8323 小时前
【Tool】Windows 计划任务 + VBScript 弹窗提醒:从零到一的完整指南
windows
2601_962201725 小时前
Java进阶,集合,Colllection,常见数据结构
java·数据结构·windows
loong_XL6 小时前
Windows WSL2 Ubuntu 搭建 Xvfb 虚拟桌面环境,完美支持多 Agent 隔离
linux·windows·ubuntu
DogDaoDao15 小时前
Windows 开发提效工具全景指南:60+ 工具的工程化分层配置
windows·git·程序员·开发工具·powershell·everything·msys2
水饺编程20 小时前
第5章,[Win32 章节] :Polygon 函数和多边形填充模式
c语言·c++·windows·visual studio
百事牛科技20 小时前
文档不想再加密了?Word打开密码移除的两种方法
windows·word
软件资深者1 天前
千千静听典藏修复版 使用教程:经典本地音乐播放器回归,歌词同步、皮肤皮肤自由换,音乐播放器怀旧新手 5 分钟上手(2026)
windows