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

1.tree of thought (使用LangChain解决4x4数独问题)

本教程将介绍如何使用LangChain库和chatglm API来解决一个4x4的数独问题。我们将通过以下步骤实现这一目标:

  1. 初始化chatglm 的聊天模型。
  2. 定义数独问题和解决方案。
  3. 创建一个自定义的检查器来验证每一步的思考。
  4. 使用ToTChain来运行整个思考过程。

1. 初始化chatglm4 的聊天模型

首先,我们需要导入langchain_openai库中的ChatOpenAI类,并初始化一个聊天模型实例。

from langchain_openai import ChatOpenAIllm = ChatOpenAI(temperature=1,model="GLM-4-Plus",openai_api_key="your api key",openai_api_base="https://open.bigmodel.cn/api/paas/v4/",max_tokens=512,
)

2. 定义数独问题和解决方案

接下来,我们定义一个4x4的数独问题和其解决方案,并生成问题描述。

sudoku_puzzle = "3,*,*,2|1,*,3,*|*,1,*,3|4,*,*,1"
sudoku_solution = "3,4,1,2|1,2,3,4|2,1,4,3|4,3,2,1"
problem_description = f"""
{sudoku_puzzle}- This is a 4x4 Sudoku puzzle.
- The * represents a cell to be filled.
- The | character separates rows.
- At each step, replace one or more * with digits 1-4.
- There must be no duplicate digits in any row, column or 2x2 subgrid.
- Keep the known digits from previous valid thoughts in place.
- Each thought can be a partial or the final solution.
""".strip()
print(problem_description)

输出结果如下:

3,*,*,2|1,*,3,*|*,1,*,3|4,*,*,1- This is a 4x4 Sudoku puzzle.
- The * represents a cell to be filled.
- The | character separates rows.
- At each step, replace one or more * with digits 1-4.
- There must be no duplicate digits in any row, column or 2x2 subgrid.
- Keep the known digits from previous valid thoughts in place.
- Each thought can be a partial or the final solution.

3. 创建自定义检查器

我们需要创建一个自定义的检查器MyChecker,用于验证每一步的思考是否有效。

import re
from typing import Tuplefrom langchain_experimental.tot.checker import ToTChecker
from langchain_experimental.tot.thought import ThoughtValidityclass MyChecker(ToTChecker):def evaluate(self, problem_description: str, thoughts: Tuple[str, ...] = ()) -> ThoughtValidity:last_thought = thoughts[-1]clean_solution = last_thought.replace(" ", "").replace('"', "")regex_solution = clean_solution.replace("*", ".").replace("|", "\\|")if sudoku_solution in clean_solution:return ThoughtValidity.VALID_FINALelif re.search(regex_solution, sudoku_solution):return ThoughtValidity.VALID_INTERMEDIATEelse:return ThoughtValidity.INVALID

4. 测试检查器

我们可以通过一些断言来测试检查器的功能。

checker = MyChecker()
assert (checker.evaluate("", ("3,*,*,2|1,*,3,*|*,1,*,3|4,*,*,1",))== ThoughtValidity.VALID_INTERMEDIATE
)
assert (checker.evaluate("", ("3,4,1,2|1,2,3,4|2,1,4,3|4,3,2,1",))== ThoughtValidity.VALID_FINAL
)
assert (checker.evaluate("", ("3,4,1,2|1,2,3,4|2,1,4,3|4,3,*,1",))== ThoughtValidity.VALID_INTERMEDIATE
)
assert (checker.evaluate("", ("3,4,1,2|1,2,3,4|2,1,4,3|4,*,3,1",))== ThoughtValidity.INVALID
)

5. 运行ToTChain

最后,我们使用ToTChain来运行整个思考过程,并尝试解决数独问题。

from langchain_experimental.tot.base import ToTChaintot_chain = ToTChain(llm=llm, checker=MyChecker(), k=30, c=5, verbose=True, verbose_llm=False
)
tot_chain.run(problem_description=problem_description)

输出结果如下:

