> ## Documentation Index
> Fetch the complete documentation index at: https://enterprise-docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 创建空知识库

> 创建一个空知识库。之后用 [从文本创建文档](/zh/2.5.x/develop/api/documents/create-document-by-text) 或 [从文件创建文档](/zh/2.5.x/develop/api/documents/create-document-by-file) 向其中添加文档。



## OpenAPI

````yaml /zh/2.5.x/develop/api/openapi_service.json post /datasets
openapi: 3.0.1
info:
  title: Dify 服务 API
  description: 用于 Dify 应用与知识库的 REST API。应用类接口使用应用 API 密钥认证，知识库类接口使用知识库 API 密钥认证。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: Dify 服务 API 的基础 URL。替换为你部署的 API 基础 URL。
    variables:
      api_base_url:
        default: api.example.com/v1
        description: API 基础 URL 的主机与路径，不含 `https://` 前缀。
security:
  - ApiKeyAuth: []
tags:
  - name: 对话消息
    description: 与聊天消息和交互相关的操作。
  - name: 文件操作
    description: 文件上传和预览操作。
  - name: 消息反馈
    description: 用户反馈操作。
  - name: 会话管理
    description: 与管理会话相关的操作。
  - name: 语音与文字转换
    description: 文字转语音和语音转文字操作。
  - name: 应用配置
    description: 获取应用设置和信息的操作。
  - name: 工作流运行
    description: 用于执行和管理工作流的操作。
  - name: 文本生成消息
    description: 文本生成相关操作。
  - name: 知识库
    description: 用于管理知识库的操作，包括创建、配置和检索。
  - name: 文档
    description: 用于在知识库中创建、更新和管理文档的操作。
  - name: 分段
    description: 用于管理分段和子分段的操作。
paths:
  /datasets:
    post:
      tags:
        - 知识库
      summary: 创建空知识库
      description: >-
        创建一个空知识库。之后用
        [从文本创建文档](/zh/2.5.x/develop/api/documents/create-document-by-text) 或
        [从文件创建文档](/zh/2.5.x/develop/api/documents/create-document-by-file)
        向其中添加文档。
      operationId: createDataset
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - name
              properties:
                name:
                  type: string
                  minLength: 1
                  maxLength: 40
                  description: 知识库名称。
                description:
                  type: string
                  maxLength: 400
                  default: ''
                  description: 知识库描述。
                indexing_technique:
                  type: string
                  enum:
                    - high_quality
                    - economy
                  nullable: true
                  description: '`high_quality` 使用嵌入模型进行精确搜索；`economy` 使用基于关键词的索引。'
                permission:
                  type: string
                  enum:
                    - only_me
                    - all_team_members
                    - partial_members
                  default: only_me
                  description: >-
                    控制谁可以访问此知识库。`only_me` 仅限创建者，`all_team_members`
                    授权整个工作区访问，`partial_members` 授权指定成员访问。
                provider:
                  type: string
                  enum:
                    - vendor
                    - external
                  default: vendor
                  description: '`vendor` 为内部知识库，`external` 为外部知识库。'
                external_knowledge_api_id:
                  type: string
                  description: 外部知识库 API 连接的 ID。
                external_knowledge_id:
                  type: string
                  description: 外部知识库的 ID。
      responses:
        '200':
          description: 知识库创建成功。
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Dataset'
              examples:
                success:
                  summary: 响应示例
                  value:
                    id: c42e2a6e-40b3-4330-96f8-f1e4d768e8c9
                    name: Product Documentation
                    description: 产品 API 技术文档
                    provider: vendor
                    permission: only_me
                    data_source_type: null
                    indexing_technique: high_quality
                    app_count: 0
                    document_count: 0
                    word_count: 0
                    created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                    created_at: 1741267200
                    updated_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                    updated_at: 1741267200
                    embedding_model: text-embedding-3-small
                    embedding_model_provider: langgenius/openai/openai
                    embedding_available: true
                    retrieval_model_dict:
                      search_method: semantic_search
                      reranking_enable: false
                      reranking_mode: null
                      reranking_model:
                        reranking_provider_name: ''
                        reranking_model_name: ''
                      weights: null
                      top_k: 3
                      score_threshold_enabled: false
                      score_threshold: null
                    tags: []
                    doc_form: text_model
                    external_knowledge_info: null
                    external_retrieval_model: null
                    doc_metadata: []
                    built_in_field_enabled: true
        '400':
          description: '`invalid_param` : 指定的嵌入模型或重排序模型未配置或不可用。'
          content:
            application/json:
              examples:
                invalid_param:
                  summary: invalid_param
                  value:
                    status: 400
                    code: invalid_param
                    message: >-
                      No Embedding Model available. Please configure a valid
                      provider in the Settings -> Model Provider.
        '409':
          description: '`dataset_name_duplicate` : 已存在同名知识库。'
          content:
            application/json:
              examples:
                dataset_name_duplicate:
                  summary: dataset_name_duplicate
                  value:
                    status: 409
                    code: dataset_name_duplicate
                    message: >-
                      The dataset name already exists. Please modify your
                      dataset name.
