首页 > 其他分享 >LlamaIndex RAG 和ReAct结合使用

LlamaIndex RAG 和ReAct结合使用

时间:2024-05-22 20:18:52浏览次数:21  
标签:engine RAG index storage sales LlamaIndex ReAct llama import

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.

  

 

标签:engine,RAG,index,storage,sales,LlamaIndex,ReAct,llama,import
From: https://www.cnblogs.com/bonelee/p/18207015

相关文章

  • 【Azure Storage Account】使用Azure Policy来检查Storage Account中是否有开启匿名访
    问题描述因为StorageAccount中的Container可以开启匿名访问,因安全要求,需要检测出那些Container开启了匿名访问。所以使用AzurePolicy策略来进行检测。但是,想使用以上规则,保存报错。Thepolicydefinition'xxxxxxx'ruleisinvalid.Theresourcetype'storageAccounts/bl......
  • 离线文档(vue/react/element/element-plus/ands vue等等)
    特殊环境需要离线状态文档的请参考这里 可以在你所想要的东西在github的官方项目上找一个名字包含‘gh-page’或者‘get-page’的分支下载下来然后本地win+Rcmd输入npminstallsgo-g全局下载sgo之后在你下载下来的文件夹根目录cmd打开终端输入sgo--fallindex.html......
  • 4/7一文讲透网络传输流程 epoll内核模型 reactor用户空间处理模型
    epoll是内核如何将由层层协议栈去除tcp头,根据四元组查socket文件,将sk_buffer放到socket接受队列的 reactor  五种IO模型,三种线程处理模型     回溯算法之全排列 将所有需要用到的数组包括路径数组状态数组都初始化好然后都放进dfs参数里面 这......
  • 整合LlamaIndex与LangChain构建高级的查询处理系统
    构建大型语言模型应用程序可能会颇具挑战,尤其是当我们在不同的框架(如Langchain和LlamaIndex)之间进行选择时。LlamaIndex在智能搜索和数据检索方面的性能令人瞩目,而LangChain则作为一个更加通用的应用程序框架,提供了更好的与各种平台的兼容性。本篇文章将介绍如何将LlamaIndex和La......
  • react eslint import { useUserStore } from "@/stores/user"; 找不到模块“@/stores/
    报错解释:这个报错通常意味着React项目中的ESLint配置不能正确识别@别名指向的目录。在JavaScript模块中,通常使用@作为特定包的别名,例如@/stores/user可能是一个指向特定stores/user模块的别名。ESLint用于代码质量和代码风格的检查,当它不能正确解析这个别名时,就会报出“找不到模......
  • 3 React Redux
    一配置基础环境1、使用CRA快速创建React项目:npxcreate-react-appreact-redux2、安装配套工具:cdreact-redux,npmi@reduxjs/toolkitreact-redux3、启动项目:npmrunstart二store目录结构设计1、通常集中状态管理的部分会单独创建一个单独的store目录2、应用通......
  • Flask + React 框架 和 Streamlit 的比较
    使用Flask+React这种框架相对于Streamlit有以下优缺点:优点灵活性和扩展性:Flask+React:提供了高度的灵活性和扩展性。你可以根据需求使用各种前端和后端技术,构建复杂的应用程序,增加额外的功能模块,并进行微服务架构的扩展。Streamlit:主要用于快速构建数据应用,适用于较小规......
  • 使用RAG-GPT集成智谱AI、DeepSeek快速搭建OpenAI Cookbook智能客服
    使用RAG-GPT集成智谱AI、DeepSeek快速搭建OpenAICookbook智能客服引言前面介绍了使用RAG-GPT和OpenAI快速搭建LangChain官网智能客服,目前国内也有一些比较不错的云端大模型API服务。本文将介绍通过RAG-GPT集成智谱AI和DeepSeek,快速搭建OpenAICookbook智能客服。RAG技术原理介......
  • 【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的Python代码
    问题描述介绍一段Python脚本,可以在微软云中国区使用。用于计算AzureStorageAccount中Container中Blob类型文件的数量和大小,脚本中允许按照容器,层(热/冷/归档),前缀,软删除/非软删除来计算数量和容量大小,默认使用的时间为以Blob的最后修改时间作为参考。执行结果参考: 参数......
  • vite创建的react项目如何兼容低版本安卓,低版本安卓不支持es6语法
    Vite是一个现代化的前端构建工具,默认情况下,它会生成基于ES6+的代码。这对于大多数现代浏览器来说是没有问题的,但对于一些较旧版本的安卓浏览器可能会遇到兼容性问题。为了使Vite创建的React项目在低版本的安卓设备上也能正常运行,我们需要确保最终的构建产物是兼容旧版浏览......