Skip to content

Knowledge Base API

API reference for the epidbot.knowledgebase module.

REST surface

The HTTP endpoints for the knowledge base live under /api/v1/kb/* — search, embedding, sources, collections, wiki/knowledge-graph endpoints and (admin-only) backend management, migration, re-embedding and recommendations. See the REST API overview.

LibbyClient

Methods

init

def __init__(
    self,
    base_url: str = None,
    timeout: float = 60.0
)

Initialize the Libby client.

Parameters:

Name Type Default Description
base_url str LIBBY_URL env Libby server URL
timeout float 60.0 Request timeout in seconds

health_check

def health_check(self) -> Dict[str, Any]

Check if Libby server is healthy.

Returns:

# Success
{"status": "healthy", "version": "1.0.0", "database": "duckdb"}

# Error
{"status": "error", "message": "Cannot connect to Libby server"}

is_available

def is_available(self) -> bool

Check if Libby server is available.

add_text

def add_text(
    self,
    text: str,
    doc_name: str,
    collection_name: str = "datasus",
    page_number: int = 0,
    auto_chunk: bool = True
) -> Dict[str, Any]

Embed raw text into the knowledge base.

Parameters:

Name Type Default Description
text str required Text content to embed
doc_name str required Document identifier
collection_name str "datasus" Collection name
page_number int 0 Page number reference
auto_chunk bool True Auto-split large text

Returns:

# Success
{
    "success": True,
    "doc_name": "my_document",
    "doc_hash": "abc123",
    "chunks_embedded": 1
}

# Error
{
    "success": False,
    "error": "Error message"
}

add_document

def add_document(
    self,
    file_path: str,
    collection_name: str = "datasus",
    chunk_size: int = 800,
    chunk_overlap: int = 100
) -> Dict[str, Any]

Upload and embed a PDF document.

Parameters:

Name Type Default Description
file_path str required Path to PDF file
collection_name str "datasus" Collection name
chunk_size int 800 Chunk size in characters
chunk_overlap int 100 Overlap between chunks

add_webpage

def add_webpage(
    self,
    url: str,
    collection_name: str = "datasus"
) -> Dict[str, Any]

Fetch a webpage, extract content, and embed.

Parameters:

Name Type Default Description
url str required URL to fetch
collection_name str "datasus" Collection name

Process:

  1. Fetches URL content
  2. Extracts text from HTML
  3. Chunks if necessary (max 1024 chars)
  4. Embeds each chunk
def search(
    self,
    query: str,
    collection_name: str = None,
    num_docs: int = 5
) -> Dict[str, Any]

Perform hybrid search (vector + keyword).

Parameters:

Name Type Default Description
query str required Search query
collection_name str None Filter by collection
num_docs int 5 Max results

Returns:

{
    "documents": [
        {
            "doc_name": "document_name",
            "content": "Document content excerpt...",
            "page_number": 1,
            "score": 0.85
        }
    ],
    "total": 5
}

list_documents

def list_documents(
    self,
    collection_name: str = None
) -> Dict[str, Any]

List all embedded documents.

Returns:

{
    "documents": [
        {
            "doc_name": "document_name",
            "collection_name": "datasus"
        }
    ],
    "total": 10
}

list_collections

def list_collections(self) -> Dict[str, Any]

List all collections with counts.

Returns:

{
    "collections": [
        {"name": "datasus", "document_count": 15},
        {"name": "epidemiology", "document_count": 5}
    ],
    "total": 2
}

document_exists

def document_exists(
    self,
    doc_name: str,
    collection_name: str = None
) -> bool

Check if document exists.

Parameters:

Name Type Default Description
doc_name str required Document name
collection_name str None Collection to search

Module Functions

get_libby_client

def get_libby_client(base_url: str = None) -> LibbyClient

Get singleton LibbyClient instance.

set_libby_url

def set_libby_url(url: str) -> None

Set Libby server URL and reset client.

Constants

LIBBY_URL = "http://localhost:8000"  # Default URL
DEFAULT_COLLECTION = "datasus"        # Default collection
REQUEST_TIMEOUT = 60.0                 # Default timeout
MAX_CHUNK_SIZE = 1024                  # Max chunk size for embedding
CHUNK_OVERLAP = 100                    # Overlap between chunks

Error Handling

All methods return error information in the response:

result = client.add_text("content", "doc")
if "error" in result:
    print(f"Error: {result['error']}")
elif result.get("success"):
    print("Success!")

Examples

Basic Usage

from epidbot.knowledgebase import LibbyClient

client = LibbyClient()

# Check health
health = client.health_check()
print(health["status"])

# Add text
result = client.add_text(
    text="Important health information...",
    doc_name="health_guide",
    collection_name="epidemiology"
)

# Search
results = client.search("dengue symptoms", num_docs=3)
for doc in results["documents"]:
    print(f"{doc['doc_name']}: {doc['score']}")

Web Page Embedding

client = LibbyClient()

result = client.add_webpage(
    url="https://portalsinan.saude.gov.br/",
    collection_name="datasus"
)

if result.get("success"):
    print(f"Added {result['chunks_embedded']} chunks")

Check for Duplicates

if not client.document_exists("my_document", "datasus"):
    client.add_text(content, "my_document", "datasus")
else:
    print("Document already exists")