一文搞懂 Langchain Models (四)

本文介绍了如何在LangChain中使用动态提示模板,包括用户输入驱动的提示生成以及Few-ShotPrompting和流式传输技术。同时,探讨了TextEmbeddingModels中的嵌入概念及其在NLP中的作用,如单词向量表示和相似性计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


theme: orange

接上文🔥🔥🔥 一文搞懂 Langchain Models (三)

提示模板(Prompt Templates )

在构建动态的、面向用户的应用程序时,一般不会对提示进行硬编码。我们需要能够在提示模板中使用用户输入来构建提示。LangChain 提供了构建这些提示模板和动态插入输入的类。

提示模板允许您传入变量值以动态调整传递给LLM的内容。

以下是来自文档的一个示例:

```python rom langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, ) ...

systemtemplate="You are a helpful assistant that translates {inputlanguage} to {outputlanguage}." systemmessageprompt = SystemMessagePromptTemplate.fromtemplate(system_template)

SystemMessagePromptTemplate(prompt=PromptTemplate(inputvariables=['inputlanguage', 'outputlanguage'], outputparser=None, partialvariables={}, template='You are a helpful assistant that translates {inputlanguage} to {outputlanguage}.', templateformat='f-string', validatetemplate=True), additionalkwargs={})

humantemplate="{text}" humanmessageprompt = HumanMessagePromptTemplate.fromtemplate(human_template)

HumanMessagePromptTemplate(prompt=PromptTemplate(inputvariables=['text'], outputparser=None, partialvariables={}, template='{text}', templateformat='f-string', validatetemplate=True), additionalkwargs={})

chatprompt = ChatPromptTemplate.frommessages([systemmessageprompt, humanmessageprompt])

ChatPromptTemplate(inputvariables=['outputlanguage', 'inputlanguage', 'text'], outputparser=None, partialvariables={}, messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(inputvariables=['inputlanguage', 'outputlanguage'], outputparser=None, partialvariables={}, template='You are a helpful assistant that translates {inputlanguage} to {outputlanguage}.', templateformat='f-string', validatetemplate=True), additionalkwargs={}), HumanMessagePromptTemplate(prompt=PromptTemplate(inputvariables=['text'], outputparser=None, partialvariables={}, template='{text}', templateformat='f-string', validatetemplate=True), additional_kwargs={})])

get a chat completion from the formatted messages

chat(chatprompt.formatprompt(inputlanguage="English", outputlanguage="French", text="I love programming.").to_messages())

AIMessage(content="J'adore la programmation.", additional_kwargs={})

```

在底层,LangChain 使用了内置的字符串库的 Formatter 类来解析使用传入变量的模板文本。这就是为什么模板中的变量周围有大括号({})。

以下是模板发生的事情的一个例子。

```python from string import Formatter

formatter = Formatter() formatstring = "Hello, {name}! You are {age} years old." result = formatter.format(formatstring, name="John", age=30)

print(result) # Output: "Hello, John! You are 30 years old." ```

LangChain 提供了关于 Chat Models 的几个指南:

  • 如何使用少数示例 “少数示例提示(Few-Shot Prompting)”是一种技术,在提示中提供预期响应的示例,以便对LLM进行“条件”设置,以指导如何回应。
  • 如何流式传输响应 通过流式传输,您可以在从 LLM 接收到响应文本时立即显示,而无需等待整个响应。这无疑增加了“聊天”的体验。

嵌入模型(Text Embedding Models)

嵌入是一种将单词、短语或句子转换为固定大小的数字列表的方法,用于自然语言处理(NLP)。这通过将它们转换为数值形式(称为“向量”)来帮助算法更好地理解和处理文本数据。嵌入展示了单词的意义和结构,相似的单词具有相似的嵌入。

一旦我们将单词转换成这些“向量”,就可以使用数学方法来计算单词之间的相似性或差异性。这被证明非常强大,也是为什么最新的 LLMs 比以前的系统更加实用。

以下是将句子 "This is how embeddings work" 转换为嵌入的高级示例。

将句子分词为单词:["This", "is", "how", "embeddings", "work"]

使用预训练的嵌入模型将每个单词转换为其相应的嵌入向量。每个向量通常表示为一组固定长度的浮点数:

image.png

现在,句子 "This is how embeddings work" 可以表示为一系列嵌入向量:

text [ [0.12, -0.23, 0.56, ..., 0.07], [-0.15, 0.28, 0.31, ..., -0.03], [0.42, -0.12, -0.67, ..., 0.09], [0.22, 0.16, 0.08, ..., -0.24], [-0.04, -0.32, 0.25, ..., 0.13] ]

一旦你拥有了向量序列,你可以运行诸如语义搜索之类的查询,以返回最相关的结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值