PgVector Hybrid Search

Combine PgVector embedding similarity and PostgreSQL full-text rank in one search score.

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector, SearchType

knowledge = Knowledge(
    vector_db=PgVector(
        table_name="hybrid_recipes",
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
        search_type=SearchType.hybrid,
        vector_score_weight=0.7,
    )
)
agent = Agent(knowledge=knowledge, search_knowledge=True)

if __name__ == "__main__":
    knowledge.insert(
        name="Recipes",
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
        metadata={"doc_type": "recipe_book"},
    )
    agent.print_response(
        "How do I make chicken and galangal in coconut milk soup?",
        markdown=True,
    )

vector_score_weight controls the weighted score. 0.7 assigns 70 percent to vector similarity and 30 percent to normalized full-text rank. The value must be between 0 and 1.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai pgvector psycopg pypdf sqlalchemy

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Export the API key

export OPENAI_API_KEY=your_openai_api_key_here

Run the example

python pgvector_hybrid_search.py

Next Steps

TaskGuide
Compare search modesSearch and retrieval
Configure PgVectorPgVector overview

Developer Resources