首页 > 其他分享 >构建基于 LlamaIndex 的RAG AI Agent

构建基于 LlamaIndex 的RAG AI Agent

时间:2024-06-18 20:30:50浏览次数:11  
标签:engine RAG index AI guidelines Agent report import tools

I built a custom AI agent that thinks and then acts. I didn't invent it though, these agents are known as ReAct Agents and I'll show you how to build one yourself using LlamaIndex in this tutorial.

我构建了一个自定义的AI智能体,它能够思考然后行动。不过,这并不是我的发明,这类智能体被称为ReAct智能体。在本教程中,我将向你展示如何使用LlamaIndex来自己构建一个这样的智能体。

Hi folks! Today, I'm super excited to show you how you can build a Python app that takes the contents of a web page and generates an optimization report based on the latest Google guidelines—all in under 10 seconds! This is perfect for bloggers and content creators who want to ensure their content meets Google's standards.

嗨,大家好!今天,我非常激动地要告诉大家如何构建一个Python应用,这个应用能够在10秒内抓取一个网页的内容,并根据最新的Google指南生成一个优化报告!这非常适合博主和内容创作者,他们想要确保自己的内容符合Google的标准。

We'll use a LlamaIndex ReActAgent and three tools that will enable the AI agent to:

我们将使用LlamaIndex ReActAgent和三个工具,这些工具将使AI智能体能够:

  • Read the content of a blog post from a given URL. 从给定的URL读取博客文章的内容。
  • Process Google's content guidelines.   处理Google的内容指南。
  • Generate a PDF report based on the content and guidelines. 基于内容和指南生成PDF报告。

This is especially useful given the recent Google updates that have affected organic traffic for many bloggers. You may want to tweak this to suit your needs but in general, this should be a great starting point if you want to explore AI Agents.

鉴于最近Google的更新对许多博主的自然流量产生了影响,这一点特别有用。你可能想要根据自己的需求进行调整,但总的来说,如果你想要探索AI智能体,这将是一个很好的起点。

Ok, let's dive in and build this!        好的,让我们深入其中并开始构建吧!

Overview and scope        概述和范围

Here's what we'll cover:        以下是我们将要涵盖的内容:

  1. Architecture overview        架构概述
  2. Setting up the environment    设置环境
  3. Creating the tools    创建工具
  4. Writing the main application    编写主应用程序
  5. Running the application    运行应用程序

1. Architecture Overview        架构概述

"ReAct" refers to Reason and Action. A ReAct agent understands and generates language, performs reasoning, and executes actions based on that understanding and since LlamaIndex provides a nice and easy-to-use interface to create ReAct agents and tools, we'll use it and OpenAI's latest model GPT-4o to build our app.

“ReAct”指的是原因(Reason)和行动(Action)。一个ReAct代理理解并生成语言,基于这种理解进行推理,并执行相应的行动。由于LlamaIndex提供了一个友好且易于使用的界面来创建ReAct代理和工具,我们将使用它以及OpenAI的最新模型GPT-4o来构建我们的应用程序。

We'll create three simple tools:        我们将创建三个简单的工具:

  • The guidelines tool: Converts Google's guidelines to embeddings for reference.

指南工具:将谷歌的指南转换为嵌入式表示以供参考。

  • The web_reader tool: Reads the contents of a given web page.

web_reader工具:读取给定网页的内容。

  • The report_generator tool: Converts the model's response from markdown to a PDF report.

report_generator工具:将模型的响应从Markdown转换为PDF报告。

2. Setting up the environment        设置环境

Let's start by creating a new project directory. Inside of it, we'll set up a new environment:

首先,我们创建一个新的项目目录。在其中,我们将设置一个新的环境:

mkdir llamaindex-react-agent-demo
cd llamaindex-react-agent-demo
python3 -m venv venv
source venv/bin/activate

Next, install the necessary packages:        接下来,安装必要的包:

pip install llama-index llama-index-llms-openai
pip install llama-index-readers-web llama-index-readers-file
pip install python-dotenv pypandoc

