首页
视频
资源
登录
原
LangChain 提示词(学习笔记)
1230
人阅读
2023/9/9 18:14
总访问:
2621026
评论:
0
收藏:
0
手机
分类:
python
![](https://img.tnblog.net/arcimg/hb/dcbb080269134178b1048148e5315d8f.png) >#LangChain 提示词(学习笔记) [TOC] 什么是提示词? ------------ tn2>提示词(Prompt)是指向模型提供的输入。 这个输入通常由多个元素构成。 `LangChain` 提供了一系列的类和函数,简化构建和处理提示词的过程。 ——提示词模板(Prompt Template):对提示词参数化,提高代码的重用性。 ——示例选择器(Example Selector):动态选择要包含在提示词中的示例 提示词模板 ------------ tn2>提示词模板提供了可重用提示词的机制。 用户通过传递一组参数给模板,实例化图一个提示词。一个提示模板可以包含: 1.对语言模型的指令 2.一组少样本示例,以帮助语言模型生成更好的回复 3.向语言模型提出的问题 <br/>一个简单的例子: ```python from langchain import PromptTemplate template = """ 你精通多种语言,是专业的翻译官。你负责{src_lang}到{dst_lang}的翻译工作。 """ prompt = PromptTemplate.from_template(template) prompt.format(src_lang="英文", dst_lang="中文") ``` ### 创建模板 tn2>`PromptTemplate` 类是 `LangChain` 提供的模版基础类,它接收两个参数: | 参数 | 描述 | | ------------ | ------------ | | input_variables | 输入变量 | | template | 模版 | tn2>模版中通过 {} 符号来引用输入变量,比如 `PromptTemplate(input_variables=["name"], template="My name is {name}.")`。 模版的实例化通过模板类实例的 format函数实现。例子如下: ```python multiple_input_prompt = PromptTemplate( input_variables=["color", "animal"], template="A {color} {animal} ." ) multiple_input_prompt.format(color="black", animal="bear") ``` #### 聊天提示词模板 tn2>聊天模型,比如 OpenAI 的GPT模型,接受一系列聊天消息作为输入,每条消息都与一个角色相关联。这个消息列表通常以一定格式串联,构成模型的输入,也就是提示词。<br/> 例如,在OpenAI [Chat Completion API](https://platform.openai.com/docs/api-reference/chat/create)中,聊天消息可以与assistant、human或system角色相关联。<br/> 为此,LangChain提供了一系列模板,以便更轻松地构建和处理提示词。建议在与聊天模型交互时优先选择使用这些与聊天相关的模板,而不是基础的PromptTemplate,以充分利用框架的优势,提高开发效率。`SystemMessagePromptTemplate`, `AIMessagePromptTemplate`, `HumanMessagePromptTemplate` 是分别用于创建不同角色提示词的模板。<br/> 我们来看一个综合示例: ```python from langchain.prompts import ( ChatPromptTemplate, PromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( AIMessage, HumanMessage, SystemMessage ) system_template="You are a professional translator that translates {src_lang} to {dst_lang}." system_message_prompt = SystemMessagePromptTemplate.from_template(system_template) human_template="{user_input}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) chat_prompt.format_prompt( src_lang="English", dst_lang="Chinese", user_input="Did you eat in this morning?" ).to_messages() ``` tn2>你应该能看到如下输出: ![](https://img.tnblog.net/arcimg/hb/260c4f0283804dc090e1098972a601d7.png) #### 样本选择器 tn2>在LLM应用开发中,可能需要从大量样本数据中,选择部分数据包含在提示词中。 样本选择器(Example Selector)正是满足该需求的组件,它也通常与少样本提示词配合使用。 `LangChain` 提供了样本选择器的基础接口类 `BaseExampleSelector`,每个选择器类必须实现的函数为 `select_examples`。`LangChain` 实现了若干基于不用应用场景或算法的选择器: —— LengthBasedExampleSelector —— MaxMarginalRelevanceExampleSelector —— NGramOverlapExampleSelector —— SemanticSimilarityExampleSelector<br/> 本讲以基于长度的样本选择器(输入越长,选择的样本越少;输入越短,选择的样本越多)`LengthBasedExampleSelector` 为例,演示用法。 ```python from langchain.prompts import PromptTemplate from langchain.prompts import FewShotPromptTemplate from langchain.prompts.example_selector import LengthBasedExampleSelector # These are a lot of examples of a pretend task of creating antonyms. examples = [ {"input": "happy", "output": "sad"}, {"input": "tall", "output": "short"}, {"input": "energetic", "output": "lethargic"}, {"input": "sunny", "output": "gloomy"}, {"input": "windy", "output": "calm"}, ] example_prompt = PromptTemplate( input_variables=["input", "output"], template="Input: {input}\nOutput: {output}", ) example_selector = LengthBasedExampleSelector( # 可选的样本数据 examples=examples, # 提示词模版 example_prompt=example_prompt, # 格式化的样本数据的最大长度,通过get_text_length函数来衡量 max_length=25 ) dynamic_prompt = FewShotPromptTemplate( example_selector=example_selector, example_prompt=example_prompt, prefix="Give the antonym of every input", suffix="Input: {adjective}\nOutput:", input_variables=["adjective"], ) # 输入量极小,因此所有样本数据都会被选中 print(dynamic_prompt.format(adjective="big")) ``` tn2>你应该能看到如下输出: ![](https://img.tnblog.net/arcimg/hb/517a35eb5c1c4005ac6b42c019a3db2e.png) tn>注:选择器实例化时,我们没有改变 `get_text_length` 函数实现,其默认实现为: ## 总结 tn2>本节课程中,我们简要介绍了LLM中的重要概念 `提示词` 并学习了如何使用 `Langchain` 的重要组件 `提示词模板`。 ### 相关文档资料链接: tn2>[Python Langchain官方文档](https://python.langchain.com/docs/get_started/introduction.html) [五里墩茶社](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篇
嵌入式
3篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
2篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术