梦日志

发布: (2025年12月17日 GMT+8 10:33)
5 min read
原文: Dev.to

I’m happy to help translate the article, but I need the text you’d like translated. Could you please paste the content you want converted to Simplified Chinese? Once I have it, I’ll keep the source line unchanged and translate the rest while preserving the formatting.

Source:

DreamLog:带有醒‑睡循环的逻辑编程

一种通过梦境自我提升的推理引擎,灵感来源于大脑在睡眠期间巩固知识的方式。

什么是 DreamLog?

DreamLog 是一种革命性的逻辑编程语言,实现了 醒‑睡循环 用于持续的自我改进。就像人类大脑在 REM 睡眠期间一样,DreamLog 在以下两种状态之间交替:

  • 🌞 醒着阶段 – 利用已有知识高效回答查询
  • 🌙 睡眠阶段 – 通过“做梦”探索新的抽象、压缩和概括

受 DreamCoder 与神经科学的启发,DreamLog 通过压缩发现一般原则——遵循 更简洁的解释覆盖更多案例更可能更真实 的洞见。

关键创新:用于优化的梦境

from dreamlog.pythonic import dreamlog
from dreamlog.kb_dreamer import KnowledgeBaseDreamer

# 醒着阶段:使用知识
kb = dreamlog(llm_provider="openai")
kb.fact("parent", "john", "mary")
kb.fact("parent", "mary", "alice")

# 睡眠阶段:梦境优化
dreamer = KnowledgeBaseDreamer(kb.provider)
session = dreamer.dream(
    kb,
    dream_cycles=3,           # 多个 REM 循环
    exploration_samples=10,  # 探索不同的优化方案
    verify=True              # 确保行为保持不变
)

print(f"Compression achieved: {session.compression_ratio:.1%}")
print(f"Generalization score: {session.generalization_score:.2f}")

特性

🧠 自我改进的知识库

  • 自动发现抽象和模式
  • 将冗余规则压缩为一般原则
  • 验证优化后行为仍然保持一致

🤖 LLM 驱动的学习

  • 在需要时生成缺失的知识
  • 基于已有事实进行上下文感知生成
  • 支持多种提供商(OpenAI、Anthropic、Ollama)

🎯 现代 Python 集成

kb = dreamlog()
kb.fact("likes", "alice", "bob") \
  .fact("likes", "bob", "alice") \
  .rule("friends", ["X", "Y"]) \
  .when("likes", ["X", "Y"]) \
  .and_("likes", ["Y", "X"])

for result in kb.query("friends", "X", "Y"):
    print(f"{result.bindings['X']} and {result.bindings['Y']} are friends")

🔄 醒‑睡架构

  • 利用 – 醒着时快速回答查询
  • 探索 – 睡眠时进行创造性的重组
  • 验证 – 确保改进不会破坏已有行为

安装

pip install dreamlog

快速入门

from dreamlog.pythonic import dreamlog

# 创建知识库
kb = dreamlog()

# 使用 S‑表达式添加事实
kb.parse("""
(parent john mary)
(parent mary alice)
(parent bob charlie)
""")

# 添加规则
kb.parse("""
(grandparent X Z) :- (parent X Y), (parent Y Z)
""")

# 查询
for result in kb.query("grandparent", "X", "alice"):
    print(f"{result.bindings['X']} is Alice's grandparent")

# 为未定义的谓词启用 LLM
kb_with_llm = dreamlog(llm_provider="openai")
# 现在对未定义谓词的查询会自动生成知识

哲学

DreamLog 体现了 智能源于探索与利用的相互作用 的原则:

  • 巩固 – 加强重要模式
  • 抽象 – 寻找一般原则
  • 压缩 – 用更少实现更多
  • 创造力 – 探索新颖的重组

这不仅仅是把 LLM 附加在逻辑编程上——它是一种全新的范式,系统的知识表示 通过使用而演化

文档

请参阅 完整文档 了解:

贡献

我们欢迎任何形式的贡献!请参阅 [CONTRIBUTING.m

Contributing 获取指南。

许可证

MIT 许可证 – 详情请参见 LICENSE

引用

如果您在研究中使用 DreamLog,请引用:

@software{dreamlog2025,
  title = {DreamLog: Logic Programming with Wake-Sleep Cycles},
  author = {queelius},
  year = {2025},
  url = {https://github.com/queelius/dreamlog}
}

由相信推理系统应该睡觉、偶尔做梦的梦想家们构建

Back to Blog

相关文章

阅读更多 »