前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >LLMCompiler执行数学计算的案例

LLMCompiler执行数学计算的案例

作者头像
马超的博客
发布2025-02-21 17:33:05
发布2025-02-21 17:33:05
4200
代码可运行
举报
文章被收录于专栏:马超的博客马超的博客
运行总次数:0
代码可运行

LLMCompiler执行数学计算的案例

 这是一个使用LLMCompiler[2]执行数学运算的完整案例,通过设置' print_dag '参数,可以完全可视化工具之间的依赖关系。Issues: A use case example[3]

案例代码

代码语言:javascript
代码运行次数:0
复制
from llmcompiler.tools.basic importTools
from llmcompiler.result.chat importChatRequest
from langchain_openai.chat_models.base importChatOpenAI
from llmcompiler.chat.run importRunLLMCompiler

chat =ChatRequest(
    message="1. (3*(4+5)/0.5)+3245)加8的结果是什么?除以100的结果是什么?分别计算这两个值的结果,然后求其平均值。"
"2. 32除以4.23等于多少,它们的乘积是多少?计算他们的平均值!"
"3. 所有平均值的乘积除以2是多少?")

tools =Tools.load_tools("../llmcompiler/tools/math")
print(tools)

llm =ChatOpenAI(model="gpt-4o", temperature=0, max_retries=3)

llm_compiler =RunLLMCompiler(chat, tools, llm, print_dag=True)
print(llm_compiler())

../llmcompiler/tools/math加载的Tool源码[4]。

LLMCompiler的LangGrap结构可视化

LLMCompiler的LangGrap结构可视化

Tasks DAG可视化

案例代码生成的8个任务的有向无环图可视化

完整运行日志

代码语言:javascript
代码运行次数:0
复制
[Math()]
==========================初始化工具集和Agent:0.0秒==========================
We can convert a graph classintoMermaid syntax.
On https://www.min2k.com/tools/mermaid/, you can view visual results of Mermaid syntax.
%%{init:{'flowchart':{'curve':'linear'}}}%%
graph TD;
    __start__([__start__]):::first
    plan_and_schedule(plan_and_schedule)
    join(join)
    __end__([__end__]):::last
    __start__ --> plan_and_schedule;
    plan_and_schedule --> join;
    join -.-> plan_and_schedule;
    join -.-> __end__;
    classDef default fill:#f2f0ff,line-height:1.2
    classDef first fill-opacity:0
    classDef last fill:#bfb6fc

================================RewriterWithout LLM ================================
请基于用户问题,并结合相关信息和参考计划,生成一个专业且简洁的最少执行计划。
相关信息中可能会包含一些相关的数据信息可以作为`常量`使用,生成`Plan`时请认真检查。
但是切记不要构造用户问题和相关信息中没有提供的`常量`信息,例如基金代码没有提供则必须使用Tool查询,这很重要。

**用户问题**
1.((3*(4+5)/0.5)+3245)加8等于多少,除以100是多少?分别计算两个值的结果然后求平均值是多少?2.32除以4.23是多少,乘积是多少?计算他们的平均值!3.所有平均值的乘积除以二是多少?

**相关信息**


**参考计划**


Let’s think step by step!

================================PlanerCompiler================================
math
---
idx:1
tool: math
args:{'problem':'3*(4+5)/0.5 + 3245 + 8'}
dependencies:[]
thought:None
---

math
---
idx:2
tool: math
args:{'problem':'((3*(4+5)/0.5 + 3245) + 8) / 100'}
dependencies:[]
thought:None
---

math
---
idx:3
tool: math
args:{'problem':'32 / 4.23'}
dependencies:[]
thought:None
---

math
---
idx:4
tool: math
args:{'problem':'32 * 4.23'}
dependencies:[]
thought:None
---

math
---
idx:5
tool: math
args:{'problem':'($3 + $4) / 2','context':['$3','$4']}
args<Analyzed>:{'problem':'($3 + $4) / 2','context':[[7.565011820330969],[135.36]]}
dependencies:[3,4]
thought:None
---

math
---
idx:6
tool: math
args:{'problem':'($1 + $2) / 2','context':['$1','$2']}
args<Analyzed>:{'problem':'($1 + $2) / 2','context':[[3307],[33.07]]}
dependencies:[1,2]
thought:None
---
math
---
idx:7
tool: math
args:{'problem':'$5 * $6 / 2','context':['$5','$6']}
args<Analyzed>:{'problem':'$5 * $6 / 2','context':[[71.46250591016549],[1670.035]]}
dependencies:[5,6]
thought:None
---

---
idx:8
tool: join
args:()
dependencies:[1,2,3,4,5,6,7]
thought:None
---
We can convert a graph classintoMermaid syntax.
On https://www.min2k.com/tools/mermaid/, you can view visual results of Mermaid syntax.
%%{init:{'flowchart':{'curve':'linear'}}}%%
graph TD;
    __start__([__start__]):::first
    __end__([__end__]):::last
1(Task1 math)
2(Task2 math)
3(Task3 math)
4(Task4 math)
5(Task5 math)
6(Task6 math)
7(Task7 math)
8(Task8 join)
3-->5;
4-->5;
1-->6;
2-->6;
5-->7;
6-->7;
1-->8;
2-->8;
3-->8;
4-->8;
5-->8;
6-->8;
7-->8;
    __start__ -->1;
    __start__ -->2;
    __start__ -->3;
    __start__ -->4;
8--> __end__;
    classDef default fill:#f2f0ff,line-height:1.2
    classDef first fill-opacity:0
    classDef last fill:#bfb6fc

==========================Iteration1,['plan_and_schedule']:22.83秒==========================
================================Joiner================================
{
"_thought_":"我已经计算了所有需要的值,并且得到了最终的结果。",
"_finish_":"1. ((3*(4+5)/0.5)+3245)加8等于3307,除以100等于33.07。它们的平均值是1670.035。2. 32除以4.23等于7.565,乘积是135.36。它们的平均值是71.4625。3. 所有平均值的乘积除以二是59672.443。"
}
==========================Iteration1,['join']:5.0秒==========================
response='1. ((3*(4+5)/0.5)+3245)加8等于3307,除以100等于33.07。它们的平均值是1670.035。2. 32除以4.23等于7.565,乘积是135.36。它们的平均值是71.4625。3. 所有平均值的乘积除以二是59672.443。' charts=[] source=[] labels=[]

Process finished withexit code 0
引用链接

[1] TOC: LLMCompiler执行数学计算的案例 [2] LLMCompiler: https://github.com/crazyyanchao/llmcompiler [3] Issues: A use case example: https://github.com/crazyyanchao/llmcompiler/issues/2 [4] ../llmcompiler/tools/math加载的Tool源码: https://github.com/crazyyanchao/llmcompiler/blob/master/llmcompiler/tools/math/math_tools.py

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 马超的博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LLMCompiler执行数学计算的案例
    • 案例代码
    • LLMCompiler的LangGrap结构可视化
    • Tasks DAG可视化
    • 完整运行日志
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档