百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

实施RAG的25种技术和方法(2/5) rag方法和agent方法区别

itomcoil 2024-12-28 13:36 28 浏览

方法 6:Self-RAG

想象一下,一个人工智能就像一个拥有强大记忆力的聪明研究员。标准 RAG 系统就像需要在单独的书籍或数据库中查找信息的助手。自我检索增强生成 (Self-RAG) 则有所不同。

将 Self-RAG 视为一位天才研究员,他可以:

  • 立即访问大量信息
  • 准确了解需要哪些细节
  • 快速找到最相关的信息
  • 创建的回复不仅准确,而且经过深思熟虑
  • 不断检查和改进自身工作

与依赖单独搜索工具的传统 AI 系统不同,Self-RAG 将搜索和回答结合为一个智能流程。这就像拥有一位专家,他不仅从多个来源阅读信息,而且还会实时主动思考、检索和综合信息。

这种方法改变了人工智能理解和回答问题的方式,使其更具适应性、更精确、更智能。Self-RAG 不仅仅是寻找信息,它还涉及以最有意义的方式理解、分析和呈现知识。

什么是 Self-RAG?

想象一下,如果人工智能像一位聪明的研究人员一样工作,它可以:

  • 问自己需要什么信息
  • 自行查找该信息
  • 检查并改进自己的答案

传统的人工智能系统有单独的部分来查找信息和回答问题。Self-RAG 将这些部分组合成一个智能系统。

Self-RAG 的工作原理

  1. 理解问题人工智能首先仔细研究问题并弄清楚它需要找到什么信息。
  2. 查找信息人工智能不使用单独的搜索工具,而是通过自己的知识库进行查找。这就像拥有一位内置图书管理员,他确切地知道在哪里可以找到合适的书。
  3. 创建答案人工智能不只是复制信息。它利用找到的细节来创建深思熟虑、准确的回答。
  4. 然后,人工智能会检查自己的答案,确保其合理且有用。

主要特点

  • 人工智能既是研究员,又是作家
  • 它可以挖掘自己的信息“库”
  • 它像一个细心的学生一样检查自己的工作
  • 无需单独的搜索和书写系统

简单示例如果你问“告诉我有关气候变化的信息”,人工智能会:

  • 理解复杂主题
  • 决定需要哪些具体信息
  • 搜索自己的知识库
  • 制定详细、准确的回复
  • 再次检查答案是否正确

Self-RAG 管道:分步实施

import os
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_groq import ChatGroq
from langchain.schema import Document
from langchain.text_splitter import CharacterTextSplitter

# Set OpenAI API Key
os.environ["GROQ_API_KEY"] = "gsk_???"

# Prompt Template for Self-RAG
SELF_RAG_PROMPT = """You are an expert assistant capable of self-retrieval. 
Use the following context to retrieve the most relevant information to answer the user's query.

Context:
{context}

Query:
{query}

Response:"""

def process_files(files):
    """
    Process text files and split them into document chunks.
    
    :param files: List of file paths
    :return: List of Document objects
    """
    documents = []
    text_splitter = CharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200)
    
    for file_path in files:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
            chunks = text_splitter.split_text(content)
            for chunk in chunks:
                documents.append(Document(page_content=chunk))
    
    return documents

def perform_self_rag(query, knowledge_base, 
                            model_name = 'llama-3.3-70b-versatile', 
                            temperature = 0.1):
    """
    Perform Self-Retrieval-Augmented Generation.
    
    :param query: User's input query
    :param knowledge_base: List of Document objects
    :param model_name: llama model to use
    :param temperature: Creativity/randomness of the response
    :return: Generated response
    """
    # Combine chunks into a single context
    context = "\n\n".join([doc.page_content for doc in knowledge_base])
    
    # Create LLM Chain
    llm = Chatgroq(model_name = model_name, temperature = temperature)
    
    prompt = PromptTemplate(
        input_variables = ["context", "query"],
        template = SELF_RAG_PROMPT
    )
    
    llm_chain = LLMChain(llm = llm, prompt = prompt)
    
    # Run query and get response
    response = llm_chain.run(context = context, query = query)
    
    return response

def main():
    # 1. Specify your paths to your knowledge base files
    knowledge_base_files = [
        'path/to/test1.txt',
        'path/to/test2.txt'
    ]
    
    # 2. Process the files
    knowledge_base = process_files(knowledge_base_files)
    
    # 3. Ask a query
    query = "What is the main topic of these documents?"
    
    # 4. Perform Self-RAG
    response = perform_self_rag(query, knowledge_base)
    
    # 5. Print the response
    print("Query:", query)
    print("Response:", response)

if __name__ == "__main__":
    main()

运行上述代码后,我们得到以下响应:

Query: What is the main topic of these documents?
Response: The main topic of these documents is India, with a focus on its cultural heritage, diversity, and achievements, as well as a specific aspect of Hindu mythology, namely the figure of Hanuman and his significance in Indian culture and tradition.

方法 7:Adaptive RAG

