Skip to content

Knowledge Bases

A knowledge base is a collection of files used to persist your data in OneContext.

One or more pipelines can be connected to a knowledge base using the KnowledgeBaseFiles step.

Connected Pipelines will be triggered automatically when any files are uploaded to the knowledgebase.

Create a Knowledge Base:

from onecontext import OneContext

oc = OneContext()

knowledgebase = oc.create_knowledgebase(name="my_kb")

Upload Files to an existing Knowledge Base:

from onecontext import OneContext

oc = OneContext()

knowledgebase = oc.KnowledgeBase(name="my_kb")

knowledgebase.upload_file("test_file.txt")

List Files in a Knowledge Base:

Query the files in a knowledge base with specific filtering, sorting and pagination:

from onecontext import OneContext

oc = OneContext()
knowledge_base = oc.KnowledgeBase(name="my_kb")

# To get 20 most recently created files
recent_files = knowledge_base.list_files(limit=20, sort="-date_created")

Delete Files from Knowledge Base

Remove a list of files from a given knowledge base

from onecontext import OneContext

oc = OneContext()
knowledge_base = oc.KnowledgeBase(name="my_kb")

# To delete certain files
knowledge_base.delete_files(['file1.txt', 'file2.pdf'])

Warning

All chunks associated with the deleted file will also be deleted.

Upload File with Metadata

Upload a file associated it with specific metadata:

from onecontext import OneContext

oc = OneContext()
knowledge_base = oc.KnowledgeBase(name="my_kb")

metadata = {"author": "John Doe", "date": "2021-12-12"}
knowledge_base.upload_file("test_file.txt", metadata)

Upload Text as File to the Knowledge Base

To upload the content of a string as a new text file and associate it with optional metadata:

from onecontext import OneContext

oc = OneContext()
knowledge_base = oc.KnowledgeBase(name="my_kb")

text_content = "Some string content"
knowledge_base.upload_text(text_content, "text_file.txt")

Upload All Supported Files from a Directory

This facilitates bulk uploading of files to your knowledge base from a specified directory:

from onecontext import OneContext

oc = OneContext()
knowledge_base = oc.KnowledgeBase(name="my_kb")

knowledge_base.upload_from_directory("/path/to/directory")