代理RAG¶
检索代理 在我们想要决定是否从索引中检索时非常有用。
要实现检索代理,我们只需要给一个LLM访问检索工具的权限。
我们可以将其纳入 LangGraph 中。
设置¶
首先,让我们下载所需的包并设置我们的API密钥:
%%capture --no-stderr
%pip install -U --quiet langchain-community tiktoken langchain-openai langchainhub chromadb langchain langgraph langchain-text-splitters
import getpass
import os
def _set_env(key: str):
if key not in os.environ:
os.environ[key] = getpass.getpass(f"{key}:")
_set_env("OPENAI_API_KEY")
为 LangGraph 开发设置 LangSmith
注册 LangSmith,以快速发现问题并提高您的 LangGraph 项目的性能。LangSmith 允许您使用跟踪数据来调试、测试和监视使用 LangGraph 构建的 LLM 应用程序 — 了解如何开始 读取更多信息。
检索器¶
首先,我们索引3篇博客文章。
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
urls = [
"https://lilianweng.github.io/posts/2023-06-23-agent/",
"https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/",
"https://lilianweng.github.io/posts/2023-10-25-adv-attack-llm/",
]
docs = [WebBaseLoader(url).load() for url in urls]
docs_list = [item for sublist in docs for item in sublist]
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=100, chunk_overlap=50
)
doc_splits = text_splitter.split_documents(docs_list)
# 添加到向量数据库
vectorstore = Chroma.from_documents(
documents=doc_splits,
collection_name="rag-chroma",
embedding=OpenAIEmbeddings(),
)
retriever = vectorstore.as_retriever()
然后我们创建一个检索工具。
from langchain.tools.retriever import create_retriever_tool
retriever_tool = create_retriever_tool(
retriever,
"retrieve_blog_posts",
"Search and return information about Lilian Weng blog posts on LLM agents, prompt engineering, and adversarial attacks on LLMs.",
)
tools = [retriever_tool]
API Reference:
create_retriever_tool
代理状态¶
我们将定义一个图。
一个 state 对象,传递给每个节点。
我们的状态将是一个 messages 的列表。
图中的每个节点都将向其中添加内容。
from typing import Annotated, Sequence
from typing_extensions import TypedDict
from langchain_core.messages import BaseMessage
from langgraph.graph.message import add_messages
class AgentState(TypedDict):
# add_messages 函数定义了如何处理更新。
# 默认是替换。add_messages 表示 "append"
messages: Annotated[Sequence[BaseMessage], add_messages]
API Reference:
BaseMessage | add_messages
节点和边¶
我们可以像这样布局一个自主的RAG图:
- 状态是一组消息
- 每个节点将更新(附加到)状态
- 条件边决定下一个访问的节点
在LangChain中使用Pydantic
这个笔记本使用了Pydantic v2 BaseModel,这需要 langchain-core >= 0.3。使用 langchain-core < 0.3 将会导致由于混合使用Pydantic v1和v2 BaseModels 而发生错误。
from typing import Annotated, Literal, Sequence
from typing_extensions import TypedDict
from langchain import hub
from langchain_core.messages import BaseMessage, HumanMessage
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field
from langgraph.prebuilt import tools_condition
# ##边缘
def grade_documents(state) -> Literal["generate", "rewrite"]:
"""
Determines whether the retrieved documents are relevant to the question.
Args:
state (messages): The current state
Returns:
str: A decision for whether the documents are relevant or not
"""
print("---CHECK RELEVANCE---")
# 数据模型
class grade(BaseModel):
"""相关性检查的二进制评分。"""
binary_score: str = Field(description="Relevance score 'yes' or 'no'")
# 大型语言模型
model = ChatOpenAI(temperature=0, model="gpt-4-0125-preview", streaming=True)
# 具有工具和验证的大型语言模型
llm_with_tool = model.with_structured_output(grade)
# 提示
prompt = PromptTemplate(
template="""You are a grader assessing relevance of a retrieved document to a user question. \n
Here is the retrieved document: \n\n {context} \n\n
Here is the user question: {question} \n
If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant. \n
Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question.""",
input_variables=["context", "question"],
)
# 链条
chain = prompt | llm_with_tool
messages = state["messages"]
last_message = messages[-1]
question = messages[0].content
docs = last_message.content
scored_result = chain.invoke({"question": question, "context": docs})
score = scored_result.binary_score
if score == "yes":
print("---DECISION: DOCS RELEVANT---")
return "generate"
else:
print("---DECISION: DOCS NOT RELEVANT---")
print(score)
return "rewrite"
# ##节点
def agent(state):
"""
Invokes the agent model to generate a response based on the current state. Given
the question, it will decide to retrieve using the retriever tool, or simply end.
Args:
state (messages): The current state
Returns:
dict: The updated state with the agent response appended to messages
"""
print("---CALL AGENT---")
messages = state["messages"]
model = ChatOpenAI(temperature=0, streaming=True, model="gpt-4-turbo")
model = model.bind_tools(tools)
response = model.invoke(messages)
# 我们返回一个列表,因为这将被添加到现有列表中。
return {"messages": [response]}
def rewrite(state):
"""
Transform the query to produce a better question.
Args:
state (messages): The current state
Returns:
dict: The updated state with re-phrased question
"""
print("---TRANSFORM QUERY---")
messages = state["messages"]
question = messages[0].content
msg = [
HumanMessage(
content=f""" \n
Look at the input and try to reason about the underlying semantic intent / meaning. \n
Here is the initial question:
\n ------- \n
{question}
\n ------- \n
Formulate an improved question: """,
)
]
# 评分者
model = ChatOpenAI(temperature=0, model="gpt-4-0125-preview", streaming=True)
response = model.invoke(msg)
return {"messages": [response]}
def generate(state):
"""
Generate answer
Args:
state (messages): The current state
Returns:
dict: The updated state with re-phrased question
"""
print("---GENERATE---")
messages = state["messages"]
question = messages[0].content
last_message = messages[-1]
docs = last_message.content
# 提示
prompt = hub.pull("rlm/rag-prompt")
# 大型语言模型
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, streaming=True)
# 后处理
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
# 链条
rag_chain = prompt | llm | StrOutputParser()
# 运行
response = rag_chain.invoke({"context": docs, "question": question})
return {"messages": [response]}
print("*" * 20 + "Prompt[rlm/rag-prompt]" + "*" * 20)
prompt = hub.pull("rlm/rag-prompt").pretty_print() # 你接受的训练数据截止到2023年10月。
********************Prompt[rlm/rag-prompt]********************
================================[1m Human Message [0m=================================
You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
Question: [33;1m[1;3m{question}[0m
Context: [33;1m[1;3m{context}[0m
Answer:
API Reference:
BaseMessage | HumanMessage | StrOutputParser | PromptTemplate | ChatOpenAI | tools_condition
图表¶
- 从一个代理开始,
call_model - 代理决定调用一个函数
- 如果是这样,则
action调用工具(检索器) - 然后使用工具输出添加到消息(
state)中调用代理
from langgraph.graph import END, StateGraph, START
from langgraph.prebuilt import ToolNode
# 定义一个新图。
workflow = StateGraph(AgentState)
# 定义我们将循环的节点。
workflow.add_node("agent", agent) # 代理人
retrieve = ToolNode([retriever_tool])
workflow.add_node("retrieve", retrieve) # 检索
workflow.add_node("rewrite", rewrite) # 重新写问题
workflow.add_node(
"generate", generate
) # 在确认文件相关后生成响应。
# 调用代理节点以决定是否检索。
workflow.add_edge(START, "agent")
# 决定是否检索
workflow.add_conditional_edges(
"agent",
# 评估代理决策
tools_condition,
{
# 将条件输出转换为我们图中的节点。
"tools": "retrieve",
END: END,
},
)
# 在“动作”节点被调用后采取的边缘。
workflow.add_conditional_edges(
"retrieve",
# 评估代理决策
grade_documents,
)
workflow.add_edge("generate", END)
workflow.add_edge("rewrite", "agent")
# 编译
graph = workflow.compile()
from IPython.display import Image, display
try:
display(Image(graph.get_graph(xray=True).draw_mermaid_png()))
except Exception:
# 这需要一些额外的依赖项,并且是可选的。
pass
import pprint
inputs = {
"messages": [
("user", "What does Lilian Weng say about the types of agent memory?"),
]
}
for output in graph.stream(inputs):
for key, value in output.items():
pprint.pprint(f"Output from node '{key}':")
pprint.pprint("---")
pprint.pprint(value, indent=2, width=80, depth=None)
pprint.pprint("\n---\n")
---CALL AGENT---
"Output from node 'agent':"
'---'
{ 'messages': [ AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_z36oPZN8l1UC6raxrebqc1bH', 'function': {'arguments': '{"query":"types of agent memory"}', 'name': 'retrieve_blog_posts'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-2bad2518-8187-4d8f-8e23-2b9501becb6f-0', tool_calls=[{'name': 'retrieve_blog_posts', 'args': {'query': 'types of agent memory'}, 'id': 'call_z36oPZN8l1UC6raxrebqc1bH'}])]}
'\n---\n'
---CHECK RELEVANCE---
---DECISION: DOCS RELEVANT---
"Output from node 'retrieve':"
'---'
{ 'messages': [ ToolMessage(content='Table of Contents\n\n\n\nAgent System Overview\n\nComponent One: Planning\n\nTask Decomposition\n\nSelf-Reflection\n\n\nComponent Two: Memory\n\nTypes of Memory\n\nMaximum Inner Product Search (MIPS)\n\n\nComponent Three: Tool Use\n\nCase Studies\n\nScientific Discovery Agent\n\nGenerative Agents Simulation\n\nProof-of-Concept Examples\n\n\nChallenges\n\nCitation\n\nReferences\n\nPlanning\n\nSubgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks.\nReflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps, thereby improving the quality of final results.\n\n\nMemory\n\nMemory\n\nShort-term memory: I would consider all the in-context learning (See Prompt Engineering) as utilizing short-term memory of the model to learn.\nLong-term memory: This provides the agent with the capability to retain and recall (infinite) information over extended periods, often by leveraging an external vector store and fast retrieval.\n\n\nTool use\n\nThe design of generative agents combines LLM with memory, planning and reflection mechanisms to enable agents to behave conditioned on past experience, as well as to interact with other agents.', name='retrieve_blog_posts', id='d815f283-868c-4660-a1c6-5f6e5373ca06', tool_call_id='call_z36oPZN8l1UC6raxrebqc1bH')]}
'\n---\n'
---GENERATE---
"Output from node 'generate':"
'---'
{ 'messages': [ 'Lilian Weng discusses short-term and long-term memory in '
'agent systems. Short-term memory is used for in-context '
'learning, while long-term memory allows agents to retain and '
'recall information over extended periods.']}
'\n---\n'