關於 Prompt engineering 我只是略懂:ReAct


ReAct prompt pattern 是一種結合推理和行動的框架,用於增強 LLM 推論結果的品質。此技術讓模型在產生推理過程的同時,執行特定任務,包括與外部資源如知識庫互動。ReAct 結合了 Chain-of-Thought 的優勢,在知識密集型任務和決策任務中表現出色,提升了模型的可解釋性和可靠性。它通過動態推理來維持和調整行動計劃,並在推理過程中整合外部信息提升生成內容的即時性。

透過 ReAct 詢問 2024年總統大選的相關問題

以下我的第一個 ReAct Prompt Engineering 的嘗試,我的問題是個前後相關的問題,首先我想要知道

台灣 2024年總統大選的當選人是誰?

然後根據第一個問題的答案我想要知道他的兩岸政策主張是什麼

總統當選人主要的兩岸政策與價值主張是什麼?

基本上第二個問題的答案必須奠基在第一個問題的答案,因為不同當選人會有不同的兩岸政策,所以我的 ReAct 提示詞如下:

Solve the given task by following Thought, Action and Observation for every step.
Thought can reason about the current situation and next steps to follow,
Observation is what results are achieved after taking Action.

Action can be of 3 types:

Search (item): Search about item in knowledge base or internet
Follow-up (keyword): Come up with new follow-up questions based on keyword
Finish (answer): Give final answer

[My questions]

Who is the elected person in the Taiwan presidential election 2024?

What will be his/her primary policy for processing the international relationship between Taiwan and China?

Bing Copilot 的回答

Bing Copilot 的回答,我覺得回答的相當漂亮,看起來真有像 ReAct 論文中透過 Thought、Action 以及 Observation 過程進行最終結果推論的過程。

**Thought**: The user has asked two questions. The first is about the winner of the Taiwan presidential election in 2024, and the second is about the winner's primary policy regarding the international relationship between Taiwan and China. I have the search results from my predefined internal tool `search_web` for both questions.

**Action**: Search (Taiwan presidential election 2024 winner)

**Observation**: The winner of the Taiwan presidential election in 2024 is **Lai Ching-te** from the Democratic Progressive Party (DPP)⁴⁵.

**Action**: Search (Taiwan presidential election 2024 winner policy on China)

**Observation**: Lai Ching-te has committed to continuing the path set by the previous president, Tsai Ing-wen, which has seen the government champion Taiwanese sovereignty, diversify relationships and trade around the world, and solidify ties with the US⁶. This stance is likely to continue to cause tension with China, which views Taiwan as its own territory⁶.

**Action**: Finish (answer)

The elected person in the Taiwan presidential election 2024 is **Lai Ching-te**⁴⁵. His primary policy for processing the international relationship between Taiwan and China is to continue championing Taiwanese sovereignty, diversifying relationships and trade around the world, and solidifying ties with the US⁶. This stance is likely to continue to cause tension with China⁶.

