AI大模型實戰篇:Basic Reflection,AI Agent的左右互搏之術

0 評論 2557 瀏覽 2 收藏 18 分鐘

文章通過實際源碼詳細介紹了Basic Reflection模式的實現方法,包括構建Generator和Reflector的過程。閱讀本文可以幫助讀者更好地理解Basic Reflection的概念和實現過程。

在前面幾篇文章中,風叔依次介紹了REWOO、Plan-and-Execute和LLM Compiler三種更側重規劃能力的AI Agent設計模式。

從最初的ReAct模式出發,加入規劃能力即演變成REWOO;再加上Replan能力即演變成Plan-and-Execute;最后再加上DAG和并行處理能力,即演變成LLM Compiler。

從這篇文章開始,我們將轉向另外幾種側重反思的AI Agent模式,我們首先從Basic Reflection開始。

一、Basic Reflection的概念

Basic Reflection可以類比于左右互博。左手是Generator,負責根據用戶指令生成結果;右手是Reflector,來審查Generator的生成結果并給出建議。在左右互搏的情況下,Generator生成的結果越來越好,Reflector的檢查越來越嚴格,輸出的結果也越來越有效。

下圖是Basic Reflection的原理,非常簡單。

  • Generator接收來自用戶的輸入,輸出initial response
  • Reflector接收來自Generator的response,根據開發者設置的要求,給出Reflections,即評語、特征、建議
  • Generator再根據Reflector給出的反饋進行修改和優化,輸出下一輪response
  • 循環往復,直到循環次數

二、Basic Reflection的實現過程

下面,風叔通過實際的源碼,詳細介紹Basic Reflection模式的實現方法,具體的源代碼地址在文章結尾處獲取。

第一步 構建Generator

在下面的例子中,我們先構建一個能夠生成5段話的文章生成器。

首先,我們給Generator約定了Prompt,告訴Generator具體的角色和目標,并且要求如果用戶給出建議,需要給出修改后的版本

然后選擇LLM模型,并構建Generator

最后要求輸出Generator生成的內容

from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_fireworks import ChatFireworks

prompt = ChatPromptTemplate.from_messages(
[
(
“system”,
“You are an essay assistant tasked with writing excellent 5-paragraph essays.”
” Generate the best essay possible for the user’s request.”
” If the user provides critique, respond with a revised version of your previous attempts.”,
),
MessagesPlaceholder(variable_name=”messages”),
]
)
llm = ChatFireworks(
model=”accounts/fireworks/models/mixtral-8x7b-instruct”,
model_kwargs={“max_tokens”: 32768},
)

generate = prompt | llm
essay = “”
request = HumanMessage(
content=”Write an essay on why the little prince is relevant in modern childhood”
)

for chunk in generate.stream({“messages”: [request]}):
print(chunk.content, end=””)
essay += chunk.content

下面是Generator的Initial Response,可以看出Generator雖然按照我們的要求生成了初始回復,但是回復的整體質量還有很大的提升空間。

Title: The Relevance of The Little Prince in Modern Childhood

The Little Prince, a novella by Antoine de Saint-Exupéry, has been a childhood favorite for generations. Despite being published over seven decades ago, its timeless themes continue to resonate with modern children, making it highly relevant in contemporary childhood.

Firstly, the story explores the complex nature of human relationships, which is particularly relevant for modern children growing up in an increasingly connected yet impersonal world. Through the little prince’s encounters with various grown-ups on different planets, the book highlights the importance of genuine connections and understanding. In an age where digital communication often replaces face-to-face interaction, this message is more pertinent than ever. The Little Prince encourages children to look beyond superficial relationships and seek deeper connections, fostering empathy and emotional intelligence.

Secondly, the book deals with the concept of responsibility and self-discovery, elements that are integral to a child’s growth. The little prince’s journey is essentially a quest for self-discovery, leading him to realize his responsibility towards his beloved rose. This narrative encourages modern children to embrace their individuality while understanding the significance of their actions. In a society that often overlooks the emotional well-being of children, The Little Prince offers a refreshing perspective on personal growth and responsibility.

Thirdly, the book addresses the challenging theme of loss and bereavement. The little prince’s departure from his asteroid and his subsequent encounters with the fox and the snake are profound reflections on the inevitability of loss and the importance of cherishing relationships. In a time when children are exposed to various forms of loss, from the death of loved ones to environmental degradation, The Little Prince provides a gentle yet powerful way to understand and cope with these experiences.

However, some critics argue that the book’s pace and abstract concepts might be challenging for modern children with short attention spans. To address this, a revised version could incorporate more visual elements and interactive activities to engage young readers better. Additionally, supplementary materials explaining the book’s themes in simpler terms could be provided for parents and educators to use in discussions with children.

In conclusion, The Little Prince remains relevant in modern childhood due to its exploration of human relationships, self-discovery, and loss. These themes, wrapped in a captivating narrative, offer valuable lessons for modern children. While some adaptations may be necessary to cater to the preferences of today’s children, the essence of the story remains a powerful tool for teaching emotional intelligence, personal growth, and resilience.

第二步 構建Reflector

針對Generator,我們構建一個專門的Reflector。

同樣的,我們要先給Reflector約定Prompt,告訴Reflector“你是一名正在給論文評分的老師。請對用戶的論文提出批評和建議,包括長度、深度、風格等要求。”

然后選擇LLM模型,并構建Reflector

reflection_prompt = ChatPromptTemplate.from_messages(
[
(
“system”,
“You are a teacher grading an essay submission. Generate critique and recommendations for the user’s submission.”
” Provide detailed recommendations, including requests for length, depth, style, etc.”,
),
MessagesPlaceholder(variable_name=”messages”),
]
)
reflect = reflection_prompt | llm

