Installation
First, install the SDK using pip:
Authentication
To use the SDK, you’ll need your API credentials:
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:
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:
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:
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:
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:
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:
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
- Always check if files exist before trying to create or update a knowledge base
- Use unique names for your knowledge bases to avoid conflicts
- Clean up resources by deleting knowledge bases when they’re no longer needed
- Handle exceptions appropriately in production code
- 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:
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
)