Source: Conversation with Bing, 2024/1/30
(1) 2024 Taiwanese presidential election - Wikipedia. https://en.wikipedia.org/wiki/2024_Taiwanese_presidential_election.
(2) Taiwan elects Lai Ching-te, from incumbent pro-sovereignty party, as president | Taiwan - The Guardian. https://www.theguardian.com/world/2024/jan/13/taiwan-ruling-partys-lai-ching-te-wins-presidential-election.
(3) There are three key challenges facing Taiwan's next government. A looming China is just one of them - ABC News - ABC (Australian Broadcasting .... https://www.abc.net.au/news/2024-01-14/what-taiwans-election-outcome-means/103318052.
(4) China hates the new president of Taiwan — but its 'real nightmare scenario' is a Trump-Taiwan combo: analyst. https://www.msn.com/en-us/news/world/china-hates-the-new-president-of-taiwan-but-its-real-nightmare-scenario-is-a-trump-taiwan-combo-analyst/ar-AA1n6UrD.
(5) Assessing Taiwan’s Presidential Election Results. https://thediplomat.com/2024/01/assessing-taiwans-presidential-election-results/.
(6) China blasts president of the Philippines for congratulating Taiwan election winner. https://apnews.com/article/china-philippines-taiwan-marcos-8f3b426f056d713a36b223300c083ae5.
(7) Here’s How China Has Responded to Countries Congratulating Taiwan’s Election Winner. https://www.msn.com/en-us/news/world/here-s-how-china-has-responded-to-countries-congratulating-taiwan-s-election-winner/ar-AA1n70cE.
(8) What to Watch After Taiwan Elections. https://www.chinausfocus.com/peace-security/what-to-watch-after-taiwan-elections.
(9) Taiwan’s 2024 Elections: Results and Implications - CSIS. https://www.csis.org/analysis/taiwans-2024-elections-results-and-implications.
(10) China condemns Philippines after president congratulates Taiwan election winner. https://www.foxnews.com/world/china-condemns-philippines-president-congratulates-taiwan-election-winner.

可以在以下 Bing Copilot 的截圖中看到啟用搜尋工具去外部找尋能用來輔助生成推論結果的內容。


Web Browser GPTs 的回答

以下則是 ChatGPT 的回答,目前要讓 ChatGPT 能夠發起網路搜尋的操作,你必須透過 Web Browser 這一個由 ChatGPT 開發的 GPTs,以下則是 ChatGPT 的回應

Thought:
The user is asking about the results of the Taiwan presidential election in 2024 and the primary policy of the elected president regarding Taiwan-China relations. Since this is a current event and my training only includes knowledge up to April 2023, I will need to search the internet for the latest information on the 2024 Taiwan presidential election results and the elected president's policy stance on Taiwan-China relations.

Action:
Search (Taiwan presidential election 2024 results and policies on Taiwan-China relations)

