Skip to main content
Tune top-k, similarity thresholds, and reranking so retrieval quality matches your use case.
from praisonaiagents import Agent, KnowledgeConfig

agent = Agent(
    name="Researcher",
    instructions="Cite retrieved passages in every answer.",
    knowledge=KnowledgeConfig(sources=["docs/"], top_k=5, similarity_threshold=0.7),
)
agent.start("Summarise the security section of our handbook.")
The user submits a query; the agent pulls the best-matching passages before composing a reply.

Quick Start

1

Simple Usage

Retrieve the top matching passages with a similarity floor.
from praisonaiagents import Knowledge

knowledge = Knowledge(sources=["docs/"], top_k=5, similarity_threshold=0.7)
2

With Configuration

Switch to hybrid search to blend keyword and semantic scoring.
from praisonaiagents import Knowledge

knowledge = Knowledge(
    sources=["docs/"],
    retrieval_strategy="hybrid",
    keyword_weight=0.3,
    semantic_weight=0.7,
)

How It Works


Basic Retrieval

from praisonaiagents import Knowledge

knowledge = Knowledge(
    sources=["docs/"],
    top_k=5,                    # Number of results
    similarity_threshold=0.7    # Minimum similarity
)

Retrieval Strategies

knowledge = Knowledge(
    sources=["docs/"],
    retrieval_strategy="similarity",
    top_k=5
)

MMR (Maximal Marginal Relevance)

knowledge = Knowledge(
    sources=["docs/"],
    retrieval_strategy="mmr",
    top_k=5,
    diversity=0.3  # Balance relevance vs diversity
)
knowledge = Knowledge(
    sources=["docs/"],
    retrieval_strategy="hybrid",
    keyword_weight=0.3,
    semantic_weight=0.7
)

Query Enhancement

Use QueryRewriterAgent for better retrieval:
from praisonaiagents import Agent, Knowledge, QueryRewriterAgent

# Rewrite queries for better retrieval
rewriter = QueryRewriterAgent()

knowledge = Knowledge(sources=["docs/"])

agent = Agent(
    name="Assistant",
    knowledge=knowledge,
    query_rewriter=rewriter
)

Best Practices

Use similarity for semantic questions, hybrid when exact keywords matter, and mmr when you need diverse rather than near-duplicate passages.
diversity=0.3 trades a little relevance for broader coverage. Raise it when top results repeat the same point.
QueryRewriterAgent reshapes short or ambiguous questions into retrieval-friendly form, improving recall without changing the user’s intent.
A high top_k with a low similarity_threshold floods the context; a low top_k with a high threshold can starve it. Adjust both when tuning quality.

Knowledge Module

Full API reference

QueryRewriterAgent

Query optimisation