Adaptive RAG 是一种更智能的方法,它通过查找和使用正确的信息来帮助 AI 系统回答问题。想象一下它就像一个灵活的研究助理,可以根据任务改变其方法。

在传统系统中,获取答案的方式总是相同的:找到一些文档,然后创建答案。但Adaptive RAG 更智能。它可以:

  • 决定需要多少研究
  • 选择查找少量或大量信息
  • 根据问题的复杂程度实时调整策略

例如,像“法国首都是哪里?”这样的简单问题可能只需要快速查找。但关于历史事件的复杂问题可能需要人工智能进行多轮研究,仔细检查不同的来源。

关键区别在于灵活性。Adaptive RAG 无需每次都遵循相同的严格步骤,而是可以:

  • 进行一次搜索
  • 进行多次搜索
  • 有时如果已经知道足够多的信息,就根本不搜索

想象一下,它就像一个聪明的图书管理员,他不会只是机械地取书,而是确切地知道每个独特问题需要什么样的研究。有时你想要一本书,有时你想探索多个书架,有时你已经知道足够多的信息可以立即回答。

这种方法有助于提高 RAG 系统效率、准确性并针对所提出的具体问题进行定制。

关键组件

1. 聪明的检索器

  • 灵活的文档搜索:使用先进的技术查找相关信息
  • Adaptive搜索:可以在不同的搜索方法之间切换
  • 动态微调:根据查询实时调整搜索策略

2. 智能生成器

  • 灵活的语言模型:使用 GPT 等先进的 AI 模型
  • 自适应响应生成:可以改变其创建答案的方式
  • 情境感知响应:根据具体问题定制答案风格

3. 反馈机制

  • 持续学习:基于以下方面提高绩效:
  • 用户反馈
  • 模型性能
  • 外部评估

先进技术:使用强化学习等方法来提高准确性

4.动态文档管理

  • 实时索引:持续更新文档集合
  • 相关性跟踪:确保信息保持最新和有用

5.混合搜索方法

  • 多种搜索方法:结合不同的检索技术
  • 上下文优先级:为每个查询选择最合适的搜索方法

6. 智能评分

  • 上下文相关性:根据文档的具体实用性对其进行排序
  • 自适应优先级:重点关注最相关的信息

主要优势

  1. 灵活性:适应不同类型的查询
  2. 效率:减少不必要的信息收集
  3. 准确性:持续提高响应质量
  4. 情境理解:提供更有针对性和更相关的答案

Adaptive RAG 管道:分步实施

import os
from typing import List, Dict, Any

# Import necessary libraries for RAG implementation
from langchain.vectorstores import FAISS
from langchain_groq import ChatGroq
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.docstore.document import Document
from langchain.embeddings import HuggingFaceEmbeddings


class AdaptiveRAGSystem:
    def __init__(self, api_key: str, document_path: str):
        """
        Initialize the Adaptive RAG System
        
        Args:
            api_key (str): Groq API key
            document_path (str): Path to the source documents
        """
        # Set API Key
        os.environ["GROQ_API_KEY"] = api_key
        
        # Document loading and preprocessing
        self.docs = self._load_and_preprocess_documents(document_path)
        
        # Initialize retrievers
        self.dense_retriever = self._create_dense_retriever()
        self.sparse_retriever = self._create_sparse_retriever()
        
        # Initialize language model
        self.llm = ChatGroq(model = "llama-3.3-70b-specdec", temperature = 0.1)

    def _load_and_preprocess_documents(self, document_path: str) -> List[Document]:
        """
        Load and split documents into manageable chunks
        
        Args:
            document_path (str): Path to source documents
        
        Returns:
            List of preprocessed document chunks
        """
        docs = TextLoader(document_path).load()
        splitter = CharacterTextSplitter(chunk_size = 1000, chunk_overlap = 200)
        return splitter.split_documents(docs)

    def _create_dense_retriever(self):
        """
        Create a dense vector retriever using embeddings
        
        Returns:
            Dense vector store retriever
        """
        dense_embeddings = HuggingFaceEmbeddings(model_name = "all-MiniLM-L6-v2")
        dense_vector_store = FAISS.from_documents(self.docs, dense_embeddings)
        return dense_vector_store.as_retriever()

    def _create_sparse_retriever(self):
        """
        Create a sparse vector retriever for keyword-based retrieval
        
        Returns:
            Sparse vector store retriever
        """
        dense_embeddings = HuggingFaceEmbeddings(model_name = "all-MiniLM-L6-v2")
        sparse_vector_store = FAISS.from_texts(
            [doc.page_content for doc in self.docs], 
            dense_embeddings
        )
        return sparse_vector_store.as_retriever()

    def adaptive_retrieval(self, query: str):
        """
        Adaptively choose retrieval method based on query complexity
        
        Args:
            query (str): User's input query
        
        Returns:
            Selected retriever based on query complexity
        """
        # Example heuristic: Use dense retriever for complex queries
        if len(query.split()) > 5:
            print("Using Dense Retriever")
            return self.dense_retriever
        else:
            print("Using Sparse Retriever")
            return self.sparse_retriever

    def process_query(self, query: str) -> Dict[str, Any]:
        """
        Process user query using adaptive retrieval
        
        Args:
            query (str): User's input query
        
        Returns:
            Dictionary containing answer and source documents
        """
        # Select appropriate retriever
        selected_retriever = self.adaptive_retrieval(query)
        
        # Create QA Chain
        qa_chain = RetrievalQA.from_chain_type(
            llm=self.llm, 
            retriever=selected_retriever, 
            chain_type="stuff", 
            return_source_documents=True
        )
        
        # Get response
        response = qa_chain.invoke({"query": query})
        
        return {
            "answer": response["result"],
            "source_documents": response["source_documents"]
        }

