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

相关推荐
问商十三载7 小时前
RAG 检索效果差怎么排查?2026 五层诊断法完整指南
开发语言·人工智能·windows·python·算法
Zguigo8 小时前
第25-26讲:计算机操作系统存储管理——分页机制、页表与快表(TLB)
linux·windows·笔记
垂直昵称14 小时前
多线程实现网页接口并发压力测试
linux·服务器·windows
%d%d215 小时前
Windows 本地部署小红书 MCP + OpenAI Tunnel + ChatGPT 接入全流程
windows·chatgpt
90后小陈老师1 天前
Windows 10 下 Gitee SSH 连接配置详解(附常见问题解决)
windows·gitee·ssh
Light Gao1 天前
MCPHub 路由原理:一个入口如何管理并调用上百个 MCP Server
windows·microsoft
郭涤生1 天前
Windows + Ubuntu 双系统启动故障修复
linux·windows·ubuntu
看浪的路人2 天前
第3讲:代码补全引擎
开发语言·windows·python
进击切图仔2 天前
在 windows 和 ubuntu 中配置 host 文件
linux·windows·ubuntu