反思/自我反思有点被低估了。如果您的应用程序依赖于提示,我强烈建议您探索这个概念。实施起来并不难,反思技术可以帮助迭代地完善 llm 响应。
from mirascope.core import BaseMessageParam, ResponseModelConfigDict, openai from pydantic import BaseModel import os os.environ["OPENAI_API_KEY"] = "" class Review(BaseModel): issues: list[str] is_good: bool model_config = ResponseModelConfigDict(strict=True) class Story(BaseModel): story: str model_config = ResponseModelConfigDict(strict=True) class StoryWriter(BaseModel): keywords: list[str] generator_history: list[openai.OpenAIMessageParam] = [] @openai.call( "gpt-4o-mini", response_model=Story, json_mode=True, call_params={"temperature": 0.8}, ) def generator(self, query: str) -> list[openai.OpenAIMessageParam]: return [ BaseMessageParam( role="system", content="You are an expert in writing short moral stories for kids below the age of 10.", ), *self.generator_history, ] @openai.call( "gpt-4o-mini", response_model=Review, json_mode=True, call_params={"temperature": 0.1}, ) def reviewer(self, story: str) -> list[openai.OpenAIMessageParam]: return [ BaseMessageParam( role="system", content="You are an expert in reviewing short moral stories for kids below the age of 10, checking whether all the keywords were used effectively and identifying issues related to relevance and ease of understanding", ), BaseMessageParam( role="user", content=f""" Review the given moral story for kids. Check if the story uses all the given keywords. Also check if the story is reasonably realistic, engaging and uses basic vocabulary that is easy to understand for kids below the age of 10. Return the issues. Finally, return True if the moral story is good enough for kids and contains all the keywords. story: {story} keywords: {self.keywords}""", ), ] def run(self, steps=3) -> str: query = f"""Generate a moral story for kids, using all the given keywords. Return only the story. {self.keywords}""" self.generator_history += [ BaseMessageParam(role="user", content=query), ] story = "" for _ in range(steps): generator_response = self.generator(query) story = generator_response.story reviewer_response = self.reviewer(story) if reviewer_response.is_good: break query = f"""Use the given feedback to improve the story. Return only the story.""" self.generator_history += [ BaseMessageParam(role="assistant", content=generator_response.story), BaseMessageParam( role="user", content=" ".join(reviewer_response.issues) + " " + query, ), ] print(self.generator_history) return story story = StoryWriter( keywords=[ "elephant", "boy", "strong", "funny", "good", "ride", "Nikolas", "road", "cap", "car", ] ).run() print("==================") print("result", story)
登录后复制
以上就是理解自我反思的简单代码(代理设计模式)的详细内容,更多请关注抖狐科技其它相关文章!
本站文章均为抖狐网站建设摘自权威资料,书籍,或网络原创文章,如有版权纠纷或者违规问题,请即刻联系我们删除,我们欢迎您分享,引用和转载,我们谢绝直接复制和抄袭!感谢...
我们猜你喜欢
-
vision和visions的区别
vision 和 visions 的区别在于复数形式:vision 指一个特定的目标或愿景,而 visions 则指多个目标或愿景,涉及更广泛的范围或多种可能性。Vision 和 Visions 之间...
-
python爬虫键值为空怎么改
处理python爬虫中键值为空的方法包括:使用默认值使用lambda函数使用try-except语句使用nonePython爬虫中,如何处理键值为空的情况 在使用Python爬虫时,有时会遇到键值为空...
-
《人中之龙:极》Switch版热销令SEGA感到惊讶
《人中之龙:极》(Yakuza Kiwami) Switch版一发售便取得了令人惊喜的销售成绩! 世嘉如龙工作室总监横山昌义在昨日的直播中透露,游戏销量“好得不得了”(selling like hot...
-
蓝屏故障码0x0000007e要怎么解决
蓝屏故障原因:硬件故障(内存损坏、驱动程序问题)或软件冲突(设备驱动程序损坏、软件不兼容)。解决方案包括:重启计算机、安全模式启动、检查内存、更新驱动程序、扫描恶意软件、还原系统、重新安装 windo...
-
春运抢票到哪里抢
春运抢票渠道包括:铁路:12306官网、手机客户端、电话订票、自助售票机、代售点民航:航空公司官网、购票平台、客服热线、机场柜台公路:公路客运站、网上购票平台、微信公众号春运抢票到哪里抢? 春运抢票是...