# Example Usage
def main():
    # Initialize the Adaptive RAG System
    rag_system = AdaptiveRAGSystem(
        api_key="gsk_", 
        document_path="/content/test1.txt"
    )
    
    # Example queries
    queries = [
        "What is discussed in the document?",
        "Tell me about the INDIA"
    ]
    
    for query in queries:
        print(f"\nQuery: {query}")
        result = rag_system.process_query(query)
        
        print("Answer:", result["answer"])
        print("\nSource Documents:")
        for doc in result["source_documents"]:
            print(doc.page_content[:200] + "...")

if __name__ == "__main__":
    main()

执行上述代码后我们得到了以下响应:

Query: What is discussed in the document?
Using Dense Retriever
Answer: The document discusses India, its geography, cultural diversity, history, economy, and its progress in various fields, highlighting its unique blend of traditional and modern aspects.

Source Documents:
India, a vibrant and diverse nation in South Asia, is known for its rich history, cultural heritage, and remarkable progress in various fields. Spanning over 3.2 million square kilometers, it is the w...

Query: Tell me about the INDIA
Using Sparse Retriever
Answer: India is a vibrant and diverse nation located in South Asia. Here are some key facts about India:

1. **Geography**: India is the world's seventh-largest country, covering an area of over 3.2 million square kilometers.
2. **Population**: With over 1.4 billion people, India is the second-most populous country in the world.
3. **Language**: India has 22 recognized regional languages, with Hindi and English serving as official languages.
4. **Culture**: India is known for its rich cultural heritage, with a diverse tapestry of languages, religions, and traditions.
5. **History**: India is home to ancient civilizations and boasts iconic landmarks like the Taj Mahal, Jaipur's palaces, and Varanasi's ghats.
6. **Economy**: India is a powerhouse in various fields, including:
 * Technology
 * Pharmaceuticals
 * Agriculture
 * Space exploration
 * Renewable energy
7. **Character**: Despite its challenges, India is a beacon of resilience and innovation, blending ancient traditions with modern advancements.

These are just a few aspects of India, but there's much more to explore and discover about this incredible country!

Source Documents:
India, a vibrant and diverse nation in South Asia, is known for its rich history, cultural heritage, and remarkable progress in various fields. Spanning over 3.2 million square kilometers, it is the w...

方法 8:REFEED RAG

REFEED(检索反馈)通过整合基于检索的反馈来改善 LLM 的输出,从而消除了重新训练的需要。这种方法通过使用相关的外部信息来增强初始反应,从而解决了幻觉和事实不准确等挑战。

它是如何工作的?

  • 初始生成:LLM 对用户的查询产生初步响应。
  • 检索:使用原始查询和初始响应,系统从维基百科等大型数据集中检索相关文档。
  • 反馈集成:检索到的信息被纳入模型的上下文中,使其能够完善其初始输出。
  • 细化:模型生成修订的响应,并通过从检索到的文档中的附加内容进行丰富。

主要优点

  • 提高检索准确性:生成多个答案选项并根据可靠性进行排序,从而获得更准确、更高质量的答复。
  • 成本效益:无需昂贵的再训练即可增强模型输出。
  • 灵活性:作为即插即用模块,可轻松与现有的 LLM 集成。

它在哪里有用?

  • 事实核查生成的内容
  • 在法律和医学等复杂领域提供可靠的信息
  • 改善客户支持响应

挑战

  • 查找完全相关的文档
  • 顺利整合新信息
  • 保持快速响应时间

REFEED RAG 管道:分步实施

import os
import torch
import faiss
import numpy as np
from typing import List, Dict, Any
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_groq import ChatGroq
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from sklearn.metrics.pairwise import cosine_similarity

