首页
视频
资源
登录
原
LangChain 输出解析器(学习笔记)
1447
人阅读
2023/9/10 23:06
总访问:
2619711
评论:
0
收藏:
0
手机
分类:
python
![](https://img.tnblog.net/arcimg/hb/dcbb080269134178b1048148e5315d8f.png) >#LangChain 输出解析器(学习笔记) [TOC] 简介 ------------ tn2>LLM的输出为文本,但在程序中除了显示文本,可能希望获得更结构化的数据。 这就是输出解析器(Output Parsers)的用武之地。 `LangChain` 为输出解析器提供了基础类 `BaseOutputParser`。 不同的输出解析器都继承自该类。它们需要实现以下两个函数: >- **get_format_instructions**:返回指令指定LLM的输出该如何格式化,该函数在实现类中必须重写。基类中的函数实现如下: ```python def get_format_instructions(self) -> str: """Instructions on how the LLM output should be formatted.""" raise NotImplementedError ``` >- **parse**:解析LLM的输出文本为特定的结构。函数签名如下: ```python def parse(self, text: str) -> T ``` tn2>`BaseOutputParser` 还提供了如下函数供重载: **parse_with_prompt**:基于提示词上下文解析LLM的输出文本为特定结构。该函数在基类中的实现为: ```python def parse_with_prompt(self, completion: str, prompt: PromptValue) -> Any: """Parse the output of an LLM call with the input prompt for context.""" return self.parse(completion) ``` LangChain支持的输出解析器 ------------ >LangChain框架提供了一系列解析器实现来满足应用在不同功能场景中的需求。它们包括且不局限于如下解析器: - List parser - Datetime parser - Enum parser - Auto-fixing parser - Pydantic parser - Retry parser - Structured output parser<br/> 本讲介绍具有代表性的两款解析器的使用。 ### List Parser tn2>List Parser将逗号分隔的文本解析为列表。 ```python from langchain.output_parsers import CommaSeparatedListOutputParser output_parser = CommaSeparatedListOutputParser() output_parser.parse("black, yellow, red, green, white, blue") ``` tn2>你应该能看到如下输出: ![](https://img.tnblog.net/arcimg/hb/8f354c92b6054dcbad786857baee29b7.png) ### Structured Output Parser tn2>当我们想要类似JSON数据结构,包含多个字段时,可以使用这个输出解析器。 该解析器可以生成指令帮助LLM返回结构化数据文本,同时完成文本到结构化数据的解析工作。 示例代码如下: ```python from langchain.output_parsers import StructuredOutputParser, ResponseSchema from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate from langchain.llms import OpenAI # 定义响应的结构(JSON),两个字段 answer和source。 response_schemas = [ ResponseSchema(name="answer", description="answer to the user's question"), ResponseSchema(name="source", description="source referred to answer the user's question, should be a website.") ] output_parser = StructuredOutputParser.from_response_schemas(response_schemas) # 获取响应格式化的指令 format_instructions = output_parser.get_format_instructions() # partial_variables允许在代码中预填充提示此模版的部分变量。 # 这类似于接口,抽象类之间的关系 prompt = PromptTemplate( template="answer the users question as best as possible.\n{format_instructions}\n{question}", input_variables=["question"], partial_variables={"format_instructions": format_instructions} ) model = OpenAI(temperature=0) response = prompt.format_prompt(question="what's the capital of France?") output = model(response.to_string()) # 格式化转换 output_parser.parse(output) ``` tn2>你应该期望能看到如下输出: ![](https://img.tnblog.net/arcimg/hb/e49e627bf2bd4159be02944f503ac546.png) tn>注,关于示例代码中引用的 `partial_variables`,请参考[Partial - Prompt Templates](https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/partial)。 总结 ------------ tn2>本节课程中,我们学习了什么是 `输出解析器` ,LangChain所支持的常见解析器,以及如何使用常见的两款解析器 `List Parser` 和 `Structured Output Parser`。 随着LangChain的发展,应该会有更多解析器出现。 相关文档资料链接 ------------ tn2> [Python Langchain官方文档](https://python.langchain.com/docs/get_started/introduction.html) [Partial - Prompt Templates](https://python.langchain.com/docs/modules/model_io/prompts/prompt_templates/partial) [五里墩茶社](https://space.bilibili.com/615957867?spm_id_from=333.337.0.0)
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
168篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
2篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
1篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术