使用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/

相关推荐
大千AI4 小时前
LangChain Core架构解析:模块化设计与LCEL原语实现原理
langchain
Chatopera 研发团队4 小时前
智能体开发,实现自定义知识库,基于 LangChain,qwen 7b, ollama, chatopera | LLMs
langchain
硅谷神农5 小时前
第一章:AI与LangChain初探 —— 你的第一个“智能”程序
langchain
硅谷神农5 小时前
第二章:模型 (Models) —— AI应用的大脑
langchain
你那个道上的1 天前
LangChain4j学习与实践
langchain·ai编程
缘友一世1 天前
LangChain【8】之工具包深度解析:从基础使用到高级实践
langchain
bytebeats1 天前
强大的代理通信其实是 A2A + MCP + LangChain
langchain·mcp
架构师那点事儿1 天前
一文带你俯瞰大模型领域的世界
langchain·aigc·ai编程
用户84913717547162 天前
🚀5 分钟实现 Markdown 智能摘要生成器:LangChain + OpenAI 实战教程
langchain·openai
金汐脉动2 天前
实践指南:从零开始搭建RAG驱动的智能问答系统
langchain