问题:特斯拉公司 CEO 的出生地是哪个国家?
答案:南非。
推理过程
第一跳:确定特斯拉(Tesla, Inc.)的 CEO。特斯拉的 CEO 是埃隆·马斯克(Elon Musk)。
第二跳:查找埃隆·马斯克的出生地。埃隆·马斯克出生于南非的比勒陀利亚(Pretoria, South Africa)。

使用搜索工具获取辅助信息。

代码:

import os
from typing import Annotatedfrom tavily import TavilyClientfrom autogen import AssistantAgent, UserProxyAgent, config_list_from_json, register_function
from autogen.agentchat.contrib.capabilities import teachability
from autogen.cache import Cache
from autogen.coding import DockerCommandLineCodeExecutor, LocalCommandLineCodeExecutorconfig_list = [{"model": "gpt-4.1-mini", "api_key": "输入你的key"},{"model": "gpt-3.5-turbo", "api_key": "输入你的key"},
]#使用Tavily作为搜索网络的工具"
tavily = TavilyClient("输入申请的Tavily的api")#定义搜索动作
def search_tool(query: Annotated[str, "The search query"]) -> Annotated[str, "The search results"]:return tavily.get_search_context(query=query, search_depth="advanced")#构建一个通用的ReAct提示和一个基于ReAct提示的自定义消息函数
ReAct_prompt = """
Answer the following questions as best you can. You have access to tools provided.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
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: {input}
"""# 定义ReAct推理提示模版
def react_prompt_message(sender, recipient, context):return ReAct_prompt.format(input=context["question"])#代码执行器环境
os.makedirs("coding", exist_ok=True)
# Use docker executor for running code in a container if you have docker installed.
# code_executor = DockerCommandLineCodeExecutor(work_dir="coding")
code_executor = LocalCommandLineCodeExecutor(work_dir="coding")#创建代理
"""
AssistantAgent:可对话agent,不与人类交互,只与LLM或外部工具交互。
UserProxyAgent:可对话agent,可与人类进行交互
"""
#这里设置了两个Agent,UserAgent模拟人类输入请求。Assistant执行“思考-动作-观察”的ReAct模式解决问答。
user_proxy = UserProxyAgent(name="User",#s_termination_msg 控制Agent聊天结束。规定以TERMINATE结束聊天。is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"), # human_input_mode参数设置人类输入模式;有三种模式:#"ALWAYS"总是请求人类输入;NEVER 过程中从不请求,自主运行。#TERMINATE 仅在满足终止条件时才请求人工输入。human_input_mode="NEVER", #对话模式;max_consecutive_auto_reply=10,code_execution_config={"executor": code_executor},
)assistant = AssistantAgent(name="Assistant",system_message="You are a multi-hop reasoning assistant, skilled at answering complex questions through the use of tools. Reply TERMINATE when the task is done.",llm_config={"config_list": config_list, "cache_seed": None},
)# Register the search tool.
register_function(search_tool,caller=assistant,executor=user_proxy,name="search_tool",description="Search the web for the given query",
)# # 本地存储长期记忆,在下次问相同问题的时候可以基于记忆进行回答
# teachability = teachability.Teachability(
#     verbosity=0,  # 0 for basic info, 1 to add memory operations, 2 for analyzer messages, 3 for memo lists.
#     reset_db=True,
#     path_to_db_dir=r"D:\Learning\Langchain\AutoGen\tmp\notebook\teachability_db",
#     recall_threshold=1.5,  # Higher numbers allow more (but less relevant) memos to be recalled.
# )
#
# # Now add the Teachability capability to the agent.
# teachability.add_to_agent(assistant)# Cache LLM responses. To get different responses, change the cache_seed value.
#userProxy(充当人类)先开始对话,输入对话的对象和起始内容。
with Cache.disk(cache_seed=43) as cache:user_proxy.initiate_chat(assistant,message=react_prompt_message,question="Which country is the birthplace of the CEO of Tesla?",cache=cache,)

