As vector search and Retrieval Augmented Generation(RAG) become mainstream for Generative AI (GenAI) use cases, we’re looking ahead to what’s next. GenAI primarily operates in a one-way direction, generating content based on input data. Generative Feedback Loops (GFL) are focused on optimizing and improving the AI’s outputs over time through a cycle of feedback and learnings based on the production data. In GFL, results generated from Large Language Models (LLMs) like GPT are vectorized, indexed, and saved back into vector storage for better-filtered semantic search operations. This creates a dynamic cycle that adapts LLMs to new and continuously changing data, and user needs. GFL offers personalized, up-to-date summaries and suggestions.
A good example of a GFL in action is personalized recommendations, targeted ads, or identifying trends, where an AI might suggest products to a user based on their browsing history, clicks, or purchases. In this article, we’ll break down how this works and how a feedback loop improves the model outputs over time. Our sample solution will suggest podcasts to users based on their listening history.
You can also quickly jump on the source code hosted on our GitHub and try it yourself.
Data flow in the Generative Feedback Loops
Use Case: Podcast Recommendations Based on Listening History
We will create feedback loops for podcast recommendations that learn user profiles and find out what they love to listen to. To develop this project we:
- Expose APIs using Azure Serverless Functions in Python to handle real-time requests, like adding new podcasts, generating recommendations, and updating user preferences.
- Store all podcasts and user data in the Neon database, including transcripts with vector representations and user listening histories.
- Retrieve a user’s listening history and generate vector embeddings for it using Azure OpenAI service.
- Compare these embeddings with the embeddings of available podcasts in the Neon database using the
pgvector
extension. Relevant podcast episodes are retrieved dynamically based on similarity scores. - Generate personalized podcast suggestions using Azure OpenAI and save these suggestions back into the Neon database for future interactions.
- When new podcasts are added or user preferences change over time, GFL provides more accurate suggestions.
How GFL helps in this use case
The feedback loop has benefits to make the suggestions more realistic:
- Dynamic: Adapts to new data (e.g., podcasts, user preferences) in real-time.
- Personalized: Continuously updates based on user interactions, ensuring recommendations remain relevant.
- Scalable: As more users and podcasts are added, the loop ensures the model improves its output without manual intervention.
Example: Personalized Podcast Suggestion
Input:
- User Listening History: “Podcasts about AI, deep learning, and data science trends.”
- Relevant Podcasts:
- “The AI Revolution”: “Exploring recent AI advancements.”
- “Data Science Today”: “Trends and techniques in data science.”
- “Deep Learning Unplugged”: “Understanding neural networks.”
Setting Up the Neon Project
Prerequisites
Before we begin, make sure you have the following:
- Python 3.8 or later version
- Azure Functions Core Tools installed
- A free Neon account
- An Azure account with an active subscription
- Visual Studio Code on one of the supported platforms.
- The Python extension for Visual Studio Code.
- The Azure Functions extension for Visual Studio Code, version 1.8.1 or later.
Create a Neon Project
- Navigate to the Neon Console
- Click “New Project”
- Select Azure as your cloud provider
- Choose East US 2 as your region
- Give your project a name (e.g., “generative-feedback-loop”)
- Click “Create Project”
- Once the project is created successfully, copy the Neon connection string. You can find the connection details in the Connection Details widget on the Neon Dashboard.
Set Up Database Tables
Open the SQL editor in Neon and execute the following script to set up the schema:
Insert Sample Data
Add sample users:
Setting Up Azure AI Service to Use Models
Let’s set up Azure AI Service and deploy two models: GPT-4 to summarize a podcast transcript and text-embedding-ada-002 to convert a text string (e.g., podcast transcript or user history) into a vector embedding.
Create an Azure OpenAI Resource
Before deploying models, you need an Azure OpenAI resource. Follow these steps:
- Go to the Azure Portal:
- Sign in with your Azure account.
- Create a New OpenAI Resource:
- Click Create a resource and search for Azure OpenAI.
- Click Create to start setting up the resource.
- Fill in the Required Fields:
- Subscription: Select your Azure subscription.
- Resource Group: Choose an existing group or create a new one to organize your resources.
- Region: Pick a region where Azure OpenAI is supported.
- Name: Provide a unique name for your resource (e.g.,
MyOpenAIResource
).
- Review and Create:
- Click Next until you reach the “Review + Create” tab.
- Review the settings and click Create.
Deploy the Models
Once your Azure OpenAI resource is created, you can deploy the models:
Deploy GPT-4o (For Chat and Query Understanding)
- Go to your Azure OpenAI resource in the Azure Portal.
- Click on the Model catalog tab.
- Find the gpt-4o model in the list.
- Click Deploy and follow the prompts:
- Provide a name for the deployment (e.g.,
gpt4o
). - Keep the default settings or adjust based on your needs.
- Provide a name for the deployment (e.g.,
- Wait for the deployment to complete. Once ready, Azure will provide:
- Endpoint URL: The URL to send API requests.
- API Key: The key to authenticate API calls.
Deploy text-embedding-ada-002 (For Embeddings)
- While in the same Model catalog, find the text-embedding-ada-002 model.
- Click Deploy and provide a deployment name (e.g.,
text-embedding-ada-002
). - Follow the same steps as above and wait for deployment.
Use the Models
To connect the Python Azure Functions application to these models, we will use the Endpoint URL and API Key together with model names from your Azure OpenAI resource.
Set Up Azure Functions Project in Python
We use Visual Studio Code to create a Python function that responds to HTTP requests. You can also initiate a function using pure CLI, but VS code makes it easy with built-in commands to generate automatically an Azure Functions project with an HTTP trigger template.
- Create a new folder and name it:
generative-feedback-loop
- Open the folder in VS code.
- In VS Code, press F1 to open the command palette and search for and run the command
Azure Functions: Create New Project...
. - Follow the Microsoft docs “Create a local function project” template to provide the information at the prompts.
- This will initiate a new function project in Python.
Project Structure
The final project structure looks like this:
Set Up Environment Variables
Create a .env
file in your Python project directory:
Add Python Dependencies
Lists Python dependencies in requirements.txt
file:
Install dependencies
Create a main Function App
Create or update function_app.py
that contains Azure Function implementations. It has three functions APIs for the feedback loop process:
- Adds New Podcasts:
POST /add_podcast
- When new episodes are added, embeddings and summaries are generated and stored in the Neon database.This provides a semantic understanding of each podcast episode.
- Updates User Preferences:
POST /update_user_history
- Updates user listening history and vector representations.
- Retrieves Dynamic Recommendations:
GET /recommend_podcasts?user_id=<user_id>
- Fetches relevant podcasts based on real-time comparisons of user embeddings with podcast embeddings (using vector similarity) to provide personalized suggestions.
To make the recommendations more appealing, Azure OpenAI generates a short summary for each episode in recommend_podcasts
function. For example, a long description will become:
Summary: “Explore the latest trends in AI and how it’s shaping our future.”
In the same function, we are updating suggested_podcasts
table each time when a suggestion is generated from the model. This data can be used to analyze past suggestions to avoid redundant recommendations. It also gives more analytics information to measure the performance of recommendations (e.g., average similarity score, engagement rate)
Create a Database Utils
Handles Neon database connections and queries:
Create an OpenAI Client
Handles embedding and summarization requests to Azure OpenAI.
Create Config File
Manages application configuration:
Run the Project Locally
Run the Azure Functions locally
To start the function locally, press F5
in the VS code. Your app starts in the Terminal panel. You can see the URL endpoints of your HTTP-triggered functions running locally.
Test APIs
Add the podcast to the Neon database:
Update the user’s listening history:
Get Recommend Podcasts:
Next Steps
- Deploy to Azure using by following this guide.
- Integrate APIs with a frontend for a complete user experience.
- This example can be further improved by analyzing semantic similarity across episodes and identifying trending topics.
Conclusion
In our podcast recommendation example, GFL is involved in understanding user preferences through their listening history. GLF helped to find relevant episodes by comparing user interests with podcast data in real time. It also improved recommendations by updating the Neon database with new podcasts and preferences.
Additional Resources
- Connect a Python application to Neon using Psycopg
- Optimize pgvector search
- Building AI-Powered Chatbots with Azure AI Studio and Neon
- AI app architecture: vector database per tenant demo
Try it
You can check out our GitHub repository, and give us any feedback on our Discord server!
Neon is a serverless Postgres platform that helps teams ship faster via instant provisioning, autoscaling, and database branching. We have a Free Plan – you can get started without a credit card.