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

相关推荐
背太阳的牧羊人17 小时前
RAG检索中使用一个 长上下文重排序器(Long Context Reorder) 对检索到的文档进行进一步的处理和排序,优化输出顺序
开发语言·人工智能·python·langchain·rag
Java知识技术分享18 小时前
使用LangChain构建第一个ReAct Agent
python·react.js·ai·语言模型·langchain
圆内~搁浅2 天前
langchain本地知识库问答机器人集成本地知识库
数据库·langchain·机器人
Neo很努力3 天前
【deepseek】本地部署+RAG知识库挂载+对话测试
自然语言处理·chatgpt·langchain·aigc·llama
merlin-mm3 天前
langchain应用-agent
langchain
朴拙数科4 天前
Langchain vs. LlamaIndex:哪个在集成MongoDB并分析资产负债表时效果更好?
数据库·mongodb·langchain
静静的喝酒7 天前
langchain学习笔记之消息存储在内存中的实现方法
langchain·消息存储·内存存储
Rickie7 天前
DeepSeek-V3 解读:优化效率与规模
langchain·rag·deepseek
少林码僧7 天前
1.5 企业级AI大模型四阶技术全景解析:从Prompt到Pre-training的进化路径
人工智能·gpt·chatgpt·langchain·prompt
Clang's Blog9 天前
使用 LangChain 对接硅基流动(SiliconFlow)API:构建一个智能对话系统
langchain