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

相关推荐
掘金詹姆斯1 天前
LangChain4j快速入门(一)
人工智能·langchain
MerlinTheMagic1 天前
Langchain coercion简介
python·langchain
cr72582 天前
使用 LangChain + Higress + Elasticsearch 构建 RAG 应用
大数据·elasticsearch·langchain
IT杨秀才2 天前
LangChain框架入门系列(5):Memory
人工智能·后端·langchain
IT杨秀才2 天前
LangChain框架入门系列(3):数据连接
人工智能·后端·langchain
IT杨秀才2 天前
LangChain框架入门系列(2):Hello LangChain
人工智能·后端·langchain
都叫我大帅哥2 天前
当AI学会“自己动脑”:AI Agent的奇幻漂流与实用指南
java·langchain·ai编程
Awesome Baron3 天前
《Learning Langchain》阅读笔记2-基于 Gemini 的 Langchain PromptTemplate 实现方式
jupyter·chatgpt·langchain·llm
ai大师3 天前
如何在LangChain中构建并使用自定义向量数据库
langchain·claude·向量数据库·中转api·apikey·中转apikey·免费apikey
朴拙数科3 天前
LangChain实现PDF中图表文本多模态数据向量化及RAG应用实战指南
langchain·pdf