Overview

The Recipe Agent generates personalized recipe recommendations based on user inputs like ingredients, time, and dietary preferences. It relies on two recipe PDFs stored in a PgVector-powered knowledgebase for fast and efficient searches, then enhances suggestions with web searches using ExaTools.

Implementation

Create a file ai_recipe_creator_agent.py with the following code:

ai_recipe_creator_agent.py
from phi.agent import Agent
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector
from phi.tools.exa import ExaTools

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge_base = PDFUrlKnowledgeBase(
    urls=[
        "https://www.poshantracker.in/pdf/Awareness/MilletsRecipeBook2023_Low%20Res_V5.pdf",
        "https://www.cardiff.ac.uk/__data/assets/pdf_file/0003/123681/Recipe-Book.pdf",
    ],
    vector_db=PgVector(table_name="recipes", db_url=db_url),  # we are using PgVector here, you can also use other vector dbs
)
knowledge_base.load(recreate=False)

recipe_agent = Agent(
    name="RecipeGenie",
    knowledge_base=knowledge_base,
    search_knowledge=True,
    tools=[ExaTools()],
    markdown=True,
    instructions=[
        "Search for recipes based on the ingredients and time available from the knowledge base.",
        "Include the exact calories, preparation time, cooking instructions, and highlight allergens for the recommended recipes.",
        "Always search exa for recipe links or tips related to the recipes apart from knowledge base.",
        "Provide a list of recipes that match the user's requirements and preferences.",
    ],
)

recipe_agent.print_response(
    "I have potatoes, tomatoes, onions, garlic, ginger, and chicken. Suggest me a quick recipe for dinner", stream=True
)

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.

2

Install required libraries

pip install phidata openai duckdb exa_py pypdf packaging
pip install sqlalchemy pgvector
pip install -U "psycopg[binary]"
3

Set environment variables

export OPENAI_API_KEY=****
export EXA_API_KEY=****
4

Run PgVector using Docker

docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v pgvolume:/var/lib/postgresql/data \
-p 5532:5432 \
--name pgvector \
phidata/pgvector:16
5

Run the agent

python ai_recipe_creator_agent.py