Skip to main content

KnowledgeBase

The KnowledgeBase class provides document ingestion, hybrid search, and lifecycle management.

Creating knowledge bases

From a URL

kb = await af.KnowledgeBase.create(
    name="API Documentation",
    description="External API docs for integration reference",
    url="https://docs.example.com",
    chunk_size=1500,
    overlap_percentage=0.15,
)

Shorthand

kb = await af.KnowledgeBase.create_from_url(
    name="Help Center",
    url="https://help.example.com",
)

Retrieving knowledge bases

# By name
kb = await af.KnowledgeBase.get("API Documentation")

# By ID
kb = await af.KnowledgeBase.get("kb_abc123")

# List all
kbs = await af.KnowledgeBase.list()
for kb in kbs:
    print(f"{kb.name}{kb.file_count} files, {kb.chunk_count} chunks")

Searching

The search method supports the full range of retrieval strategies:
results = await kb.search(
    query="How do I authenticate API requests?",
    limit=10,
    search_type="hybrid",             # "semantic", "keyword", or "hybrid"
    similarity_threshold=0.1,
    enable_mmr=True,                   # Maximal Marginal Relevance
    mmr_lambda=0.5,                    # 1.0 = pure relevance, 0.0 = pure diversity
    enable_query_rewrite=True,         # LLM-powered query rewriting
    enable_hyde=True,                  # Hypothetical Document Embeddings
    enable_query_expansion=False,
)

for result in results:
    print(f"[{result.score:.2f}] {result.source_file}")
    print(f"  {result.content[:200]}...")

SearchResult fields

FieldTypeDescription
idstrChunk ID
contentstrChunk text content
scorefloatSimilarity score
source_file`strNone`Source file name
chunk_indexintPosition of chunk in source document
metadatadictAdditional chunk metadata

Managing knowledge bases

# Update metadata
await kb.update(
    description="Updated description",
    tags=["api", "reference"],
)

# Re-index all documents
await kb.refresh()

# Get files
files = await kb.get_files()

# Get documents (chunks)
docs = await kb.get_documents()

# Get stats
stats = await kb.get_stats()
print(f"Files: {stats['file_count']}, Chunks: {stats['chunk_count']}")

# Delete a specific document
await kb.delete_document("doc_abc123")

# Delete the entire KB
await kb.delete()

Binding to agents

agent = await af.Agent.get("SupportAgent")
await agent.register_knowledge_base(kb)

# The agent now searches this KB automatically during conversations
response = await agent.run("How do I authenticate API requests?")

Aliases

# KnowledgeBase can be referenced as KB for brevity
from agentflow import KB

kb = await KB.get("API Documentation")