Code
agentic_rag_with_reranking.py
Copy
Ask AI
"""
1. Run: `pip install openai agno cohere lancedb tantivy sqlalchemy pandas` to install the dependencies
2. Export your OPENAI_API_KEY and CO_API_KEY
3. Run: `python cookbook/agent_basics/rag/agentic_rag_with_reranking.py` to run the agent
"""
from agno.agent import Agent
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reranker.cohere import CohereReranker
from agno.models.openai import OpenAIResponses
from agno.vectordb.lancedb import LanceDb, SearchType
knowledge = Knowledge(
# Use LanceDB as the vector database and store embeddings in the `agno_docs` table
vector_db=LanceDb(
uri="tmp/lancedb",
table_name="agno_docs",
search_type=SearchType.hybrid,
embedder=OpenAIEmbedder(
id="text-embedding-3-small"
), # Use OpenAI for embeddings
reranker=CohereReranker(
model="rerank-multilingual-v3.0"
), # Use Cohere for reranking
),
)
knowledge.add_content_sync(
name="Agno Docs", url="https://docs.agno.com/introduction.md"
)
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
# Agentic RAG is enabled by default when `knowledge` is provided to the Agent.
knowledge=knowledge,
markdown=True,
)
if __name__ == "__main__":
# Load the knowledge base, comment after first run
# agent.knowledge.load(recreate=True)
agent.print_response("What are Agno's key features?")
Usage
1
Set up your virtual environment
Copy
Ask AI
uv venv --python 3.12
source .venv/bin/activate
2
Install dependencies
Copy
Ask AI
uv pip install -U agno cohere lancedb tantivy sqlalchemy pandas
3
Set environment variables
Copy
Ask AI
export OPENAI_API_KEY=your_openai_api_key
export CO_API_KEY=your_cohere_api_key
4
Run Agent
Copy
Ask AI
python agentic_rag_with_reranking.py