理解自我反思的简单代码(代理设计模式)
来源:dev.to
时间:2024-12-09 14:09:40 262浏览 收藏
有志者,事竟成!如果你在学习文章,那么本文《理解自我反思的简单代码(代理设计模式)》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
反思/自我反思有点被低估了。如果您的应用程序依赖于提示,我强烈建议您探索这个概念。实施起来并不难,反思技术可以帮助迭代地完善 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. \n story: {story} \n 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)
以上就是《理解自我反思的简单代码(代理设计模式)》的详细内容,更多关于的资料请关注golang学习网公众号!
声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
216 收藏
-
403 收藏
-
282 收藏
-
213 收藏
-
104 收藏
-
167 收藏
-
445 收藏
-
268 收藏
-
112 收藏
-
460 收藏
-
269 收藏
-
259 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习