简单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()
相关推荐
云飞云共享云桌面5 小时前
传统工作站 vs 云飞云共享云桌面:制造业设计云桌面选型深度对比
运维·服务器·前端·网络·3d·架构·制造
UXbot5 小时前
如何选择适合公司项目的UI设计工具?企业选型指南
前端·低代码·ui·团队开发·原型模式·设计规范·web app
llz_1126 小时前
web-第四次课后作业
前端·spring boot·web
武清伯MVP7 小时前
前端跨域方案大合集
前端·javascript
小刘|7 小时前
Spring AI Alibaba 集成和风天气 API 实战
java·服务器·前端
星星在线7 小时前
我是怎么把页面图片流量砍掉一半的
前端·javascript
木叶子---8 小时前
前端打包出错
前端·人工智能·tensorflow
JAVA面经实录9178 小时前
前端系统化学习计划表(含完整知识思维导图)
前端·学习
本末倒置1839 小时前
开发了一个所见所得的md编辑器,致敬Typora大佬
前端
kyriewen9 小时前
TypeScript 高级类型:我用 infer 写了一个类型安全的 EventBus,终于搞懂了泛型约束
前端·javascript·typescript