class REFEEDRAG:
    def __init__(
        self, 
        groq_api_key: str,
        embedding_model: str = 'sentence-transformers/all-MiniLM-L6-v2',
        model_name: str = "llama-3.3-70b-specdec",
        retrieval_top_k: int = 5,
        recursive_depth: int = 3,
        similarity_threshold: float = 0.7,
        temperature: float = 0.0
    ):
        """
        Initialize REFEED RAG with Groq integration
        
        Args:
            groq_api_key (str): API key for Groq
            embedding_model (str): Embedding model for vector representation
            model_name (str): llama model to use
            retrieval_top_k (int): Number of top documents to retrieve
            recursive_depth (int): Depth of recursive feedback
            similarity_threshold (float): Similarity cutoff for document relevance
            temperature (float): Sampling temperature for response generation
        """
        # Set Groq API Key
        os.environ["GROQ_API_KEY"] = groq_api_key
        
        # Initialize Groq Language Model
        self.llm = ChatGroq(
            model = model_name, 
            temperature = temperature
        )
        
        # Embedding and retrieval configuration
        self.embedding_model = HuggingFaceEmbeddings(model_name = embedding_model)
        self.retrieval_top_k = retrieval_top_k
        self.recursive_depth = recursive_depth
        self.similarity_threshold = similarity_threshold
        
        # Initialize Faiss index
        self.faiss_index = None
        self.document_store = []
        
        # Create context-aware prompt template
        self.prompt_template = PromptTemplate(
            input_variables = ["context", "query"],
            template = """Use the following context to provide a comprehensive and precise answer to the query:

Context:
{context}

Query: {query}

Answer the query directly and comprehensively, using only the information from the provided context. If the context does not contain sufficient information, state that clearly."""
        )
        
        # Create LLM Chain
        self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
    
    def prepare_documents(self, documents: List[str], chunk_size: int = 512) -> List[str]:
        """
        Split documents into chunks for better indexing and retrieval
        
        Args:
            documents (List[str]): Original documents
            chunk_size (int): Size of text chunks
        
        Returns:
            List[str]: Processed document chunks
        """
        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size = chunk_size,
            chunk_overlap = 50
        )
        
        chunks = []
        for doc in documents:
            chunks.extend(text_splitter.split_text(doc))
        
        return chunks
    
    def create_vector_index(self, chunks: List[str]):
        """
        Create Faiss vector index for efficient similarity search
        
        Args:
            chunks (List[str]): Document chunks
        """
        # Embed chunks
        embeddings = np.array([
            self.embedding_model.embed_query(chunk) 
            for chunk in chunks
        ])
        
        # Create Faiss index
        dimension = embeddings.shape[1]
        index = faiss.IndexFlatL2(dimension)
        index.add(embeddings)
        
        self.faiss_index = index
        self.document_store = chunks
    
    def recursive_retrieval(self, query: str) -> List[str]:
        """
        Perform recursive document retrieval with feedback mechanism
        
        Args:
            query (str): Input query
        
        Returns:
            List[str]: Retrieved and refined documents
        """
        query_embedding = np.array(self.embedding_model.embed_query(query)).reshape(1, -1)
        
        retrieved_docs = []
        visited_indices = set()
        
        for _ in range(self.recursive_depth):
            # Perform similarity search
            distances, indices = self.faiss_index.search(query_embedding, self.retrieval_top_k)
            
            # Filter and aggregate documents
            for idx, (dist, doc_idx) in enumerate(zip(distances[0], indices[0])):
                if doc_idx not in visited_indices and dist < self.similarity_threshold:
                    retrieved_docs.append(self.document_store[doc_idx])
                    visited_indices.add(doc_idx)
                    
                    # Recursive feedback: use retrieved doc as auxiliary query
                    aux_query_embedding = np.array(
                        self.embedding_model.embed_query(self.document_store[doc_idx])
                    ).reshape(1, -1)
                    query_embedding = (query_embedding + aux_query_embedding) / 2
        
        return retrieved_docs
    
    def generate_response(self, query: str) -> str:
        """
        Generate response using retrieved documents and Groq LLM
        
        Args:
            query (str): Input query
        
        Returns:
            str: Generated response
        """
        # Retrieve documents
        retrieved_docs = self.recursive_retrieval(query)
        
        # Combine retrieved docs as context
        context = " ".join(retrieved_docs)
        
        # Generate response using LLM Chain
        response = self.chain.run(
            context = context, 
            query = query
        )
        
        return response
    
    def process_query(self, documents: List[str], query: str) -> str:
        """
        Complete workflow: prepare documents, index, and generate response
        
        Args:
            documents (List[str]): Source documents
            query (str): Input query
        
        Returns:
            str: Final generated response
        """
        # Extract text content if documents are Document objects
        if hasattr(documents[0], 'page_content'):
            documents = [doc.page_content for doc in documents]
        
        # Prepare and index documents
        chunks = self.prepare_documents(documents)
        self.create_vector_index(chunks)
        
        # Generate response
        response = self.generate_response(query)
        return response

# Example Usage
def main():
    # Replace with your actual Groq API Key
    GROQ_API_KEY = "gsk_"
    
    data = TextLoader("/content/test1.txt")
    load = data.load()
    
    query = "What is machine learning and how does it relate to artificial intelligence?"
    
    refeed_rag = REFEEDRAG(
        groq_api_key = GROQ_API_KEY,
        model_name = "llama-3.3-70b-specdec",
        temperature = 0.1
    )
    
    response = refeed_rag.process_query(load, query)
    print("Response:", response)

if __name__ == "__main__":
    main()

执行上述代码后我们得到了以下响应:

Response: Machine Learning (ML) is a subset of artificial intelligence that focuses on developing systems capable of learning and improving from experience without being explicitly programmed. This indicates a direct relationship between ML and artificial intelligence, where ML is a specific part of the broader field of artificial intelligence, utilizing statistical techniques and algorithms to enable computers to analyze data, identify patterns, and make predictions or decisions.

方法 9:使用检索增强生成 (RAG) 进行 ReAct 推理和行动

ReAct 是一种创新方法,它通过将推理与可操作步骤无缝结合,改变了大型语言模型 (LLM) 解决复杂问题的方式。通过在认知处理和任务执行之间建立动态交互,ReAct 使 AI 模型能够更有效、更智能地应对复杂挑战。

关键操作原则该方法围绕推理和行动的同步舞蹈展开,其中模型生成逻辑思维序列和相应的特定任务干预。这种方法允许人工智能系统:

  • 动态制定和完善行动计划
  • 适应意外情况
  • 与外部信息源进行有意义的互动

Fundamental Mechanics ReAct 使模型能够:

  • 生成逐步的推理轨迹,阐明他们的决策过程
  • 执行有针对性的行动,收集更多背景信息或解决特定的子任务
  • 创建透明、可解释的问题解决轨迹,揭示模型的底层逻辑

独特优势 与将推理与行动分开的传统方法不同,ReAct 创建了一个更全面的问题解决框架。这种整合提供了:

  • 更细致入微、更复杂的决策能力
  • 提高人工智能推理的透明度
  • 增强对复杂变化环境的适应能力

实际实施 成功部署 ReAct 需要:

  • 精心设计的提示,鼓励结构化推理
  • 用于执行特定操作的强大接口
  • 支持连贯逻辑进展的框架

潜在应用 ReAct 可以彻底改变各个领域,包括:

  • 自动化规划系统
  • 智能虚拟助手
  • 高级信息检索和复杂查询解析

固有挑战 尽管 ReAct 前景光明,但它仍面临一些实施障碍:

  • 开发能够持续引出有意义的推理行动序列的提示
  • 确保生成动作的准确性和情境适当性
  • 管理同时推理和执行的计算需求

ReAct 通过无缝集成推理和行动能力,标志着语言建模领域的重大突破。

ReAct RAG 管道:分步实施

import os
from langchain.vectorstores import Chroma
from langchain.docstore.document import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_groq import ChatGroq

from langchain.chains import RetrievalQA
from langchain.agents import create_react_agent
from langchain.agents import AgentExecutor
from langchain import hub

from langchain.agents import AgentType
from langchain_community.document_loaders import PyPDFLoader

# Set up API key
os.environ["GROQ_API_KEY"] = "gsk_"

def load_pdf_documents(pdf_file):
    """
    Load and split PDF documents into chunks
    """
    # Read PDF

    # pdf_reader = PdfReader(pdf_file)
    loader = PyPDFLoader(pdf_file)
    documents = loader.load()

    # Split text into chunks
    text_splitter = RecursiveCharacterTextSplitter(
        chunk_size = 1000,
        chunk_overlap = 200,
        length_function = len
    )
    texts = text_splitter.split_text(documents)

    # Create documents
    documents = [Document(page_content=t) for t in texts]

    return documents

# Load and embed documents
try:
    # Try to load existing vectorstore
    vectorstore = Chroma(
        persist_directory = "./storage/GOOGLE",
        embedding_function = HuggingFaceEmbeddings(model_name = "all-MiniLM-L6-v2")
    )
    index_loaded = True
except:
    # If no existing vectorstore, create new one
    documents = load_pdf_documents("/content/goog-10-k-2023_removed.pdf")

    # Create embeddings
    # embeddings = OpenAIEmbeddings()
    embeddings = HuggingFaceEmbeddings(model_name = "all-MiniLM-L6-v2")

    # Create vectorstore
    vectorstore = Chroma.from_documents(
        documents,
        embeddings,
        persist_directory="./storage/GOOGLE"
    )
    index_loaded = False

# Initialize language model
llm = ChatGroq(
    model = "llama-3.3-70b-specdec",
    temperature = 0.1,
    streaming = True
)

# Create retrieval QA chain
retrieval_qa = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type = "stuff",
    retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
)

# Fetch the prompt from hub (or create a custom one)
prompt = hub.pull("hwchase17/react")

# Create tools for the agent
tools = [
    Tool(
        name = "Alphabet Inc Document QA",
        func = retrieval_qa.run,
        description = (
            "Provides information about Alphabet Inc.'s Annual Report (Form 10-K) "
            "for the fiscal year ending December 31, 2023. Includes financial statements, "
            "management's discussion and analysis, business overview, risk factors, and other "
            "SEC-required information."
        )
    )
]

# Create the agent
agent = create_react_agent(
    llm = llm, 
    tools = tools, 
    prompt = prompt
)

# Create the agent executor
agent_executor = AgentExecutor(
    agent = agent, 
    tools = tools, 
    verbose = True,
    max_iterations = 5,
    handle_parsing_errors = True
)

