LangChain-25 ReAct 让大模型自己思考和决策下一步 AutoGPT实现途径、AGI重要里程碑

news2025/4/19 8:42:42

请添加图片描述

背景介绍

大模型ReAct(Reasoning and Acting)是一种新兴的技术框架,旨在通过逻辑推理和行动序列的构建,使大型语言模型(LLM)能够达成特定的目标。这一框架的核心思想是赋予机器模型类似人类的推理和行动能力,从而在各种任务和环境中实现更高效、更智能的决策和操作。

核心组成

ReAct框架主要由三个关键概念组成:Thought(思考)、Act(行动)、和Obs(观察)。

  • Thought:由LLM模型生成,是LLM产生行为和依据的基础。它代表了模型在面对特定任务时的逻辑推理过程,是决策的前提。
  • Act:指LLM判断本次需要执行的具体行为。这通常涉及选择合适的工具或API,并生成所需的参数,以实现目标行动。
  • Obs:LLM框架对于外界输入的获取,类似于LLM的“五官”,将外界的反馈信息同步给LLM模型,协助模型进一步的做分析或者决策。

安装依赖


Prompt

# Get the prompt to use - you can modify this!
# Answer the following questions as best you can. You have access to the following tools:
#
# {tools}
#
# Use the following format:
#
# Question: the input question you must answer
# Thought: you should always think about what to do
# Action: the action to take, should be one of [{tool_names}]
# Action Input: the input to the action
# Observation: the result of the action
# ... (this Thought/Action/Action Input/Observation can repeat N times)
# Thought: I now know the final answer
# Final Answer: the final answer to the original input question
#
# Begin!
#
# Question: {input}
# Thought:{agent_scratchpad}

编写代码

from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import OpenAI


tools = [TavilySearchResults(max_results=1)]
# Get the prompt to use - you can modify this!
# Answer the following questions as best you can. You have access to the following tools:
#
# {tools}
#
# Use the following format:
#
# Question: the input question you must answer
# Thought: you should always think about what to do
# Action: the action to take, should be one of [{tool_names}]
# Action Input: the input to the action
# Observation: the result of the action
# ... (this Thought/Action/Action Input/Observation can repeat N times)
# Thought: I now know the final answer
# Final Answer: the final answer to the original input question
#
# Begin!
#
# Question: {input}
# Thought:{agent_scratchpad}
prompt = hub.pull("hwchase17/react")

# Choose the LLM to use
llm = OpenAI(
    model="gpt-3.5-turbo",
    temperature=0
)

# Construct the ReAct agent
agent = create_react_agent(llm, tools, prompt)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

message1 = agent_executor.invoke({"input": "what is LangChain?"})
print(f"message1: {message1}")

执行结果

我们可以看到,大模型自己进行思考,并进行下一步。(详细可看执行日志)

➜ python3 test26.py


> Entering new AgentExecutor chain...
 I should search for LangChain to see what it is
