Skip to content

Feishu docs

FeishuDocsReader #

Bases: BaseReader

飞书文档阅读器。

从谷歌文档中读取页面

Source code in llama_index/readers/feishu_docs/base.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class FeishuDocsReader(BaseReader):
    """飞书文档阅读器。

    从谷歌文档中读取页面

"""

    host = "https://open.feishu.cn"
    documents_raw_content_url_path = "/open-apis/docx/v1/documents/{}/raw_content"
    tenant_access_token_internal_url_path = (
        "/open-apis/auth/v3/tenant_access_token/internal"
    )

    def __init__(self, app_id, app_secret) -> None:
        """Args:
    app_id:应用程序的唯一标识符,在创建应用程序后获得。
    app_secret:应用程序密钥,在创建应用程序后获得。
"""
        super().__init__()
        self.app_id = app_id
        self.app_secret = app_secret

        self.tenant_access_token = ""
        self.expire = 0

    def load_data(self, document_ids: List[str]) -> List[Document]:
        """从输入目录加载数据。

Args:
    document_ids(List[str]):文档id的列表。
"""
        if document_ids is None:
            raise ValueError('Must specify a "document_ids" in `load_kwargs`.')

        results = []
        for document_id in document_ids:
            doc = self._load_doc(document_id)
            results.append(Document(text=doc, extra_info={"document_id": document_id}))
        return results

    def _load_doc(self, document_id) -> str:
        """从飞书文档中加载文档。

Args:
    document_id: 文档的ID。

Returns:
    文档内容。
"""
        url = self.host + self.documents_raw_content_url_path.format(document_id)
        if self.tenant_access_token == "" or self.expire < time.time():
            self._update_tenant_access_token()
        headers = {
            "Authorization": f"Bearer {self.tenant_access_token}",
            "Content-Type": "application/json; charset=utf-8",
        }
        response = requests.get(url, headers=headers)
        return response.json()["data"]["content"]

    def _update_tenant_access_token(self):
        """用于更新租户访问令牌。"""
        url = self.host + self.tenant_access_token_internal_url_path
        headers = {"Content-Type": "application/json; charset=utf-8"}
        data = {"app_id": self.app_id, "app_secret": self.app_secret}
        response = requests.post(url, data=json.dumps(data), headers=headers)
        self.tenant_access_token = response.json()["tenant_access_token"]
        self.expire = time.time() + response.json()["expire"]

    def set_lark_domain(self):
        """默认的API端点是为飞书设计的,如果要切换到Lark,我们应该使用set_lark_domain。"""
        self.host = "https://open.larksuite.com"

load_data #

load_data(document_ids: List[str]) -> List[Document]

从输入目录加载数据。

Source code in llama_index/readers/feishu_docs/base.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    def load_data(self, document_ids: List[str]) -> List[Document]:
        """从输入目录加载数据。

Args:
    document_ids(List[str]):文档id的列表。
"""
        if document_ids is None:
            raise ValueError('Must specify a "document_ids" in `load_kwargs`.')

        results = []
        for document_id in document_ids:
            doc = self._load_doc(document_id)
            results.append(Document(text=doc, extra_info={"document_id": document_id}))
        return results

set_lark_domain #

set_lark_domain()

默认的API端点是为飞书设计的,如果要切换到Lark,我们应该使用set_lark_domain。

Source code in llama_index/readers/feishu_docs/base.py
94
95
96
def set_lark_domain(self):
    """默认的API端点是为飞书设计的,如果要切换到Lark,我们应该使用set_lark_domain。"""
    self.host = "https://open.larksuite.com"