Knowledge Base SDK

Create a Knowledge Base from Python, add and remove files, and delete it, using the fi.kb client.

📝
TL;DR

Create a Knowledge Base from a set of policy files, add a file to it, remove one, then delete it, all through the KnowledgeBase client.

For a reference-style walkthrough of each method with no error handling, see Manage with the SDK. This cookbook runs the same six operations end to end, including the update_kb failure you’ll hit if you forget kb_name, and checks each step against the client’s real return values instead of its cached state.

TimeDifficultyPackage
10 minBeginnerfutureagi
Prerequisites
  • Future AGI account → app.futureagi.com
  • API keys: FI_API_KEY and FI_SECRET_KEY (see Get your API keys)
  • Python 3.11
  • A few local documents to index (PDF, DOCX, TXT, or RTF)

Install

pip install futureagi
export FI_API_KEY="your-api-key"
export FI_SECRET_KEY="your-secret-key"

Tutorial

Initialize the client

from fi.kb import KnowledgeBase

kb_client = KnowledgeBase()

KnowledgeBase() reads FI_API_KEY and FI_SECRET_KEY from the environment, so no arguments are required. Pass kb_name here instead if you want the client to attach to a knowledge base that already exists.

Create a knowledge base with files

kb_client = kb_client.create_kb(
    name="support-policies",
    file_paths=["docs/refund-policy.txt", "docs/shipping-policy.txt"],
)

print("Created support-policies")

create_kb returns the client itself, so kb_client stays the same object. If the call returns without raising, the knowledge base was created with both files.

Add a file to the knowledge base

update_kb requires kb_name: it looks the knowledge base up by name before it applies the change, so it has no default to fall back on. Leaving it out fails:

kb_client.update_kb(file_paths=["docs/warranty-policy.txt"])
TypeError: update_kb() missing 1 required positional argument: 'kb_name'

Pass it explicitly:

kb_client = kb_client.update_kb(
    kb_name="support-policies",
    file_paths=["docs/warranty-policy.txt"],
)

print("Added warranty-policy.txt")

If the call returns without raising, the file was added.

List the files in a knowledge base

for file in kb_client.kb.files:
    print(file)

This loop prints the file IDs the knowledge base holds. kb_client.kb is refreshed after every create or update call; the delete calls in the next two steps don’t update this cache.

Delete specific files

kb_client = kb_client.delete_files_from_kb(
    file_names=["shipping-policy.txt"],
)

delete_files_from_kb doesn’t raise if the deletion succeeds. file_names takes the file’s base name as stored in the knowledge base, not its local path: docs/shipping-policy.txt from step 2 is removed here by shipping-policy.txt.

Delete the knowledge base

kb_client.delete_kb(kb_names=["support-policies"])

from fi.kb import KnowledgeBase

KnowledgeBase(kb_name="support-policies")

You should see:

SDKException: Knowledge Base with name 'support-policies' not found. Please create it first or verify the name.

That confirms the knowledge base is gone: nothing by that name can be resolved anymore.

Troubleshooting

SymptomCauseFix
TypeError: update_kb() missing 1 required positional argument: 'kb_name'update_kb called with only file_pathsPass kb_name="support-policies" (or the knowledge base’s actual name) alongside file_paths
SDKException: Knowledge Base with name '...' not found on KnowledgeBase(kb_name=...)kb_name was passed for a knowledge base that doesn’t exist yetDrop kb_name and call create_kb() first, or check the name for typos
UnsupportedFileType on create_kb or update_kbA file extension isn’t PDF, DOCX, TXT, or RTFConvert the file or remove it from file_paths
FileNotFoundException on create_kb, or SDKException: Knowledge Base update failed due to a file processing issue. on update_kbA path in file_paths doesn’t exist relative to the current working directoryCheck the path, or pass an absolute path
InvalidAuthError (403) on any callFI_API_KEY or FI_SECRET_KEY missing or invalidRe-run the export block in the current shell, then verify the keys in Settings > API Keys
SDKException: File with same name already exists.Two files in the same create_kb or update_kb call share a base nameRename or drop the duplicate, then retry
SDKException: Maximum knowledge base size exceeded.The upload would push the knowledge base over the 1 GB total capDrop files until the knowledge base fits the cap, then retry

Next: see the full parameter and return details for every method in the Knowledge Base SDK reference.

Was this page helpful?

Questions & Discussion