To convert the contents to a PDF we'll use a third-party tool called pandoc. You can follow the steps as outlined here to set it up on your machine.

为了将内容转换为PDF,我们将使用一个名为pandoc的第三方工具。你可以按照这里概述的步骤在你的机器上设置它。

Finally, we'll create a .env file in the root directory and add our OpenAI API Key as follows:

最后,我们将在根目录中创建一个.env文件,并添加我们的OpenAI API密钥,如下所示:

OPENAI_API_KEY="PASTE_KEY_HERE"

3. Creating the Tools        创建工具

谷歌内容嵌入指南(Google Content Guidelines for Embeddings)

Navigate to any page in your browser. In this tutorial, I'm using this page. Once you're there, convert it to a PDF. In general, you can do this by clicking on "File -> Export as PDF..." or something similar depending on your browser.

在浏览器中导航到任何页面。在本教程中,我使用这个页面。到达后,将其转换为PDF。通常,你可以通过点击“文件 -> 导出为PDF...”或类似选项(取决于你的浏览器)来完成此操作。

Save Google's content guidelines as a PDF and place it in a data folder. Then, create a tools folder and add a guidelines.py file:

将谷歌的内容指南保存为PDF并放入一个数据文件夹中。然后,创建一个tools文件夹并在其中添加一个guidelines.py文件

import os 

from llama_index.core import StorageContext, VectorStoreIndex, load_index_from_storage
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.readers.file import PDFReader

...

After adding the required packages, we'll convert our PDF to embeddings and then create a VectorStoreIndex:

在添加必要的包之后,我们将把PDF转换为嵌入,然后创建一个VectorStoreIndex:

...

data = PDFReader().load_data(file=file_path)
index = VectorStoreIndex.from_documents(data, show_progress=False)

...

Then, we return a QueryEngineTool which can be used by the agent:

然后,我们返回一个QueryEngineTool,该工具可以被代理使用:

...

query_engine = index.as_query_engine()
    
guidelines_engine = QueryEngineTool(
    query_engine=query_engine,
    metadata=ToolMetadata(
        name="guidelines_engine",
        description="This tool can retrieve content from the guidelines"
    )
)

Web Page Reader        网页阅读器

Next, we'll write some code to give the agent the ability to read the contents of a webpage. Create a web_reader.py file in the tools folder:

接下来,我们将编写一些代码,以使代理能够读取网页的内容。在tools文件夹中创建一个web_reader.py文件:

# web_reader.py

from llama_index.core import SummaryIndex
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.readers.web import SimpleWebPageReader

...

url = "https://www.gettingstarted.ai/crewai-beginner-tutorial"

documents = SimpleWebPageReader(html_to_text=True).load_data([url])
index = SummaryIndex.from_documents(documents)

I'm using a SummaryIndex to process the documents, there are multiple other index types that you could decide to choose based on your data.

我使用了一个SummaryIndex来处理文档,你也可以根据你的数据选择多种其他索引类型。

I'm also using SimpleWebPageReader to pull the contents of URL. Alternatively, you could implement your own function, but we'll just use this data loader to keep things simple.

我也在使用SimpleWebPageReader来拉取URL的内容。另外,你也可以实现自己的函数,但为了保持简单,我们将只使用这个数据加载器。

Next, we'll build the QueryEngineTool object which will be provided to the agent just like we've done before:

接下来,我们将构建QueryEngineTool对象,并将其提供给代理,就像我们之前所做的那样:

query_engine = index.as_query_engine()

web_reader_engine = QueryEngineTool(
    query_engine=query_engine,
    metadata=ToolMetadata(
        name="web_reader_engine",
        description="This tool can retrieve content from a web page"
    )
)

Ok, cool. Now let's wrap up the tool and create our PDF report generator.

好的,很棒。现在让我们整合这个工具并创建我们的PDF报告生成器。

PDF Report Generator        PDF报告生成器