# Run the query
response = agent_executor.invoke({
    "input": "What are the main drivers behind Alphabet Inc.'s investment in AI and how do these align with their long-term business strategy?"
})

print(response['output'])

执行上述代码后我们得到了以下响应:

> Entering new AgentExecutor chain...
To answer this question, I need to understand the main drivers behind Alphabet Inc.'s investment in AI and how these align with their long-term business strategy. This information is likely to be found in the company's annual report, specifically in the sections related to business overview, management's discussion and analysis, and risk factors.

Action: Alphabet Inc. Document QA
Action Input: What are the main drivers behind Alphabet Inc.'s investment in AI and how do these align with their long-term business strategy?Alphabet Inc., the parent company of Google, has been heavily investing in Artificial Intelligence (AI) in recent years. The main drivers behind this investment are:

1. **Improving search and advertising capabilities**: AI can help Google improve its search results, making them more accurate and relevant to users. This, in turn, can lead to increased advertising revenue, which is a significant portion of Alphabet's income.
2. **Enhancing user experience**: AI-powered technologies, such as natural language processing and computer vision, can be used to develop more intuitive and personalized user interfaces, improving the overall user experience across Google's products and services.
3. **Expanding into new markets**: AI has the potential to enable new products and services, such as autonomous vehicles (Waymo), smart home devices (Nest), and healthcare technologies (Verily). These investments can help Alphabet expand into new markets and diversify its revenue streams.
4. **Competing with other tech giants**: The AI landscape is highly competitive, with other tech giants like Amazon, Microsoft, and Facebook also investing heavily in AI research and development. Alphabet's investment in AI is, in part, a response to this competition, as the company seeks to maintain its leadership position in the tech industry.

These drivers align with Alphabet's long-term business strategy in several ways:

1. **Diversification**: Alphabet's investment in AI is part of its broader strategy to diversify its revenue streams beyond advertising. By developing new AI-powered products and services, the company can reduce its dependence on advertising revenue and create new growth opportunities.
2. **Innovation**: AI is a key area of innovation for Alphabet, and the company's investment in AI research and development is driven by a desire to stay at the forefront of technological advancements.
3. **Sustainability**: Alphabet's investment in AI is also driven by a desire to create sustainable, long-term value for the company and its stakeholders. By developing AI-powered technologies that can improve people's lives and create new opportunities, Alphabet aims to create a positive social impact while also driving business growth.
4. **Moat-building**: Alphabet's investment in AI can also be seen as a way to build a competitive moat around its business. By developing unique AI-powered capabilities, the company can create barriers to entry for competitors and maintain its market leadership position.

Overall, Alphabet's investment in AI is a key component of its long-term business strategy, driven by a desire to innovate, diversify, and create sustainable value for the company and its stakeholders.I now know the final answer

Final Answer: The main drivers behind Alphabet Inc.'s investment in AI are improving search and advertising capabilities, enhancing user experience, expanding into new markets, and competing with other tech giants. These drivers align with Alphabet's long-term business strategy by enabling diversification, innovation, sustainability, and moat-building, ultimately driving business growth and creating long-term value for the company and its stakeholders.

> Finished chain.
The main drivers behind Alphabet Inc.'s investment in AI are improving search and advertising capabilities, enhancing user experience, expanding into new markets, and competing with other tech giants. These drivers align with Alphabet's long-term business strategy by enabling diversification, innovation, sustainability, and moat-building, ultimately driving business growth and creating long-term value for the company and its stakeholders.

方法 10:RAPTOR(树状检索的递归抽象处理)

RAPTOR 通过创建复杂的多层知识表示,彻底改变了大型语言模型的信息检索。与检索碎片化文本块的传统方法不同,这种方法构建了一个智能的分层信息树,可以捕捉细微的语义关系和上下文深度。

基础架构 该方法通过复杂的多阶段过程转变文档分析:

初始转换 该方法首先将文档分解为细粒度组件,将每个部分转换为丰富、语义密集的矢量表示。这种初始分解允许进行复杂的信息映射和比较分析。

智能聚类和汇总 RAPTOR 的核心是一种先进的聚类机制,它:

  • 对语义相关的文本片段进行分组
  • 使用复杂的语言模型生成简洁、有意义的摘要
  • 不断地重新嵌入这些摘要以保持语义完整性

分层知识结构 系统创建一个动态的多级树,其中:

  • 叶节点保留原始文本细节
  • 摘要节点提炼集体见解
  • 每个节点都带有一个独特的语义嵌入,代表其信息本质

检索策略 RAPTOR 提供两种创新的检索方法:

  • 层次遍历:系统地探索从广泛主题到具体细节的信息
  • 直接比较:实现基于事实的查询的快速、精确的信息提取

变革性优势 通过重新构想信息检索,RAPTOR 实现了:

  • 更细致入微、情境更丰富的推理能力
  • 显著提高计算效率
  • 前所未有的综合复杂相互关联知识的能力

实际应用 这一突破性方法的优点在于:

  • 先进的问答系统
  • 综合研究综合
  • 复杂的数据分析和报告

