Self-RAG¶
自我反思可以增强 RAG,从而能够纠正低质量的检索或生成结果。
最近的一些论文关注了这一主题,但实现这些想法可能很棘手。
在这里,我们展示如何使用 LangGraph 实现 Self RAG
论文 here 中的想法。
依赖项¶
设置 OPENAI_API_KEY
Self-RAG 细节¶
Self-RAG 是一篇最近的论文,介绍了一种用于自我反思 RAG 的有趣方法。
该框架训练一个 LLM(例如,LLaMA2-7b 或 13b)生成令牌,这些令牌以几种方式控制 RAG 过程
-
我应该从检索器
R
中检索吗 - -
令牌:
Retrieve
- 输入:
x (问题)
或x (问题)
,y (生成)
- 决定何时使用
R
检索D
个块 -
输出:
yes, no, continue
-
检索到的段落
D
与问题x
相关吗 -
- 令牌:ISREL
¶
- 输入:(
x (问题)
,d (块)
) 对于D
中的d
d
提供有用的信息来解决x
-
输出:
relevant, irrelevant
-
来自
D
中每个块的 LLM 生成与该块相关吗(幻觉等) - -
令牌:
ISSUP
- 输入:
x (问题)
,d (块)
,y (生成)
对于D
中的d
y (生成)
中所有值得验证的陈述都得到d
的支持-
输出:
{fully supported, partially supported, no support
-
来自
D
中每个块的 LLM 生成是对x (问题)
的有用回应 - -
令牌:
ISUSE
- 输入:
x (问题)
,y (生成)
对于D
中的d
y (生成)
是对x (问题)
的有用回应。- 输出:
{5, 4, 3, 2, 1}
我们可以将其表示为一个图
让我们使用 LangGraph 从头开始实现其中的一些想法。
设置¶
加载环境变量¶
在 repo 文件夹的根目录中添加一个 .env
变量,其中包含您的变量。
安装依赖项¶
npm install cheerio zod langchain @langchain/community @langchain/openai @langchain/core @langchain/textsplitters
import { CheerioWebBaseLoader } from "@langchain/community/document_loaders/web/cheerio";
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { OpenAIEmbeddings } from "@langchain/openai";
const 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/",
];
const docs = await Promise.all(
urls.map((url) => new CheerioWebBaseLoader(url).load()),
);
const docsList = docs.flat();
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 500,
chunkOverlap: 250,
});
const docSplits = await textSplitter.splitDocuments(docsList);
// Add to vectorDB
const vectorStore = await MemoryVectorStore.fromDocuments(
docSplits,
new OpenAIEmbeddings({ model: "text-embedding-3-large" }),
);
const retriever = vectorStore.asRetriever();
状态¶
我们将定义一个图。
我们的状态将是一个 object
。
我们可以从任何图节点访问它,如 state.key
。
import { Annotation } from "@langchain/langgraph";
import { type DocumentInterface } from "@langchain/core/documents";
// Represents the state of our graph.
const GraphState = Annotation.Root({
documents: Annotation<DocumentInterface[]>({
reducer: (x, y) => y ?? x ?? [],
}),
question: Annotation<string>({
reducer: (x, y) => y ?? x ?? "",
}),
generation: Annotation<string>({
reducer: (x, y) => y ?? x,
default: () => "",
}),
generationVQuestionGrade: Annotation<string>({
reducer: (x, y) => y ?? x,
}),
generationVDocumentsGrade: Annotation<string>({
reducer: (x, y) => y ?? x,
}),
});
节点和边¶
每个 node
将简单地修改 state
。
每个 edge
将选择接下来调用哪个 node
。
我们可以将 self-RAG
布置成一个图。
这是我们的图流程
import { z } from "zod";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { pull } from "langchain/hub";
import { ChatOpenAI } from "@langchain/openai";
import { StringOutputParser } from "@langchain/core/output_parsers";
import type { RunnableConfig } from "@langchain/core/runnables";
import { formatDocumentsAsString } from "langchain/util/document";
// Define the LLM once. We'll reuse it throughout the graph.
const model = new ChatOpenAI({
model: "gpt-4o",
temperature: 0,
});
/**
* Retrieve documents
*
* @param {typeof GraphState.State} state The current state of the graph.
* @param {RunnableConfig | undefined} config The configuration object for tracing.
* @returns {Promise<Partial<typeof GraphState.State>>} The new state object.
*/
async function retrieve(
state: typeof GraphState.State,
config?: RunnableConfig
): Promise<Partial<typeof GraphState.State>> {
console.log("---RETRIEVE---");
const documents = await retriever
.withConfig({ runName: "FetchRelevantDocuments" })
.invoke(state.question, config);
return {
documents,
};
}
/**
* Generate answer
*
* @param {typeof GraphState.State} state The current state of the graph.
* @param {RunnableConfig | undefined} config The configuration object for tracing.
* @returns {Promise<Partial<typeof GraphState.State>>} The new state object.
*/
async function generate(
state: typeof GraphState.State
): Promise<Partial<typeof GraphState.State>> {
console.log("---GENERATE---");
// Pull in the prompt
const prompt = await pull<ChatPromptTemplate>("rlm/rag-prompt");
// Construct the RAG chain by piping the prompt, model, and output parser
const ragChain = prompt.pipe(model).pipe(new StringOutputParser());
const generation = await ragChain.invoke({
context: formatDocumentsAsString(state.documents),
question: state.question,
});
return {
generation,
};
}
/**
* Determines whether the retrieved documents are relevant to the question.
*
* @param {typeof GraphState.State} state The current state of the graph.
* @param {RunnableConfig | undefined} config The configuration object for tracing.
* @returns {Promise<Partial<typeof GraphState.State>>} The new state object.
*/
async function gradeDocuments(
state: typeof GraphState.State
): Promise<Partial<typeof GraphState.State>> {
console.log("---CHECK RELEVANCE---");
// pass the name & schema to `withStructuredOutput` which will force the model to call this tool.
const llmWithTool = model.withStructuredOutput(
z
.object({
binaryScore: z
.enum(["yes", "no"])
.describe("Relevance score 'yes' or 'no'"),
})
.describe(
"Grade the relevance of the retrieved documents to the question. Either 'yes' or 'no'."
),
{
name: "grade",
}
);
const prompt = ChatPromptTemplate.fromTemplate(
`You are a grader assessing relevance of a retrieved document to a user question.
Here is the retrieved document:
{context}
Here is the user question: {question}
If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant.
Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question.`
);
// Chain
const chain = prompt.pipe(llmWithTool);
const filteredDocs: Array<DocumentInterface> = [];
for await (const doc of state.documents) {
const grade = await chain.invoke({
context: doc.pageContent,
question: state.question,
});
if (grade.binaryScore === "yes") {
console.log("---GRADE: DOCUMENT RELEVANT---");
filteredDocs.push(doc);
} else {
console.log("---GRADE: DOCUMENT NOT RELEVANT---");
}
}
return {
documents: filteredDocs,
};
}
/**
* Transform the query to produce a better question.
*
* @param {typeof GraphState.State} state The current state of the graph.
* @param {RunnableConfig | undefined} config The configuration object for tracing.
* @returns {Promise<Partial<typeof GraphState.State>>} The new state object.
*/
async function transformQuery(
state: typeof GraphState.State
): Promise<Partial<typeof GraphState.State>> {
console.log("---TRANSFORM QUERY---");
// Pull in the prompt
const prompt = ChatPromptTemplate.fromTemplate(
`You are generating a question that is well optimized for semantic search retrieval.
Look at the input and try to reason about the underlying sematic intent / meaning.
Here is the initial question:
\n ------- \n
{question}
\n ------- \n
Formulate an improved question: `
);
// Construct the chain
const chain = prompt.pipe(model).pipe(new StringOutputParser());
const betterQuestion = await chain.invoke({ question: state.question });
return {
question: betterQuestion,
};
}
/**
* Determines whether to generate an answer, or re-generate a question.
*
* @param {typeof GraphState.State} state The current state of the graph.
* @returns {"transformQuery" | "generate"} Next node to call
*/
function decideToGenerate(state: typeof GraphState.State) {
console.log("---DECIDE TO GENERATE---");
const filteredDocs = state.documents;
if (filteredDocs.length === 0) {
// All documents have been filtered checkRelevance
// We will re-generate a new query
console.log("---DECISION: TRANSFORM QUERY---");
return "transformQuery";
}
// We have relevant documents, so generate answer
console.log("---DECISION: GENERATE---");
return "generate";
}
/**
* Determines whether the generation is grounded in the document.
*
* @param {typeof GraphState.State} state The current state of the graph.
* @param {RunnableConfig | undefined} config The configuration object for tracing.
* @returns {Promise<Partial<typeof GraphState.State>>} The new state object.
*/
async function generateGenerationVDocumentsGrade(
state: typeof GraphState.State
): Promise<Partial<typeof GraphState.State>> {
console.log("---GENERATE GENERATION vs DOCUMENTS GRADE---");
const llmWithTool = model.withStructuredOutput(
z
.object({
binaryScore: z
.enum(["yes", "no"])
.describe("Relevance score 'yes' or 'no'"),
})
.describe(
"Grade the relevance of the retrieved documents to the question. Either 'yes' or 'no'."
),
{
name: "grade",
}
);
const prompt = ChatPromptTemplate.fromTemplate(
`You are a grader assessing whether an answer is grounded in / supported by a set of facts.
Here are the facts:
\n ------- \n
{documents}
\n ------- \n
Here is the answer: {generation}
Give a binary score 'yes' or 'no' to indicate whether the answer is grounded in / supported by a set of facts.`
);
const chain = prompt.pipe(llmWithTool);
const score = await chain.invoke({
documents: formatDocumentsAsString(state.documents),
generation: state.generation,
});
return {
generationVDocumentsGrade: score.binaryScore,
};
}
function gradeGenerationVDocuments(state: typeof GraphState.State) {
console.log("---GRADE GENERATION vs DOCUMENTS---");
const grade = state.generationVDocumentsGrade;
if (grade === "yes") {
console.log("---DECISION: SUPPORTED, MOVE TO FINAL GRADE---");
return "supported";
}
console.log("---DECISION: NOT SUPPORTED, GENERATE AGAIN---");
return "not supported";
}
/**
* Determines whether the generation addresses the question.
*
* @param {typeof GraphState.State} state The current state of the graph.
* @param {RunnableConfig | undefined} config The configuration object for tracing.
* @returns {Promise<Partial<typeof GraphState.State>>} The new state object.
*/
async function generateGenerationVQuestionGrade(
state: typeof GraphState.State
): Promise<Partial<typeof GraphState.State>> {
console.log("---GENERATE GENERATION vs QUESTION GRADE---");
const llmWithTool = model.withStructuredOutput(
z
.object({
binaryScore: z
.enum(["yes", "no"])
.describe("Relevance score 'yes' or 'no'"),
})
.describe(
"Grade the relevance of the retrieved documents to the question. Either 'yes' or 'no'."
),
{
name: "grade",
}
);
const prompt = ChatPromptTemplate.fromTemplate(
`You are a grader assessing whether an answer is useful to resolve a question.
Here is the answer:
\n ------- \n
{generation}
\n ------- \n
Here is the question: {question}
Give a binary score 'yes' or 'no' to indicate whether the answer is useful to resolve a question.`
);
const chain = prompt.pipe(llmWithTool);
const score = await chain.invoke({
question: state.question,
generation: state.generation,
});
return {
generationVQuestionGrade: score.binaryScore,
};
}
function gradeGenerationVQuestion(state: typeof GraphState.State) {
console.log("---GRADE GENERATION vs QUESTION---");
const grade = state.generationVQuestionGrade;
if (grade === "yes") {
console.log("---DECISION: USEFUL---");
return "useful";
}
console.log("---DECISION: NOT USEFUL---");
return "not useful";
}
构建图¶
这只是遵循了我们在上图中概述的流程。
import { END, START, StateGraph } from "@langchain/langgraph";
const workflow = new StateGraph(GraphState)
// Define the nodes
.addNode("retrieve", retrieve)
.addNode("gradeDocuments", gradeDocuments)
.addNode("generate", generate)
.addNode(
"generateGenerationVDocumentsGrade",
generateGenerationVDocumentsGrade
)
.addNode("transformQuery", transformQuery)
.addNode(
"generateGenerationVQuestionGrade",
generateGenerationVQuestionGrade
);
// Build graph
workflow.addEdge(START, "retrieve");
workflow.addEdge("retrieve", "gradeDocuments");
workflow.addConditionalEdges("gradeDocuments", decideToGenerate, {
transformQuery: "transformQuery",
generate: "generate",
});
workflow.addEdge("transformQuery", "retrieve");
workflow.addEdge("generate", "generateGenerationVDocumentsGrade");
workflow.addConditionalEdges(
"generateGenerationVDocumentsGrade",
gradeGenerationVDocuments,
{
supported: "generateGenerationVQuestionGrade",
"not supported": "generate",
}
);
workflow.addConditionalEdges(
"generateGenerationVQuestionGrade",
gradeGenerationVQuestion,
{
useful: END,
"not useful": "transformQuery",
}
);
// Compile
const app = workflow.compile();
运行图¶
const inputs = {
question: "Explain how the different types of agent memory work.",
};
const config = { recursionLimit: 50 };
const prettifyOutput = (output: Record<string, any>) => {
const key = Object.keys(output)[0];
const value = output[key];
console.log(`Node: '${key}'`);
if (key === "retrieve" && "documents" in value) {
console.log(`Retrieved ${value.documents.length} documents.`);
} else if (key === "gradeDocuments" && "documents" in value) {
console.log(`Graded documents. Found ${value.documents.length} relevant document(s).`);
} else {
console.dir(value, { depth: null });
}
}
for await (const output of await app.stream(inputs, config)) {
prettifyOutput(output);
console.log("\n---ITERATION END---\n");
}
---RETRIEVE---
Node: 'retrieve'
Retrieved 4 documents.
---ITERATION END---
---CHECK RELEVANCE---
---GRADE: DOCUMENT RELEVANT---
---GRADE: DOCUMENT NOT RELEVANT---
---GRADE: DOCUMENT RELEVANT---
---GRADE: DOCUMENT RELEVANT---
---DECIDE TO GENERATE---
---DECISION: GENERATE---
Node: 'gradeDocuments'
Graded documents. Found 3 relevant document(s).
---ITERATION END---
---GENERATE---
Node: 'generate'
{
generation: 'Short-term memory in agents involves in-context learning, which is limited by the finite context window length of the model. Long-term memory allows the agent to retain and recall extensive information over extended periods by using an external vector store and fast retrieval mechanisms. Sensory memory involves learning embedding representations for raw inputs like text and images.'
}
---ITERATION END---
---GENERATE GENERATION vs DOCUMENTS GRADE---
---GRADE GENERATION vs DOCUMENTS---
---DECISION: SUPPORTED, MOVE TO FINAL GRADE---
Node: 'generateGenerationVDocumentsGrade'
{ generationVDocumentsGrade: 'yes' }
---ITERATION END---
---GENERATE GENERATION vs QUESTION GRADE---
---GRADE GENERATION vs QUESTION---
---DECISION: USEFUL---
Node: 'generateGenerationVQuestionGrade'
{ generationVQuestionGrade: 'yes' }
---ITERATION END---
在此处查看 LangSmith 追踪 here。¶