/images/hugo/avatar.png

Langchain Rag 抽象详解

上一节我们了解了 Langchain 中有关 RAG 的抽象。这一节我们来学习这些抽象的详细定义。

1. Document

1
2
3
4
BaseMedia
 └── Blob           ← 原始二进制数据
Document           ← 可用于 LLM 的文档
BaseDocumentTransformer ← 文档的处理/转换器
  • BaseMedia / Blob 强调 原始内容
  • Document 强调 可处理、可索引、可喂给 LLM 的内容
  • BaseDocumentTransformer 强调 对文档的加工或转换逻辑

1.1 Document

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class BaseMedia(Serializable):


    # The ID field is optional at the moment.
    # It will likely become required in a future major release after
    # it has been adopted by enough vectorstore implementations.
    id: Optional[str] = Field(default=None, coerce_numbers_to_str=True)
    """An optional identifier for the document.

    Ideally this should be unique across the document collection and formatted
    as a UUID, but this will not be enforced.

    .. versionadded:: 0.2.11
    """

    metadata: dict = Field(default_factory=dict)


class Blob(BaseMedia):
    data: Union[bytes, str, None] = None
    """Raw data associated with the blob."""
    mimetype: Optional[str] = None
    """MimeType not to be confused with a file extension."""
    encoding: str = "utf-8"
    """Encoding to use if decoding the bytes into a string.

    Use utf-8 as default encoding, if decoding to string.
    """
    path: Optional[PathLike] = None
    """Location where the original content was found."""

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        frozen=True,
    )


class Document(BaseMedia):
    page_content: str
    """String text."""
    type: Literal["Document"] = "Document"

比较复杂的是 Blob,下面是其属性方法的说明

Langchain Rag 相关抽象

这个周期里我们来学习 RAG 相关的知识。我们首先会介绍 Langchain 中有关 RAG 的抽象,然后去看 RagFlow 这个开源项目。

1. Langchain Rag

学习 Langchian 中有关 RAG 的抽象之前,我们先看一个使用 Langchian 实现 Rag 的简单示例。

ABcoder MCP

前面我们了解了 ABcoder Parse 解析过程,并了解到 Parse 会将解析的结果保存为 Repository 结构体,并使用 Json 序列化保存到文件中。这一节我们来学习 ABcoder MCP 的实现。

1 MCP

1.1 入口

main 函数 MCP 启动的入口如下,核心是 mcp.NewServer 函数,该函数会创建一个 MCP 服务器,然后调用 ServeStdio 函数启动服务器。

ABcoder Parse

1. Abcoder 介绍

下面是我在trae 结合 abcoder 提问获取的有关 abcoder 代码结构的回复。

提问: 请你以表格的方式列举出 abcoder 包含哪些模块,这些模块的作用,提供了哪些接口和对象。并以简化代码的方式给出abcoder 核心代码的实现框架,展示这些接口和对象的关系。

MCP 基础

MCP(Model Context Protocol)一种基于 JSON-RPC 2.0 的通信协议,它的目标是让 LLM(或 LLM 代理)与外部数据源、工具、API、文件系统等通过 统一接口 对接。