C:\Users\32564\AppData\Local\Temp\ipykernel_5080\1223294212.py:6: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.tot_chain.run(problem_description=problem_description)
d:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([1m> Entering new ToTChain chain...[0m
Starting the ToT solve procedure.
[33;1m[1;3mThought: 3,*,*,2|1,*,3,*|*,1,*,3|4,*,*,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([31;1m[1;3m    Thought: 3,1,*,2|1,*,3,*|*,1,*,3|4,*,*,1
[0m[31;1m[1;3m    Thought: 3,*,4,2|1,*,3,*|*,1,*,3|4,*,*,1
[0m[33;1m[1;3m    Thought: 3,*,*,2|1,2,3,*|*,1,*,3|4,*,*,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m        Thought: 3,*,*,2|1,2,3,4|*,1,*,3|4,*,*,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m            Thought: 3,*,*,2|1,2,3,4|2,1,*,3|4,*,*,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                Thought: 3,*,*,2|1,2,3,4|2,1,4,3|4,*,*,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                    Thought: 3,*,*,2|1,2,3,4|2,1,4,3|4,3,*,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                        Thought: 
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                            Thought: 
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                                Thought: 3,*,*,2|1,2,3,4|2,1,4,3|4,3,2,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                                    Thought: 3,*,*,2|1,2,3,4|2,1,4,3|4,3,2,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([31;1m[1;3m                                        Thought: 3,1,*,2|1,2,3,4|2,1,4,3|4,3,2,1
[0m[33;1m[1;3m                                        Thought: 3,4,*,2|1,2,3,4|2,1,4,3|4,3,2,1
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                                            Thought: 
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                                                Thought: 
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([33;1m[1;3m                                                    Thought: 
[0md:\soft\anaconda\envs\langchain\Lib\site-packages\langchain\chains\llm.py:341: UserWarning: The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.warnings.warn([32;1m[1;3m                                                        Thought: 3,4,1,2|1,2,3,4|2,1,4,3|4,3,2,1
[0m
[1m> Finished chain.[0m'3,4,1,2|1,2,3,4|2,1,4,3|4,3,2,1'

通过以上步骤,我们成功地使用LangChain解决了一个4x4的数独问题。希望这个教程对你有所帮助!如果有任何问题,欢迎随时提问。

相关文章:

1.tree of thought (使用LangChain解决4x4数独问题)

本教程将介绍如何使用LangChain库和chatglm API来解决一个4x4的数独问题。我们将通过以下步骤实现这一目标: 初始化chatglm 的聊天模型。定义数独问题和解决方案。创建一个自定义的检查器来验证每一步的思考。使用ToTChain来运行整个思考过程。 1. 初始化chatglm4…...

网络基础(4)IP协议

经过之前的学习对传输协议的学习,对于传输协议从系统底层到应用层对于socket套接字的学习已经有了一套完整的理论。 对于网络的层状结构,现在已经学习到了应用层和传输层: 在之前的学习中,通信的双方都只考虑了双方的传输层的东西&#xff0…...

124. 二叉树中的最大路径和【 力扣(LeetCode) 】

文章目录 零、原题链接一、题目描述二、测试用例三、解题思路四、参考代码 零、原题链接 124. 二叉树中的最大路径和 一、题目描述 二叉树中的 路径 被定义为一条节点序列,序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 至多出现一次 。该路径…...

echarts:简单实现默认显示两柱子折线,点击按钮后显示新的柱子

问&#xff1a; 用echarts实现&#xff1a;默认显示两柱子折线&#xff0c;点击“税率”按钮&#xff0c;显示税率柱子&#xff0c;之前的两柱子折线消失 回答&#xff1a; <!DOCTYPE html> <html lang"zh"> <head><meta charset"UTF-8…...

视频里的音频怎么提取出来成单独文件?音频提取照着这些方法做

在数字时代&#xff0c;视频与音频的分离与重组已成为日常需求之一。无论是出于制作背景音乐、保存讲座内容&#xff0c;还是编辑播客素材&#xff0c;提取视频中的音频并将其保存为单独文件都显得尤为重要。视频里的音频怎么提取出来成单独文件&#xff1f;本文将详细介绍几种…...

Excel——宏教程(精简版)

一、宏的简介 1、什么是宏&#xff1f; Excel宏是一种自动化工具&#xff0c;它允许用户录制一系列操作并将其转换为VBA(Visual Basic for Applications)代码。这样&#xff0c;用户可以在需要时执行这些操作&#xff0c;以自动化Excel任务。 2、宏的优点 我们可以利用宏来…...

C++中的std::tuple和std::pair

在C标准库中&#xff0c;std::tuple和std::pair是两种极具实用性的数据结构&#xff0c;它们都具备存储多个元素的功能&#xff0c;但各自有其独特的适用环境和特性。本文旨在深入探讨这两者之间的区别&#xff0c;并阐述在不同应用场景下应如何合理选择使用。 一、基本概念 s…...

引力搜索算法

引力搜索算法过程&#xff0c;包括了初始化、适应度评估、质量计算、加速度计算、更新速度和位置的一些步骤。 import numpy as np import random as rd from math import exp, sqrt import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotli…...

【时间之外】IT人求职和创业应知【35】-RTE三进宫

目录 新闻一&#xff1a;京东工业发布11.11战报&#xff0c;多项倍增数据体现工业经济信心提升 新闻二&#xff1a;阿里云100万核算力支撑天猫双11&#xff0c;弹性计算规模刷新纪录 新闻三&#xff1a;声网CEO赵斌&#xff1a;RTE将成为生成式AI时代AI Infra的关键部分 认知…...

Linux的目录结构

/ ├── bin # Binary - 存放用户可以直接使用的基本二进制可执行文件 ├── sbin # System Binaries - 存放系统管理员专用的二进制可执行文件 ├── usr # Unix System Resources - 存放用户使用的软件和库文件 │ ├── bin # Binary - 用户级应用程序…...

python: generator IDAL and DAL using sql server 2019

其它数据库也是一样的思维方式 create IDAL # encoding: utf-8 # 版权所有 2024 ©涂聚文有限公司 # 许可信息查看&#xff1a;言語成了邀功盡責的功臣&#xff0c;還需要行爲每日來值班嗎 # 描述&#xff1a; # Author : geovindu,Geovin Du 涂聚文. # IDE : P…...

命令执行简单

前言&#xff1a;小迪安全2022第一节反弹shell&#xff0c;小迪用的是两台都是云服务器&#xff0c;没有服务器可以在自己的主机上搭建也是可以的&#xff0c;主机上搭两个网站 思路&#xff1a;生成一个木马文件&#xff0c;下载到本机&#xff0c;然后利用本机上传到目标主机…...

【一句话经验】亚马逊云EC2 ubuntu24.04.1开启ROOT登录Permission denied (publickey)

按照常规的方法SSH登录会一直报错&#xff1a; Permission denied (publickey) 因为亚马逊云的默认配置不是在/etc/ssh/sshd_config&#xff0c;而是在引入的文件里了&#xff0c;所以在instance控制台输入这行命令来解除登录限制&#xff1a; sudo sed -i s/^PasswordAuthe…...

百度智能云千帆大模型平台引领企业创新增长

本文整理自百度世界大会 2024——「智能跃迁 产业加速」论坛的同名演讲。 更多大会演讲内容&#xff0c;请访问&#xff1a; https://baiduworld.baidu.com 首先&#xff0c;跟大家分享一张图&#xff0c;这个是我们目前大模型应用落地的场景分布。可以看到&#xff0c;大模型…...

【Linux】深入理解GCC/G++编译流程及库文件管理

目录 1.背景知识 2.gcc/g如何完成编译 (1) 预处理&#xff08;进行宏替换&#xff09; (2) 编译&#xff08;生成汇编&#xff09; (3) 汇编&#xff08;生成机器可识别代码&#xff09; (4) 链接&#xff08;生成可执行文件或库文件&#xff09; (5) 总结 (6) 函数库 …...

【Unity基础】对比Unity中两种粒子系统

在Unity中&#xff0c;Particle System和Visual Effect Graph (VFX) 都是用于创建粒子效果的工具&#xff0c;但它们的设计目标、使用场景和功能特点有所不同。以下是详细对比&#xff1a; 1. Particle System 特点 传统粒子系统&#xff0c;Unity自带的模块化粒子特效工具。…...

琐碎笔记——pytest实现前置、后置、参数化、跳过用例执行以及重试

pytest的fixture中文介绍可参考&#xff08;不过文档稍微有点老&#xff09;&#xff1a; https://www.osgeo.cn/pytest/fixture.html#what-fixtures-are pytest各个作用域的fixture scope “function” 可作用于每个用例 fixture使用的声明放在类定义前面&#xff0c;类中的…...

C# 深层副本与浅层副本 深拷贝与浅拷贝

C# 深层副本与浅层副本 数据复制是编程中的重要任务。 对象是 OOP 中的复合数据类型。 对象中的成员字段可以按值或按引用存储。 可以以两种方式执行复制。 浅表副本将所有值和引用复制到新实例中。 引用所指向的数据不会被复制&#xff1b; 仅指针被复制。 新的引用指向原始…...

CH06_Lambda表达式

第6章&#xff1a;Lambda表达式 本章目标 为什么要学习C#编程语言 了解C#相关常识 C#开发工具Visual Studio安装 掌握C#程序的开发步骤 掌握C#的注释 掌握C#的常用转义符 本章内容 lambda表达式演变史 C# 匿名函数的演变历史可以追溯到 C# 语言的不同版本&#xff0c;…...

大模型本地部署实践:Ollama+Open-WebUI(MacOS)

目录 什么是Ollama Ollama安装 对话界面可视化&#xff1f;Open-WebUI&#xff01; 安装Open-WebUI 什么是Ollama Ollama是一个为简化大语言模型本地部署与交互的开源框架。它提供了用户友好的接口&#xff0c;帮助开发者和模型爱好者在没有依赖外部API的基础上高效地运行、…...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

使用分级同态加密防御梯度泄漏

抽象 联邦学习 &#xff08;FL&#xff09; 支持跨分布式客户端进行协作模型训练&#xff0c;而无需共享原始数据&#xff0c;这使其成为在互联和自动驾驶汽车 &#xff08;CAV&#xff09; 等领域保护隐私的机器学习的一种很有前途的方法。然而&#xff0c;最近的研究表明&…...

江苏艾立泰跨国资源接力:废料变黄金的绿色供应链革命

在华东塑料包装行业面临限塑令深度调整的背景下&#xff0c;江苏艾立泰以一场跨国资源接力的创新实践&#xff0c;重新定义了绿色供应链的边界。 跨国回收网络&#xff1a;废料变黄金的全球棋局 艾立泰在欧洲、东南亚建立再生塑料回收点&#xff0c;将海外废弃包装箱通过标准…...

Frozen-Flask :将 Flask 应用“冻结”为静态文件

Frozen-Flask 是一个用于将 Flask 应用“冻结”为静态文件的 Python 扩展。它的核心用途是&#xff1a;将一个 Flask Web 应用生成成纯静态 HTML 文件&#xff0c;从而可以部署到静态网站托管服务上&#xff0c;如 GitHub Pages、Netlify 或任何支持静态文件的网站服务器。 &am…...

【git】把本地更改提交远程新分支feature_g

创建并切换新分支 git checkout -b feature_g 添加并提交更改 git add . git commit -m “实现图片上传功能” 推送到远程 git push -u origin feature_g...

【python异步多线程】异步多线程爬虫代码示例

claude生成的python多线程、异步代码示例&#xff0c;模拟20个网页的爬取&#xff0c;每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程&#xff1a;允许程序同时执行多个任务&#xff0c;提高IO密集型任务&#xff08;如网络请求&#xff09;的效率…...

RNN避坑指南:从数学推导到LSTM/GRU工业级部署实战流程

本文较长&#xff0c;建议点赞收藏&#xff0c;以免遗失。更多AI大模型应用开发学习视频及资料&#xff0c;尽在聚客AI学院。 本文全面剖析RNN核心原理&#xff0c;深入讲解梯度消失/爆炸问题&#xff0c;并通过LSTM/GRU结构实现解决方案&#xff0c;提供时间序列预测和文本生成…...

html-<abbr> 缩写或首字母缩略词

定义与作用 <abbr> 标签用于表示缩写或首字母缩略词&#xff0c;它可以帮助用户更好地理解缩写的含义&#xff0c;尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时&#xff0c;会显示一个提示框。 示例&#x…...

零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)

本期内容并不是很难&#xff0c;相信大家会学的很愉快&#xff0c;当然对于有后端基础的朋友来说&#xff0c;本期内容更加容易了解&#xff0c;当然没有基础的也别担心&#xff0c;本期内容会详细解释有关内容 本期用到的软件&#xff1a;yakit&#xff08;因为经过之前好多期…...

音视频——I2S 协议详解

I2S 协议详解 I2S (Inter-IC Sound) 协议是一种串行总线协议&#xff0c;专门用于在数字音频设备之间传输数字音频数据。它由飞利浦&#xff08;Philips&#xff09;公司开发&#xff0c;以其简单、高效和广泛的兼容性而闻名。 1. 信号线 I2S 协议通常使用三根或四根信号线&a…...