Files
FastGPT/docSite/content/zh-cn/docs/guide/knowledge_base/api_dataset.md
Archer 3a5d725efd feature: 4.10.1 (#5201)
* add dynamic inputRender (#5127)

* dynamic input component

* fix

* fix

* fix

* perf: dynamic render input

* update doc

* perf: error catch

* num input ui

* fix form render (#5177)

* perf: i18n check

* add log

* doc

* Sync dataset  (#5181)

* perf: api dataset create (#5047)

* Sync dataset (#5120)

* add

* wait

* restructure dataset sync, update types and APIs, add sync hints, and remove legacy logic

* feat: add function to retrieve real file ID from third-party doc library and rename team permission check function for clarity

* fix come console

* refactor: rename team dataset limit check functions for clarity, update API dataset sync limit usage, and rename root directory to "ROOT_FOLDER"

* frat: update sync dataset login

* fix delete.ts

* feat: update pnpm-lock.yaml to include bullmq, fix comments in api.d.ts and type.d.ts, rename API file ID field, optimize dataset sync logic, and add website sync feature with related APIs

* feat: update CollectionCard to support site dataset sync, add API root ID constant and init sync API

* feat: add RootCollectionId constant to replace hardcoded root ID

---------

Co-authored-by: dreamer6680 <146868355@qq.com>

* perf: code

* feat: update success message for dataset sync, revise related i18n texts, and optimize file selection logic (#5166)

Co-authored-by: dreamer6680 <146868355@qq.com>

* perf: select file

* Sync dataset (#5180)

* feat: update success message for dataset sync, revise related i18n texts, and optimize file selection logic

* fix: make listfile function return rawid string

---------

Co-authored-by: dreamer6680 <146868355@qq.com>

* init sh

* fix: ts

---------

Co-authored-by: dreamer6680 <1468683855@qq.com>
Co-authored-by: dreamer6680 <146868355@qq.com>

* update doc

* i18n

---------

Co-authored-by: heheer <heheer@sealos.io>
Co-authored-by: dreamer6680 <1468683855@qq.com>
Co-authored-by: dreamer6680 <146868355@qq.com>
2025-07-11 17:02:48 +08:00

5.5 KiB
Raw Blame History

title, description, icon, draft, toc, weight
title description icon draft toc weight
API 文件库 FastGPT API 文件库功能介绍和使用方式 language false true 405

背景

目前 FastGPT 支持本地文件导入,但是很多时候,用户自身已经有了一套文档库,如果把文件重复导入一遍,会造成二次存储,并且不方便管理。因为 FastGPT 提供了一个 API 文件库的概念,可以通过简单的 API 接口,去拉取已有的文档库,并且可以灵活配置是否导入。

API 文件库能够让用户轻松对接已有的文档库,只需要按照 FastGPT 的 API 文件库规范,提供相应文件接口,然后将服务接口的 baseURL 和 token 填入知识库创建参数中,就能直接在页面上拿到文件库的内容,并选择性导入

如何使用 API 文件库

创建知识库时,选择 API 文件库类型,然后需要配置两个关键参数:文件服务接口的 baseURL 和用于身份验证的请求头信息。只要提供的接口规范符合 FastGPT 的要求,系统就能自动获取并展示完整的文件列表,可以根据需要选择性地将文件导入到知识库中。

你需要提供两个参数:

  • baseURL: 文件服务接口的 baseURL
  • authorization: 用于身份验证的请求头信息,实际请求格式为 Authorization: Bearer <token>

接口规范

接口响应格式:

type ResponseType = {
  success: boolean;
  message: string;
  data: any;
}

数据类型:

// 文件列表中,单项的文件类型
type FileListItem = {
  id: string;
  parentId: string  //也可能为 null 或者 undefined 类型;
  name: string;
  type: 'file' | 'folder';
  hasChild?: boolean; // 是否有子文档folder 强制为 true
  updateTime: Date;
  createTime: Date;
}

1. 获取文件树

{{< tabs tabTotal="2" >}} {{< tab tabName="请求示例" >}} {{< markdownify >}}

{{% alert icon=" " context="success" %}}

  • parentId - 父级 id可选或者 null | undefined。
  • searchKey - 检索词,可选 {{% /alert %}}
curl --location --request POST '{{baseURL}}/v1/file/list' \
--header 'Authorization: Bearer {{authorization}}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "parentId": "",
    "searchKey": ""
}'

{{< /markdownify >}} {{< /tab >}}

{{< tab tabName="响应示例" >}} {{< markdownify >}}

{
    "code": 200,
    "success": true,
    "message": "",
    "data": [
        {
            "id": "xxxx",
            "parentId": "xxxx",
            "type": "file",  // file | folder
            "hasChild": true, // 是否有子文档folder 会强制为 true
            "name":"test.json",
            "updateTime":"2024-11-26T03:05:24.759Z",
            "createTime":"2024-11-26T03:05:24.759Z"
        }
   ]
}

{{< /markdownify >}} {{< /tab >}} {{< /tabs >}}

2. 获取单个文件内容(文本内容或访问链接)

{{< tabs tabTotal="3" >}} {{< tab tabName="请求示例" >}} {{< markdownify >}}

curl --location --request GET '{{baseURL}}/v1/file/content?id=xx' \
--header 'Authorization: Bearer {{authorization}}'

{{< /markdownify >}} {{< /tab >}}

{{< tab tabName="响应示例" >}} {{< markdownify >}}

{
    "code": 200,
    "success": true,
    "message": "",
    "data": {
        "title": "文档标题",
        "content": "FastGPT 是一个基于 LLM 大语言模型的知识库问答系统,提供开箱即用的数据处理、模型调用等能力。同时可以通过 Flow 可视化进行工作流编排,从而实现复杂的问答场景!\n",
        "previewUrl": "xxxx"
    }
}

{{% alert icon=" " context="success" %}}

  • title - 文件标题。
  • content - 文件内容,直接拿来用。
  • previewUrl - 文件链接,系统会请求该地址获取文件内容。

contentpreviewUrl二选一返回,如果同时返回则 content 优先级更高,返回 previewUrl时,则会访问该链接进行文档内容读取。

{{% /alert %}}

{{< /markdownify >}} {{< /tab >}} {{< /tabs >}}

3. 获取文件阅读链接(用于查看原文)

{{< tabs tabTotal="2" >}} {{< tab tabName="请求示例" >}} {{< markdownify >}}

id 为文件的 id。

curl --location --request GET '{{baseURL}}/v1/file/read?id=xx' \
--header 'Authorization: Bearer {{authorization}}'

{{< /markdownify >}} {{< /tab >}}

{{< tab tabName="响应示例" >}} {{< markdownify >}}

{
    "code": 200,
    "success": true,
    "message": "",
    "data": {
        "url": "xxxx"
    }
}

{{% alert icon=" " context="success" %}}

  • url - 文件访问链接,拿到后会自动打开。 {{% /alert %}}

{{< /markdownify >}} {{< /tab >}} {{< /tabs >}}

4. 获取文件详细信息(用于获取文件信息)

{{< tabs tabTotal="2" >}} {{< tab tabName="请求示例" >}} {{< markdownify >}}

id 为文件的 id。

curl --location --request GET '{{baseURL}}/v1/file/detail?id=xx' \
--header 'Authorization: Bearer {{authorization}}'

{{< /markdownify >}} {{< /tab >}}

{{< tab tabName="响应示例" >}} {{< markdownify >}}

{
    "code": 200,
    "success": true,
    "message": "",
    "data": {
        "id": "xxxx",
        "parentId": "xxxx",
        "type": "file",  // file | folder
        "hasChild": true, // 是否有子文档folder 会强制为 true
        "name":"test.json",
        "updateTime":"2024-11-26T03:05:24.759Z",
        "createTime":"2024-11-26T03:05:24.759Z"
    }
}

{{< /markdownify >}} {{< /tab >}} {{< /tabs >}}