北京設(shè)計(jì)機(jī)構(gòu)合肥關(guān)鍵詞優(yōu)化平臺
一、基于LangChain源碼react來解析prompt engineering
在LangChain源碼中一個(gè)特別重要的部分就是react,它的基本概念是,LLM在推理時(shí)會產(chǎn)生很多中間步驟而不是直接產(chǎn)生結(jié)果,這些中間步驟可以被用來與外界進(jìn)行交互,然后產(chǎn)生new context:
我們先看下textworld_prompt這個(gè)文件,按照前面提到的“thought”,“action”,“observation”的結(jié)構(gòu)給出了如下樣例,其中observation部分以文字的形式提供了一種類似于解題的思路來說明如何完成具體的action:
另外看一下wiki_prompt這個(gè)文件,在所給的例子中,首先也是給出一個(gè)question,然后針對問題給出thought,也就是如何進(jìn)行推理:
Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
Thought: I need to search Colorado orogeny, find the area that the eastern sector of the Colorado orogeny extends into, then find the elevation range of the area.
Action: Search[Colorado orogeny]
在這里,執(zhí)行action的不是LLM,而是agent,執(zhí)行wiki搜索之后返回結(jié)果:
Observation: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.
Thought: It does not mention the eastern sector. So I need to look up eastern sector.
Action: Lookup[eastern sector]
從上面的返回結(jié)果看,并沒有提到關(guān)鍵信息“eastern sector”,因此上面的thought提示需要查找這部分信息:
Observation: (Result 1 / 1) The eastern sector extends into the High Plains and is called the Central Plains orogeny.
Thought: The eastern sector of Colorado orogeny extends into the High Plains. So I need to search High Plains and find its elevation range.
Action: Search[High Plains]
根據(jù)上面查詢“eastern sector”的結(jié)果,接下來需要搜索關(guān)于“High Plains”的信息:
Observation: High Plains refers to one of two distinct land regions
Thought: I need to instead search High Plains (United States).
Action: Search[High Plains (United States)]
由于上面的搜索結(jié)果提到了“two distinct land regions”,所以接下來的步驟就是搜索關(guān)于“High Plains (United States)”的信息:
Observation: The High Plains are a subregion of the Great Plains. From east to west, the High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130 m).[3]
Thought: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer is 1,800 to 7,000 ft.
Action: Finish[1,800 to 7,000 ft]
基于上面的搜索結(jié)果進(jìn)行推理,最后獲得了我們想要的數(shù)據(jù):1,800 to 7,000 ft。
上面重復(fù)迭代多次的observation+thought+action就構(gòu)成了一個(gè)鏈?zhǔn)降倪^程。
二、結(jié)合Chain of Thought(COT)經(jīng)典案例剖析prompt
接下來我們針對這樣一個(gè)鏈?zhǔn)降倪^程,來看一個(gè)應(yīng)用案例。在這個(gè)例子中,我們使用了OpenAI的API來調(diào)用GPT-3.5模型,并沒有使用LangChain的方式:
我們來看下關(guān)于“Chain-of-Thought Prompting”是如何進(jìn)行構(gòu)造的,這個(gè)prompt是跟客戶查詢有關(guān)的,定義了多個(gè)步驟來回答客戶的問題:
Step 1是檢查用戶的問題是否針對一個(gè)具體的產(chǎn)品或者一組產(chǎn)品來說的
Step 2是檢查用戶問題涉及到的產(chǎn)品是否是以下列出的這些產(chǎn)品,這里可能是為了演示的方便,所以直接以文本的形式呈現(xiàn)出來,當(dāng)然這些產(chǎn)品信息可以存儲在數(shù)據(jù)庫里
Step 3判斷如果用戶問題涉及以上產(chǎn)品,那么列出用戶針對產(chǎn)品會提什么問題的任意假設(shè):
Step 4基于現(xiàn)有的產(chǎn)品信息來判斷用戶提出的問題是否有匹配的答案:
?????? Step 5提示應(yīng)該以對用戶友好的方式來修正用戶提到的不正確的問題假設(shè),也就是說用戶只能針對5個(gè)可用的產(chǎn)品來提相關(guān)問題:
以上部分都可以看做是基本的上下文信息(system message),接下來設(shè)定user_message的內(nèi)容,調(diào)用方法get_completion_from_messages獲得結(jié)果:
打印的結(jié)果如下,由于用戶問題提到了具體產(chǎn)品和價(jià)格,所以step 2給出了每一種產(chǎn)品的價(jià)格,并對用戶的假設(shè)進(jìn)行了判斷:
設(shè)定用戶的問題如下:
do you sell tvs
這次給出的結(jié)果如下,推理步驟1和2共同判斷了用戶提到的TVs不在當(dāng)前可用產(chǎn)品列表中,所以模型在運(yùn)行時(shí)會按照之前的系統(tǒng)設(shè)定來給出恰當(dāng)?shù)幕貜?fù)給用戶:
Step 1:#### The user is asking about a specific product category, TVs.
Step 2:#### The list of available products does not include any TVs.
Response to user:#### I'm sorry, but we do not sell TVs at this time. Our store specializes in computers and laptops. However, if you are interested in purchasing a computer or laptop, please let me know and I would be happy to assist you.
這個(gè)例子看上去不復(fù)雜,但是它的意義重大,因?yàn)樯厦嫠械南到y(tǒng)信息都是私有數(shù)據(jù),如果已經(jīng)告訴模型只能從私有數(shù)據(jù)中去查詢,那么模型就不會從其它地方去獲取數(shù)據(jù),譬如針對上面step 2所列的信息,可以修改為從數(shù)據(jù)庫或者vector store中進(jìn)行查詢(也就是使用具體的工具),如果使用LangChain,那么它可以幫我們封裝這個(gè)過程,如果不使用LangChain,那么可以自己來封裝。
If the user is asking about \
specific products, identify whether \
the products are in the following list.
接下來設(shè)定用戶信息如下:
推理過程如下,step1 是針對用戶問題的理解,因此step 2列出了當(dāng)前可用的產(chǎn)品信息,由于用戶問題中并沒有給出明確的假設(shè),所以step 4的內(nèi)容是基于LLM的理解給出的,然后與本地的私有數(shù)據(jù)進(jìn)行匹配,之后基于這些信息來回復(fù)給用戶,所以整個(gè)過程都是LLM來驅(qū)動的:
Step 1:#### The user is asking for a recommendation for a laptop based on their profession.
Step 2:#### The available laptops are:
1. TechPro Ultrabook
2. BlueWave Gaming Laptop
3. PowerLite Convertible
4. TechPro Desktop
5. BlueWave Chromebook
Step 3:#### There are no assumptions made by the user in this message.
Step 4:#### Based on the user's profession as an iOS developer, they would require a laptop with a powerful processor and sufficient RAM to handle development tasks. The TechPro Ultrabook and the BlueWave Gaming Laptop would be suitable options for an iOS developer due to their powerful processors and high RAM capacity.
Response to user:#### As an iOS developer, I would recommend either the TechPro Ultrabook or the BlueWave Gaming Laptop. Both laptops have powerful processors and high RAM capacity, which are essential for development tasks.
通過以上經(jīng)典案例展示了用LLM來驅(qū)動一切,驅(qū)動的關(guān)鍵在于你自己的prompt要寫得很清楚。