下面是針對Generator的Initial Response,Reflector反饋的評語和建議。Reflector給出了B+的評級,并且按照我們的要求,從文章的長度、深度、風格等維度給出了具體的建議和總結。

Essay Grade: B+

The essay you submitted provides a clear and well-structured argument about the relevance of The Little Prince in modern childhood. You have demonstrated a strong understanding of the text and its themes, and have effectively applied them to the context of contemporary childhood. However, there are some areas where improvement could be made to enhance the depth, style, and overall flow of your essay.

1. Length: While your essay is well-written and informative, it is relatively brief. Expanding on each point with more detailed analysis and examples would strengthen your argument and demonstrate a more comprehensive understanding of the text. Aim for a minimum of 500 words to allow for a more in-depth exploration of your ideas.

2. Depth: Although you have touched upon the relevance of the novel’s themes, further analysis is needed to truly establish its significance in modern childhood. For example, when discussing the complex nature of human relationships, delve into how the digital age affects children’s communication skills, and how The Little Prince addresses this issue. Providing concrete examples from the text and connecting them to real-world scenarios will make your argument more compelling.

3. Style: To engage your readers more effectively, consider varying your sentence structure and length. Using a mix of simple, compound, and complex sentences will improve the flow of your essay and make it more engaging to read. Additionally, watch your tense consistency. Ensure that you maintain the same tense throughout your essay to avoid confusion.

4. Recommendations: While your suggestions for adaptation are a good start, they could be expanded upon to provide more comprehensive recommendations. For example, you may want to discuss different methods of incorporating visual elements and interactive activities, such as illustrations, quizzes, or discussion questions. This will demonstrate that you have thoughtfully considered the needs of modern children and have developed strategies to address these challenges.

5. Conclusion: Your conclusion could benefit from a stronger summarization of your key points and an assertive final statement about the relevance of The Little Prince in modern childhood. Tying all your arguments together in a concise and powerful manner will leave a lasting impression on your readers and solidify your position.

Overall, your essay is well-researched and provides a solid foundation for a compelling argument about the relevance of The Little Prince in modern childhood. With some expansion, deeper analysis, and stylistic improvements, your essay can achieve an even higher level of excellence.

第三步 循環執行

接下來,我們就可以循環執行這個“生成 – 檢查”的過程,重復規定的次數,或者約定當Generator的生成結果達到多少分時,停止循環。

大家可以看到,在循環過程中,我們也加入了人類的反饋建議,有助于Generator和Reflector學習迭代。

for chunk in generate.stream(
{“messages”: [request, AIMessage(content=essay), HumanMessage(content=reflection)]}
):
print(chunk.content, end=””)

第四步 構建流程圖

下面,我們構建流程圖,將Generator、Reflector等節點添加進來,循環執行并輸出結果。

from typing import Annotated, List, Sequence
from langgraph.graph import END, StateGraph, START
from langgraph.graph.message import add_messages
from typing_extensions import TypedDict

class State(TypedDict):
messages: Annotated[list, add_messages]

async def generation_node(state: Sequence[BaseMessage]):
return await generate.ainvoke({“messages”: state})

async def reflection_node(messages: Sequence[BaseMessage]) -> List[BaseMessage]:
# Other messages we need to adjust
cls_map = {“ai”: HumanMessage, “human”: AIMessage}
# First message is the original user request. We hold it the same for all nodes
translated = [messages[0]] + [
cls_map[msg.type](content=msg.content) for msg in messages[1:]
]
res = await reflect.ainvoke({“messages”: translated})
# We treat the output of this as human feedback for the generator
return HumanMessage(content=res.content)

builder = StateGraph(State)
builder.add_node(“generate”, generation_node)
builder.add_node(“reflect”, reflection_node)
builder.add_edge(START, “generate”)

def should_continue(state: List[BaseMessage]):
if len(state) > 6:
# End after 3 iterations
return END
return “reflect”

builder.add_conditional_edges(“generate”, should_continue)
builder.add_edge(“reflect”, “generate”)
graph = builder.compile()

async for event in graph.astream(
[
HumanMessage(
content=”Generate an essay on the topicality of The Little Prince and its message in modern life”
)
],
):
print(event)
print(“—“)

至此,Basic Reflection的完整流程就介紹完了,非常簡單,相信哪怕是沒有AI基礎的同學也能看懂??梢躁P注風叔或在評論區留言,回復關鍵詞【BR源碼】,獲取Basic Reflection設計模式的完整源代碼。

三、總結

Basic Reflection的架構,非常適合于進行相對比較發散的內容生成類工作,比如文章寫作、圖片生成、代碼生成等等。

總體而言,Basic Reflection是一種非常高效的反思類AI Agent設計模式。Basic Reflection 的思路非常樸素,使用成本較低。但是在實際應用中,Basic Reflection也面臨著一些缺陷:

  • 對于一些比較復雜的問題,顯然需要Generator具備更強大的推理能力
  • Generator生成的結果可能會過于發散,和我們要求的結果相去甚遠
  • 在一些復雜場景下,Generator和Reflector之間的循環次數不太好定義,如果次數太少,生成效果不夠理想;如果次數太多,對token的消耗會很大。

我們有兩種方法來優化Basic Reflection,一種是邊推理邊執行的Self Discover模式,一種是增加了強化學習的Reflexion模式。

在下一篇文章中,風叔將介紹Self Discover模式。

本文由人人都是產品經理作者【風叔】,微信公眾號:【風叔云】,原創/授權 發布于人人都是產品經理,未經許可,禁止轉載。

題圖來自Unsplash,基于 CC0 協議。

更多精彩內容,請關注人人都是產品經理微信公眾號或下載App
評論
評論請登錄
  1. 目前還沒評論,等你發揮!