当前位置: 首页 > news >正文

LangChain 11实现思维树Implementing the Tree of Thoughts in LangChain’s Chain

思维之树( Tree of Thoughts ToT)是一个算法,它结合了普林斯顿大学和谷歌DeepMind在本文中提出的大型语言模型(LLMs)和启发式搜索。看起来这个算法正在被实现到谷歌正在开发的多模式生成AI Gemini中。

现在,让我们简要地谈谈 ToT(思维树)的思维过程。

在通常的 CoT(思维链)方法中,LLM(语言模型)倾向于在解决问题时线性地进行思考,如果在这个过程中出现错误,它们倾向于沿着错误的标准继续进行。

相比之下,在 ToT(思维树)方法中,LLM 在每个思维阶段都对自己进行评估,并及早停止低效的方法,转而采用替代方法。

在这里插入图片描述

代码实现

在LangChain的Chain中实施此项工作时,我利用了以下视频发布者提供的提示。简单地解释一下这个过程,首先生成广泛的想法,然后评估每个想法,深入探讨,并选择最有成功前景的想法。

PromptTemplate用于定义Tree of Thoughts提示的树形结构,每个步骤都实现了链。

from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAItemplate ="""
Step1 :I have a problem related to {input}. Could you brainstorm three distinct solutions? Please consider a variety of factors such as {perfect_factors}
A:
"""prompt = PromptTemplate(input_variables=["input","perfect_factors"],template = template                      
)chain1 = LLMChain(llm=ChatOpenAI(temperature=0, model="gpt-4"),prompt=prompt,output_key="solutions"
)template ="""
Step 2:For each of the three proposed solutions, evaluate their potential. Consider their pros and cons, initial effort needed, implementation difficulty, potential challenges, and the expected outcomes. Assign a probability of success and a confidence level to each option based on these factors{solutions}A:"""prompt = PromptTemplate(input_variables=["solutions"],template = template                      
)chain2 = LLMChain(llm=ChatOpenAI(temperature=0, model="gpt-4"),prompt=prompt,output_key="review"
)template ="""
Step 3:For each solution, deepen the thought process. Generate potential scenarios, strategies for implementation, any necessary partnerships or resources, and how potential obstacles might be overcome. Also, consider any potential unexpected outcomes and how they might be handled.{review}A:"""prompt = PromptTemplate(input_variables=["review"],template = template                      
)chain3 = LLMChain(llm=ChatOpenAI(temperature=0, model="gpt-4"),prompt=prompt,output_key="deepen_thought_process"
)template ="""
Step 4:Based on the evaluations and scenarios, rank the solutions in order of promise. Provide a justification for each ranking and offer any final thoughts or considerations for each solution
{deepen_thought_process}A:"""prompt = PromptTemplate(input_variables=["deepen_thought_process"],template = template                      
)chain4 = LLMChain(llm=ChatOpenAI(temperature=0, model="gpt-4"),prompt=prompt,output_key="ranked_solutions"
)

我们使用“SequentialChain”连接这四个链条。一个链条的输出成为下一个链条的输入。

from langchain.chains import SequentialChainoverall_chain = SequentialChain(chains=[chain1, chain2, chain3, chain4],input_variables=["input", "perfect_factors"],output_variables=["ranked_solutions"],verbose=True
)print(overall_chain({"input":"human colonization of Mars", "perfect_factors":"The distance between Earth and Mars is very large, making regular resupply difficult"}))

输出

