LlamaIndex RAG 和ReAct结合使用

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

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提取两部分财报数据,然后进行比较。

  

运行结果:

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 @ 2024-05-22 20:16  bonelee  阅读(66)  评论(0编辑  收藏  举报