AI大模型實(shí)戰(zhàn)篇:Basic Reflection,AI Agent的左右互搏之術(shù)
文章通過實(shí)際源碼詳細(xì)介紹了Basic Reflection模式的實(shí)現(xiàn)方法,包括構(gòu)建Generator和Reflector的過程。閱讀本文可以幫助讀者更好地理解Basic Reflection的概念和實(shí)現(xiàn)過程。
在前面幾篇文章中,風(fēng)叔依次介紹了REWOO、Plan-and-Execute和LLM Compiler三種更側(cè)重規(guī)劃能力的AI Agent設(shè)計(jì)模式。
從最初的ReAct模式出發(fā),加入規(guī)劃能力即演變成REWOO;再加上Replan能力即演變成Plan-and-Execute;最后再加上DAG和并行處理能力,即演變成LLM Compiler。
從這篇文章開始,我們將轉(zhuǎn)向另外幾種側(cè)重反思的AI Agent模式,我們首先從Basic Reflection開始。
一、Basic Reflection的概念
Basic Reflection可以類比于左右互博。左手是Generator,負(fù)責(zé)根據(jù)用戶指令生成結(jié)果;右手是Reflector,來審查Generator的生成結(jié)果并給出建議。在左右互搏的情況下,Generator生成的結(jié)果越來越好,Reflector的檢查越來越嚴(yán)格,輸出的結(jié)果也越來越有效。
下圖是Basic Reflection的原理,非常簡單。
- Generator接收來自用戶的輸入,輸出initial response
- Reflector接收來自Generator的response,根據(jù)開發(fā)者設(shè)置的要求,給出Reflections,即評語、特征、建議
- Generator再根據(jù)Reflector給出的反饋進(jìn)行修改和優(yōu)化,輸出下一輪response
- 循環(huán)往復(fù),直到循環(huán)次數(shù)
二、Basic Reflection的實(shí)現(xiàn)過程
下面,風(fēng)叔通過實(shí)際的源碼,詳細(xì)介紹Basic Reflection模式的實(shí)現(xiàn)方法,具體的源代碼地址在文章結(jié)尾處獲取。
第一步 構(gòu)建Generator
在下面的例子中,我們先構(gòu)建一個(gè)能夠生成5段話的文章生成器。
首先,我們給Generator約定了Prompt,告訴Generator具體的角色和目標(biāo),并且要求如果用戶給出建議,需要給出修改后的版本
然后選擇LLM模型,并構(gòu)建Generator
最后要求輸出Generator生成的內(nèi)容
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_fireworks import ChatFireworksprompt = 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雖然按照我們的要求生成了初始回復(fù),但是回復(fù)的整體質(zhì)量還有很大的提升空間。
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.
第二步 構(gòu)建Reflector
針對Generator,我們構(gòu)建一個(gè)專門的Reflector。
同樣的,我們要先給Reflector約定Prompt,告訴Reflector“你是一名正在給論文評分的老師。請對用戶的論文提出批評和建議,包括長度、深度、風(fēng)格等要求?!?/p>
然后選擇LLM模型,并構(gòu)建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+的評級,并且按照我們的要求,從文章的長度、深度、風(fēng)格等維度給出了具體的建議和總結(jié)。
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.
第三步 循環(huán)執(zhí)行
接下來,我們就可以循環(huán)執(zhí)行這個(gè)“生成 – 檢查”的過程,重復(fù)規(guī)定的次數(shù),或者約定當(dāng)Generator的生成結(jié)果達(dá)到多少分時(shí),停止循環(huán)。
大家可以看到,在循環(huán)過程中,我們也加入了人類的反饋建議,有助于Generator和Reflector學(xué)習(xí)迭代。
for chunk in generate.stream(
{“messages”: [request, AIMessage(content=essay), HumanMessage(content=reflection)]}
):
print(chunk.content, end=””)
第四步 構(gòu)建流程圖
下面,我們構(gòu)建流程圖,將Generator、Reflector等節(jié)點(diǎn)添加進(jìn)來,循環(huán)執(zhí)行并輸出結(jié)果。
from typing import Annotated, List, Sequence
from langgraph.graph import END, StateGraph, START
from langgraph.graph.message import add_messages
from typing_extensions import TypedDictclass 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基礎(chǔ)的同學(xué)也能看懂。可以關(guān)注風(fēng)叔或在評論區(qū)留言,回復(fù)關(guān)鍵詞【BR源碼】,獲取Basic Reflection設(shè)計(jì)模式的完整源代碼。
三、總結(jié)
Basic Reflection的架構(gòu),非常適合于進(jìn)行相對比較發(fā)散的內(nèi)容生成類工作,比如文章寫作、圖片生成、代碼生成等等。
總體而言,Basic Reflection是一種非常高效的反思類AI Agent設(shè)計(jì)模式。Basic Reflection 的思路非常樸素,使用成本較低。但是在實(shí)際應(yīng)用中,Basic Reflection也面臨著一些缺陷:
- 對于一些比較復(fù)雜的問題,顯然需要Generator具備更強(qiáng)大的推理能力
- Generator生成的結(jié)果可能會過于發(fā)散,和我們要求的結(jié)果相去甚遠(yuǎn)
- 在一些復(fù)雜場景下,Generator和Reflector之間的循環(huán)次數(shù)不太好定義,如果次數(shù)太少,生成效果不夠理想;如果次數(shù)太多,對token的消耗會很大。
我們有兩種方法來優(yōu)化Basic Reflection,一種是邊推理邊執(zhí)行的Self Discover模式,一種是增加了強(qiáng)化學(xué)習(xí)的Reflexion模式。
在下一篇文章中,風(fēng)叔將介紹Self Discover模式。
本文由人人都是產(chǎn)品經(jīng)理作者【風(fēng)叔】,微信公眾號:【風(fēng)叔云】,原創(chuàng)/授權(quán) 發(fā)布于人人都是產(chǎn)品經(jīng)理,未經(jīng)許可,禁止轉(zhuǎn)載。
題圖來自Unsplash,基于 CC0 協(xié)議。
- 目前還沒評論,等你發(fā)揮!