Knowledge
This section describes the knowledge base functionality available in the Dify client library.
Dataset Management
- class KnowledgeDataset
Represents a knowledge dataset that can contain multiple documents.
- property documents list[KnowledgeDocument]
Returns all documents in the dataset.
- get_document(document_id: str) KnowledgeDocument
Retrieve a specific document by ID.
- create_document_by_text(text: str, settings: KnowledgeSegmentSettings = None) KnowledgeDocument
Create a new document from text content.
- create_document_by_file(file_path: str, settings: KnowledgeSegmentSettings = None) KnowledgeDocument
Create a new document from a file.
- update_document_from_file(document_id: str, file_path: str, settings: KnowledgeSegmentSettings = None) KnowledgeDocument
Update an existing document with new file content.
- delete()
Delete the entire dataset.
Document
- class KnowledgeDocument
Represents a document within a knowledge dataset.
- __init__(id: str, client: DifyKnowledgeClient, dataset: KnowledgeDataset, batch_id: str | None = None)
Initialize a new document.
- property segments list[KnowledgeSegment]
Returns all segments in the document.
- create_segments(segments: list[KnowledgeDocumentSegmentSettings]) list[KnowledgeSegment]
Create multiple segments in the document.
- get_segment(segment_id: str) KnowledgeSegment
Retrieve a specific segment by ID.
- property indexing_status DocumentIndexingStatuses
Get the current indexing status of the document.
- wait_for_indexing(timeout: int = 60) DocumentIndexingStatuses
Wait for document indexing to complete.
- property data KnowledgeDocumentData
Get detailed document data.
- delete()
Delete the document.
Segment
- class KnowledgeSegment
Represents a segment within a document.
- __init__(id: str, client: DifyKnowledgeClient, dataset: KnowledgeDataset, document: KnowledgeDocument)
Initialize a new segment.
- update(settings: KnowledgeDocumentSegmentSettings)
Update segment settings.
- delete()
Delete the segment.
Data Models
- class DatasetPermissionEnum(str, Enum)
Enumeration of dataset permission levels.
- ONLY_ME
Only the creator can access
- ALL_TEAM
All team members can access
- PARTIAL_TEAM
Selected team members can access
- class DocumentIndexingStatuses(str, Enum)
Enumeration of document indexing states.
- WAITING
- PARSING
- CLEANING
- SPLITTING
- COMPLETED
- INDEXING
- ERROR
- PAUSED
- class KnowledgeToken
Represents an API token for knowledge operations.
- id: str
- type: Literal["dataset"]
- token: str
- last_used_at: Optional[int]
- created_at: int
- class KnowledgeSegmentSettings
Settings for knowledge segment processing.
- name: Optional[str]
- indexing_technique: Literal["high_quality", "economy"]
- process_rule: ProcessRule
- class KnowledgeDocumentSegmentSettings
Settings for document segments.
- content: str
- Text content / question content
- answer: Optional[str]
- Answer content for Q&A mode
- keywords: Optional[list[str]]
- Optional keywords
- class KnowledgeDocumentData
Detailed document information.
- id: str
- name: str
- data_source_type: str
- indexing_status: DocumentIndexingStatuses
- tokens: int
- segment_count: int
- average_segment_length: int
- hit_count: int
- display_status: Literal["queuing", "paused", "indexing", "error", "available", "disabled", "archived"]