For this one, we'll use a FunctionTool instead of a QueryEngineTool since the agent won't be querying an index but rather executing a Python function to generate the report.

对于这一项,我们将使用FunctionTool而不是QueryEngineTool,因为代理不会查询索引,而是执行一个Python函数来生成报告。

Start by creating a report_generator.py file in the tools folder:

首先,在tools文件夹中创建一个report_generator.py文件:

# report_generator.py

...

import tempfile
import pypandoc

from llama_index.core.tools import FunctionTool

def generate_report(md_text, output_file):
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as temp_md:
        temp_md.write(md_text.encode("utf-8"))
        temp_md_path = temp_md.name
        
    try:
        output = pypandoc.convert_file(temp_md_path, "pdf", outputfile=output_file)
        return "Success"
    
    finally:
        os.remove(temp_md_path)
        
report_generator = FunctionTool.from_defaults(
    fn=generate_report,
    name="report_generator",
    description="This tool can generate a PDF report from markdown text"
)

4. Writing the Main Application        编写主应用程序

Awesome! All good. Now we'll put everything together in a main.py file:

太棒了!一切都很好。现在我们将把所有内容整合到一个main.py文件中:

# main.py

...

# llama_index
from llama_index.llms.openai import OpenAI
from llama_index.core.agent import ReActAgent

# tools
from tools.guidelines import guidelines_engine
from tools.web_reader import web_reader_engine
from tools.report_generator import report_generator

llm = OpenAI(model="gpt-4o")

agent = ReActAgent.from_tools(
    tools=[
        guidelines_engine, # <---
        web_reader_engine, # <---
        report_generator # <---
        ],
    llm=llm,
    verbose=True
)

...

As you can see, we start by importing the required packages and our tools, then we'll use the ReActAgent class to create our agent.

如你所见,我们首先导入所需的包和我们的工具,然后我们将使用ReActAgent类来创建我们的代理。

To create a simple chat loop, we'll write the following code and then run the app:

为了创建一个简单的聊天循环,我们将编写以下代码,然后运行应用程序:

...

while True:
    user_input = input("You: ")
    if user_input.lower() == "quit":
        break
    response = agent.chat(user_input)
    print("Agent: ", response)

5. Running the Application        运行应用程序

It's showtime! Let's run the application from the terminal:

是时候展示了!让我们从终端运行应用程序:

python main.py

Feel free to use the following prompt, or customize it as you see fit:

请随意使用以下提示,或根据需要进行自定义:

"Based on the given web page, develop actionable tips including how to rewrite some of the content to optimize it in a way that is more aligned with the content guidelines. You must explain in a table why each suggestion improves content based on the guidelines, then create a report."

“基于给定的网页,开发可操作的建议,包括如何重写部分内容以优化内容,使其更符合内容指南。你必须在表格中解释为什么每个建议都能根据指南改进内容,然后创建一份报告。”

The agent will process the request, and call upon the tools as needed to generate a PDF report with actionable tips and explanations.

代理将处理请求,并在需要时调用这些工具来生成包含可操作建议和解释的PDF报告。

The whole process will look something like this:        整个过程将如下所示:

You can clearly see how the agent is reasoning and thinking about the task at hand and then devising a plan on how to execute it. With the assistance of the tools that we've created, we can give it extra capabilities, like generating a PDF report.

你可以清楚地看到代理是如何对当前任务进行推理和思考的,然后制定一个执行计划。借助我们创建的工具,我们可以赋予它额外的功能,比如生成PDF报告。

Here's the final PDF report:        这是最终的PDF报告:

Conclusion        总结

And that's it! You've built a smart AI agent that can optimize your blog content based on Google's guidelines. This tool can save you a lot of time and ensure your content is always up to standard.

就这样!你已经构建了一个智能的AI代理,可以根据Google的指南优化你的博客内容。这个工具可以为你节省大量时间,并确保你的内容始终符合标准。

附录:相关程序代码

main.py

# main.py

import os
from dotenv import load_dotenv

load_dotenv()

