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

相关推荐
北斗落凡尘20 小时前
LangGraph 入门实战(9)--中断
后端·langchain
赵广陆1 天前
企业实战:主体识别
langchain·pdf·langgraph
小马过河R1 天前
Graph Engineering 深度解析:模型越强,越需要给它画好“地图”
人工智能·langchain·graph·ai工程化·harness·驾驭工程
jyOverQ1 天前
LangGraph 记忆管理详解:短期记忆、长期记忆与 Runtime Context
python·langchain
做前端的娜娜子1 天前
文本切片与召回(Chunk、Overlap 到混合检索)学习笔记
langchain·openai·掘金·金石计划
赵广陆1 天前
企业实战:Milvues向量数据库实践
数据库·pycharm·langchain
CC大煊2 天前
从夯到拉:锐评国内向量模型选型
人工智能·ai·架构·langchain
北斗落凡尘2 天前
LangGraph 入门实战(8)--绑定工具
python·langchain