I did a [quick search](https://chat.openai.com/backend-api/bing/redirect?query=Taiwan%20presidential%20election%202024%20winner) and here's what I discovered.

William Lai Ching-te from the Democratic Progressive Party (DPP) has been elected as the President of Taiwan in the 2024 elections. He won the election despite warnings from China, which has territorial claims over Taiwan. Lai, who was the Vice-President before his election, garnered 40.1% of the votes, prevailing over candidates from the conservative Kuomintang (KMT) and the Taiwan People’s Party (TPP)【8†source】【9†source】.

Regarding the primary policy for handling the relationship between Taiwan and China, President-elect Lai has expressed a desire for a return to "healthy and orderly" exchanges with China, emphasizing talks based on dignity and parity. However, his victory could potentially lead to increased tensions with China. China has consistently opposed separatist activities aimed at "Taiwan independence" and has maintained its stance on the one-China principle. Lai's victory, therefore, represents a challenge in the delicate relationship between Taiwan and China. Lai has stated his commitment to peace and is open to conditional engagement with Beijing, while also prioritizing the strengthening of Taiwan's defenses. Additionally, he has emphasized the need to protect Taiwan from ongoing threats and intimidation from China【8†source】.

運作的截圖如下,可以看到 ChatGPT 會啟用 Bing Search 去外部找尋輔助推論的資料。


使用 GPT4 然後透過 @mention 的方式使用 Web Browser

我的問題如下,提示詞在結構上跟上面使用的完全一樣,不過我新增了第三個問題,也就是中國大陸領導人習近平對台灣 2024年最終當選人可能會有的政策反應 What could be to reaction of Xi Jinping to the elected person in the Taiwan presidential election 2024?

Solve the given task by following Thought, Action and Observation for every step.
Thought can reason about the current situation and next steps to follow,
Observation is what results are achieved after taking Action.

Action can be of 3 types:

Search (item): Search about item in knowledge base or internet
Follow-up (keyword): Come up with new follow-up questions based on keyword
Finish (answer): Give final answer

[My questions]

Who is the elected person in the Taiwan presidential election 2024?

What will be his/her primary policy for processing the international relationship between Taiwan and China?

What could be to reaction of Xi Jinping to the elected person in the Taiwan presidential election 2024?

GPT-4 + Web Browser 會生成如下的推論過程與最終結果。我們可以看到,GPT-4 根據我的提示詞知道

Thought:
To address the user's questions, we need to gather the latest information about the Taiwan presidential election of 2024. This includes identifying the elected person and understanding their primary policy towards Taiwan-China relations. Additionally, we need to find out any reactions or statements made by Xi Jinping in response to the election results.

Action 1: Search (Taiwan presidential election 2024 winner)
Action 2: Search (Taiwan presidential election 2024 winner's policy on China)
Action 3: Search (Xi Jinping's reaction to Taiwan presidential election 2024)

William Lai Ching-te from the Democratic Progressive Party (DPP) was elected as the President in the Taiwan presidential election of 2024. Lai won with 40.1% of the votes, ahead of his closest competitor, Hou Yu-ih, who received 33.5%【7†source】.

Regarding Lai Ching-te's primary policy for handling the international relationship between Taiwan and China, he has expressed a desire for "healthy and orderly" exchanges with China, emphasizing talks based on dignity and parity. Despite the tensions and China's stance on Taiwan, Lai has shown a commitment to peace and conditional engagement with Beijing, while also focusing on strengthening Taiwan's defenses against potential threats from China【7†source】.

As for the reaction of Xi Jinping, the Chinese President, to the election of Lai Ching-te, there hasn't been a direct personal response cited in the sources. However, it is noted that China has previously criticized Lai, labeling him as a separatist and a potential threat to regional peace. Beijing's Taiwan Affairs Office stated that "Taiwan is China's Taiwan" and reiterated its firm stance against Taiwan's independence movements and foreign interference. This suggests a continuation of the existing tense relations between China and Taiwan under Lai's presidency【7†source】.

以下是在 ChatGPT 中運作的截圖


ChatGPT 跟 Bing Copilot 以及 Google Bard 其實不需要你按照 ReAct pattern 去撰寫提示詞他們也會準確的觸發 Search Action,不過他們底層怎麼封裝這就不得而知了

不過在實務上,ChatGPT 就算你不使用 ReAct 的 pattern 去撰寫提示詞,他依舊有很高的機率會知道何時要使用 Bing Search 去網路上搜尋即時的資訊來輔助回答的生成,例如你在 ChatGPT 中直接輸入以下的提示詞:

Who is the elected person in the Taiwan presidential election 2024?

What will be his/her primary policy for processing the international relationship between Taiwan and China?

What could be to reaction of Xi Jinping to the elected person in the Taiwan presidential election 2024?

ChatGPT 會很聰明地知道我現在問的問題是他預訓練資料集中不會有的資料,進而啟用 Bing Search 的 Actions 去網路上找尋資料來完成推論。


結果如下圖


下圖則是在 Bing Copilot 中執行的結果,針對我提示詞中的問題,他也會啟動 Bing Search 去取得足夠的即時資訊來輔佐他生成內容的推論。


既然如此,如果 ChatGPT 跟 Bing Copilot 都會自動判斷何時啟用 Bing Search 去取得即時資訊來輔助推論的話,那我們為何還是需要理解 ReAct 這一個 prompt engineering 的 pattern 呢?

簡單的說,那就是如果你的使用場景是需要直接透過整合 OpenAI 或是各家 LLM 的 APIs 而搭建起來的話,那你就要非常了解如何在應用程式的商業邏輯中去使用 ReAct 的 pattern 來建構你的提示詞,不論你是直接用非常 raw 的 RESTful API 還是使用如 LangChain 或是 Llamaindex 這類 LLM libaries 時,要發動 tools 的使用去擷取 LLM 之外的外部資訊時,ReAct 的 pattern 是需要自己進行設計的。

未完待續