Action: tavily_search_results_json
Action Input: "LangChain"[{'url': 'https://towardsdatascience.com/getting-started-with-langchain-a-beginners-guide-to-building-llm-powered-applications-95fc8898732c', 'content': 'linkedin.com/in/804250ab\nMore from Leonie Monigatti and Towards Data Science\nLeonie Monigatti\nin\nTowards Data Science\nRetrieval-Augmented Generation (RAG): From Theory to LangChain Implementation\nFrom the theory of the original academic paper to its Python implementation with OpenAI, Weaviate, and LangChain\n--\n2\nMarco Peixeiro\nin\nTowards Data Science\nTimeGPT: The First Foundation Model for Time Series Forecasting\nExplore the first generative pre-trained forecasting model and apply it in a project with Python\n--\n22\nRahul Nayak\nin\nTowards Data Science\nHow to Convert Any Text Into a Graph of Concepts\nA method to convert any text corpus into a Knowledge Graph using Mistral 7B.\n--\n32\nLeonie Monigatti\nin\nTowards Data Science\nRecreating Andrej Karpathy’s Weekend Project\u200a—\u200aa Movie Search Engine\nBuilding a movie recommender system with OpenAI embeddings and a vector database\n--\n3\nRecommended from Medium\nKrishna Yogi\nBuilding a question-answering system using LLM on your private data\n--\n6\nRahul Nayak\nin\nTowards Data Science\nHow to Convert Any Text Into a Graph of Concepts\nA method to convert any text corpus into a Knowledge Graph using Mistral 7B.\n--\n32\nLists\nPredictive Modeling w/ Python\nPractical Guides to Machine Learning\nNatural Language Processing\nChatGPT prompts\nOnkar Mishra\nUsing langchain for Question Answering on own data\nStep-by-step guide to using langchain to chat with own data\n--\n10\nAmogh Agastya\nin\nBetter Programming\nHarnessing Retrieval Augmented Generation With Langchain\nImplementing RAG using Langchain\n--\n6\nAnindyadeep\nHow to integrate custom LLM using langchain. This is part 1 of my mini-series: Building end to end LLM powered applications without Open AI’s API\n--\n3\nAkriti Upadhyay\nin\nAccredian\nImplementing RAG with Langchain and Hugging Face\nUsing Open Source for Information Retrieval\n--\n6\nHelp\nStatus\nAbout\nCareers\nBlog\nPrivacy\nTerms\nText to speech\nTeams A Beginner’s Guide to Building LLM-Powered Applications\nA LangChain tutorial to build anything with large language models in Python\nLeonie Monigatti\nFollow\nTowards Data Science\n--\n27\nShare\n GitHub - hwchase17/langchain: ⚡ Building applications with LLMs through composability ⚡\n⚡ Building applications with LLMs through composability ⚡ Production Support: As you move your LangChains into…\ngithub.com\nWhat is LangChain?\nLangChain is a framework built to help you build LLM-powered applications more easily by providing you with the following:\nIt is an open-source project (GitHub repository) created by Harrison Chase.\n --\n--\n27\nWritten by Leonie Monigatti\nTowards Data Science\nDeveloper Advocate @'}] I should read the first search result to learn more about LangChain
Action: tavily_search_results_json
Action Input: "LangChain tutorial"[{'url': 'https://python.langchain.com/docs/get_started/quickstart', 'content': "Once we have a key we'll want to set it as an environment variable by running:\nIf you'd prefer not to set an environment variable you can pass the key in directly via the openai_api_key named parameter when initiating the OpenAI LLM class:\nLangSmith\u200b\nMany of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls.\n The fact that LLM and ChatModel accept the same inputs means that you can directly swap them for one another in most chains without breaking anything,\nthough it's of course important to think about how inputs are being coerced and how that may affect model performance.\n The base message interface is defined by BaseMessage, which has two required attributes:\nLangChain provides several objects to easily distinguish between different roles:\nIf none of those roles sound right, there is also a ChatMessage class where you can specify the role manually.\n This chain will take input variables, pass those to a prompt template to create a prompt, pass the prompt to a language model, and then pass the output through an (optional) output parser.\n Next steps\u200b\nWe've touched on how to build an application with LangChain, how to trace it with LangSmith, and how to serve it with LangServe.\n"}] I should read the LangChain tutorial to learn more about LangChain
Action: tavily_search_results_json
Action Input: "LangChain tutorial"[{'url': 'https://python.langchain.com/docs/additional_resources/tutorials', 'content': 'Learn how to use Langchain, a Python library for building AI applications with natural language processing and generation. Explore books, handbooks, cheatsheets, courses, and tutorials by various authors and topics.'}] I should read the LangChain tutorial to learn more about LangChain
Action: tavily_search_results_json

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1593519.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

js基础知识+练习

一,JavaScript简单了解 1.什么是JavaScript JavaScript简称JS,是较为流行的一种前端编程语言,是一种脚本语言,通过解释器运行,主要在客户端(浏览器)上运行,现在也可以基于node.js在服…

程序员的故事:麦哲伦死于JAVA之争

程序员的故事:麦哲伦死于JAVA之争 1400年,永乐年间,永乐皇帝七点钟准时上班了,清了清嗓子,问道:大家都到了没有?今天我们开个会,主要是讲一下项目用什么语言? 元朝的时候…

airtest-ios真机搭建实践

首先阅读4 ios connection - Airtest Project Docs 在Windows环境下搭建Airtest对iOS真机进行自动化测试的过程相对复杂,因为iOS的自动化测试通常需要依赖Mac OS系统,但理论上借助一些工具和服务,Windows用户也可以间接完成部分工作。下面是…

Python中的回调函数和C中函数指针什么关系?

你好,我是安然无虞。 Python 回调 在Python中,‘回调函数’ (callback) 是指一个作为参数传递给其它代码的函数。 目的是在后者完成某些操作后调用这个传递进来的函数。 回调允许在执行异步操作或处理事件时通知调用者代码。 回调函数通常用于&#…

家庭网络防御系统搭建-虚拟机安装siem/securityonion网络连接问题汇总

由于我是在虚拟机中安装的security onion,在此过程中,遇到很多的网络访问不通的问题,通过该文章把网络连接问题做一下梳理。如果直接把securityonion 安装在物理机上,网络问题则会少很多。 NAT无法访问虚拟机 security onion虚拟…

团结引擎+OpenHarmony 1配置篇

团结引擎OpenHarmony 1 配置篇 app团结鸿蒙化第一课一 DevEco Studio 下载安装二 团结引擎三 出包 app团结鸿蒙化第一课 1 团结引擎配置2 DevEco Studio 配置 一 DevEco Studio 下载安装 申请开发者套件 1 注册华为账号 签署协议 官网 2 认真填写 DevEco Studio 开发套件申请…

某网站sign签名参数与数据响应加密逆向分析

