LlamaIndex RAG 和ReAct結合使用

bonelee發表於2024-05-22

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.

  

相關文章