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 管道|快速组装业务流程,串行 / 并行是开发主流

相关推荐
熊明才12 小时前
WSL2 网络突然瘫痪(Network is unreachable / 没有 eth0)最短修复指南(2026年9月20日亲测可用)
linux·windows·wsl2
xbzb17 小时前
Windows 启动故障修复知识整理:蓝屏 / 无限重启 / 盘符 X / 无 PE 盘
windows
我命由我1234517 小时前
Mac 操作系统 - 一些使用记录
运维·windows·学习·系统安全·运维开发·mac·学习方法
kakakahahahaha1 天前
Windows Steam 游戏存档在哪里?定位和恢复排查流程
windows·其他·电脑·笔记本电脑·内容运营·软件需求
Xy-unu1 天前
Windows 开启外接显示器 HDR 后外接显示器无信号、无法扩展模式故障记录
windows
troy1281 天前
Codex 安全盲区:代码漏洞生成实测
windows·python·ci/cd·pycharm·django·github·fastapi
其实防守也摸鱼1 天前
常见安全架构中 Shiro 的认证确认机制解析
运维·服务器·数据库·windows·github
五_谷_丰_登2 天前
windows批处理脚本(bat)问题解决方案大全——持续更新
windows·bat
YCOSA20252 天前
迎双节 雨晨 Windows 11 IoT 企业版 LTSC 26H1 轻装 28000.2956
windows·物联网
csdn_aspnet2 天前
Windows 如何隐藏 IIS Web 服务响应头中的 IIS Server 版本信息
windows·iis·url rewrite