使用LangChain控制大模型的输出——解析器Parser

LangChain框架中有两个好用的工具:

提示词模板(PromptTemplate)用于指定LLM的输入,解析器(Parser)来正确解释LLM给出的输出

即:

  1. 提示词模板 (PromptTemplate):用于格式化地接受输入string变量,作为完整的提示词。

    给生产{product}的公司起个名字。其中的product是可以变化的。

  2. 解析器 (Parser):用于格式化大模型的输出,控制输出格式(不会有多余的文字描述)。

    dict格式的 {"product_name":"袜子", "company_name":"袜界精品"}

环境配置

python 复制代码
from langchain_community.chat_models import ChatZhipuAI
import os
os.environ["ZHIPUAI_API_KEY"] = "463xxxxxxDLN"	# 替换为你的Api-Key
# 指定zhipu大模型
chat = ChatZhipuAI(model="glm-4",temperature=0.5)

1.仅用PromptTemplate

python 复制代码
from langchain.prompts import ChatPromptTemplate

country = """中国"""
country_template = """
任务:输入一个国家,输出国家的首都
语言:中文

按json格式输出,输出格式如下:
country_name
capital_name

国家: {country_name}
"""

prompt_template = ChatPromptTemplate.from_template(country_template)
# print(prompt_template)

messages = prompt_template.format_messages(country_name = country)
response = chat(messages)
print(response.content)

输出是dict格式,偶尔会出现其他文字表述,且response中的keyvalue是取不出来的。

2.使用PromptTemplate + Parser

[步骤1]. 指定响应模式

python 复制代码
from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser

## [步骤1]. 指定响应模式
# 定义国家名称的响应模式
country_schema = ResponseSchema(name="country_name", description="国家的名称。")
# 定义首都名称的响应模式
capital_schema = ResponseSchema(name="capital_name", description="对应国家的首都名称。")
# 将两个模式放入列表中
response_schemas = [country_schema, capital_schema]

# 使用 `from_response_schemas` 方法创建 `StructuredOutputParser` 类的实例。
# 该方法接收 `response_schemas` 参数,即包含多个模式的列表。
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
print(output_parser)

输出如下:

StructuredOutputParser(response_schemas=[ResponseSchema(name='country_name ', description='Name of a country.', type='string'), ResponseSchema(name='capital_name', description='Capital of the corresponding country.', type='string')])

[步骤2]. 将响应格式化并插入到提示词中

法1. 用ChatPromptTemplate

python 复制代码
## [步骤2]. 将响应格式化并插入到提示词中
from langchain.prompts import ChatPromptTemplate
# 获取格式化指令
format_instructions = output_parser.get_format_instructions()

# 定义模板字符串,用于构建提示词
country = "中国"
country_template = """\
任务: 输入一个国家,输出国家的首都
语言:中文

国家: {country_name}
{format_instructions}
"""
# 使用模板字符串创建 `ChatPromptTemplate` 实例
prompt_template = ChatPromptTemplate.from_template(country_template)
# 格式化消息,传入具体的国家名称和格式化指令
messages = prompt_template.format_messages(country_name=country,
                                           format_instructions=format_instructions)
# 发送消息并获取响应
response = chat(messages)
# 打印响应内容
print(response.content)

法2. 用HumanMessagePromptTemplate

python 复制代码
from langchain.prompts import HumanMessagePromptTemplate
format_instructions = output_parser.get_format_instructions()

country_template = """\
任务: 输入一个国家,输出国家的首都
语言:中文

国家: {country_name}
{format_instructions}
"""

prompt = ChatPromptTemplate(
    messages=[HumanMessagePromptTemplate.from_template(country_template)],
    input_variables=["country_name"],
    partial_variables={"format_instructions": format_instructions}
)

messages = prompt.format_prompt(country_name = "中国")
response = chat(messages.to_messages())
print(response.content)

输出如下:

```json

{

"country_name": "中国",

"capital_name": "北京"

}

```

[步骤3]. 解析响应

python 复制代码
## [步骤3]. 解析响应
# 使用 `output_parser` 解析响应内容
parsed_response = output_parser.parse(response.content)
# 打印解析后的响应
print(parsed_response)

输出是dict格式,且一定可以取出dict中的keyvalue

反推过来,response中一定不会有其他文字。

完整代码

python 复制代码
from langchain_community.chat_models import ChatZhipuAI
import os
os.environ["ZHIPUAI_API_KEY"] = "463xxxxxxDLN"	# 替换为你的Api-Key
# 指定zhipu大模型
chat = ChatZhipuAI(model="glm-4",temperature=0.5)

from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser

## 步骤1. 指定响应模式
# 定义国家名称的响应模式
country_schema = ResponseSchema(name="country_name", description="国家的名称。")
# 定义首都名称的响应模式
capital_schema = ResponseSchema(name="capital_name", description="对应国家的首都名称。")
# 将两个模式放入列表中
response_schemas = [country_schema, capital_schema]

# 使用 `from_response_schemas` 方法创建 `StructuredOutputParser` 类的实例。
# 该方法接收 `response_schemas` 参数,即包含多个模式的列表。
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)


## 步骤2. 将响应格式化并插入到提示词中
from langchain.prompts import ChatPromptTemplate
# 获取格式化指令
format_instructions = output_parser.get_format_instructions()

# 定义模板字符串,用于构建提示词
country_template = """\
任务: 输入一个国家,输出国家的首都
语言:中文

国家: {country_name}
{format_instructions}
"""
# 使用模板字符串创建 `ChatPromptTemplate` 实例
prompt_template = ChatPromptTemplate.from_template(country_template)
# 格式化消息,传入具体的国家名称和格式化指令
messages = prompt_template.format_messages(country_name="中国",
                                           format_instructions=format_instructions)
# 发送消息并获取响应
response = chat(messages)
# 打印响应内容
print(response.content)

## 步骤3. 解析响应
# 使用 `output_parser` 解析响应内容
parsed_response = output_parser.parse(response.content)
# 打印解析后的响应
print(parsed_response)

更多LangChain的 解析器 Parse 使用教程:https://python.langchain.com.cn/docs/modules/model_io/output_parsers/

相关推荐
工藤学编程28 分钟前
零基础学AI大模型之LangChain智能体执行引擎AgentExecutor
人工智能·langchain
Smoothzjc5 小时前
别再只把AI当聊天机器人了!揭秘大模型进化的终极形态,看完颠覆你的认知!
后端·langchain·ai编程
SCBAiotAigc6 小时前
langchain1.x学习笔记(三):langchain之init_chat_model的新用法
人工智能·python·langchain·langgraph·deepagents
工藤学编程11 小时前
零基础学AI大模型之LangChain智能体之initialize_agent开发实战
人工智能·langchain
ohyeah1 天前
打造 AI 驱动的 Git 提交规范助手:基于 React + Express + Ollama+langchain 的全栈实践
langchain·全栈·ollama
XiaoYu20021 天前
第11章 LangChain
前端·javascript·langchain
猫头虎1 天前
2025年AI领域年度深度总结:始于DeepSeek R1开源发布,终于Manus天价出海
人工智能·langchain·开源·prompt·aigc·ai编程·编程技术
真上帝的左手1 天前
26. AI-框架工具-LangChain & LangGraph
人工智能·langchain
大模型真好玩1 天前
LangGraph智能体开发设计模式(四)——LangGraph多智能体设计模式:网络架构
人工智能·langchain·agent
测试游记1 天前
基于 FastGPT 的 LangChain.js + RAG 系统实现
开发语言·前端·javascript·langchain·ecmascript