LangChain Demo | Agent X ReAct X wikipedia 询问《三体》的主要内容

背景

LangChain学习中,尝试改了一下哈里森和吴恩达课程当中的问题,看看gpt-3.5-turbo在集成了ReAct和wikipedia后,如何回答《三体》的主要内容是什么这个问题,当然,主要是为了回答这问题时LangChain内部发生了什么。所以将langchain.debug 设置为 True。

代码出奇简洁👇

代码

import sys
import os# 添加 parent_dir 到 sys.path
current_dir = os.path.dirname(__file__)
parent_dir = os.path.dirname(current_dir)
sys.path.append(parent_dir)from constants import PROXY_URL,KEYimport warnings
warnings.filterwarnings("ignore")import langchain
langchain.debug = Truefrom langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType
from langchain.python import PythonREPL
from langchain.chat_models import ChatOpenAIllm = ChatOpenAI(temperature=0.0,model_name="gpt-3.5-turbo", openai_api_key=KEY,openai_api_base=PROXY_URL
)tools = load_tools(["wikipedia"],llm=llm
)agent= initialize_agent(tools, #第二步加载的工具llm, #第一步初始化的模型agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,  #代理类型handle_parsing_errors=True, #处理解析错误verbose = True #输出中间步骤
)response = agent("What is the main content of the science fiction novel The Three-Body Problem?")print(response)

输出异常繁琐👇

控制台输出

[chain/start] [1:chain:AgentExecutor] Entering Chain run with input:      
{
  "input": "What is the main content of the science fiction novel The Three-Body Problem?"
}
[chain/start] [1:chain:AgentExecutor > 2:chain:LLMChain] Entering Chain run with input:
{
  "input": "What is the main content of the science fiction novel The Three-Body Problem?",
  "agent_scratchpad": "",
  "stop": [
    "Observation:"
  ]
}
[llm/start] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] 
Entering LLM run with input:
{
  "prompts": [
    "System: Answer the following questions as best you can. You have access to the following tools:\n\nwikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\n\nThe way you use the tools is by specifying a json blob.\nSpecifically, this json should have a `action` key (with the name of the tool 
to use) and a `action_input` key (with the input to the tool going here).\n\nThe only values that should be in the \"action\" field are: wikipedia\n\nThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n\n```\n{\n  \"action\": $TOOL_NAME,\n  \"action_input\": $INPUT\n}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding.\nHuman: What is the main content of the science fiction novel The Three-Body Problem?"
  ]
}
[llm/end] [1:chain:AgentExecutor > 2:chain:LLMChain > 3:llm:ChatOpenAI] [3.96s] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "Thought: I can use Wikipedia to find information about the main content of the science fiction novel \"The Three-Body Problem\".\n\nAction:\n```\n{\n  \"action\": \"wikipedia\",\n  \"action_input\": \"The 
Three-Body Problem (novel)\"\n}\n```",
        "generation_info": {
          "finish_reason": "stop",
          "logprobs": null
        },
        "type": "ChatGeneration",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": [
            "langchain",
            "schema",
            "messages",
            "AIMessage"
          ],
          "kwargs": {
            "content": "Thought: I can use Wikipedia to find information about the main content of the science fiction novel \"The Three-Body Problem\".\n\nAction:\n```\n{\n  \"action\": \"wikipedia\",\n  \"action_input\": \"The Three-Body Problem (novel)\"\n}\n```",
            "additional_kwargs": {}
          }
        }
      }
    ]
  ],
  "llm_output": {
    "token_usage": {
      "completion_tokens": 55,
      "prompt_tokens": 306,
      "total_tokens": 361
    },
    "model_name": "gpt-3.5-turbo",
    "system_fingerprint": "fp_3bc1b5746c"
  },
  "run": null
}
[chain/end] [1:chain:AgentExecutor > 2:chain:LLMChain] [3.96s] Exiting Chain run with output:
{
  "text": "Thought: I can use Wikipedia to find information about the main content of the science fiction novel \"The Three-Body Problem\".\n\nAction:\n```\n{\n  \"action\": \"wikipedia\",\n  \"action_input\": \"The Three-Body Problem (novel)\"\n}\n```"
}
[tool/start] [1:chain:AgentExecutor > 4:tool:wikipedia] Entering Tool run 
with input:
"The Three-Body Problem (novel)"
[tool/end] [1:chain:AgentExecutor > 4:tool:wikipedia] [5.47s] Exiting Tool run with output:
"Page: The Three-Body Problem (novel)
Summary: The Three-Body Problem (Chinese: 三体; lit. 'Three-Body') is a story by Chinese science fiction author Liu Cixin, the first novel in the Remembrance of Earth's Past trilogy. The series portrays a fictional past, present and future wherein Earth encounters an alien civilization from a nearby system of three sun-like stars orbiting one another, a representative example of the three-body problem in orbital mechanics.
The story was originally serialized in Science Fiction World in 2006 before it was published as a standalone book in 2008. In 2006, it received the 
Galaxy Award for Chinese science fiction. In 2012, it was described as one of China's most successful full-length novels of the past two decades. The English translation by Ken Liu was published by Tor Books in 2014. That 
translation was the first novel by an Asian writer to win a Hugo Award for Best Novel; it was also nominated for the Nebula Award for Best Novel.The book has been adapted into other media. In 2015, a Chinese film adaptation of the same name was in production, but it was never released. A Chinese TV series, Three-Body, released in early 2023 to critical success locally. An English language Netflix series, 3 Body Problem, was released in March 2024.

