LangChain for LLM Application Development 系列課程筆記
LLM鏈 (LLM Chain)
連續鏈 (Sequential Chains)
路由鏈 (Router Chain)
最基本的LLMChain
單元使用示意
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain
llm = ChatOpenAI(temperature=0.9)
prompt = ChatPromptTemplate.from_template(
"What is the best name to describe \
a company that makes {product}?"
)
chain = LLMChain(llm=llm, prompt=prompt)
product = "Queen Size Sheet Set"
chain.run(product)
'Royal Comfort Linens'
在只有一個輸入和一個輸出的簡單情境下,藉由SimpleSequentialChain
串連兩個LLMChain
SimpleSequentialChain
是一個用於組合多個 LLMChain
的結構,允許按順序連續處理不同的語言任務。它比 SequentialChain
簡單,因為它直接將一個 LLMChain
的輸出作為下一個 LLMChain
的輸入,沒有額外的中間變量或複雜的資料流程管理。這種設計使得 SimpleSequentialChain
非常適合於簡單的順序任務,如在本例中先生成公司名稱,然後再為這個名稱創建描述。它簡化了連續處理步驟的實施,適合於那些需要順序處理但又不需要複雜資料管理的情景。
SimpleSequentialChain
範例中可以看到chain1產出的結果(company_name)傳遞給chain2
from langchain.chains import SimpleSequentialChain
llm = ChatOpenAI(temperature=0.9)
# prompt template 1
first_prompt = ChatPromptTemplate.from_template(
"What is the best name to describe \
a company that makes {product}?"
)
# Chain 1
chain_one = LLMChain(llm=llm, prompt=first_prompt)
# prompt template 2
second_prompt = ChatPromptTemplate.from_template(
"Write a 20 words description for the following \
company:{company_name}"
)
# chain 2
chain_two = LLMChain(llm=llm, prompt=second_prompt)
串連兩個LLMChain產出結果
overall_simple_chain = SimpleSequentialChain(chains=[chain_one, chain_two],
verbose=True)
overall_simple_chain.run(product)
> Entering new SimpleSequentialChain chain...
RegalRest Linens
RegalRest Linens offers luxurious and high-quality linens for hotels, resorts, and high-end establishments. Experience ultimate comfort and elegance.
多個輸入或輸出的情境
SequentialChain
是一種結構,用於將多個 LLMChain (語言模型鏈)依序連接起來。在這個案例中,每一個 LLMChain
代表一個特定的處理步驟(如翻譯、摘要製作等),而 SequentialChain
則確保這些步驟按順序執行。這種結構允許複雜的資料處理流程被拆分為較小、較易於管理的單元,並依序進行,從而有效地處理複雜的語言任務。
SequentialChain
output_key
指定)
overall_chain = SequentialChain(
chains=[chain_one, chain_two, chain_three, chain_four],
input_variables=["Review"],
output_variables=["English_Review", "summary","followup_message"])
from langchain.chains import SequentialChain
llm = ChatOpenAI(temperature=0.9, model=llm_model)
# prompt template 1: translate to english
first_prompt = ChatPromptTemplate.from_template(
"Translate the following review to english:"
"\n\n{Review}"
)
# chain 1: input= Review and output= English_Review
chain_one = LLMChain(llm=llm,
prompt=first_prompt,
output_key="English_Review")
second_prompt = ChatPromptTemplate.from_template(
"Can you summarize the following review in 1 sentence:"
"\n\n{English_Review}")
# chain 2: input= English_Review and output= summary
chain_two = LLMChain(llm=llm,
prompt=second_prompt,
output_key="summary")
# prompt template 3: translate to english
third_prompt = ChatPromptTemplate.from_template(
"What language is the following review:\n\n{Review}"
)
# chain 3: input= Review and output= language
chain_three = LLMChain(llm=llm,
prompt=third_prompt,
output_key="language"
)
# prompt template 4: follow up message
fourth_prompt = ChatPromptTemplate.from_template(
"Write a follow up response to the following "
"summary in the specified language:"
"\n\nSummary: {summary}\n\nLanguage: {language}"
)
# chain 4: input= summary, language and output= followup_message
chain_four = LLMChain(llm=llm,
prompt=fourth_prompt, output_key="followup_message"
)
# overall_chain: input= Review
# and output= English_Review,summary, followup_message
overall_chain = SequentialChain(
chains=[chain_one, chain_two, chain_three, chain_four],
input_variables=["Review"],
output_variables=["English_Review", "summary","followup_message"],
verbose=True
)
review = df.Review[5]
# overall_chain(review)
results = overall_chain(review)
pd.set_option('max_colwidth', None)
pd.DataFrame.from_dict(results,orient='index').T
Router Chain的目的是當接收到用戶的問題時,它能自動判斷哪一個範疇的模板最適合用來回答該問題。例如,如果問題是關於物理的,它將選擇物理模板來生成回答。這大大提高了回答的質量和相關性
Router Chain
prompt_infos
清單中LLMChain
,其任務是使用該範疇的模板進行問題的回答。MULTI_PROMPT_ROUTER_TEMPLATE
,其功能是根據用戶的輸入選擇最適合的範疇模板router_chain
的LLMRouterChain
router_chain
、所有範疇的destination_chains
和一個default_chain
(當無法找到適合的範疇時使用)建立了一個名為chain的MultiPromptChainphysics_template = """You are a very smart physics professor. \
You are great at answering questions about physics in a concise\
and easy to understand manner. \
When you don't know the answer to a question you admit\
that you don't know.
Here is a question:
{input}"""
math_template = """You are a very good mathematician. \
You are great at answering math questions. \
You are so good because you are able to break down \
hard problems into their component parts,
answer the component parts, and then put them together\
to answer the broader question.
Here is a question:
{input}"""
history_template = """You are a very good historian. \
You have an excellent knowledge of and understanding of people,\
events and contexts from a range of historical periods. \
You have the ability to think, reflect, debate, discuss and \
evaluate the past. You have a respect for historical evidence\
and the ability to make use of it to support your explanations \
and judgements.
Here is a question:
{input}"""
computerscience_template = """ You are a successful computer scientist.\
You have a passion for creativity, collaboration,\
forward-thinking, confidence, strong problem-solving capabilities,\
understanding of theories and algorithms, and excellent communication \
skills. You are great at answering coding questions. \
You are so good because you know how to solve a problem by \
describing the solution in imperative steps \
that a machine can easily interpret and you know how to \
choose a solution that has a good balance between \
time complexity and space complexity.
Here is a question:
{input}"""
prompt_infos 是一個包含四個字典的列表。每個字典代表一個專門的問答模板,旨在幫助語言模型以某一領域的專家身份回答問題。以下以physics為例
name
:模板的名稱,此處為 "physics"description
:描述此模板的用途,此處表示它適合回答有關物理的問題prompt_template
:使用先前定義的 physics_template 作為該模板的具體內容
prompt_infos = [
{
"name": "physics",
"description": "Good for answering questions about physics",
"prompt_template": physics_template
},
{
"name": "math",
"description": "Good for answering math questions",
"prompt_template": math_template
},
{
"name": "History",
"description": "Good for answering history questions",
"prompt_template": history_template
},
{
"name": "computer science",
"description": "Good for answering computer science questions",
"prompt_template": computerscience_template
}]
from langchain.chains.router import MultiPromptChain
from langchain.chains.router.llm_router import LLMRouterChain,RouterOutputParser
from langchain.prompts import PromptTemplate
llm = ChatOpenAI(temperature=0, model=llm_model)
destination_chains
與default_prompt
為不同的問答領域建立專門的LLMChain物件,並同時保留一個預設的LLMChain用於通用問題
destination_chains
destination_chains = {}
for p_info in prompt_infos:
name = p_info["name"]
prompt_template = p_info["prompt_template"]
prompt = ChatPromptTemplate.from_template(template=prompt_template)
chain = LLMChain(llm=llm, prompt=prompt)
destination_chains[name] = chain
destinations = [f"{p['name']}: {p['description']}" for p in prompt_infos]
destinations_str = "\n".join(destinations)
# destinations
# ['physics: Good for answering questions about physics',
# 'math: Good for answering math questions',
# 'History: Good for answering history questions',
# 'computer science: Good for answering computer science questions']
default_prompt
default_prompt = ChatPromptTemplate.from_template("{input}")
default_chain = LLMChain(llm=llm, prompt=default_prompt)
MULTI_PROMPT_ROUTER_TEMPLATE
Router Chain最核心邏輯的部分
根據一系列的候選提示和給定的輸入,導引語言模型選擇和(可能)修改最合適的提示。這有助於根據特定的問題或輸入定制語言模型的響應
destination
: 用來指定要使用的提示名稱或"DEFAULT"next_inputs
: 原始輸入的可能修改版本destination
字段必須是下面指定的候選提示名稱之一,或者如果輸入不適合任何候選提示,則可以是"DEFAULT"next_inputs
可以是原始輸入
MULTI_PROMPT_ROUTER_TEMPLATE = """Given a raw text input to a \
language model select the model prompt best suited for the input. \
You will be given the names of the available prompts and a \
description of what the prompt is best suited for. \
You may also revise the original input if you think that revising\
it will ultimately lead to a better response from the language model.
<< FORMATTING >>
Return a markdown code snippet with a JSON object formatted to look like:
\```json
{{{{
"destination": string \ name of the prompt to use or "DEFAULT"
"next_inputs": string \ a potentially modified version of the original input
}}}}
\```
REMEMBER: "destination" MUST be one of the candidate prompt \
names specified below OR it can be "DEFAULT" if the input is not\
well suited for any of the candidate prompts.
REMEMBER: "next_inputs" can just be the original input \
if you don't think any modifications are needed.
<< CANDIDATE PROMPTS >>
{destinations}
<< INPUT >>
{{input}}
<< OUTPUT (remember to include the ```json)>>"""
LLMRouterChain
、MultiPromptChain
將所有之前創建的專家模板、默認鏈接和路由鏈接組合起來,形成一個完整的、能夠智能地根據輸入選擇最合適回答方法的鏈接系統
設置路由模板 (router_template):
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(
destinations=destinations_str
)
這裡使用MULTI_PROMPT_ROUTER_TEMPLATE字符串模板並替換其內部的{destinations}部分,填充之前計算的destinations_str。結果是一個自定義的模板,其中包含了所有可用的模板名稱和描述。
創建路由提示 (router_prompt):
router_prompt = PromptTemplate(
template=router_template,
input_variables=["input"],
output_parser=RouterOutputParser(),
)
這裡使用剛才生成的router_template來創建一個PromptTemplate物件,名稱為router_prompt。
這個提示將用於指導語言模型決定根據提供的輸入使用哪一個專家模板來回答問題。
創建路由鏈接 (router_chain):
router_chain = LLMRouterChain.from_llm(llm, router_prompt)
使用上一步中的router_prompt和llm語言模型,我們創建了一個名為router_chain的LLMRouterChain物件。該物件的目的是在接收到一個輸入問題時,根據router_prompt的指引,決定使用哪一個專家模板來回答。
創建多提示鏈接 (chain):
chain = MultiPromptChain(router_chain=router_chain,
destination_chains=destination_chains,
default_chain=default_chain, verbose=True
)
最後,使用前面的結果創建了一個MultiPromptChain物件,名稱為chain。這個物件將協調所有的鏈接:它首先使用router_chain來決定要使用哪一個destination_chains中的專家模板。如果沒有合適的專家模板,則會使用default_chain來回答問題。