KnowledgeBase Class

The KnowledgeBase class provides a client for managing knowledge bases, including creating, updating, and deleting knowledge bases and their files.

Initialization

def __init__(
    self,
    kbase: Optional[KnowledgeBaseConfig] = None,
    fi_api_key: Optional[str] = None,
    fi_secret_key: Optional[str] = None,
    fi_base_url: Optional[str] = None,
    **kwargs,
)

Arguments:

  • kbase (Optional[KnowledgeBaseConfig]): The configuration for the knowledge base. If provided and has no ID, the config will be fetched by name.
  • fi_api_key (Optional[str]): API key for authentication.
  • fi_secret_key (Optional[str]): Secret key for authentication.
  • fi_base_url (Optional[str]): Base URL for the API.
  • **kwargs: Additional keyword arguments for advanced configuration.

Raises:

  • SDKException: If the knowledge base is not found or already exists.

KnowledgeBaseConfig Class

The KnowledgeBaseConfig class defines the configuration and metadata for a knowledge base.

class KnowledgeBaseConfig(BaseModel):
    id: Optional[uuid.UUID] = None
    name: str
    status: str = StatusType.PROCESSING.value
    last_error: Optional[str] = None
    files: List[str] = []

Attributes:

  • id (Optional[uuid.UUID]): Unique identifier for the knowledge base.
  • name (str): Name of the knowledge base.
  • status (str): Status of the knowledge base (default: "PROCESSING").
  • last_error (Optional[str]): Last error message, if any.
  • files (List[str]): List of file names associated with the knowledge base.

Instance Methods

create_kb

Creates a new knowledge base and optionally uploads files.

def create_kb(
    self,
    name: Optional[str] = None,
    file_paths: Optional[Union[str, List[str]]] = [],
) -> "KnowledgeBase"

Arguments:

  • name (Optional[str]): Name of the knowledge base.
  • file_paths (Optional[Union[str, List[str]]]): List of file paths or a directory path to upload.

Returns:

  • KnowledgeBase instance (self, for chaining)

Raises:

  • SDKException: If a knowledge base already exists or file upload fails.

update_kb

Updates the name of the knowledge base and/or adds files to it.

def update_kb(
    self,
    name: Optional[str] = None,
    file_paths: Optional[Union[str, List[str]]] = [],
) -> "KnowledgeBase"

Arguments:

  • name (Optional[str]): New name for the knowledge base.
  • file_paths (Optional[Union[str, List[str]]]): List of file paths or a directory path to upload.

Returns:

  • KnowledgeBase instance (self, for chaining)

Raises:

  • SDKException: If update fails or file upload fails.

delete_files_from_kb

Deletes files from the knowledge base.

def delete_files_from_kb(
    self,
    file_names: List[str],
) -> "KnowledgeBase"

Arguments:

  • file_names (List[str]): List of file names to delete.

Returns:

  • KnowledgeBase instance (self, for chaining)

Raises:

  • SDKException: If deletion fails.

delete_kb

Deletes a knowledge base by ID(s).

def delete_kb(
    self,
    kb_ids: Optional[Union[str, List[str]]] = None,
) -> "KnowledgeBase"

Arguments:

  • kb_ids (Optional[Union[str, List[str]]]): List of knowledge base IDs or a single ID to delete. If not provided, deletes the currently configured knowledge base.

Returns:

  • KnowledgeBase instance (self, for chaining)

Raises:

  • SDKException: If deletion fails.

Example Usage

from fi.knowledgebase import KnowledgeBase

# Initialize client
kb_client = KnowledgeBase(fi_api_key="your_api_key", fi_secret_key="your_secret_key")

# Create a new knowledge base with files
kb_client.create_kb(name="My Knowledge Base", file_paths=["/path/to/file1.pdf", "/path/to/file2.txt"])

# Update the knowledge base (add more files or rename)
kb_client.update_kb(name="Updated KB Name", file_paths=["/path/to/newfile.docx"])

# Delete files from the knowledge base
kb_client.delete_files_from_kb(file_names=["file1.pdf"])

# Delete the knowledge base
kb_client.delete_kb()