{"input": "human colonization of Mars","perfect_factors": "The distance between Earth and Mars is very large, making regular resupply difficult","ranked_solutions": {"Ranking_1": {"Justification": "Using In-Situ Resource Utilization is the most promising solution due to its potential to provide the necessary resources for a Mars colony and reduce the need for resupply missions from Earth. The medium initial effort, implementation difficulty, and potential challenges are outweighed by the high probability of success and 70% confidence level.","In_Situ_Resource_Utilization_ISRU": {"Pros": "This solution could provide the necessary resources for a Mars colony and reduce the need for resupply missions from Earth.","Cons": "ISRU is technically challenging and would require significant investment in research and development.","Initial_Effort": "Medium. This would require the development of new technology and the establishment of infrastructure on Mars.","Implementation_Difficulty": "Medium. ISRU is a complex task that requires advanced technology.","Potential_Challenges": "Technical difficulties, high costs.","Expected_Outcomes": "If successful, ISRU could provide a steady supply of resources for a Mars colony.","Probability_of_Success": "High. ISRU is already being tested by NASA and other space agencies.","Confidence_Level": "70%"}},"Ranking_2": {"Justification": "Building a self-sustaining colony is a promising solution due to its potential to make the Mars colony self-sufficient. However, the high initial effort, implementation difficulty, and potential challenges make it less promising than the first solution. The medium probability of success and 60% confidence level also contribute to its ranking.","Building_a_Self_Sustaining_Colony": {"Pros": "This solution could make the Mars colony self-sufficient, reducing the need for resupply missions from Earth.","Cons": "Building a self-sustaining colony is a complex task that requires advanced technology and a lot of resources.","Initial_Effort": "High. This would require the development of new technology and the establishment of infrastructure on Mars.","Implementation_Difficulty": "High. Building a self-sustaining colony is a complex task that requires advanced technology.","Potential_Challenges": "Technical difficulties, high costs.","Expected_Outcomes": "If successful, a self-sustaining colony could reduce the need for resupply missions from Earth.","Probability_of_Success": "Medium. While there are significant challenges, there is also a lot of interest in building a self-sustaining colony on Mars.","Confidence_Level": "60%"}},"Ranking_3": {"Justification": "While asteroid mining has the potential to provide a steady supply of resources for a Mars colony, the high initial effort, implementation difficulty, and potential challenges make it a less promising solution compared to others. The medium probability of success and 50% confidence level also contribute to its lower ranking.","Terraforming_Mars": {"Pros": "This solution could make Mars more habitable for humans, reducing the need for life support systems and making the colony more self-sufficient.","Cons": "Terraforming is a long-term process that could take centuries or even millennia. It would also require a massive amount of resources and energy.","Initial_Effort": "Extremely High. Terraforming would require a massive amount of resources and energy.","Implementation_Difficulty": "Extremely High. Terraforming is a long-term process that could take centuries or even millennia.","Potential_Challenges": "Technical difficulties, high costs, time scale.","Expected_Outcomes": "If successful, terraforming could make Mars more habitable for humans.","Probability_of_Success": "Low. Terraforming is a theoretical concept and has never been attempted before.","Confidence_Level": "20%"}}}
}

最后,我认为不仅仅使用提示中的思维树,而且将其引入到代理人的规划过程中,可以提高工具选择和规划的准确性。

参考

https://medium.com/@astropomeai/implementing-the-tree-of-thoughts-in-langchains-chain-f2ebc5864fac

相关文章:

LangChain 11实现思维树Implementing the Tree of Thoughts in LangChain’s Chain

思维之树( Tree of Thoughts ToT)是一个算法,它结合了普林斯顿大学和谷歌DeepMind在本文中提出的大型语言模型(LLMs)和启发式搜索。看起来这个算法正在被实现到谷歌正在开发的多模式生成AI Gemini中。 现在&#xff0…...

Drools 7 Modify 和对象直接赋值差异

modify代表修改fact,会再次触发符合条件的rule对象直接修改只是java 操作,不会会再次触发符合条件的rule 以下为测试代码-drl部分 package org.drools.learnimport org.drools.learn.ModifyTest.Message;global java.util.List listrule "Stateles…...

vivado产生报告阅读分析21

其他命令选项 • -of_objects <suggestion objects> &#xff1a; 启用特定建议的报告。在此模式下运行时 &#xff0c; report_qor_suggestions 不会生成新建议。此命令可快速执行 &#xff0c; 读取 RQS 文件后 &#xff0c; 此命令可用于查看其中包 含的建议。其…...

9.Docker的虚悬镜像-Dangling Image

1.虚悬镜像的概念 虚悬镜像 (Dangling Image) 指的是仓库名 (镜像名) 和标签 TAG 都是 的镜像。 2.构建本地虚悬镜像 这里我以unbuntu为例来说明。 2.1 编写Dockerfile文件 FROM ubuntu:22.042.2 根据Dockerfile文件构建虚悬镜像 docker build .上面这段命令&#xff0c…...

02- OpenCV:加载、修改、保存图像

目录 1、加载图像&#xff08;cv::imread&#xff09; 2、显示图像 (cv::namedWindos 与cv::imshow) 3、修改图像 (cv::cvtColor) 4、保存图像(cv::imwrite) 5、代码演示 1、加载图像&#xff08;cv::imread&#xff09; cv::imread 是 OpenCV 库中用于读取图像文件的函数…...

