Elasticsearch

Load a PDF into an Elasticsearch index and answer questions from it with an agent.

Code

elasticsearch_db.py
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.elasticsearch import Elasticsearch

knowledge = Knowledge(
    name="Elasticsearch Recipe Knowledge Base",
    description="This is a knowledge base that uses Elasticsearch",
    vector_db=Elasticsearch(
        index_name="recipe",
    ),
)

knowledge.insert(
    name="Thai Recipes",
    url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
    metadata={"doc_type": "recipe_book"},
)

agent = Agent(
    knowledge=knowledge,
    # Enable the agent to search the knowledge base
    search_knowledge=True,
    # A db is required for the agent to read its own chat history
    db=SqliteDb(db_file="tmp/elasticsearch.db"),
    # Enable the agent to read the chat history
    read_chat_history=True,
)
agent.print_response("How to make Thai curry?")

Usage

Set up your virtual environment

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

Install dependencies

uv pip install -U "elasticsearch[async]" pypdf openai sqlalchemy agno

Set environment variables

export OPENAI_API_KEY=xxx

Run Elasticsearch

docker run -d \
  --name elasticsearch \
  -p 9200:9200 \
  -p 9300:9300 \
  -e "discovery.type=single-node" \
  -e "xpack.security.enabled=false" \
  -e "ES_JAVA_OPTS=-Xms1g -Xmx1g" \
  docker.elastic.co/elasticsearch/elasticsearch:9.1.0

Run Agent

python elasticsearch_db.py