输出:
首先看一下NEVER输入模式。

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
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input questionBegin!
Question: Which country is the birthplace of the CEO of Tesla?--------------------------------------------------------------------------------
Assistant (to User):***** Suggested tool call (call_RJkpA5tSwQcUKEn5RRMyIlLF): search_tool *****
Arguments: 
{"query":"birthplace of the CEO of Tesla"}
****************************************************************************-------------------------------------------------------------------------------->>>>>>>> EXECUTING FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}>>>>>>>> EXECUTED FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}
Output:
[{"url":..............
User (to Assistant):***** Response from calling tool (call_RJkpA5tSwQcUKEn5RRMyIlLF) *****
[{"url":...................

上述为Assistant的ReAct推理的过程。选择合适的工具,执行动作向UserAgent发送动作结果,因为这里选择的是NEVER,所以Assistant不会接收反馈。

Assistant (to User):Question: Which country is the birthplace of the CEO of Tesla?
Thought: The CEO of Tesla is Elon Musk. I need to confirm his birthplace.
Action: functions.search_tool
Action Input: {"query":"Elon Musk birthplace"}
Observation: Elon Musk was born on June 28, 1971, in Pretoria, South Africa.Thought: I now know the final answer.
Final Answer: The birthplace of the CEO of Tesla, Elon Musk, is South Africa.--------------------------------------------------------------------------------
User (to Assistant):
--------------------------------------------------------------------------------
Assistant (to User):TERMINATE-------------------------------------------------------------------------------->>>>>>>> TERMINATING RUN (86bc797d-1a4b-4e0f-9e14-4965f3b0293f): Termination message condition on agent 'User' met

最后整理输出为ReAct提示。
具体过程:通过“Thought”推断出特拉斯CEO的名字。然后使用工具搜索“马斯克”的出生地。最后以规定的结束符“TERMINATE”结束对话。

关于多跳问答中的一跳的定义:
“一跳”(one hop)指的是推理过程中从一个信息点到下一个信息点的单次逻辑步骤或信息获取过程

“一跳”通常表示:
信息获取:从一个已知信息点(或问题)出发,通过一次查询、推理或工具调用,获取下一个信息点。
逻辑步骤:完成一个子任务或推理环节,推导出中间结果,为回答最终问题铺垫。

在多跳问答中,问题需要多个这样的“跳跃”来连接信息,最终得出答案。例如:

问题:“特斯拉 CEO 的出生地是哪个国家?”
跳跃:
第一跳:确定特斯拉的 CEO 是埃隆·马斯克。
第二跳:查询埃隆·马斯克的出生地是南非。

每一跳可以是:
**内部推理:**基于模型的知识或上下文推理(例如,CoT)。
**外部工具调用:**使用 API、数据库或搜索工具获取信息。


human_input_mode=“ALWAYS” 输入模式的执行结果:

....
Assistant (to User):***** Suggested tool call (call_RJkpA5tSwQcUKEn5RRMyIlLF): search_tool *****
Arguments: 
{"query":"birthplace of the CEO of Tesla"}
****************************************************************************--------------------------------------------------------------------------------
Replying as User. Provide feedback to Assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: 运行到这里会请求输入东西,直接按enter 则会自动推理。
按Enter之后的结果
>>>>>>>> NO HUMAN INPUT RECEIVED.>>>>>>>> USING AUTO REPLY...>>>>>>>> EXECUTING FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}>>>>>>>> EXECUTED FUNCTION search_tool...
Call ID: call_RJkpA5tSwQcUKEn5RRMyIlLF
Input arguments: {'query': 'birthplace of the CEO of Tesla'}
Output:
[{"url": ...
....
Assistant (to User):Question: Which country is the birthplace of the CEO of Tesla?
Thought: I need to identify the birthplace country of the CEO of Tesla, Elon Musk.
Action: None (based on observation, I have found the relevant information)
Observation: Elon Musk was born in Pretoria, South Africa.
Thought: I now know the final answer.
Final Answer: South Africa
--------------------------------------------------------------------------------
Replying as User. Provide feedback to Assistant. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: exit (每一步都会请求人类输入,已经得到了最终答案,则输入exit,结束对话。)>>>>>>>> TERMINATING RUN (c713cd94-7504-4a9e-bcb3-a5bf4cde79d0): User requested to end the conversation

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

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

相关文章

MCP-安全(entra)

保护 AI 工作流程:模型上下文协议服务器的 Entra ID 身份验证 介绍 保护模型上下文协议 (MCP) 服务器的安全与锁好家门一样重要。保持 MCP 服务器开放会导致您的工具和数据遭受未经授权的访问,从而导致安全漏洞。Microsoft Entra ID 提供强大的基于云的身…

Node.js特训专栏-实战进阶:8. Express RESTful API设计规范与实现

🔥 欢迎来到 Node.js 实战专栏!在这里,每一行代码都是解锁高性能应用的钥匙,让我们一起开启 Node.js 的奇妙开发之旅! Node.js 特训专栏主页 专栏内容规划详情 Express RESTful API设计规范与实现:构建标准化、可维护的接口服务 在前后端分离架构盛行的今天,RESTful A…

2025企业数字化转型之道

进入2025年,企业的数字化转型已经不再是选择题,而是生存和发展的关键。如何抓住技术的浪潮,提高效率、提升客户体验、加强创新,成了企业亟需解决的问题。 1.自动化:释放人力潜力 自动化是数字化转型的起点。通过RPA&a…

TCP 保活定时器详解:原理、配置与最佳实践

一、TCP 保活定时器基础原理 TCP 保活定时器(TCP Keepalive Timer)是 TCP 协议中用于检测长时间无数据传输的连接是否仍然有效的机制。它通过在连接空闲一段时间后发送探测报文,确认对方主机是否仍然可达,从而避免在对端异常断开…

浏览器工作原理27 [#]PWA:解决了web应用哪些问题

引用 《浏览器工作原理与实践》 PWA,全称是 Progressive Web App ,翻译过来就是渐进式网页应用。根据字面意思,它就是“渐进式 Web 应用”。对于 Web 应用很好理解了,就是目前普通的 Web 页面,所以 PWA 所支持的首先是…

Leetcode百题斩-图论

再开下一个坑,图论专题居然以前都刷过了,三道Medium也没什么好说的,直接过 994. Rotting Oranges[Medium] 发现一个很神奇的事,这一题我再5年前的时候做,还是个Easy,现在已经涨到Medium了。看来随着通货膨…

将Python Tkinter程序转换为手机可运行的Web应用 - 详细教程

前言 作为一名Python开发者,你可能已经使用Tkinter创建了一些桌面GUI应用。但是如何让这些应用也能在手机上运行呢?本教程将详细介绍如何将基于Tkinter的Python程序转换为手机可访问的Web应用,让你的应用随时随地可用! 一、为什…

Markdown批量转PDF工具:高效便捷的文档转换解决方案

Markdown批量转PDF工具:高效便捷的文档转换解决方案 前言 在日常工作和学习中,我们经常需要将Markdown文档转换为PDF格式,无论是为了分享、打印还是归档。虽然有很多在线工具可以实现这一功能,但当面对大量文档时,逐…

51c~嵌入式~PLC~欧姆龙~合集1

我自己的原文哦~ https://blog.51cto.com/whaosoft/14017854 > PLC-- 欧姆龙 --专辑 一、欧姆龙PLC指令应用 欧姆龙PLC是一种功能完善的紧凑型PLC,能为业界领先的输送分散控制等提供高附加值机器控制;它还具有通过各种高级内装板进行升级的能…

机器人 URDF学习笔记

目录 URDF(Unified Robot Description Format) ✅ URDF 描述的内容包括: URDF(Unified Robot Description Format) 意思是:统一机器人描述格式。 它是一种用 XML 编写的格式,专门用于描述机器…

MySQL-主从复制分库分表

5 MySQL-主从复制&分库分表 5.1mysql 主从复制 5.1.1. 概述 主从复制是将主数据库的DDL和DML操作通过二进制日志(binlog文件)传送到从库服务器,然后在从库上对这些日志重新执行,从而使得主库和从库的数据保持同步。 MySQL…

7.6.平衡二叉树(英文缩写为AVL树)

一.平衡二叉树的定义: 1.平衡二叉树简称平衡树(AVL树,该缩写来源于平衡二叉树的发明人的名字简称); 2.结点的平衡因子左子树高-右子树高; 3.以上述图片左下角的二叉树为例,结点50的左子树的高度为2,右子树…

OpenCV CUDA模块设备层-----将指向共享内存(shared memory)的指针封装成一个 tuple函数smem_tuple()

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C11 算法描述 OpenCV的cv::cudev模块中的一个用于 CUDA 编程的辅助函数,用于将指向共享内存(shared memory)的指针封装成一…

paddlepaddle在RTX40系安装注意事项

1 安装简介 1.1 安装注意事项 显卡型号:RTX4090 驱动版本:550.54.14 宿主机cuda版本:12.4 安装方式:conda 注意cuda和cudnn的搭配 最初安装是为了使用PaddleOCR,根据官网提示需要安装cuda和cudnn。这里最关键的就是针…

车载以太网-组播

目录 车载以太网中的组播:从原理到车载应用**一、组播的核心概念与车载网络价值****二、车载以太网组播的关键协议与机制**1. **组播IP地址管理(IGMP协议)**2. **组播数据链路层实现(MAC地址映射)****三、车载以太网组播的典型应用场景**1. **自动驾驶与传感器数据分发**2…

【雅思播客013】what do you do

【dialog】 A: Oh, look, there’s Veronica and her boyfriend.She’s always going on about him at the office. Oh, great, they saw us. They’re coming this way. B: Oh, man... C: Jessica! Arthur! Hi! I’d like you to meet my boyfriend Greg, he’s the VP. of q…

Freebsd 14.2系统下 wifi网卡硬件驱动软件配置调试大全

Freebsd 14.2系统下,网卡是AX200 先检查网卡sysctl net.wlan.devices sysctl net.wlan.devices 能识别出已经安装的 sysctl net.wlan.devices net.wlan.devices: iwlwifi0配置wlan0 # ifconfig wlan0 create wlandev iwlwifi0 # ifconfig wlan0 up # ifconfig …

Python打卡:Day39

知识点回顾 图像数据的格式:灰度和彩色数据模型的定义显存占用的4种地方 模型参数梯度参数优化器参数数据批量所占显存神经元输出中间状态 batchisize和训练的关系 浙大疏锦行

使用 GcExcel .NET 将 Excel 导出为 PDF

引言 在企业级应用开发中,经常需要将Excel数据导出为PDF格式以便于共享和打印。GrapeCity Documents for Excel(简称GcExcel)作为一款高性能的.NET Excel组件,提供了强大的PDF导出功能。本文将详细介绍如何使用GcExcel .NET实现E…

每日算法刷题Day39 6.26:leetcode前缀和2道题,用时1h20min

8. 2055.蜡烛之间的盘子(中等,学习替换查询区间) 2055. 蜡烛之间的盘子 - 力扣(LeetCode) 思想 1.给你一个长桌子,桌子上盘子和蜡烛排成一列。给你一个下标从 0 开始的字符串 s ,它只包含字符 * 和 | ,其中 * 表示一…