Notebooks
M
MongoDB
Self Reflecting Gift Agent Haystack

Self Reflecting Gift Agent Haystack

agentsartificial-intelligencellmsmongodb-genai-showcasenotebooksgenerative-airag

Self-Reflecting Gift Agent with Haystack and MongoDB Atlas

This notebook demonstrates how to build a self-reflecting gift selection agent using Haystack and MongoDB Atlas!

The agent will help optimize gift selections based on children's wishlists and budget constraints, using MongoDB Atlas vector search for semantic matching and implementing self-reflection to ensure the best possible gift combinations.

Components to use in this notebook:

Prerequisites

Before running this notebook, you'll need:

  • A MongoDB Atlas account and cluster
  • Python environment with haystack-ai, mongodb-atlas-haystack and other required packages
  • OpenAI API key for GPT-4 and text-embedding-3-small access
[ ]

Configure Environment

  • Create a free MongoDB Atlas account at https://www.mongodb.com/cloud/atlas/register
  • Create a new cluster (free tier is sufficient). Find more details in this tutorial
  • Create a database user with read/write permissions
  • Get your connection string from Atlas UI (Click "Connect" > "Connect your application")
  • Connection string should look like this mongodb+srv://<db_username>:<db_password>@<clustername>.xxxxx.mongodb.net/?retryWrites=true.... Replace <db_password> in the connection string with your database user's password
  • Enable network access from your IP address in the Network Access settings (have 0.0.0.0/0 address in your network access list).

Set up your MongoDB Atlas and OpenAI credentials:

[ ]

Create Sample Gift Dataset

Let's create a dataset of gifts with prices and categories:

[ ]

Initialize MongoDB Atlas

First, we need to set up our MongoDB Atlas collection and create a vector search index. This step is crucial for enabling semantic search capabilities:

[ ]
New search index named vector_index is building.
Polling to check if the index is ready. This may take up to a minute.
vector_index is ready for querying.

Initialize Document Store and Index Documents

Now let's set up the MongoDBAtlasDocumentStore and index our gift data:

[ ]
Calculating embeddings: 100%|██████████| 1/1 [00:00<00:00,  1.25it/s]
{'doc_embedder': {'meta': {'model': 'text-embedding-3-small',
,   'usage': {'prompt_tokens': 54, 'total_tokens': 54}}},
, 'doc_writer': {'documents_written': 5}}

Create Self-Reflecting Gift Selection Pipeline

Now comes the fun part! Create a pipeline that can:

  1. Take a gift request query
  2. Find relevant gifts using vector search
  3. Self-reflect on selections to optimize for budget and preferences

You need a custom GiftChecker component that can if the more optimizateion is required. Learn how to write your Haystack component in Docs: Creating Custom Components

[ ]
<haystack.core.pipeline.pipeline.Pipeline object at 0x7d5853ba7160>
,🚅 Components
,  - text_embedder: OpenAITextEmbedder
,  - retriever: MongoDBAtlasEmbeddingRetriever
,  - prompt_builder: PromptBuilder
,  - checker: GiftChecker
,  - llm: OpenAIGenerator
,🛤️ Connections
,  - text_embedder.embedding -> retriever.query_embedding (List[float])
,  - retriever.documents -> prompt_builder.documents (List[Document])
,  - prompt_builder.prompt -> llm.prompt (str)
,  - checker.gifts_to_check -> prompt_builder.gifts_to_check (str)
,  - llm.replies -> checker.replies (List[str])

Test Your Gift Selection Agent

Let's test our pipeline with a sample query:

[ ]
Not optimized yet, could find better gift combinations
Science Kit, LEGO Star Wars Set
    Total cost: $84.98
    This selection is under budget and suits the child's interest in science and building things.
    So, Santa says, ""!