Page: Three-body problem
Summary: In physics and classical mechanics, the three-body problem is the problem of taking the initial positions and velocities (or momenta) of three point masses and solving for their subsequent motion according to Newton's laws of motion and Newton's law of universal gravitation. The three-body problem is a special case of the n-body problem. Unlike two-body problems, no general closed-form solution exists, as the resulting dynamical system is chaotic for most initial conditions, and numerical methods are generally required.
Historically, the first specific three-body problem to receive extended study was the one involving the Moon, Earth, and the Sun. In an extended modern sense, a three-body problem is any problem in classical mechanics or quantum mechanics that models the motion of three particles.

Page: 3 Body Problem (TV series)
Summary: 3 Body Problem is an American science fiction television series created by David Benioff, D. B. Weiss and Alexander Woo, loosely based on the Hugo Award-winning Chinese novel The Three-Body Problem by Liu Cixin. It is the second live-action adaptation after the 2023 Chinese television series.
The series premiered on Netflix with its eight episodes on March 21, 2024."
[chain/start] [1:chain:AgentExecutor > 5:chain:LLMChain] Entering Chain run with input:
{
  "input": "What is the main content of the science fiction novel The Three-Body Problem?",
  "agent_scratchpad": "This was your previous work (but I haven't seen any of it! I only see what you return as final answer):\nThought: I can use Wikipedia to find information about the main content of the science fiction novel \"The Three-Body Problem\".\n\nAction:\n```\n{\n  \"action\": \"wikipedia\",\n  \"action_input\": \"The Three-Body Problem (novel)\"\n}\n```\nObservation: Page: The Three-Body Problem (novel)\nSummary: The Three-Body Problem (Chinese: 三体; lit. 'Three-Body') is a story by Chinese science fiction author Liu Cixin, the first novel in the Remembrance of Earth's Past trilogy. The series portrays a fictional past, present and future wherein Earth encounters an alien civilization from a nearby system of three sun-like stars orbiting one another, a representative example of the three-body problem in orbital mechanics.\nThe story was originally serialized in Science Fiction World in 2006 before it was published as a standalone book in 2008. In 2006, it received the Galaxy Award for Chinese science fiction. In 2012, it was described as one of China's most successful full-length novels of the past two decades. The English translation by Ken Liu was published by Tor Books in 2014. That translation was the first novel by an 
Asian writer to win a Hugo Award for Best Novel; it was also nominated for the Nebula Award for Best Novel.The book has been adapted into other media. In 2015, a Chinese film adaptation of the same name was in production, 
but it was never released. A Chinese TV series, Three-Body, released in early 2023 to critical success locally. An English language Netflix series, 
3 Body Problem, was released in March 2024.\n\nPage: Three-body problem\nSummary: In physics and classical mechanics, the three-body problem is the 
problem of taking the initial positions and velocities (or momenta) of three point masses and solving for their subsequent motion according to Newton's laws of motion and Newton's law of universal gravitation. The three-body problem is a special case of the n-body problem. Unlike two-body problems, no general closed-form solution exists, as the resulting dynamical system is chaotic for most initial conditions, and numerical methods are generally required.\nHistorically, the first specific three-body problem to receive extended study was the one involving the Moon, Earth, and the Sun. In an extended modern sense, a three-body problem is any problem in classical mechanics or quantum mechanics that models the motion of three particles.\n\n\n\nPage: 3 Body Problem (TV series)\nSummary: 3 Body Problem is an 
American science fiction television series created by David Benioff, D. B. Weiss and Alexander Woo, loosely based on the Hugo Award-winning Chinese 
novel The Three-Body Problem by Liu Cixin. It is the second live-action adaptation after the 2023 Chinese television series.\nThe series premiered on Netflix with its eight episodes on March 21, 2024.\n\n\nThought:",      
  "stop": [
    "Observation:"
  ]
}
[llm/start] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] 
Entering LLM run with input:
{
  "prompts": [
    "System: Answer the following questions as best you can. You have access to the following tools:\n\nwikipedia: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\n\nThe way you use the tools is by specifying a json blob.\nSpecifically, this json should have a `action` key (with the name of the tool 
to use) and a `action_input` key (with the input to the tool going here).\n\nThe only values that should be in the \"action\" field are: wikipedia\n\nThe $JSON_BLOB should only contain a SINGLE action, do NOT return a list of multiple actions. Here is an example of a valid $JSON_BLOB:\n\n```\n{\n  \"action\": $TOOL_NAME,\n  \"action_input\": $INPUT\n}\n```\n\nALWAYS use the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction:\n```\n$JSON_BLOB\n```\nObservation: the result of the action\n... (this Thought/Action/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Reminder to always use the exact characters `Final Answer` when responding.\nHuman: What is the main content of the science fiction novel The Three-Body Problem?\n\nThis was your previous work (but I haven't seen any of it! I only see what you return as final answer):\nThought: I can use Wikipedia to find information about the main content of the science fiction novel \"The 
Three-Body Problem\".\n\nAction:\n```\n{\n  \"action\": \"wikipedia\",\n  
\"action_input\": \"The Three-Body Problem (novel)\"\n}\n```\nObservation: Page: The Three-Body Problem (novel)\nSummary: The Three-Body Problem (Chinese: 三体; lit. 'Three-Body') is a story by Chinese science fiction author Liu Cixin, the first novel in the Remembrance of Earth's Past trilogy. 
The series portrays a fictional past, present and future wherein Earth encounters an alien civilization from a nearby system of three sun-like stars orbiting one another, a representative example of the three-body problem 
in orbital mechanics.\nThe story was originally serialized in Science Fiction World in 2006 before it was published as a standalone book in 2008. In 2006, it received the Galaxy Award for Chinese science fiction. In 2012, 
it was described as one of China's most successful full-length novels of the past two decades. The English translation by Ken Liu was published by Tor Books in 2014. That translation was the first novel by an Asian writer 
to win a Hugo Award for Best Novel; it was also nominated for the Nebula Award for Best Novel.The book has been adapted into other media. In 2015, a Chinese film adaptation of the same name was in production, but it was never released. A Chinese TV series, Three-Body, released in early 2023 to critical success locally. An English language Netflix series, 3 Body Problem, was released in March 2024.\n\nPage: Three-body problem\nSummary: In physics and classical mechanics, the three-body problem is the problem of taking the initial positions and velocities (or momenta) of three point masses and solving for their subsequent motion according to Newton's laws of motion and Newton's law of universal gravitation. The three-body problem is a special case of the n-body problem. Unlike two-body problems, no general closed-form solution exists, as the resulting dynamical system is chaotic for most initial conditions, and numerical methods are generally required.\nHistorically, the first specific three-body problem to receive extended study was the one involving the Moon, Earth, and the Sun. In an extended modern sense, a three-body problem is any problem in classical mechanics 
or quantum mechanics that models the motion of three particles.\n\n\n\nPage: 3 Body Problem (TV series)\nSummary: 3 Body Problem is an American science fiction television series created by David Benioff, D. B. Weiss and Alexander Woo, loosely based on the Hugo Award-winning Chinese novel The Three-Body Problem by Liu Cixin. It is the second live-action adaptation after the 2023 Chinese television series.\nThe series premiered on Netflix with its eight episodes on March 21, 2024.\n\n\nThought:"
  ]
}
[llm/end] [1:chain:AgentExecutor > 5:chain:LLMChain > 6:llm:ChatOpenAI] [4.10s] Exiting LLM run with output:
{
  "generations": [
    [
      {
        "text": "I have found information about the main content of the science fiction novel \"The Three-Body Problem\" by Liu Cixin. It is a story that portrays a fictional past, present, and future where Earth encounters an alien civilization from a nearby system of three sun-like stars orbiting one another. The novel is the first in the Remembrance of Earth's Past trilogy and has received critical acclaim.\n\nFinal Answer: The main content of the science fiction novel \"The Three-Body Problem\" involves Earth encountering an alien civilization from a nearby system of three sun-like stars.",
        "generation_info": {
          "finish_reason": "stop",
          "logprobs": null
        },
        "type": "ChatGeneration",
        "message": {
          "lc": 1,
          "type": "constructor",
          "id": [
            "langchain",
            "schema",
            "messages",
            "AIMessage"
          ],
          "kwargs": {
            "content": "I have found information about the main content of the science fiction novel \"The Three-Body Problem\" by Liu Cixin. It is 
a story that portrays a fictional past, present, and future where Earth encounters an alien civilization from a nearby system of three sun-like stars orbiting one another. The novel is the first in the Remembrance of Earth's Past trilogy and has received critical acclaim.\n\nFinal Answer: The main content of the science fiction novel \"The Three-Body Problem\" involves Earth encountering an alien civilization from a nearby system of three sun-like stars.",
            "additional_kwargs": {}
          }
        }
      }
    ]
  ],
  "llm_output": {
    "token_usage": {
      "completion_tokens": 113,
      "prompt_tokens": 924,
      "total_tokens": 1037
    },
    "model_name": "gpt-3.5-turbo",
    "system_fingerprint": "fp_3bc1b5746c"
  },
  "run": null
}
[chain/end] [1:chain:AgentExecutor > 5:chain:LLMChain] [4.10s] Exiting Chain run with output:
{
  "text": "I have found information about the main content of the science 
fiction novel \"The Three-Body Problem\" by Liu Cixin. It is a story that 
portrays a fictional past, present, and future where Earth encounters an alien civilization from a nearby system of three sun-like stars orbiting one another. The novel is the first in the Remembrance of Earth's Past trilogy and has received critical acclaim.\n\nFinal Answer: The main content of the science fiction novel \"The Three-Body Problem\" involves Earth encountering an alien civilization from a nearby system of three sun-like stars."
}
[chain/end] [1:chain:AgentExecutor] [13.57s] Exiting Chain run with output:
{
  "output": "The main content of the science fiction novel \"The Three-Body Problem\" involves Earth encountering an alien civilization from a nearby system of three sun-like stars."
}
{'input': 'What is the main content of the science fiction novel The Three-Body Problem?', 'output': 'The main content of the science fiction novel 
"The Three-Body Problem" involves Earth encountering an alien civilization from a nearby system of three sun-like stars.'}

最终结果

The Three-Body Problem" involves Earth encountering an alien civilization from a nearby system of three sun-like stars. 

翻译:《三体问题》涉及地球遇到来自附近三颗类太阳恒星系统的外星文明。

 

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

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

相关文章

基于大型语言模型的智能体(Agent)研究综述--人大

内容概述 论文地址:https://arxiv.org/pdf/2308.11432.pdf 这篇综述内容有35页,内容很多,俗话说一图胜千言,作者提供了5张精美的图片和3个表格,把这些搞明白后对这篇综述也就理解差不多了。文章的总体结构如下由6部分…

基于GaN的半导体光学放大器SOA

摘要 基于GaN的材料可覆盖很宽的光谱范围,以紫外、紫、蓝、绿和红波发射的激光二极管已经商业化。基于GaN的半导体光学放大器(SOA)具有提高激光二极管输出功率的能力,因此SOA将有很多潜在应用。未来需要利用短波、超快脉冲特性的…

Linux知识点记录

Linux知识点记录 1. 后台运行应用程序方法一:&方法二:nohup & 2. 一个shell脚本中执行多个应用程序3. 2>&14. shell脚本清除日志5. 通过grep查找匹配字符串 1. 后台运行应用程序 参考文章:https://blog.csdn.net/Pan_peter/…

常见的四种限流算法及基础实现

常见的四种限流算法及基础实现 什么是限流有哪些限流算法?限流算法固定窗口滑动窗口漏桶算法令牌算法 什么是限流 限流是对某一时间窗口内的请求数进行限制,保持系统的可用性和稳定性,防止因流量暴增而导致的系统运行缓慢或宕机。 在高并发…

用kimichat批量识别出图片版PDF文件中的文字内容

图片版的PDF文件,怎么才能借助AI工具来提取其中全部的文字内容呢? 第一步:将PDF文件转换成图片格式 具体方法参见文章:《零代码编程:用kimichat将图片版PDF自动批量分割成多个图片》 第二步:识别图片中的…

修改亚马逊云科技账户的密码和MFA

要使用AWS CLI删除当前账户的多因素认证(MFA)设备并修改密码,你需要先确保已安装并配置了AWS CLI,并且你的账户有足够的权限执行这些操作。下面是如何分步进行的指导: 1. 删除MFA设备 首先,你需要找出MFA设备的序列号或ARN。可以…

Dynamo之雪花分形(衍生式设计)

你好,这里是BIM的乐趣,我是九哥~ 今天简单分享一些我收集的Dynamo的雪花分形案例吧,不过多讲解了,有兴趣的小伙伴,可以私信“雪花分形”获取案例文件,下面基本以分享为主: ******多图预警****…

IDEA 在 Windows 系统上常用的快捷键

IntelliJ IDEA 在 Windows 系统上常用的快捷键的详细介绍: 基本编辑操作: 复制(Ctrl C):复制选中的内容。剪切(Ctrl X):剪切选中的内容。粘贴(Ctrl V)&…

GD32F470_GY-SHT31-D 数字温湿度传感器模块移植

2.11 SHT30温湿度传感器 2.11.1 模块来源 采购链接: GY-SHT31-D 数字温湿度传感器模块 资料下载链接: https://pan.baidu.com/s/1kisMJspcV6Qdr1ye9ElOlQ 2.11.2 规格参数 工作电压:2.4-5.5V 工作电流:0.2~1500uA 温度测量范围&a…

构建未来交通:香橙派OPI Airpro上的智能交通监管系统

引言: 随着城市化进程的加速,交通管理变得越来越复杂。 传统的交通监管系统往往无法有效应对日益增长的车辆数量和复杂的交通状况。因此,我们需要一种更加智能和自适应的解决方案来提高交通效率并减少事故发生率。 香橙派OPI Airpro以其强大的…

ComfyUI ClipSeg插件报错- resize_image出错应该怎么办

上一篇刚介绍了这个插件,结果emm..很快发现事情并不简单...结果又报错了。 后台报错信息: Unused or unrecognized kwargs: padding. !!! Exception during processing !!! Traceback (most recent call last): File "F:\ComfyUI-aki\execution.p…

Open-Sora环境搭建推理测试

引子 Sora,2024年2月15日,OpenAI发布的人工智能文生视频大模型。支持60秒视频生成,震荡了国内国际学术圈、广告圈、AI教培圈。Sora最主要有三个优点:第一,“60s超长视频”,之前文本生成视频大模型一直无法真…

【数据结构】顺序表与链表

摘要: 顺序表和链表是两种常见的线性数据结构,它们在存储和操作数据时具有各自的特点和优势。本文将介绍顺序表和链表的概念、特点以及基本操作,并通过C语言代码实现一个简单的顺序表和链表结构,以帮助读者更好地理解它们的原理和…

数据库重点知识(个人整理笔记)

目录 1. 索引是什么? 1.1. 索引的基本原理 2. 索引有哪些优缺点? 3. MySQL有哪几种索引类型? 4. mysql聚簇和非聚簇索引的区别 5. 非聚簇索引一定会回表查询吗? 6. 讲一讲前缀索引? 7. 为什么索引结构默认使用B…

竞赛常考的知识点大总结(五)动态规划

DP问题的性质 动态规划(Dynamic Programming,DP)是指在解决动态规划问题时所依赖的一些基本特征和规律。动态规划是一种将复杂问题分解为更小子问题来解决的方法,它适用于具有重叠子问题和最优子结构性质的问题。动态规划问题通常…

【Visual Studio】将项目下的文件夹所有文件随编译自动复制输出到运行目录

要将项目根目录下的文件夹内容输出到运行目录,去处理其中的子文件夹和文件,逐个手动设置文件属性或进行复制显然不是一个可行的方法,因为这既繁琐又低效,那有没有更加高效的方式呢 文章目录 选择文件夹修改配置文件输出文件夹 这里…

FreeRtos入门-3 信号量(计数值、二进制、互斥量、递归锁)

信号量 计数量 二进制 互斥量 递归锁 创建 xSemCalc xSemaphoreCreateCounting(10, 0);//计数最大值10,初始值0 xSemUart xSemaphoreCreateBinary(); xSemUART xSemaphoreCreateMutex(); xSemUART xSemaphoreCreateRecursiveMutex(); 释放 xSemaphore…

【c语言】自定义类型:枚举类型【详解】

枚举类型 枚举类型的声明 枚举顾名思义就是⼀⼀列举。 把可能的取值⼀⼀列举。 ⽐如我们现实⽣活中 ⼀周的星期⼀到星期⽇是有限的7天,可以⼀⼀列举 性别有:男、⼥、保密,也可以⼀⼀列举 ⽉份有12个⽉,也可以⼀⼀列举 三原⾊&am…

Linux集群(一)Nginx搭建

目录 一、Nginx介绍 1.什么是Nginx 2.Nginx的特点 二、Nginx配置 1.jdk的安装 1.1检查jdk版本 1.2上传并安装jdk 2.安装Tomcat 3.下载Nginx 3.1安装依赖包 ​编辑 3.2安装Nginx 3.3运行 三、Nginx中的常用命令​编辑 一、Nginx介绍 1.什么是Nginx Nginx(…

【FAQ】HarmonyOS SDK 闭源开放能力 —Asset Store Kit

1.问题描述 使用关键资产API需要配置SystemCapability.Security.Asset,但不知道syscap.json文件应该配置在哪里,文档也没找到。 解决方案 新增关键资产等API可以直接参考开发指南里的示例代码进行调用: https://developer.huawei.com/cons…