# llama_index
from llama_index.llms.openai import OpenAI
from llama_index.core.agent import ReActAgent

# tools
from tools.guidelines import guidelines_engine
from tools.web_reader import web_reader_engine
from tools.report_generator import report_generator
from tools.web_loader import web_loader

llm = OpenAI(model="gpt-4o")

agent = ReActAgent.from_tools(
    tools=[
        guidelines_engine,
        web_reader_engine,
        report_generator
        ],
    llm=llm,
    verbose=True
)

while True:
    user_input = input("You: ")
    if user_input.lower() == "quit":
        break
    response = agent.chat(user_input)
    print("Agent: ", response)

tools/guidelines.py

# guidelines.py

import os
from llama_index.core import StorageContext, VectorStoreIndex, load_index_from_storage
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.readers.file import PDFReader

def load_index(file_path, index_name):
    data = PDFReader().load_data(file=file_path)
    
    if os.path.exists('embeddings/' + index_name):
        index = load_index_from_storage(
            StorageContext.from_defaults(persist_dir='embeddings/' + index_name)
        )
    else:
        index = VectorStoreIndex.from_documents(data, show_progress=False)
        index.storage_context.persist(persist_dir='embeddings/' + index_name)
        
    return index

def asQueryEngineTool(index):
    query_engine = index.as_query_engine()
    
    return QueryEngineTool(
        query_engine=query_engine,
        metadata=ToolMetadata(
            name="guidelines_engine",
            description="This tool can retrieve content from the guidelines"
        )
    )
    
file_path = os.path.join("data", "content-guidelines.pdf")
guidelines_index = load_index(file_path, index_name="guidelines")

guidelines_engine = asQueryEngineTool(guidelines_index)

tools/web_reader.py

from llama_index.core import SummaryIndex
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.readers.web import SimpleWebPageReader

def load_index(url: str):
    documents = SimpleWebPageReader(html_to_text=True).load_data([url])
    index = SummaryIndex.from_documents(documents)
    
    return index

def asQueryEngineTool(index):
    
    query_engine = index.as_query_engine()
    
    return QueryEngineTool(
        query_engine=query_engine,
        metadata=ToolMetadata(
            name="web_reader_engine",
            description="This tool can retrieve content from a web page"
        )
    )
    
index = load_index(
    url="https://www.gettingstarted.ai/crewai-beginner-tutorial"
)

web_reader_engine = asQueryEngineTool(index)

web_reader_engine = QueryEngineTool(
    query_engine=query_engine,
    metadata=ToolMetadata(
        name="web_reader_engine",
        description="This tool can retrieve content from a web page"
    )
)

tools/report_generator.py

import os
import tempfile
import pypandoc

from llama_index.core.tools import FunctionTool

def generate_report(md_text, output_file):
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as temp_md:
        temp_md.write(md_text.encode("utf-8"))
        temp_md_path = temp_md.name
        
    try:
        output = pypandoc.convert_file(temp_md_path, "pdf", outputfile=output_file)
        return "Success"
    
    finally:
        os.remove(temp_md_path)
        
report_generator = FunctionTool.from_defaults(
    fn=generate_report,
    name="report_generator",
    description="This tool can generate a PDF report from markdown text"
)

标签:engine,RAG,index,AI,guidelines,Agent,report,import,tools
From: https://blog.csdn.net/suiusoar/article/details/139779123

