> ## Documentation Index
> Fetch the complete documentation index at: https://futureagi.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create a Knowledge Base using SDK

> This guide will help you get started with the Knowledge Base (KB) Python SDK.

## Installation

First, install the SDK using pip:

```bash theme={null}
pip install futureagi
```

## Authentication

To use the SDK, you'll need your API credentials:

```python theme={null}
from fi.kb import KnowledgeBase

client = KnowledgeBase(
    fi_api_key="YOUR_API_KEY",
    fi_secret_key="YOUR_SECRET_KEY"
)
```

## Basic Operations

### Creating a Knowledge Base

Create a new knowledge base with files:

```python theme={null}
kb_client = client.create_kb(
    name="my-knowledge-base",
    file_paths=["path/to/file1.txt", "path/to/file2.txt"]
)

print(f"Created KB: {kb_client.kb.id} with name: {kb_client.kb.name}")

```

### Updating a Knowledge Base

Add more files to an existing knowledge base:

```python theme={null}
updated_kb = kb_client.update_kb(file_paths=["path/to/new_file.txt"])
print(f"Updated KB: {updated_kb.kb.id} with name: {updated_kb.kb.name}")

```

### Deleting Files from a Knowledge Base

Remove specific files from your knowledge base:

```python theme={null}
if hasattr(kb_client.kb, 'files') and kb_client.kb.files:
    file_id = kb_client.kb.files[0]
    kb_client = kb_client.delete_files_from_kb(file_ids=[file_id])
    print(f"Deleted file with ID: {file_id}")

```

### Deleting a Knowledge Base

Delete an entire knowledge base:

```python theme={null}
kb_id = kb_client.kb.id
kb_client = kb_client.delete_kb(kb_ids=[kb_id])
print(f"Successfully deleted KB: {kb_id}")

```

## Complete Example

Here's a complete example showing all operations:

```python theme={null}
from fi.kb import KnowledgeBase
import time

# Initialize the client
client = KnowledgeBase(
    fi_api_key="YOUR_API_KEY",
    fi_secret_key="YOUR_SECRET_KEY"
)

# Create a new knowledge base
kb_name = "test-kb-" + str(int(time.time()))
kb_client = client.create_kb(
    name=kb_name,
    file_paths=["file1.txt", "file2.txt"]
)

# Update the knowledge base with new files
updated_kb = kb_client.update_kb(file_paths=["file3.txt"])

# Delete a file from the knowledge base
if hasattr(updated_kb.kb, 'files') and updated_kb.kb.files:
    file_id = updated_kb.kb.files[0]
    kb_client.delete_files_from_kb(file_ids=[file_id])

# Delete the knowledge base
kb_client.delete_kb(kb_ids=[updated_kb.kb.id])

```

## Error Handling

The SDK includes built-in error handling. Always wrap your operations in try-except blocks:

```python theme={null}
try:
    kb_client = client.create_kb(
        name="my-kb",
        file_paths=["file.txt"]
    )
except Exception as e:
    print(f"Failed to create KB: {str(e)}")

```

## Best Practices

1. Always check if files exist before trying to create or update a knowledge base
2. Use unique names for your knowledge bases to avoid conflicts
3. Clean up resources by deleting knowledge bases when they're no longer needed
4. Handle exceptions appropriately in production code
5. Keep your API credentials secure and never commit them to version control

## Additional Configuration

You can also specify a custom base URL for development or testing:

```python theme={null}
client = KnowledgeBase(
    fi_api_key="YOUR_API_KEY",
    fi_secret_key="YOUR_SECRET_KEY",
    fi_base_url="https://api.futureagi.com"  # Optional custom base URL
)

```
