实施RAG的25种技术和方法(2/5) rag方法和agent方法区别
itomcoil 2024-12-28 13:36 24 浏览
方法 6:Self-RAG
想象一下,一个人工智能就像一个拥有强大记忆力的聪明研究员。标准 RAG 系统就像需要在单独的书籍或数据库中查找信息的助手。自我检索增强生成 (Self-RAG) 则有所不同。
将 Self-RAG 视为一位天才研究员,他可以:
- 立即访问大量信息
- 准确了解需要哪些细节
- 快速找到最相关的信息
- 创建的回复不仅准确,而且经过深思熟虑
- 不断检查和改进自身工作
与依赖单独搜索工具的传统 AI 系统不同,Self-RAG 将搜索和回答结合为一个智能流程。这就像拥有一位专家,他不仅从多个来源阅读信息,而且还会实时主动思考、检索和综合信息。
这种方法改变了人工智能理解和回答问题的方式,使其更具适应性、更精确、更智能。Self-RAG 不仅仅是寻找信息,它还涉及以最有意义的方式理解、分析和呈现知识。
什么是 Self-RAG?
想象一下,如果人工智能像一位聪明的研究人员一样工作,它可以:
- 问自己需要什么信息
- 自行查找该信息
- 检查并改进自己的答案
传统的人工智能系统有单独的部分来查找信息和回答问题。Self-RAG 将这些部分组合成一个智能系统。
Self-RAG 的工作原理
- 理解问题人工智能首先仔细研究问题并弄清楚它需要找到什么信息。
- 查找信息人工智能不使用单独的搜索工具,而是通过自己的知识库进行查找。这就像拥有一位内置图书管理员,他确切地知道在哪里可以找到合适的书。
- 创建答案人工智能不只是复制信息。它利用找到的细节来创建深思熟虑、准确的回答。
- 然后,人工智能会检查自己的答案,确保其合理且有用。
主要特点
- 人工智能既是研究员,又是作家
- 它可以挖掘自己的信息“库”
- 它像一个细心的学生一样检查自己的工作
- 无需单独的搜索和书写系统
简单示例如果你问“告诉我有关气候变化的信息”,人工智能会:
- 理解复杂主题
- 决定需要哪些具体信息
- 搜索自己的知识库
- 制定详细、准确的回复
- 再次检查答案是否正确
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. 智能评分
- 上下文相关性:根据文档的具体实用性对其进行排序
- 自适应优先级:重点关注最相关的信息
主要优势
- 灵活性:适应不同类型的查询
- 效率:减少不必要的信息收集
- 准确性:持续提高响应质量
- 情境理解:提供更有针对性和更相关的答案
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) 的五种更高级的方法:
- Self-RAG:将检索和生成结合为单一、自给自足过程的智能系统,可提高准确性和适应性。
- Adaptive RAG:一种灵活的方法,可根据查询复杂性动态调整检索策略,提高效率和相关性。
- REFEED RAG:一种反馈驱动的方法,使用相关的外部信息改进初始响应,从而无需重新训练即可提高准确性。
- ReAct RAG:推理与行动的创新融合,使人工智能模型能够通过动态交互更有效地解决复杂问题。
- RAPTOR:一种复杂的、多层次的知识表示,可以捕捉细微的语义关系,从而改善复杂信息的检索和综合。
未完待续......
相关推荐
- PS小技巧 调整命令,让人物肤色变得更加白皙 #后期修图
-
我们来看一下如何去将人物的皮肤变得更加的白皙。·首先选中图层,Ctrl键加J键复制一层。·打开这里的属性面板,选择快速操作删除背景,这样就会将人物进行单独的抠取。·接下来在上方去添加一个黑白调整图层,...
- 把人物肤色提亮的方法和技巧
-
PS后期调白肤色提亮照片的方法。一白遮百丑,所以对于Photoshop后期来说把人物肤色调白是一项非常重要的任务。就拿这张素材图片来说,这张素材图片人脸的肤色主要偏红、偏黄,也不够白皙,该怎样对它进行...
- 《Photoshop教程》把美女图片调成清爽色彩及润肤技巧
-
关注PS精品教程,每天不断更新~~室内人物图片一般会偏暗,人物脸部、肤色及背景会出现一些杂点。处理之前需要认真的给人物磨皮及美白,然后再整体润色。最终效果原图一、用修补工具及图章工具简单去除大一点的黑...
- PS后期对皮肤进行美白的技巧
-
PS后期进行皮肤美白的技巧。PS后期对皮肤进行美白的技巧:·打开素材图片之后直接复制原图。·接下来直接点击上方的图像,选择应用图像命令。·在通道这里直接选择红通道,混合这里直接选择柔光,然后点击确定。...
- 493 [PS调色]调模特通透肤色
-
效果对比:效果图吧:1、光位图:2、拍摄参数:·快门:160;光圈:8;ISO:1003、步骤分解图:用曲线调整图层调出基本色调。用可选颜色调整图层调整红色、黄色、白色和灰色4种颜色的混合比例。用色彩...
- 先选肤色再涂面部,卡戴珊的摄影师透露:为明星拍完照后怎么修图
-
据英国媒体12月17日报道,真人秀明星金·卡戴珊终于承认,她把女儿小北P进了家族的圣诞贺卡,怪不得粉丝们都表示这张贺卡照得非常失败。上周,这位39岁的女星遭到了一些粉丝针对这张照片的批评,她于当地时间...
- 如何在PS中运用曲线复制另一张照片的色调
-
怎样把另一张作品的外观感觉,套用到自己的照片上?单靠肉眼来猜,可能很不容易,而来自BenSecret的教学,关键是在PS使用了两个工具,让你可以准确比较两张照片的曝光、色调与饱和度,方便你调整及复制...
- PS在LAB模式下调出水嫩肤色的美女
-
本PS教程主要使用Photoshop使用LAB模式调出水嫩肤色的美女,教程调色比较独特。作者比较注重图片高光部分的颜色,增加质感及肤色调红润等都是在高光区域完成。尤其在Lab模式下,用高光选区调色后图...
- 在Photoshop图像后期处理中如何将人物皮肤处理得白皙通透
-
我们在人像后期处理中,需要将人物皮肤处理的白皙通透,处理方法很多,大多数都喜欢使用曲线、磨皮等进行调整,可以达到亮但是不透,最终效果往往不是很好,今天就教大家一种如何将任务皮肤处理得白皙通透,希望能帮...
- PS调色自学教程:宝宝照片快速调通透,简单实用!
-
PS调色自学教程:宝宝照片快速调通透。·首先复制图层,然后选择进入ACR滤镜,选择曲线锁定照片的亮部,也就高光位置,其他部位补亮一点,尤其是阴影的部位补亮多一些,让画面的层次均匀一点。·然后回到基本项...
- 【干货】如何利用PS进行人物美化
-
人物图像美化在Photoshop中非常常用,Photoshop作为一款功能强大的图像处理软件,不仅可以对人像进行基本的调色、美化和修复等处理,还可以改变人物的线条和幅度,如调整脸部器官和脸型的大小、调...
- 教大家一种可以快速把肤色处理均匀的方法@抖音短视频
-
快速把肤色处理均匀的方法。今天教大家一种可以快速把肤色处理均匀的方法。像这张照片整体肤色走紫红色,但是局部偏黄缘处理起来非常的麻烦。其实我们只需要新建空白图层,图层混合模式更改为颜色,再选择画笔工具把...
- PS调色教程 利用RAW调出干净通透的肤色
-
要么不发,要么干货。后期教程来噜~用RAW调出干净通透的肤色。这次终于不会原片比PS后好看了吧。如果你依然这么觉得,请不要残忍的告诉我这个事实,泪谢TAT)附送拍摄花絮,感谢各位的支持更多风格请关注m...
- photoshop后期皮肤变白的技巧
-
PS后期皮肤变白的技巧。1.PS后期让皮肤变白的方法有很多种,接下来教你一种非常简单容易上手的方法。2.打开素材图片之后,直接在小太极下拉框的位置添加一个纯色调整图层,颜色设置一个纯白色,点击...
- Photoshop调出人物的淡雅粉嫩肤色教程
-
本教程主要使用Photoshop调出人物的淡雅粉嫩肤色教程,最终的效果非常的通透迷人,下面让我们一起来学习.出自:86ps效果图:原图:1、打开原图复制一层。2、用Topaz滤镜磨皮(点此下载)。3、...
- 一周热门
- 最近发表
- 标签列表
-
- ps像素和厘米换算 (32)
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)