文章目录 1. 写在前面2. 接口分析3. 断点分析4. 扣代码 【🏠作者主页】:吴秋霖 【💼作者介绍】:擅长爬虫与JS加密逆向分析!Python领域优质创作者、CSDN博客专家、阿里云博客专家、华为云享专家。一路走来长期坚守并致力…

【吊打面试官系列】Java高并发篇 - Java 中你怎样唤醒一个阻塞的线程?

大家好,我是锋哥。今天分享关于 【Java 中你怎样唤醒一个阻塞的线程?】面试题,希望对大家有帮助; Java 中你怎样唤醒一个阻塞的线程? 在Java中,可以通过以下方式唤醒一个阻塞的线程: 使用Objec…

[dvwa] sql injection

sql injection 0x01 low sql语句没有过滤 经典注入,通过逻辑or为真相当于select * from users where true,99换成1也成 用union select 对齐列数,查看数据库信息 1’ union select 1,2# order by探测对齐列数更方便 1’ or 11 order b…

【网络安全】WebPack源码(前端源码)泄露 + jsmap文件还原

前言 webpack是一个JavaScript应用程序的静态资源打包器。它构建一个依赖关系图,其中包含应用程序需要的每个模块,然后将所有这些模块打包成一个或多个bundle。大部分Vue等项目应用会使用webpack进行打包,使用webpack打包应用程序会在网站js…

C语言 | Leetcode C语言题解之第28题找出字符串中第一个匹配项的下标

题目&#xff1a; 题解&#xff1a; int strStr(char* haystack, char* needle) {int n strlen(haystack), m strlen(needle);if (m 0) {return 0;}int pi[m];pi[0] 0;for (int i 1, j 0; i < m; i) {while (j > 0 && needle[i] ! needle[j]) {j pi[j - …

体验Humane AI:我与可穿戴AI别针的生活

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

使用Redis实现用户最近浏览记录

系列文章目录 文章目录 系列文章目录前言 前言 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。点击跳转到网站&#xff0c;这篇文章男女通用&#xff0c;看懂了就去分享给你的码吧。 Redis是一个key-va…

算法库应用- 表的自然链接

功 能: 设计算法,将两个单链表数组的特定位序, 相同者,链接起来 编程人: 王涛 详细博客:https://blog.csdn.net/qq_57484399/article/details/127161982 时 间: 2024.4.14 版 本: V1.0 V1.0 main.cpp /***************************************** 功 能: 设计算法,将两个…

学浪视频怎么缓存?

缓存学浪视频轻而易举&#xff01;推荐使用“小浪助手”&#xff0c;一款便捷的工具&#xff0c;助你轻松实现。工具已经预先打包好&#xff0c;需要的朋友可以自行下载。快试试&#xff0c;畅享学习吧&#xff01; 学浪下载器链接&#xff1a;https://pan.baidu.com/s/1y7vcq…

【C++学习】深入理解C++异常处理机制:异常类型,捕获和处理策略

文章目录 ♫一.异常的提出♫二.异常的概念♫三.异常的使用♫3.1 异常的抛出和捕获♫3.2.异常的重新抛出♫3.3异常安全♫3.4 异常规范 ♫4.自定义异常体系♫5.C标准库的异常体系♫6.异常的优缺点 ♫一.异常的提出 之前&#xff1a; C语言传统的处理错误的方式与带来的弊端&…

【C++庖丁解牛】底层为红黑树结构的关联式容器--哈希容器(unordered_map和unordered_set)

&#x1f341;你好&#xff0c;我是 RO-BERRY &#x1f4d7; 致力于C、C、数据结构、TCP/IP、数据库等等一系列知识 &#x1f384;感谢你的陪伴与支持 &#xff0c;故事既有了开头&#xff0c;就要画上一个完美的句号&#xff0c;让我们一起加油 目录 1. unordered系列关联式容…

多语言婚恋交友APP开发流程一览

近年来&#xff0c;随着全球化的发展和人们对跨文化交流的需求增加&#xff0c;多语言婚恋交友APP的需求逐渐增长。开发这类APP需要考虑到不同语言和文化下用户的需求&#xff0c;涉及到一系列独特的流程和挑战。本文将从专家角度为您解析多语言婚恋交友APP的开发流程&#xff…

GPT演变:从GPT到ChatGPT

Transformer 论文 Attention Is All You Need The dominant sequence transduction models are based on complex recurrent or convolutional neural networks in an encoder-decoder configuration. The best performing models also connect the encoder… https://arxiv.o…

一文理解java多线程之生产者消费者模型(三种实现)

生产者消费者模型 本文目录 生产者消费者模型基本介绍实现思路synchronized wait notify实现缓冲区生产者消费者测试代码思考 lock condition实现缓冲区生产者、消费者、测试代码 阻塞队列实现缓冲区生产者、消费者、测试代码思考 总结 基本介绍 什么是生产者消费者模型&am…