LlamaIndex RAG 和ReAct结合使用

LlamaIndex RAG 和ReAct结合使用示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
os.environ['OpenAI_API_KEY'] = 'sk-pxxxxhU7F5Zrc'
os.environ['SERPAPI_API_KEY'] = '950fbdxxxx9b0fexxxx'
 
# 加载电商财报数据
from llama_index.core import SimpleDirectoryReader
 
A_docs = SimpleDirectoryReader(
    input_files=["./data/电商A-Third Quarter 2023 Results.pdf"]
).load_data()
B_docs = SimpleDirectoryReader(
    input_files=["./data/电商B-Third Quarter 2023 Results.pdf"]
).load_data()
 
 
 
# 从文档中创建索引
from llama_index.core import VectorStoreIndex
A_index = VectorStoreIndex.from_documents(A_docs)
B_index = VectorStoreIndex.from_documents(B_docs)
 
# 持久化索引(保存到本地)
from llama_index.core import StorageContext
A_index.storage_context.persist(persist_dir="./storage/A")
B_index.storage_context.persist(persist_dir="./storage/B")
 
 
# 从本地读取索引
try:
    storage_context = StorageContext.from_defaults(
        persist_dir="./storage/A"
    )
    A_index = load_index_from_storage(storage_context)
 
    storage_context = StorageContext.from_defaults(
        persist_dir="./storage/B"
    )
    B_index = load_index_from_storage(storage_context)
 
    index_loaded = True
except:
    index_loaded = False
 
 
# 创建查询引擎
A_engine = A_index.as_query_engine(similarity_top_k=3)
B_engine = B_index.as_query_engine(similarity_top_k=3)
 
 
# 配置查询工具
from llama_index.core.tools import QueryEngineTool
from llama_index.core.tools import ToolMetadata
query_engine_tools = [
    QueryEngineTool(
        query_engine=A_engine,
        metadata=ToolMetadata(
            name="A_Finance",
            description=(
                "用于提供A公司的财务信息 "
            ),
        ),
    ),
    QueryEngineTool(
        query_engine=B_engine,
        metadata=ToolMetadata(
            name="B_Finance",
            description=(
                "用于提供A公司的财务信息 "
            ),
        ),
    ),
]
 
 
# 配置大模型
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-3.5-turbo-0613")
 
 
# 创建ReAct Agent
from llama_index.core.agent import ReActAgent
agent = ReActAgent.from_tools(query_engine_tools, llm=llm, verbose=True)
 
 
# 让Agent完成任务
agent.chat("比较一下两个公司的销售额")

 

核心做法是通过RAG提取两部分财报数据,然后进行比较。

  

运行结果:

1
2
3
4
5
6
7
8
9
10
Thought: The current language of the user is: zh. I need to use a tool to help me answer the question.
Action: A_Finance
Action Input: {'input': 'sales'}
Observation: Sales and marketing expenses increased by 12.4% to US$ 918.0 million in the third quarter of 2023 from US$ 816.7 million in the third quarter of 2022. The breakdown of sales and marketing expenses for major reporting segments shows varying trends, with Digital Entertainment expenses decreasing by 59.2%, E-commerce expenses increasing by 49.7%, and Digital Financial Services expenses decreasing by 80.8%.
Thought: I have obtained the sales and marketing expenses information for one company. Now I need to gather the same information for the other company to compare their sales revenues.
Action: B_Finance
Action Input: {'input': 'sales'}
Observation: Revenue from various business segments, including China commerce retail, China commerce wholesale, International commerce retail, and other segments, contributed to the overall sales growth reported in the third quarter of 2023. The increase in revenue was driven by factors such as customer management services, direct sales, international commerce growth, and strong performance in digital media and entertainment businesses. Additionally, efforts to promote innovation and competitiveness in businesses like DingTalk have also positively impacted sales.
Thought: I have obtained the sales revenue information for both companies. Now I can compare their sales revenues.
Answer: Based on the information provided, Company A experienced an increase in sales and marketing expenses, while Company B reported growth in revenue from various business segments. However, without specific sales revenue figures for both companies, it is difficult to make a direct comparison of their sales revenues.

  

 

posted @   bonelee  阅读(171)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
历史上的今天:
2021-05-22 burpsuite中的active scan也可以做漏洞扫描 只是能力不太强---扫描一个站点有waf就给你屏蔽了,换个IP即可
2018-05-22 leetcode 572. Subtree of Another Tree
2018-05-22 英特尔® Software Guard Extensions 教程系列:第一部分,英特尔® SGX 基础
2018-05-22 英特尔® Software Guard Extensions 教程系列:第一部分,英特尔® SGX 基础
2017-05-22 csharp OverflowException——超出数值范围会抛出异常
2017-05-22 Elias-Fano编码算法——倒排索引压缩用,本质上就是桶排序数据结构思路
点击右上角即可分享
微信分享提示