4面试题--数据库(mysql)

执⾏⼀条 select / update 语句&#xff0c;在 MySQL 中发⽣了什么&#xff1f; Server 层负责建⽴连接、分析和执⾏ SQL。MySQL ⼤多数的核⼼功能模块都在这实现&#xff0c;主要包括 连接器&#xff0c;查询缓存&#xff08;8.0版本去除&#xff0c;因为每次更新将会清空该…...

【LeeCode】283.移动零

给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。 解【做的有点呆&#xff0c;额外设置了计数器变量统计0的个数再从后往前赋0】&#xff1a…...

OSG粒子系统与阴影-自定义粒子系统示例<2>(5)

自定义粒子系统示例(二) 目前自定义粒子的方法有很多&#xff0c;在OSG 中使用的是 Billboard 技术与色彩融合技术。色彩融合是一种高级的渲染技术&#xff0c;如果读者有兴趣&#xff0c;可参看 OSG 粒子系统实现的源代码。这里采用简单的布告牌技术(osg::Billboard)与动画来实…...

微软 Edge 浏览器目前无法支持 avif 格式

avif 格式在微软 Edge 浏览器中还是没有办法支持。 如果你希望能够查看 avif 格式&#xff0c;那么只能通过浏览器打开&#xff0c;然后浏览器将会把这个文件格式下载到本地。 avif 格式已经在其他的浏览器上得到了广泛的支持&#xff0c;目前不支持的可能就只有 Edge 浏览器。…...

用python实现文字转语音的5个较好用的模块

文章目录 一. 用 gtts 模块二. 用pyttsx3模块基本使用直接朗读更改语音、速率和音量 三. baidu-aip四. pywin32五. speech 一. 用 gtts 模块 参考文档&#xff1a;https://gtts.readthedocs.io/en/latest/ 使用前需要先安装&#xff1a;pip3 install gtts &#xff0c;样例如…...

Windows Server 2012R2 修复CVE-2016-2183(SSL/TLS)漏洞的办法

一、漏洞说明 Windows server 2012R2远程桌面服务SSL加密默认是开启的,且有默认的CA证书。由于SSL/ TLS自身存在漏洞缺陷,当开启远程桌面服务,使用漏洞扫描工具扫描,发现存在SSL/TSL漏洞。远程主机支持的SSL加密算法提供了中等强度的加密算法,目前,使用密钥长度大于等于5…...

python统计字符串中大小写字符个数的性能实测与分析

给定一个字符串&#xff0c;统计字符串中大写字符个数&#xff0c;有如下三种方法&#xff1a; # method1 s1 len(re.findall(r[A-Z],content)) # method2 s2 sum(1 for c in content if c.isupper()) # method3 s3 0 for c in content:if c.isupper()True:s31经过多次实测…...

时间序列预测实战(十九)魔改Informer模型进行滚动长期预测(科研版本)

论文地址->Informer论文地址PDF点击即可阅读 代码地址-> 论文官方代码地址点击即可跳转下载GIthub链接 个人魔改版本地址-> 文章末尾 一、本文介绍 在之前的文章中我们已经讲过Informer模型了&#xff0c;但是呢官方的预测功能开发的很简陋只能设定固定长度去预测未…...

[PyTorch][chapter 64][强化学习-DQN]

前言&#xff1a; DQN 就是结合了深度学习和强化学习的一种算法&#xff0c;最初是 DeepMind 在 NIPS 2013年提出&#xff0c;它的核心利润包括马尔科夫决策链以及贝尔曼公式。 Q-learning的核心在于Q表格&#xff0c;通过建立Q表格来为行动提供指引&#xff0c;但这适用于状态…...

用好语言模型:temperature、top-p等核心参数解析

编者按&#xff1a;我们如何才能更好地控制大模型的输出? 本文将介绍几个关键参数&#xff0c;帮助读者更好地理解和运用 temperature、top-p、top-k、frequency penalty 和 presence penalty 等常见参数&#xff0c;以优化语言模型的生成效果。 文章详细解释了这些参数的作用…...

python之pycryptodome模块,加密算法库

一、简介 PyCryptodome是PyCrypto库的一个分支&#xff0c;它是Python中最受欢迎的密码学库之一。PyCryptodome提供了许多密码学算法和协议的实现&#xff0c;包括对称加密、非对称加密、消息摘要、密码哈希、数字签名等。它还提供了一些其他功能&#xff0c;如密码学安全随机…...

