简单react agent(没有抽象成基类、子类,直接用)--- 非workflow版 ------demo1

代码

可直接运行

复制代码
"""
简单Agent示例 - 继承StandardReActWorkflow

演示如何快速创建一个标准ReAct模式的Agent,
只需要定义工具函数即可。
"""

from langchain_core.tools import tool
from typing import List, Dict
from langchain_core.tools import BaseTool

from base import StandardReActWorkflow, LLMConfig


class SimpleCalculatorAgent(StandardReActWorkflow):
    """
    简单计算器Agent
    
    功能:
    - 加法运算
    - 乘法运算
    - 天气查询
    - 电影信息查询
    """
    
    def __init__(self, llm_config: LLMConfig = None):
        """初始化简单计算器Agent"""
        super().__init__(llm_config)
    
    @tool
    def add(self, a: int, b: int) -> int:
        """加法运算"""
        result = a + b
        print(f"执行加法: {a} + {b} = {result}")
        return result
    
    @tool
    def multiply(self, a: int, b: int) -> int:
        """乘法运算"""
        result = a * b
        print(f"执行乘法: {a} * {b} = {result}")
        return result
    
    @tool
    def query_weather(self, location: str) -> str:
        """查询天气"""
        print(f"------------------------------------------调用 query_weather ------------------------------------------------")
        result = f"{location}: 天气是暴风雨"
        return result
    
    @tool
    def query_film_info(self, film: str) -> str:
        """查询电影票房等信息"""
        print(f"------------------------------------------调用 query_film_info ------------------------------------------------")
        result = f"{film}: 电影票房是80亿"
        return result
    
    def get_tools(self) -> List[BaseTool]:
        """返回工具列表"""
        return [self.add, self.multiply, self.query_weather, self.query_film_info]
    
    def get_tools_dict(self) -> Dict[str, BaseTool]:
        """返回工具名称到工具对象的映射"""
        return {
            "add": self.add,
            "multiply": self.multiply,
            "query_weather": self.query_weather,
            "query_film_info": self.query_film_info
        }


def main():
    """主函数 - 演示Agent使用"""
    print("=== 简单计算器Agent示例 ===\n")
    
    # 创建Agent实例(使用默认配置)
    agent = SimpleCalculatorAgent()
    
    # 测试用例1:数学计算
    print("测试1: 数学计算")
    result1 = agent.run("请计算 3 * 12 的结果")
    print(f"结果: {result1}\n")
    
    # 测试用例2:复合查询(天气 + 电影信息)
    print("测试2: 复合查询")
    result2 = agent.run("上海天气如何,以及电影《哪吒》的票房是多少")
    print(f"结果: {result2}\n")
    
    # 测试用例3:多步计算
    print("测试3: 多步计算")
    result3 = agent.run("先计算 5 + 3,然后把结果乘以 4")
    print(f"结果: {result3}\n")


if __name__ == "__main__":
    main()
相关推荐
Momo__5 分钟前
Vite 8.1 打包开发模式——10000 组件冷启动 15 倍,"No Bundle Dev" 七年后被自己终结
前端·vue.js·vite
YHL7 分钟前
🤖 Agent 智能体开发实战 · 第一课:Tool Use —— 让大模型自动干活
前端·javascript·人工智能
Monki9 分钟前
AI 规范式 UI 设计,一键批量出页,高效落地不翻车
前端
山河木马10 分钟前
渲染管线-计算得到gl_FragColor(片元着色器)之后续GPU流程
前端·webgl·计算机图形学
不想说话的麋鹿13 分钟前
「项目实战」我用 Codex + GPT-5.5 + Trellis "氛围编程",独立做了一个情侣厨房小程序
前端·agent·全栈
接着奏乐接着舞。2 小时前
【2026年7月最新】69道RAG面试题
前端·人工智能·后端·aigc·embedding·rag
以和为贵2 小时前
前端也能搞懂 Agent:从 Function Calling 到自主编排
前端·人工智能·架构
风止何安啊2 小时前
🚦 前端并发请求 “交通管制”:别让你的接口堵成早高峰
前端·javascript·面试
J船长2 小时前
扫盲烂笔头:A 记录、CNAME 的作用, Nginx的用途,为啥叫反向代理
前端
妙码生花2 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(二十六):icon 组件封装
前端·vue.js·typescript