分類目錄:《大模型從入門到應(yīng)用》總目錄
LangChain系列文章:
- 基礎(chǔ)知識(shí)
- 快速入門
- 安裝與環(huán)境配置
- 鏈(Chains)、代理(Agent:)和記憶(Memory)
- 快速開發(fā)聊天模型
- 模型(Models)
- 基礎(chǔ)知識(shí)
- 大型語言模型(LLMs)
- 基礎(chǔ)知識(shí)
- LLM的異步API、自定義LLM包裝器、虛假LLM和人類輸入LLM(Human Input LLM)
- 緩存LLM的調(diào)用結(jié)果
- 加載與保存LLM類、流式傳輸LLM與Chat Model響應(yīng)和跟蹤tokens使用情況
- 聊天模型(Chat Models)
- 基礎(chǔ)知識(shí)
- 使用少量示例和響應(yīng)流式傳輸
- 文本嵌入模型
- Aleph Alpha、Amazon Bedrock、Azure OpenAI、Cohere等
- Embaas、Fake Embeddings、Google Vertex AI PaLM等
- 提示(Prompts)
- 基礎(chǔ)知識(shí)
- 提示模板
- 基礎(chǔ)知識(shí)
- 連接到特征存儲(chǔ)
- 創(chuàng)建自定義提示模板和含有Few-Shot示例的提示模板
- 部分填充的提示模板和提示合成
- 序列化提示信息
- 示例選擇器(Example Selectors)
- 輸出解析器(Output Parsers)
- 記憶(Memory)
- 基礎(chǔ)知識(shí)
- 記憶的類型
- 會(huì)話緩存記憶、會(huì)話緩存窗口記憶和實(shí)體記憶
- 對(duì)話知識(shí)圖譜記憶、對(duì)話摘要記憶和會(huì)話摘要緩沖記憶
- 對(duì)話令牌緩沖存儲(chǔ)器和基于向量存儲(chǔ)的記憶
- 將記憶添加到LangChain組件中
- 自定義對(duì)話記憶與自定義記憶類
- 聊天消息記錄
- 記憶的存儲(chǔ)與應(yīng)用
- 索引(Indexes)
- 基礎(chǔ)知識(shí)
- 文檔加載器(Document Loaders)
- 文本分割器(Text Splitters)
- 向量存儲(chǔ)器(Vectorstores)
- 檢索器(Retrievers)
- 鏈(Chains)
- 基礎(chǔ)知識(shí)
- 通用功能
- 自定義Chain和Chain的異步API
- LLMChain和RouterChain
- SequentialChain和TransformationChain
- 鏈的保存(序列化)與加載(反序列化)
- 鏈與索引
- 文檔分析和基于文檔的聊天
- 問答的基礎(chǔ)知識(shí)
- 圖問答(Graph QA)和帶來源的問答(Q&A with Sources)
- 檢索式問答
- 文本摘要(Summarization)、HyDE和向量數(shù)據(jù)庫(kù)的文本生成
- 代理(Agents)
- 基礎(chǔ)知識(shí)
- 代理類型
- 自定義代理(Custom Agent)
- 自定義MRKL代理
- 帶有ChatModel的LLM聊天自定義代理和自定義多操作代理(Custom MultiAction Agent)
- 工具
- 基礎(chǔ)知識(shí)
- 自定義工具(Custom Tools)
- 多輸入工具和工具輸入模式
- 人工確認(rèn)工具驗(yàn)證和Tools作為OpenAI函數(shù)
- 工具包(Toolkit)
- 代理執(zhí)行器(Agent Executor)
- 結(jié)合使用Agent和VectorStore
- 使用Agents的異步API和創(chuàng)建ChatGPT克隆
- 處理解析錯(cuò)誤、訪問中間步驟和限制最大迭代次數(shù)
- 為代理程序設(shè)置超時(shí)時(shí)間和限制最大迭代次數(shù)和為代理程序和其工具添加共享內(nèi)存
- 計(jì)劃與執(zhí)行
- 回調(diào)函數(shù)(Callbacks)
創(chuàng)建自定義提示模板
假設(shè)我們希望LLM根據(jù)函數(shù)名稱生成該函數(shù)的英文語言解釋。為了實(shí)現(xiàn)這個(gè)任務(wù),我們將創(chuàng)建一個(gè)自定義的提示模板,以函數(shù)名稱作為輸入,并格式化提示模板以提供函數(shù)的源代碼。LangChain提供了一組默認(rèn)的提示模板,可用于生成各種任務(wù)的提示。但是,在某些情況下,默認(rèn)的提示模板可能無法滿足我們的需求。例如,我們可能希望創(chuàng)建一個(gè)具有特定動(dòng)態(tài)指令的提示模板,以適應(yīng)我們的語言模型。在這種情況下,我們可以創(chuàng)建自定義的提示模板。
有兩種不同的提示模板:
- 字符串提示模板:提供一個(gè)簡(jiǎn)單的字符串格式提示
- 聊天提示模板:生成一個(gè)更結(jié)構(gòu)化的聊天API使用的提示
在本文中,我們將使用字符串提示模板創(chuàng)建一個(gè)自定義提示。要?jiǎng)?chuàng)建自定義字符串提示模板,有兩個(gè)要求:
- 它具有
input_variables
屬性,用于公開提示模板期望的輸入變量 - 它公開一個(gè)
format
方法,該方法接受與預(yù)期的input_variables
相對(duì)應(yīng)的關(guān)鍵字參數(shù),并返回格式化的提示
我們將創(chuàng)建一個(gè)自定義的提示模板,它以函數(shù)名稱作為輸入,并格式化提示以提供函數(shù)的源代碼。為了實(shí)現(xiàn)這一點(diǎn),讓我們首先創(chuàng)建一個(gè)函數(shù),該函數(shù)將根據(jù)函數(shù)名稱返回函數(shù)的源代碼。
import inspect
def get_source_code(function_name):
# Get the source code of the function
return inspect.getsource(function_name)
接下來,我們將創(chuàng)建一個(gè)自定義的提示模板,該模板以函數(shù)名稱作為輸入,并格式化提示模板以提供函數(shù)的源代碼:
from langchain.prompts import StringPromptTemplate
from pydantic import BaseModel, validator
class FunctionExplainerPromptTemplate(StringPromptTemplate, BaseModel):
"""一個(gè)自定義的提示模板,接受函數(shù)名作為輸入,并格式化提示模板以提供函數(shù)的源代碼。"""
@validator("input_variables")
def validate_input_variables(cls, v):
"""驗(yàn)證輸入變量的正確性。"""
if len(v) != 1 or "function_name" not in v:
raise ValueError("function_name必須是唯一的輸入變量。")
return v
def format(self, **kwargs) -> str:
# 獲取函數(shù)的源代碼
source_code = get_source_code(kwargs["function_name"])
# 生成要發(fā)送給語言模型的提示
prompt = f"""
給定函數(shù)名和源代碼,生成一個(gè)關(guān)于函數(shù)的英文語言解釋。
函數(shù)名:{kwargs["function_name"].__name__}
源代碼:
{source_code}
解釋:
"""
return prompt
def _prompt_type(self):
return "function-explainer"
現(xiàn)在我們已經(jīng)創(chuàng)建了一個(gè)自定義的提示模板,我們可以使用它來生成我們?nèi)蝿?wù)的提示:
fn_explainer = FunctionExplainerPromptTemplate(input_variables=["function_name"])
# 為函數(shù)"get_source_code"生成一個(gè)提示
prompt = fn_explainer.format(function_name=get_source_code)
print(prompt)
輸出:
給定函數(shù)名和源代碼,生成一個(gè)關(guān)于函數(shù)的英文語言解釋。
函數(shù)名:get_source_code
源代碼:
def get_source_code(function_name):
# Get the source code of the function
return inspect.getsource(function_name)
解釋:
創(chuàng)建含有Few-Shot示例的提示模板
在下文中,我們將學(xué)習(xí)如何創(chuàng)建含有Few-Shot示例的提示模板。我們將使用FewShotPromptTemplate
類來創(chuàng)建一個(gè)含有Few-Shot示例的提示模板。該類可以接受一組示例或者一個(gè)ExampleSelector
對(duì)象。在下文中,我們將分別為自我提問與搜索配置Few-Shot示例討論這兩種選項(xiàng)。
使用示例集
首先,創(chuàng)建一個(gè)Few-Shot示例的列表。每個(gè)示例應(yīng)該是一個(gè)字典,其中鍵是輸入變量,值是這些輸入變量的值。
from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplate
examples = [
{
"question": "Who lived longer, Muhammad Ali or Alan Turing?",
"answer":
"""
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
"""
},
{
"question": "When was the founder of craigslist born?",
"answer":
"""
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952
"""
},
{
"question": "Who was the maternal grandfather of George Washington?",
"answer":
"""
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
"""
},
{
"question": "Are both the directors of Jaws and Casino Royale from the same country?",
"answer":
"""
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate Answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate Answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate Answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate Answer: New Zealand.
So the final answer is: No
"""
}
]
然后,我們可以為Few Shot示例創(chuàng)建格式化程序。配置一個(gè)將Few Shot示例格式化為字符串的格式化程序。該格式化程序應(yīng)該是一個(gè)PromptTemplate
對(duì)象。
example_prompt = PromptTemplate(input_variables=["question", "answer"], template="Question: {question}\n{answer}")
print(example_prompt.format(**examples[0]))
Question: Who lived longer, Muhammad Ali or Alan Turing?
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
最后,創(chuàng)建一個(gè)FewShotPromptTemplate
對(duì)象。該對(duì)象接受Few Shot示例和Few Shot示例的格式化程序作為輸入。
prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"]
)
print(prompt.format(input="Who was the father of Mary Ball Washington?"))
輸出:
Question: Who lived longer, Muhammad Ali or Alan Turing?
Are follow up questions needed here: Yes.
Follow up: How old was Muhammad Ali when he died?
Intermediate answer: Muhammad Ali was 74 years old when he died.
Follow up: How old was Alan Turing when he died?
Intermediate answer: Alan Turing was 41 years old when he died.
So the final answer is: Muhammad Ali
Question: When was the founder of craigslist born?
Are follow up questions needed here: Yes.
Follow up: Who was the founder of craigslist?
Intermediate answer: Craigslist was founded by Craig Newmark.
Follow up: When was Craig Newmark born?
Intermediate answer: Craig Newmark was born on December 6, 1952.
So the final answer is: December 6, 1952
Question: Who was the maternal grandfather of George Washington?
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
Question: Are both the directors of Jaws and Casino Royale from the same country?
Are follow up questions needed here: Yes.
Follow up: Who is the director of Jaws?
Intermediate Answer: The director of Jaws is Steven Spielberg.
Follow up: Where is Steven Spielberg from?
Intermediate Answer: The United States.
Follow up: Who is the director of Casino Royale?
Intermediate Answer: The director of Casino Royale is Martin Campbell.
Follow up: Where is Martin Campbell from?
Intermediate Answer: New Zealand.
So the final answer is: No
Question: Who was the father of Mary Ball Washington?
使用示例選擇器
我們將重復(fù)使用上文中的示例集和格式化程序。但是,與其直接將示例輸入到FewShotPromptTemplate
對(duì)象中,我們將把它們輸入到一個(gè)ExampleSelector
對(duì)象中。在下文中,我們將使用SemanticSimilarityExampleSelector
類。該類根據(jù)示例與輸入之間的相似度選擇Few-Shot示例。它使用嵌入模型計(jì)算輸入與Few-Shot示例之間的相似度,并使用向量存儲(chǔ)執(zhí)行最近鄰搜索。
from langchain.prompts.example_selector import SemanticSimilarityExampleSelector
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
example_selector = SemanticSimilarityExampleSelector.from_examples(
# 這是可供選擇的示例列表。
examples,
# 這是用于生成嵌入的嵌入類,用于衡量語義相似度。
OpenAIEmbeddings(),
# 這是用于存儲(chǔ)嵌入并進(jìn)行相似度搜索的向量存儲(chǔ)類。
Chroma,
# 這是要生成的示例數(shù)量。
k=1
)
# 選擇與輸入最相似的示例。
question = "Who was the father of Mary Ball Washington?"
selected_examples = example_selector.select_examples({"question": question})
print(f"Examples most similar to the input: {question}")
for example in selected_examples:
print("\n")
for k, v in example.items():
print(f"{k}: {v}")
輸出:
Running Chroma using direct local API.
Using DuckDB in-memory for database. Data will be transient.
Examples most similar to the input: Who was the father of Mary Ball Washington?
question: Who was the maternal grandfather of George Washington?
answer:
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
我們還可以將示例選擇器應(yīng)用于FewShotPromptTemplate
。創(chuàng)建一個(gè)FewShotPromptTemplate
對(duì)象。該對(duì)象接收示例選擇器和用于Few-Shot示例的格式化程序:
prompt = FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
suffix="Question: {input}",
input_variables=["input"]
)
print(prompt.format(input="Who was the father of Mary Ball Washington?"))
輸出:
Question: Who was the maternal grandfather of George Washington?
Are follow up questions needed here: Yes.
Follow up: Who was the mother of George Washington?
Intermediate answer: The mother of George Washington was Mary Ball Washington.
Follow up: Who was the father of Mary Ball Washington?
Intermediate answer: The father of Mary Ball Washington was Joseph Ball.
So the final answer is: Joseph Ball
Question: Who was the father of Mary Ball Washington?文章來源:http://www.zghlxwxcb.cn/news/detail-622263.html
參考文獻(xiàn):
[1] LangChain ????? 中文網(wǎng),跟著LangChain一起學(xué)LLM/GPT開發(fā):https://www.langchain.com.cn/
[2] LangChain中文網(wǎng) - LangChain 是一個(gè)用于開發(fā)由語言模型驅(qū)動(dòng)的應(yīng)用程序的框架:http://www.cnlangchain.com/文章來源地址http://www.zghlxwxcb.cn/news/detail-622263.html
到了這里,關(guān)于自然語言處理從入門到應(yīng)用——LangChain:提示(Prompts)-[提示模板:創(chuàng)建自定義提示模板和含有Few-Shot示例的提示模板]的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!