理解自我反思的简单代码(代理设计模式)
来源:dev.to
2024-12-09 14:09:40
0浏览
收藏
有志者,事竟成!如果你在学习文章,那么本文《理解自我反思的简单代码(代理设计模式)》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

反思/自我反思有点被低估了。如果您的应用程序依赖于提示,我强烈建议您探索这个概念。实施起来并不难,反思技术可以帮助迭代地完善 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删除
为什么使用 `np.unique()` 函数得到的唯一值是自动排序的?
- 上一篇
- 为什么使用 `np.unique()` 函数得到的唯一值是自动排序的?
- 下一篇
- 汇泽平板h10g怎么刷机?
查看更多
最新文章
-
- 文章 · python教程 | 2小时前 |
- Python 3.14 自由线程程序怎样显式保护共享状态
- 337浏览 收藏
-
- 文章 · python教程 | 4小时前 | 标准库 · Python教程 · Python Traversable importlib.resources 包内资源
- Python importlib.resources Traversable 怎么读取包内目录
- 359浏览 收藏
-
- 文章 · python教程 | 8小时前 | 时区 · python · Python zoneinfo reset_tzpath TZPATH 自定义时区库 ZoneInfo缓存
- Python zoneinfo.reset_tzpath 怎么切换自定义时区库
- 176浏览 收藏
-
- 文章 · python教程 | 10小时前 | Python教程 · Python 并行执行 全局状态 InterpreterPoolExecutor
- Python InterpreterPoolExecutor 怎么隔离不同任务的全局状态
- 130浏览 收藏
-
- 文章 · python教程 | 12小时前 |
- Python zip strict=True 在第几次迭代发现长度不一致
- 437浏览 收藏
-
- 文章 · python教程 | 16小时前 | python · Python 类型提示 typing get_protocol_members Protocol
- Python get_protocol_members 怎么读取 Protocol 成员集合
- 487浏览 收藏
-
- 文章 · python教程 | 1天前 | 文件处理 · python · Python TempFile 临时文件 SpooledTemporaryFile rollover
- Python SpooledTemporaryFile 怎么手动触发写入磁盘
- 307浏览 收藏
-
- 文章 · python教程 | 1天前 |
- Python Path.info 缓存的文件类型信息什么时候会过期
- 438浏览 收藏
-
- 文章 · python教程 | 1天前 |
- Python asyncio.Queue shutdown 后等待者会收到什么
- 124浏览 收藏
-
- 文章 · python教程 | 1天前 |
- Python asyncio.Barrier 等待任务被取消后会怎样
- 166浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 360次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 416次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 428次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 381次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 207次使用
查看更多
相关文章
-
- Python sqlite3 Connection serialize 怎么导出数据库快照:备份窗口、内存占用与恢复校验
- 2026-08-26 501浏览
-
- Python监控网页状态:requests异常处理实战
- 2026-05-29 501浏览
-
- TensorFlow模型部署为API的TF Serving方法
- 2026-05-26 501浏览
-
- Python字符串编码转换:encode与decode详解
- 2026-05-16 501浏览
-
- TensorFlow裁剪无用算子方法详解
- 2026-05-15 501浏览

