分類目錄:《大模型從入門到應(yīng)用》總目錄
LangChain系列文章:
- 基礎(chǔ)知識
- 快速入門
- 安裝與環(huán)境配置
- 鏈(Chains)、代理(Agent:)和記憶(Memory)
- 快速開發(fā)聊天模型
- 模型(Models)
- 基礎(chǔ)知識
- 大型語言模型(LLMs)
- 基礎(chǔ)知識
- LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(Human Input LLM)
- 緩存LLM的調(diào)用結(jié)果
- 加載與保存LLM類、流式傳輸LLM與Chat Model響應(yīng)和跟蹤tokens使用情況
- 聊天模型(Chat Models)
- 基礎(chǔ)知識
- 使用少量示例和響應(yīng)流式傳輸
- 文本嵌入模型
- Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
- Embaas、Fake Embeddings、Google Vertex AI PaLM等
- 提示(Prompts)
- 基礎(chǔ)知識
- 提示模板
- 基礎(chǔ)知識
- 連接到特征存儲
- 創(chuàng)建自定義提示模板和含有Few-Shot示例的提示模板
- 部分填充的提示模板和提示合成
- 序列化提示信息
- 示例選擇器(Example Selectors)
- 輸出解析器(Output Parsers)
- 記憶(Memory)
- 基礎(chǔ)知識
- 記憶的類型
- 會話緩存記憶、會話緩存窗口記憶和實體記憶
- 對話知識圖譜記憶、對話摘要記憶和會話摘要緩沖記憶
- 對話令牌緩沖存儲器和基于向量存儲的記憶
- 將記憶添加到LangChain組件中
- 自定義對話記憶與自定義記憶類
- 聊天消息記錄
- 記憶的存儲與應(yīng)用
- 索引(Indexes)
- 基礎(chǔ)知識
- 文檔加載器(Document Loaders)
- 文本分割器(Text Splitters)
- 向量存儲器(Vectorstores)
- 檢索器(Retrievers)
- 鏈(Chains)
- 基礎(chǔ)知識
- 通用功能
- 自定義Chain和Chain的異步API
- LLMChain和RouterChain
- SequentialChain和TransformationChain
- 鏈的保存(序列化)與加載(反序列化)
- 鏈與索引
- 文檔分析和基于文檔的聊天
- 問答的基礎(chǔ)知識
- 圖問答(Graph QA)和帶來源的問答(Q&A with Sources)
- 檢索式問答
- 文本摘要(Summarization)、HyDE和向量數(shù)據(jù)庫的文本生成
- 代理(Agents)
- 基礎(chǔ)知識
- 代理類型
- 自定義代理(Custom Agent)
- 自定義MRKL代理
- 帶有ChatModel的LLM聊天自定義代理和自定義多操作代理(Custom MultiAction Agent)
- 工具
- 基礎(chǔ)知識
- 自定義工具(Custom Tools)
- 多輸入工具和工具輸入模式
- 人工確認(rèn)工具驗證和Tools作為OpenAI函數(shù)
- 工具包(Toolkit)
- 代理執(zhí)行器(Agent Executor)
- 結(jié)合使用Agent和VectorStore
- 使用Agents的異步API和創(chuàng)建ChatGPT克隆
- 處理解析錯誤、訪問中間步驟和限制最大迭代次數(shù)
- 為代理程序設(shè)置超時時間和限制最大迭代次數(shù)和為代理程序和其工具添加共享內(nèi)存
- 計劃與執(zhí)行
- 回調(diào)函數(shù)(Callbacks)
如果我們擁有大量的示例,我們可能需要選擇在提示中包含哪些示例。ExampleSelector
是負(fù)責(zé)執(zhí)行此操作的類。 其基本接口定義如下所示:
class BaseExampleSelector(ABC):
"""Interface for selecting examples to include in prompts."""
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""Select which examples to use based on the inputs."""
它只需要暴露一個select_examples
方法,該方法接收輸入變量并返回一個示例列表。具體如何選擇這些示例取決于每個具體實現(xiàn)。
自定義示例選擇器(Custom Example Selector)
自定義示例選擇器從給定的示例的列表中選擇固定個示例。一個ExampleSelector
必須實現(xiàn)兩個方法:
- 一個
add_example
方法,它接受一個示例并將其添加到ExampleSelector
中 - 一個
select_examples
方法,它接受輸入變量(用戶輸入),并返回要在few-shot提示中使用的示例列表
讓我們實現(xiàn)一個簡單的自定義ExampleSelector
,它只隨機選擇兩個示例。
實現(xiàn)自定義示例選擇器
from langchain.prompts.example_selector.base import BaseExampleSelector
from typing import Dict, List
import numpy as np
class CustomExampleSelector(BaseExampleSelector):
def __init__(self, examples: List[Dict[str, str]]):
self.examples = examples
def add_example(self, example: Dict[str, str]) -> None:
"""Add new example to store for a key."""
self.examples.append(example)
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""Select which examples to use based on the inputs."""
return np.random.choice(self.examples, size=2, replace=False)
使用自定義示例選擇器
examples = [
{"foo": "1"},
{"foo": "2"},
{"foo": "3"}
]
# 初始化示例選擇器
example_selector = CustomExampleSelector(examples)
# 選擇示例
example_selector.select_examples({"foo": "foo"})
# -> [{'foo': '2'}, {'foo': '3'}]
# 向示例集合添加新示例
example_selector.add_example({"foo": "4"})
example_selector.examples
# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]
# 選擇示例
example_selector.select_examples({"foo": "foo"})
# -> [{'foo': '1'}, {'foo': '4'}]
基于長度的示例選擇器(LengthBased ExampleSelector)
基于長度的示例選擇器根據(jù)示例的長度來選擇要使用的示例。當(dāng)我們擔(dān)心構(gòu)建的提示內(nèi)容超過上下文窗口的長度時這種示例選擇器將非常有用。對于較長的輸入,它會選擇較少的示例進(jìn)行包含,而對于較短的輸入,它會選擇更多的示例。
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(
# These are the examples it has available to choose from.
examples=examples,
# This is the PromptTemplate being used to format the examples.
example_prompt=example_prompt,
# This is the maximum length that the formatted examples should be.
# Length is measured by the get_text_length function below.
max_length=25,
# This is the function used to get the length of a string, which is used
# to determine which examples to include. It is commented out because
# it is provided as a default value if none is specified.
# get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x))
)
dynamic_prompt = FewShotPromptTemplate(
# We provide an ExampleSelector instead of examples.
example_selector=example_selector,
example_prompt=example_prompt,
prefix="Give the antonym of every input",
suffix="Input: {adjective}\nOutput:",
input_variables=["adjective"],
)
# An example with small input, so it selects all examples.
print(dynamic_prompt.format(adjective="big"))
輸出:
Give the antonym of every input
Input: happy
Output: sad
Input: tall
Output: short
Input: energetic
Output: lethargic
Input: sunny
Output: gloomy
Input: windy
Output: calm
Input: big
Output:
當(dāng)輸入較長時:
# An example with long input, so it selects only one example.
long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else"
print(dynamic_prompt.format(adjective=long_string))
Give the antonym of every input
輸出:
Input: happy
Output: sad
Input: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else
Output:
我們還可以新增一個示例:
# You can add an example to an example selector as well.
new_example = {"input": "big", "output": "small"}
dynamic_prompt.example_selector.add_example(new_example)
print(dynamic_prompt.format(adjective="enthusiastic"))
輸出:
Give the antonym of every input
Input: happy
Output: sad
Input: tall
Output: short
Input: energetic
Output: lethargic
Input: sunny
Output: gloomy
Input: windy
Output: calm
Input: big
Output: small
Input: enthusiastic
Output:
最大邊際相關(guān)性示例選擇器(Maximal Marginal Relevance ExampleSelector)
最大邊際相關(guān)性示例選擇器根據(jù)示例與輸入的相似度以及多樣性進(jìn)行選擇。它通過找到與輸入具有最大余弦相似度的示例的嵌入,然后迭代地添加它們,同時對它們與已選擇示例的接近程度進(jìn)行懲罰,來實現(xiàn)這一目標(biāo)。
from langchain.prompts.example_selector import MaxMarginalRelevanceExampleSelector, SemanticSimilarityExampleSelector
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
example_prompt = PromptTemplate(
input_variables=["input", "output"],
template="Input: {input}\nOutput: {output}",
)
# 這些是一個虛構(gòu)任務(wù)創(chuàng)建反義詞的許多示例。
examples = [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"},
{"input": "energetic", "output": "lethargic"},
{"input": "sunny", "output": "gloomy"},
{"input": "windy", "output": "calm"},
]
example_selector = MaxMarginalRelevanceExampleSelector.from_examples(
# 這是可供選擇的示例列表。
examples,
# 這是用于生成嵌入向量以測量語義相似性的嵌入類。
OpenAIEmbeddings(),
# 這是用于存儲嵌入向量并進(jìn)行相似性搜索的 VectorStore 類。
FAISS,
# 這是要生成的示例數(shù)量。
k=2
)
mmr_prompt = FewShotPromptTemplate(
# 我們提供一個 ExampleSelector 而不是示例列表。
example_selector=example_selector,
example_prompt=example_prompt,
prefix="給出每個輸入的反義詞",
suffix="輸入:{adjective}\n輸出:",
input_variables=["adjective"],
)
# 輸入是一個情感,因此應(yīng)該選擇 happy/sad 示例作為第一個示例
print(mmr_prompt.format(adjective="worried"))
輸出:
Give the antonym of every input
Input: happy
Output: sad
Input: windy
Output: calm
Input: worried
Output:
我們還可以與僅基于相似性進(jìn)行選擇的情況進(jìn)行比較:
# 使用 SemanticSimilarityExampleSelector 而不是 MaxMarginalRelevanceExampleSelector。
example_selector = SemanticSimilarityExampleSelector.from_examples(
# 這是可供選擇的示例列表。
examples,
# 這是用于生成嵌入向量以測量語義相似性的嵌入類。
OpenAIEmbeddings(),
# 這是用于存儲嵌入向量并進(jìn)行相似性搜索的 VectorStore 類。
FAISS,
# 這是要生成的示例數(shù)量。
k=2
)
similar_prompt = FewShotPromptTemplate(
# 我們提供一個 ExampleSelector 而不是示例列表。
example_selector=example_selector,
example_prompt=example_prompt,
prefix="給出每個輸入的反義詞",
suffix="輸入:{adjective}\n輸出:",
input_variables=["adjective"],
)
print(similar_prompt.format(adjective="worried"))
輸出:
Give the antonym of every input
Input: happy
Output: sad
Input: sunny
Output: gloomy
Input: worried
Output:
N-Gram重疊示例選擇器(N-Gram Overlap ExampleSelector)
NGramOverlapExampleSelector
根據(jù)示例與輸入之間的n-gram重疊得分選擇和排序示例。n-gram重疊得分是一個介于0.0和1.0之間的浮點數(shù)。該選擇器允許設(shè)置一個閾值分?jǐn)?shù)。n-gram 重疊得分小于或等于閾值的示例將被排除。默認(rèn)情況下,閾值設(shè)置為-1.0,因此不會排除任何示例,只會重新排序它們。將閾值設(shè)置為0.0將排除與輸入沒有n-gram重疊的示例。
from langchain.prompts import PromptTemplate
from langchain.prompts.example_selector.ngram_overlap import NGramOverlapExampleSelector
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
example_prompt = PromptTemplate(
input_variables=["input", "output"],
template="Input: {input}\nOutput: {output}",
)
# 這是一個假設(shè)任務(wù)(創(chuàng)建反義詞)的許多示例。
examples = [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"},
{"input": "energetic", "output": "lethargic"},
{"input": "sunny", "output": "gloomy"},
{"input": "windy", "output": "calm"},
]
# 這些是虛構(gòu)的翻譯任務(wù)的示例。
examples = [
{"input": "See Spot run.", "output": "Ver correr a Spot."},
{"input": "My dog barks.", "output": "Mi perro ladra."},
{"input": "Spot can run.", "output": "Spot puede correr."},
]
example_prompt = PromptTemplate(
input_variables=["input", "output"],
template="Input: {input}\nOutput: {output}",
)
example_selector = NGramOverlapExampleSelector(
# 這些是可供選擇的示例。
examples=examples,
# 用于格式化示例的 PromptTemplate。
example_prompt=example_prompt,
# 選擇器停止的閾值分?jǐn)?shù)。
# 默認(rèn)值為 -1.0。
threshold=-1.0,
# 對于負(fù)閾值:
# 選擇器按照 ngram 重疊得分對示例進(jìn)行排序,不排除任何示例。
# 對于大于 1.0 的閾值:
# 選擇器排除所有示例,并返回一個空列表。
# 對于等于 0.0 的閾值:
# 選擇器根據(jù) ngram 重疊得分對示例進(jìn)行排序,
# 并排除與輸入沒有 ngram 重疊的示例。
)
dynamic_prompt = FewShotPromptTemplate(
# 我們提供 ExampleSelector 而不是示例。
example_selector=example_selector,
example_prompt=example_prompt,
prefix="給出每個輸入的西班牙語翻譯",
suffix="輸入:{sentence}\n輸出:",
input_variables=["sentence"],
)
# 一個與“Spot can run.”有較大ngram重疊的示例輸入
# 與“My dog barks.”沒有重疊
print(dynamic_prompt.format(sentence="Spot can run fast."))
輸出:
Give the Spanish translation of every input
Input: Spot can run.
Output: Spot puede correr.
Input: See Spot run.
Output: Ver correr a Spot.
Input: My dog barks.
Output: Mi perro ladra.
Input: Spot can run fast.
Output:
我們還可以向NGramOverlapExampleSelector
添加示例:
new_example = {"input": "Spot plays fetch.", "output": "Spot juega a buscar."}
example_selector.add_example(new_example)
print(dynamic_prompt.format(sentence="Spot can run fast."))
輸出:
Give the Spanish translation of every input
Input: Spot can run.
Output: Spot puede correr.
Input: See Spot run.
Output: Ver correr a Spot.
Input: Spot plays fetch.
Output: Spot juega a buscar.
Input: My dog barks.
Output: Mi perro ladra.
Input: Spot can run fast.
Output:
我們還以設(shè)置一個閾值,決定哪些示例會被排除:
# 例如,將閾值設(shè)為0.0
# 會排除與輸入沒有ngram重疊的示例。
# 因為"My dog barks."與"Spot can run fast."沒有ngram重疊,
# 所以它被排除在外。
example_selector.threshold=0.0
print(dynamic_prompt.format(sentence="Spot can run fast."))
輸出:
Give the Spanish translation of every input
Input: Spot can run.
Output: Spot puede correr.
Input: See Spot run.
Output: Ver correr a Spot.
Input: Spot plays fetch.
Output: Spot juega a buscar.
Input: Spot can run fast.
Output:
我們也可以設(shè)置一個小的非零閾值:
example_selector.threshold=0.09
print(dynamic_prompt.format(sentence="Spot can play fetch."))
輸出:
Give the Spanish translation of every input
Input: Spot can run.
Output: Spot puede correr.
Input: Spot plays fetch.
Output: Spot juega a buscar.
Input: Spot can play fetch.
Output:
我們再嘗試設(shè)置大于1.0的閾值:
example_selector.threshold=1.0+1e-9
print(dynamic_prompt.format(sentence="Spot can play fetch."))
Give the Spanish translation of every input
輸出:
Input: Spot can play fetch.
Output:
相似性示例選擇器
語義相似性示例選擇器根據(jù)輸入與示例的相似性選擇示例,它通過找到具有最大余弦相似度的嵌入的示例來實現(xiàn)這一點:
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
example_prompt = PromptTemplate(
input_variables=["input", "output"],
template="Input: {input}\nOutput: {output}",
)
以下是一個虛構(gòu)任務(wù)的許多示例,用于創(chuàng)建反義詞:
examples = [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"},
{"input": "energetic", "output": "lethargic"},
{"input": "sunny", "output": "gloomy"},
{"input": "windy", "output": "calm"},
]
使用這些示例,可以創(chuàng)建一個語義相似性示例選擇器:
example_selector = SemanticSimilarityExampleSelector.from_examples(
# 這是可供選擇的示例列表。
examples,
# 這是用于生成嵌入的嵌入類,用于衡量語義相似性。
OpenAIEmbeddings(),
# 這是用于存儲嵌入并進(jìn)行相似性搜索的VectorStore類。
Chroma,
# 這是要生成的示例數(shù)量。
k=1
)
similar_prompt = FewShotPromptTemplate(
# 我們提供了一個ExampleSelector而不是示例列表。
example_selector=example_selector,
example_prompt=example_prompt,
prefix="給出每個詞的反義詞",
suffix="輸入:{adjective}\n輸出:",
input_variables=["adjective"],
)
通過使用這個示例選擇器,我們可以根據(jù)輸入的相似性來選擇示例,并將其應(yīng)用于生成反義詞的問題:
Running Chroma using direct local API.
Using DuckDB in-memory for database. Data will be transient.
輸入worried
是一種情感,因此應(yīng)選擇happy/sad
示例:
print(similar_prompt.format(adjective="worried"))
輸出:
給出每個詞的反義詞
輸入:happy
輸出:sad
輸入:worried
輸出:
輸入fat
是一種度量,因此應(yīng)選擇tall/short
示例:
print(similar_prompt.format(adjective="fat"))
輸出:
給出每個詞的反義詞
輸入:happy
輸出:sad
輸入:fat
輸出:
我們還可以將新示例添加到SemanticSimilarityExampleSelector
中:
similar_prompt.example_selector.add_example({"input": "enthusiastic", "output": "apathetic"})
print(similar_prompt.format(adjective="joyful"))
輸出:文章來源:http://www.zghlxwxcb.cn/news/detail-630640.html
給出每個詞的反義詞
輸入:happy
輸出:sad
輸入:joyful
輸出:
參考文獻(xiàn):
[1] LangChain官方網(wǎng)站:https://www.langchain.com/
[2] LangChain ????? 中文網(wǎng),跟著LangChain一起學(xué)LLM/GPT開發(fā):https://www.langchain.com.cn/
[3] LangChain中文網(wǎng) - LangChain 是一個用于開發(fā)由語言模型驅(qū)動的應(yīng)用程序的框架:http://www.cnlangchain.com/文章來源地址http://www.zghlxwxcb.cn/news/detail-630640.html
到了這里,關(guān)于自然語言處理從入門到應(yīng)用——LangChain:提示(Prompts)-[示例選擇器(Example Selectors)]的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!