Retrieval Methods
Configure retrieval strategies for knowledge bases
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
- Simple Usage
Retrieve the top matching passages with a similarity floor.
python from praisonaiagents import Knowledge knowledge = Knowledge(sources=["docs/"], top_k=5, similarity_threshold=0.7) - With Configuration
Switch to hybrid search to blend keyword and semantic scoring.
python 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
Similarity Search
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
)Hybrid Search
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
Pick the strategy that matches the query type
Use similarity for semantic questions, hybrid when exact keywords matter, and mmr when you need diverse rather than near-duplicate passages.
Balance diversity with MMR
diversity=0.3 trades a little relevance for broader coverage. Raise it when top results repeat the same point.
Rewrite vague queries before retrieval
QueryRewriterAgent reshapes short or ambiguous questions into retrieval-friendly form, improving recall without changing the user’s intent.
Tune top_k and threshold together
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.