components:
  schemas:
    Dataset:
      type: object
      properties:
        id:
          type: string
          description: 知识库的唯一标识符。
        name:
          type: string
          description: 知识库的显示名称。在工作区内唯一。
        description:
          type: string
          description: 描述知识库用途或内容的可选文本。
        provider:
          type: string
          description: 供应商类型。内部管理为 `vendor`，外部知识库连接为 `external`。
        permission:
          type: string
          description: 控制谁可以访问此知识库。可选值：`only_me`、`all_team_members`、`partial_members`。
        data_source_type:
          type: string
          description: 文档的数据源类型，尚未配置时为 `null`。
        indexing_technique:
          type: string
          description: '`high_quality` 使用嵌入模型进行精确搜索；`economy` 使用基于关键词的索引。'
        app_count:
          type: integer
          description: 当前使用该知识库的应用数量。
        document_count:
          type: integer
          description: 知识库中的文档总数。
        word_count:
          type: integer
          description: 所有文档的总字数。
        created_by:
          type: string
          description: 创建该知识库的用户 ID。
        created_at:
          type: number
          description: 创建时间戳（Unix 纪元，单位为秒）。
        updated_by:
          type: string
          description: 最后更新该知识库的用户 ID。
        updated_at:
          type: number
          description: 最后更新时间戳（Unix 纪元，单位为秒）。
        embedding_model:
          type: string
          description: 用于索引的嵌入模型名称。
        embedding_model_provider:
          type: string
          description: 嵌入模型供应商标识符（例如 `openai`）。
        embedding_available:
          type: boolean
          description: 配置的嵌入模型当前是否可用。
        retrieval_model_dict:
          type: object
          description: 知识库的检索配置。
          properties:
            search_method:
              type: string
              description: >-
                用于检索的搜索方式。`keyword_search` 表示关键词匹配，`semantic_search`
                表示基于嵌入的语义相似度，`full_text_search` 表示全文索引，`hybrid_search`
                表示语义和关键词混合搜索。
            reranking_enable:
              type: boolean
              description: 是否启用重排序。
            reranking_mode:
              type: string
              nullable: true
              description: >-
                重排序模式。`reranking_model` 表示基于模型的重排序，`weighted_score`
                表示基于分数的加权。重排序禁用时为 `null`。
            reranking_model:
              type: object
              description: 重排序模型配置。
              properties:
                reranking_provider_name:
                  type: string
                  description: 重排序模型供应商标识符（例如 `cohere`）。
                reranking_model_name:
                  type: string
                  description: 重排序模型名称。
            weights:
              type: object
              nullable: true
              description: 混合搜索的权重配置。
              properties:
                weight_type:
                  type: string
                  description: 平衡语义搜索和关键词搜索权重的策略。
                vector_setting:
                  type: object
                  description: 语义搜索权重设置。
                  properties:
                    vector_weight:
                      type: number
                      description: 分配给语义（向量）搜索结果的权重。
                    embedding_provider_name:
                      type: string
                      description: 用于向量搜索的嵌入模型供应商。
                    embedding_model_name:
                      type: string
                      description: 用于向量搜索的嵌入模型名称。
                keyword_setting:
                  type: object
                  description: 关键词搜索权重设置。
                  properties:
                    keyword_weight:
                      type: number
                      description: 分配给关键词搜索结果的权重。
            top_k:
              type: integer
              description: 返回的最大结果数。
            score_threshold_enabled:
              type: boolean
              description: 是否启用分数阈值过滤。
            score_threshold:
              type: number
              description: 结果的最低相关性分数。仅在 `score_threshold_enabled` 为 `true` 时生效。
        tags:
          type: array
          description: 与该知识库关联的标签。
          items:
            type: object
            properties:
              id:
                type: string
                description: 标签标识符。
              name:
                type: string
                description: Tag name.
              type:
                type: string
                description: 标签类型。知识库标签始终为 `knowledge`。
        doc_form:
          type: string
          description: >-
            文档分块模式。`text_model` 表示标准文本分块，`hierarchical_model` 表示父子结构，`qa_model`
            表示问答对提取。
        external_knowledge_info:
          type: object
          nullable: true
          description: 外部知识库的连接详情。当 `provider` 为 `external` 时存在。
          properties:
            external_knowledge_id:
              type: string
              description: 外部知识库的 ID。
            external_knowledge_api_id:
              type: string
              description: 外部知识库 API 连接的 ID。
            external_knowledge_api_name:
              type: string
              description: 外部知识库 API 的显示名称。
            external_knowledge_api_endpoint:
              type: string
              description: 外部知识库 API 的端点 URL。
        external_retrieval_model:
          type: object
          nullable: true
          description: 外部知识库的检索设置。内部知识库时为 `null`。
          properties:
            top_k:
              type: integer
              description: 从外部知识库返回的最大结果数量。
            score_threshold:
              type: number
              description: 最低相关性得分阈值。
            score_threshold_enabled:
              type: boolean
              description: 是否启用分数阈值过滤。
        doc_metadata:
          type: array
          description: 知识库的元数据字段定义。
          items:
            type: object
            properties:
              id:
                type: string
                description: 元数据字段标识符。
              name:
                type: string
                description: 元数据字段名称。
              type:
                type: string
                description: 元数据字段值类型。
        built_in_field_enabled:
          type: boolean
          description: 是否启用内置元数据字段（例如 `document_name`、`uploader`）。
        partial_member_list:
          type: array
          nullable: true
          items:
            type: string
          description: >-
            当 `permission` 为 `partial_members` 时被授予访问权限的成员账户
            ID。更新响应中始终返回；详情响应中仅当 `permission` 为 `partial_members` 时返回。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        每个请求都通过 API Key 认证：`Authorization: Bearer {API_KEY}`。应用接口使用应用 API
        Key，知识库接口使用知识库 API
        Key（[快速开始](/zh/2.5.x/develop/api/guides/get-started)）。


        API Key 应保存在服务端，切勿嵌入客户端代码。缺失或无效的 Key 会返回 HTTP `401`（`unauthorized`）。

````