实施细节 成功部署需要:

  • 健全的树木维护协议
  • 高保真摘要技术
  • 可扩展的架构设计

新出现的挑战 该方法面临关键的技术障碍:

  • 保持实时knowledge currency
  • 在摘要过程中保持信息保真度
  • 在广泛的知识领域中有效扩展

RAPTOR 代表了 RAG 系统在理解、检索和合成复杂信息方面的重大进步,弥合了原始数据和有意义的见解之间的差距。

RAPTOR 管道:逐步实施

import os
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_groq import ChatGroq
from langchain_community.embeddings import HuggingFaceEmbeddings
import pandas as pd
import numpy as np
import umap
# from sklearn.mixture import GaussianMixturModel
from sklearn.mixture import GaussianMixture as GMM

from sklearn.metrics import silhouette_score

# Set up Groq API key
os.environ["GROQ_API_KEY"] = "gsk_"
# Load documents
loader = TextLoader("test1.txt")
documents = loader.load()

# Split documents into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size = 200, chunk_overlap = 20)
texts = text_splitter.split_documents(documents)

# Tokenization function
def count_tokens(text):
    # Use a simple tokenization method
    return len(text.split())

# Calculate token lengths
token_lengths = [count_tokens(doc.page_content) for doc in texts]

# Embedding model (using HuggingFace instead of OpenAI)
embed_model = HuggingFaceEmbeddings(
    model_name = "sentence-transformers/all-MiniLM-L6-v2"
)

# Generate embeddings
global_embeddings = np.array([
    embed_model.embed_query(doc.page_content) for doc in texts
])

# Dimensionality reduction using UMAP
reducer = umap.UMAP(n_components=2, random_state=42)
reduced_embeddings = reducer.fit_transform(global_embeddings)

# Determine optimal number of clusters
# Determine optimal number of clusters
def find_optimal_clusters(embeddings, max_clusters=10):
    silhouette_scores = []
    n_samples = embeddings.shape[0]  # Get the number of samples (documents)
    
    # Ensure we have enough data points to cluster
    min_clusters = 2 if n_samples > 2 else n_samples
    max_clusters = min(max_clusters, n_samples)
    
    for n_clusters in range(min_clusters, max_clusters + 1):
        gmm = GMM(n_components=n_clusters, random_state=42)
        labels = gmm.fit_predict(embeddings)
        score = silhouette_score(embeddings, labels)
        silhouette_scores.append(score)
    
    return np.argmax(silhouette_scores) + min_clusters


# Find optimal clusters
n_clusters = find_optimal_clusters(reduced_embeddings)

# Perform clustering
gmm = GMM(n_components=n_clusters, random_state=42)
cluster_labels = gmm.fit_predict(reduced_embeddings)

# Prepare data for summarization
df = pd.DataFrame({
    'text': [doc.page_content for doc in texts],
    'embeddings': list(global_embeddings),
    'cluster': cluster_labels
})

# Initialize Groq LLM (using Llama model)
llm = ChatGroq(
    temperature=0.1, 
    model_name="llama-3.3-70b-specdec"
)

# Function to summarize cluster texts
def summarize_cluster(cluster_texts):
    # Join texts with a separator
    combined_text = " ### ".join(cluster_texts)
    
    # Create summarization prompt
    prompt = f"Provide a detailed, concise summary of the following related texts:\n\n{combined_text}"
    
    # Generate summary
    summary = llm.invoke(prompt).content
    return summary

# Summarize each cluster
cluster_summaries = {}
for cluster in set(cluster_labels):
    cluster_texts = df[df['cluster'] == cluster]['text'].tolist()
    cluster_summaries[cluster] = summarize_cluster(cluster_texts)

# Prepare final retrieval
final_summary = summarize_cluster(list(cluster_summaries.values()))

# Retrieval function (Collapse Tree Retrieval)
def retrieve_documents(query, documents, top_k=5):
    # Embed query
    query_embedding = embed_model.embed_query(query)
    
    # Calculate similarity
    similarities = [np.dot(query_embedding, doc_emb) for doc_emb in global_embeddings]
    
    # Sort and retrieve top documents
    top_indices = sorted(range(len(similarities)), key=lambda i: similarities[i], reverse=True)[:top_k]
    
    return [texts[i].page_content for i in top_indices]

# Example usage
def answer_question(query):
    # Retrieve relevant documents
    retrieved_docs = retrieve_documents(query, texts)
    
    # Prepare context
    context = "\n\n".join(retrieved_docs)
    
    # Create prompt
    prompt = f"Answer the following question based only on the provided context:\n\nQuestion: {query}\n\nContext: {context}"
    
    # Generate answer
    answer = llm.invoke(prompt).content
    
    return answer

# Example query
question = "Who was Subhash chandra bose?"
print(answer_question(question))

执行上述代码后我们得到了以下响应:

Subhas Chandra Bose was an Indian nationalist who defied British authority in India. He joined the nationalist movement led by Mahatma Gandhi and the Indian National Congress, and later became a leader in his own right. He is seen as a hero by many in India for his efforts to secure India's independence, although his legacy is mixed due to his tactics, which included identifying with the Axis powers during World War II.

结论

在 RAG 系列的第二部分中,我们讨论了实现检索增强生成 (RAG) 的五种更高级的方法:

  1. Self-RAG:将检索和生成结合为单一、自给自足过程的智能系统,可提高准确性和适应性。
  2. Adaptive RAG:一种灵活的方法,可根据查询复杂性动态调整检索策略,提高效率和相关性。
  3. REFEED RAG:一种反馈驱动的方法,使用相关的外部信息改进初始响应,从而无需重新训练即可提高准确性。
  4. ReAct RAG:推理与行动的创新融合,使人工智能模型能够通过动态交互更有效地解决复杂问题。
  5. RAPTOR:一种复杂的、多层次的知识表示,可以捕捉细微的语义关系,从而改善复杂信息的检索和综合。

未完待续......

相关推荐

第十章:优化设计与自动化工作流(优化设计是)

以下重点讲解优化设计、自动化脚本编写以及与其他工具(如Python、优化算法库)的集成,提升CFD仿真的工程应用效率。目标:掌握参数化扫描、优化算法集成和批量任务管理,实现从单次模拟到自动化设计探索的...

安装python语言,运行你的第一行代码

#01安装Python访问Python官方(https://www.python.org/),下载并安装最新版本的Python。确保安装过程中勾选“Addpython.exetoPAT...

Python安装(python安装的库在哪个文件夹)

Windows系统1.安装python1.1下载Python安装包打开官方网站:https://www.python.org/downloads/点击"DownloadPython3.1...

比pip快100倍的Python包安装工具(python2.7.5安装pip)

简介uv是一款开源的Python包安装工具,GitHubstar高达56k,以性能极快著称,具有以下特性(官方英文原文):Asingletooltoreplacepip,pip-tool...

【跟着豆包AI学Python】Python环境的安装,编写第一个程序

最近几年,人工智能越来越走进人们的日常生活,国内各大公司都推出了自己的AI助手,例如:阿里旗下的通义千问、百度旗下的文心一言、腾讯的腾讯元宝、深度求索的deepseek等,元宝就是字节跳动公司推出的A...

Python3+ 变量命名全攻略:PEP8 规范 + 官方禁忌...

Python3+变量命名规则与约定详解一、官方命名规则(必须遵守)1.合法字符集变量名只能包含:大小写字母(a-z,A-Z)数字(0-9)下划线(_)2.禁止数字开头合法:user_age,...

Python程序打包为EXE的全面指南:从入门到精通

引言在Python开发中,将程序打包成可执行文件(EXE)是分发应用程序的重要环节。通过打包,我们可以创建独立的可执行文件,让没有安装Python环境的用户也能运行我们的程序。本篇文章将详细介绍如何使...

别再纸上谈兵了!手把手教你安装GraalVM,让你的代码瞬间起飞!

各位老铁们,是不是每次看到我吹嘘GraalVM的各种神迹,心里都痒痒的?想让自己的Java程序秒启动?想让Python脚本跑得比平时快好几倍?想体验一把多语言无缝协作的快感?但一想到要下载、配置、敲命...

纠结坏了!从 Python 3.8 升级到 3.14 真有必要吗?

点赞、收藏、加关注,下次找我不迷路"我电脑里装的Python3.8,现在都出3.14了,要不要赶紧升级啊?会不会像手机系统更新一样,越升级越卡?"相信很多刚入门的朋友都有类...

win10下python3.13.3最新版本解释器的下载与安装

一、python3.13.3下载官方下载地址:https://www.python.org/1.浏览器访问https://www.python.org/这个地址,进入python的网站,点击【Dowl...

Python简介与开发环境搭建详细教程

1.1Python简介与开发环境搭建详细教程一、Python语言简介1.Python的核心特点2.Python的应用领域表1.1Python主要应用领域领域典型应用常用库Web开发网站后端D...

python开发小游戏案例(python游戏开发入门经典教程)

#头条创作挑战赛#假设你正在开发一个小型游戏,需要实现角色移动、障碍物生成、碰撞检测等功能。你可以使用Python和Pygame库来开发这个游戏。输入以下命令来安装Pygame:pipinstall...

Python编程:从入门到实践 第十一、二 章 武装飞船

假设我们有一个名为AnonymousSurvey的类,用于收集匿名调查的答案。该类包含以下方法:classAnonymousSurvey:def__init__(self,questi...

「Python系列」python几个重要模块的安装(二)

一、python的pygame的安装:安装地址:https://www.cnblogs.com/charliedaifu/p/9938542.htmlpyagme包下载地址:https://down...

python如何绘制消消乐小游戏(python字母消消乐)

要开发一款消消乐的小游戏界面,我们可以使用Python的pygame库。首先需要安装pygame库,然后创建一个窗口,加载游戏素材,处理游戏逻辑,以及绘制游戏界面。以下是一个简单的消消乐游戏界面实现:...