IDEA如何将本地项目推送到GitHub上?

大家好&#xff0c;我是G探险者。 IntelliJ IDEA 是一个强大的集成开发环境&#xff08;IDE&#xff09;&#xff0c;它支持多种编程语言和工具。它也内置了对Git和GitHub的支持&#xff0c;让开发者可以轻松地将本地项目推送到GitHub上。以下是一个操作手册&#xff0c;描述了…...

Leetcode—45.跳跃游戏II【中等】

2023每日刷题&#xff08;四十&#xff09; Leetcode—45.跳跃游戏II 贪心法思想 实现代码 #define MAX(a, b) (a > b ? (a) : (b))int jump(int* nums, int numsSize) {int start 0;int end 1;int ans 0;int maxStride 0;while(end < numsSize) {maxStride 0;fo…...

基于Vue+SpringBoot的木马文件检测系统

项目编号&#xff1a; S 041 &#xff0c;文末获取源码。 \color{red}{项目编号&#xff1a;S041&#xff0c;文末获取源码。} 项目编号&#xff1a;S041&#xff0c;文末获取源码。 目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 木马分类模块2.3 木…...

springboot内置Tomcat流程

1、org.springframework.boot.SpringApplication#initialize setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));加载了org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext 2、spring refres…...

UE5 学习系列(二)用户操作界面及介绍

这篇博客是 UE5 学习系列博客的第二篇&#xff0c;在第一篇的基础上展开这篇内容。博客参考的 B 站视频资料和第一篇的链接如下&#xff1a; 【Note】&#xff1a;如果你已经完成安装等操作&#xff0c;可以只执行第一篇博客中 2. 新建一个空白游戏项目 章节操作&#xff0c;重…...

地震勘探——干扰波识别、井中地震时距曲线特点

目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波&#xff1a;可以用来解决所提出的地质任务的波&#xff1b;干扰波&#xff1a;所有妨碍辨认、追踪有效波的其他波。 地震勘探中&#xff0c;有效波和干扰波是相对的。例如&#xff0c;在反射波…...

Java如何权衡是使用无序的数组还是有序的数组

在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

django filter 统计数量 按属性去重

在Django中&#xff0c;如果你想要根据某个属性对查询集进行去重并统计数量&#xff0c;你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求&#xff1a; 方法1&#xff1a;使用annotate()和Count 假设你有一个模型Item&#xff0c;并且你想…...

2.Vue编写一个app

1.src中重要的组成 1.1main.ts // 引入createApp用于创建应用 import { createApp } from "vue"; // 引用App根组件 import App from ./App.vue;createApp(App).mount(#app)1.2 App.vue 其中要写三种标签 <template> <!--html--> </template>…...

React19源码系列之 事件插件系统

事件类别 事件类型 定义 文档 Event Event 接口表示在 EventTarget 上出现的事件。 Event - Web API | MDN UIEvent UIEvent 接口表示简单的用户界面事件。 UIEvent - Web API | MDN KeyboardEvent KeyboardEvent 对象描述了用户与键盘的交互。 KeyboardEvent - Web…...

VTK如何让部分单位不可见

最近遇到一个需求&#xff0c;需要让一个vtkDataSet中的部分单元不可见&#xff0c;查阅了一些资料大概有以下几种方式 1.通过颜色映射表来进行&#xff0c;是最正规的做法 vtkNew<vtkLookupTable> lut; //值为0不显示&#xff0c;主要是最后一个参数&#xff0c;透明度…...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

C++ Visual Studio 2017厂商给的源码没有.sln文件 易兆微芯片下载工具加开机动画下载。

1.先用Visual Studio 2017打开Yichip YC31xx loader.vcxproj&#xff0c;再用Visual Studio 2022打开。再保侟就有.sln文件了。 易兆微芯片下载工具加开机动画下载 ExtraDownloadFile1Info.\logo.bin|0|0|10D2000|0 MFC应用兼容CMD 在BOOL CYichipYC31xxloaderDlg::OnIni…...

佰力博科技与您探讨热释电测量的几种方法

热释电的测量主要涉及热释电系数的测定&#xff0c;这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中&#xff0c;积分电荷法最为常用&#xff0c;其原理是通过测量在电容器上积累的热释电电荷&#xff0c;从而确定热释电系数…...