解鎖Agentic RAG,使用LangChain和OpenAI進行實踐
Agentic RAG技術是一種新興的智能體驅動解決方案,能夠應對復雜的多文檔問答挑戰。這項技術通過智能化手段,不僅提升了我們處理文本數據的效率,還極大地增強了問答系統的準確性和深度。本文帶大家來了解這項技術,闡述其如何提升信息檢索和分析的效率與準確性。
1 Agentic RAG簡介
Agentic RAG是由智能體驅動的技術,能夠靈活處理多文檔問答任務。這項技術不僅能比較文檔、總結內容,還能對多個摘要進行對比分析。
正因如此,我們不完全依賴于大型語言模型,而是通過智能體來承擔那些需要深思熟慮、邏輯推理、工具運用以及持續學習的任務。Agentic RAG通過智能化手段,提高了解決復雜問題的能力,讓問答更加高效和精確。
2 基本架構
基本架構是為每個文檔設置一個文檔智能體,每個文檔智能體都能在其自己的文檔內執行問答和摘要。
然后,設置一個頂級智能體(元智能體)來管理所有這些較低級別的文檔智能體。
3 技術棧
技術棧包括以下幾個核心組件:
- Langchain:特別是LCEL,這是用于構建大型語言模型(LLM)應用的編排框架。
- OpenAI:提供我們需要的大型語言模型(LLM)。
- FAISS-cpu:用作向量存儲解決方案。
- 數據源:我們使用ArxivLoader來檢索arXiv上發表文章的元數據。
4 代碼實現
安裝所需的依賴項:
!pip install -qU langchain langchain_openai langgraph arxiv duckduckgo-search
!pip install -qU faiss-cpu pymupdf
設置環境變量:
from google.colab import userdata
from uuid import uuid4
import os
#
os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY')
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_PROJECT"] = f"AIE1 - LangGraph - {uuid4().hex[0:8]}"
os.environ["LANGCHAIN_API_KEY"] = userdata.get('LANGCHAIN_API_KEY')
使用LCEL實例化一個簡單的檢索鏈。
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import ArxivLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
# 加載特定主題的文檔
docs = ArxivLoader(query="Retrieval Augmented Generation", load_max_docs=5).load()
# 將文檔分割成更小的塊
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=350, chunk_overlap=50
)
chunked_documents = text_splitter.split_documents(docs)
#
# 實例化嵌入模型
embeddings = OpenAIEmbeddings(model="text-embedding-3-small",openai_api_key=os.environ['OPENAI_API_KEY'])
# 創建索引-將文檔塊加載到向量存儲中
faiss_vectorstore = FAISS.from_documents(
documents=chunked_documents,
embedding=embeddings,
)
# 創建檢索器
retriever = faiss_vectorstore.as_retriever()
生成RAG提示:
from langchain_core.prompts import ChatPromptTemplate
RAG_PROMPT = """\
使用以下上下文回答用戶的查詢。如果你不能回答問題,請回答“我不知道”。
問題:
{question}
上下文:
{context}
"""
rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
實例化LLM:
from langchain_openai import ChatOpenAI
openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo")
構建LCEL RAG鏈:
from operator import itemgetter
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnablePassthrough
retrieval_augmented_generation_chain = (
{"context": itemgetter("question")
| retriever, "question": itemgetter("question")}
| RunnablePassthrough.assign(cnotallow=itemgetter("context"))
| {"response": rag_prompt | openai_chat_model, "context": itemgetter("context")}
)
#
retrieval_augmented_generation_chain
響應:
{
"response": "AIMessage(cnotallow='Retrieval Augmented Generation 是一種結合了深度學習技術和傳統檢索技術的文本生成范式。它通過顯式獲取知識以插件方式,引領了許多NLP任務的可擴展性和可能緩解文本生成的難度。它涉及從檢索到的人類編寫的參考資料中生成文本,而不是從頭開始生成。', response_metadata={'token_usage': {'completion_tokens': 73, 'prompt_tokens': 2186, 'total_tokens': 2259}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None})",
"context": "[Document(page_cnotallow='...'), Document(page_cnotallow='...'), Document(page_cnotallow='...')]"
}
構建工具箱:
為了使智能體能夠回答各種問題并引入外部知識,通常會給其配備一個工具箱。
LangChain社區的存儲庫(https://github.com/langchain-ai/langchain/tree/master/libs/community/langchain_community/tools)中提供了眾多工具,這里選擇了幾種來使用,這樣可以更直觀地展示LangGraph的循環特性。
具體來說,將整合以下工具:
- Duck Duck Go網絡搜索(https://github.com/langchain-ai/langchain/tree/master/libs/community/langchain_community/tools/ddg_search)
- Arxiv學術文獻檢索(https://github.com/langchain-ai/langchain/tree/master/libs/community/langchain_community/tools/arxiv))
from langchain_community.tools.ddg_search import DuckDuckGoSearchRun
from langchain_community.tools.arxiv.tool import ArxivQueryRun
from langgraph.prebuilt import ToolExecutor
tool_belt = [
DuckDuckGoSearchRun(),
ArxivQueryRun()
]
tool_executor = ToolExecutor(tool_belt)
實例化OpenAI函數調用:
from langchain_core.utils.function_calling import convert_to_openai_function
#
model = ChatOpenAI(temperature=0)
#
functions = [convert_to_openai_function(t) for t in tool_belt]
model = model.bind_functions(functions)
使用LangGraph:
LangGraph通過StatefulGraph來實現,這個結構使用AgentState對象在圖的不同節點間傳遞信息。
雖然有很多配置選項,但核心是AgentState對象,它被存儲在一個TypedDict中。這個對象的鍵是“messages”,對應的值是一個BaseMessages的序列,每當狀態發生變化時,新的消息就會被添加到這個序列中。
from typing import TypedDict, Annotated, Sequence
import operator
from langchain_core.messages import BaseMessage
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], operator.add]
構建節點
call_model是一個節點,將...嗯...調用模型
call_tool是一個節點,將調用工具
```python
from langgraph.prebuilt import ToolInvocation
import json
from langchain_core.messages import FunctionMessage
def call_model(state):
messages = state["messages"]
response = model.invoke(messages)
return {"messages" : [response]}
def call_tool(state):
last_message = state["messages"][-1]
action = ToolInvocation(
tool=last_message.additional_kwargs["function_call"]["name"],
tool_input=json.loads(
last_message.additional_kwargs["function_call"]["arguments"]
)
)
response = tool_executor.invoke(action)
function_message = FunctionMessage(cnotallow=str(response), name=action.tool)
return {"messages" : [function_message]}
構建工作流:
from langgraph.graph import StateGraph, END
workflow = StateGraph(AgentState)
workflow.add_node("agent", call_model)
workflow.add_node("action", call_tool)
workflow.nodes
設置入口點:
workflow.set_entry_point("agent")
構建條件邊進行路由:
def should_continue(state):
last_message = state["messages"][-1]
if"function_call"notin last_message.additional_kwargs:
return"end"
return"continue"
workflow.add_conditional_edges(
"agent",
should_continue,
{
"continue" : "action",
"end" : END
}
)
最后將條件邊連接到智能體節點和動作節點:
workflow.add_edge("action", "agent")
編譯工作流:
app = workflow.compile()
#
app
調用LangGraph - 提問:
from langchain_core.messages import HumanMessage
inputs = {"messages" : [HumanMessage(cnotallow="在大型語言模型的背景下,RAG是什么?它是什么時候出現的?")]}
response = app.invoke(inputs)
print(response)
響應:
{
"messages": [
HumanMessage(cnotallow="在大型語言模型的背景下,RAG是什么?它是什么時候出現的?"),
AIMessage(cnotallow='', additional_kwargs={'function_call': {'arguments': '{"query":"RAG in the context of Large Language Models"}', 'name': 'duckduckgo_search'}}, response_metadata={'token_usage': {'completion_tokens': 25, 'prompt_tokens': 171, 'total_tokens': 196}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'function_call', 'logprobs': None}),
FunctionMessage(cnotallow="大型語言模型(LLMs)是處理和生成文本的非常強大的工具。然而,它們天生難以理解更廣泛的信息背景,特別是在處理長篇對話或復雜任務時。這就是大型上下文窗口和檢索增強生成(RAG)發揮作用的地方。這些先進的、泛化的語言模型是在龐大的數據集上訓練的,使它們能夠理解和生成類似人類的文本。在RAG的背景下,LLMs被用來基于用戶查詢和從向量數據庫檢索的上下文信息生成完整的響應。在語言模型快速發展的背景下,檢索增強生成(RAG)和長上下文大型語言模型(LLMs)之間的辯論引起了廣泛關注。檢索增強生成(RAG)是一個AI框架,通過將模型基于外部知識源來補充LLM的內部信息表示,從而提高LLM生成響應的質量。在基于LLM的問答系統中實施RAG有兩個主要好處:它確保模型有... RAG代表檢索增強生成。RAG使大型語言模型(LLM)能夠訪問和利用最新的信息。因此,它提高了LLM的響應質量和相關性。下面是一個簡單的RAG實現圖。", name='duckduckgo_search'),
AIMessage(cnotallow="在大型語言模型(LLMs)的背景下,RAG代表檢索增強生成。它是一個AI框架,通過將模型基于外部知識源來補充LLM的內部信息表示,從而提高LLM生成響應的質量。RAG使LLM能夠訪問和利用最新的信息,從而提高模型生成的響應的相關性和質量。RAG作為增強LLM在理解和生成類似人類文本的能力的方式,在語言模型快速發展的領域中出現。", response_metadata={'token_usage': {'completion_tokens': 117, 'prompt_tokens': 491, 'total_tokens': 608}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3bc1b5746c', 'finish_reason': 'stop', 'logprobs': None})
]
}
響應內容:
RAG代表檢索增強生成,在大型語言模型(LLMs)的背景下。它是一個AI框架,通過將模型基于外部知識源來補充LLM的內部信息表示,從而提高LLM生成響應的質量。RAG使LLM能夠訪問和利用最新的信息,從而提高模型生成的響應的相關性和質量。RAG作為增強LLM在理解和生成類似人類文本的能力的方式,在語言模型快速發展的領域中出現。
提問:
問題 = "檢索增強生成論文的主要作者是誰 - 他們參加了哪所大學?"
inputs = {"messages" : [HumanMessage(cnotallow=問題)]}
response = app.invoke(inputs) print(response['messages'][-1].content)
**響應:**
關于“檢索增強生成”論文的主要作者是Huayang Li。不幸的是,提供的摘要中沒有提到他們參加的大學。
**提問:**
問題 = "檢索增強生成論文的主要作者是誰?"
inputs = {"messages" : [HumanMessage(cnotallow=問題)]}
response = app.invoke(inputs)
print(response['messages'][-1].content)
響應:論文“檢索增強文本生成”的主要作者是Huayang Li、Yixuan Su、Deng Cai、Yan Wang和Lemao Liu。
本文轉載自 ??AI科技論談??,作者: AI科技論談