相关文章

  • 5个好用的AI绘画软件推荐,小白也能轻松上手
    前言随着人工智能技术的飞速发展,AI绘画软件已经成为艺术创作领域的新宠。这些软件不仅能够提供强大的绘画辅助功能,而且操作简便,即使是绘画新手也能轻松上手。本文将为您推荐5款好用的AI绘画软件,帮助您开启艺术创作的大门。1.StartAIstartAI是一款PSAI插件,仅需简单输入几个关......
  • Stable Diffusion 3 大模型文生图“开源英雄”笔记本部署和使用教程,轻松实现AI绘图自
    备受期待的StableDiffusion3(以下亦简称SD3)如期向公众开源了(StableDiffusion3Medium),作为StabilityAI迄今为止最先进的文本生成图像的开源大模型,SD3在图像质量、文本内容生成、复杂提示理解和资源效率方面有了显著提升,被誉为AI文生图领域的开源英雄。StableDiffusion3Medi......
  • 论文阅读:T-RAG: LESSONS FROM THE LLM TRENCHES
    T-RAG:LESSONSFROMTHELLMTRENCHES(https://arxiv.org/abs/2402.07483)https://github.com/jiangnanboy/paper_read_note一.概述大型语言模型(llm)越来越多地应用于各个领域,包括对私有企业文档的问答,其中数据安全性和鲁棒性至关重要。检索增强生成(retrieve-augmented......
  • 论文阅读:UniMS-RAG: Unified Multi-Source RAG for Personalised Dialogue
    UniMS-RAG:UnifiedMulti-SourceRAGforPersonalisedDialogue(https://arxiv.org/abs/2401.13256)https://github.com/jiangnanboy/paper_read_note一.概述本研究探讨如何分解RAG过程,加入多文件检索、记忆和个人信息等元素。大型语言模型(llm)在自然语言任务中表现出色,但......
  • python系列&AI系列:cannot import name ‘ForkProcess‘ from ‘multiprocessing.conte
    cannotimportname‘ForkProcess‘from‘multiprocessing.context‘问题解决cannotimportname‘ForkProcess‘from‘multiprocessing.context‘问题解决问题描述问题原因解决方案cannotimportname‘ForkProcess‘from‘multiprocessing.context‘问......
  • Sora不香了,Runway Gen-3震撼发布!AI电影时代真的要来了!(附与快手可灵对比测试)
    文章首发于公众号:X小鹿AI副业大家好,我是程序员X小鹿,前互联网大厂程序员,自由职业2年+,也一名AIGC爱好者,持续分享更多前沿的「AI工具」和「AI副业玩法」,欢迎一起交流~AI视频太卷了!刚上线的快手可灵(Kling)、LumaAI的DreamMachine的热乎劲还没过,Runway又发布重磅消......
  • 【文末附gpt升级秘笈】SDCon 2024全球软件研发技术大会:引领AI 2.0时代的软件开发新篇
    SDCon2024全球软件研发技术大会:引领AI2.0时代的软件开发新篇章一、引言随着人工智能技术的飞速发展,我们迎来了AI2.0时代。在这个时代,人工智能技术不仅深刻影响着我们的日常生活,更在软件研发领域掀起了一场革命。AI原生应用的出现,使得每行代码、每个应用都有可能迎来被智能......
  • 高效、智能、安全:小型机房EasyCVR+AI视频综合监控解决方案
    一、背景需求分析随着信息技术的迅猛发展,小型机房在企事业单位中扮演着越来越重要的角色。为了确保机房的安全稳定运行,远程监控成为了必不可少的手段。二、视频监控视频监控是机房远程监控的重要组成部分。通过安装IP摄像机及部署视频监控系统EasyCVR平台,可以实现机房的实时监......
  • AI从云端到边缘:人员入侵检测算法的技术原理和视频监控方案应用
    在当今数字化、智能化的时代,安全已成为社会发展的重要基石。特别是在一些关键领域,如公共安全、智能化监管以及智慧园区/社区管理等,确保安全无虞至关重要。而人员入侵检测AI算法作为一种先进的安全技术,正逐渐在这些领域发挥着不可替代的作用。传统的人工监控方式往往难以做到全天......
  • 【ai】如何在ollama中随意使用hugging face上的gguf开源模型
    【背景】ollama的pull命令可以直接pullollama列表中现有的模型,但是ollama可以直接pull的模型大都是英语偏好(llama2有直接可以pull的chinese版本),而huggingface上则有大量多语种训练的模型,如果能直接使用huggingface上的gguf开源模型,那就自由多了,本篇介绍方法。【命令】......