🧠 Day 2 – Auto-Tagging Thoughts with GPT

Part of the ThoughtArchive Build SeriesDay 1 we saved raw thoughts into Supabase.Today we’re making them smart by auto-tagging each thought using OpenAI.

🚀 What We’re Building Today

When a new thought is saved, we’ll:

  1. Send it to OpenAI using the Chat Completions API

  2. Get back tags and a group

  3. Save that data into the same Supabase row

No backend code. No extra steps. All automated.

🧠 Step 1: Create openai.ts

This file sends your thought to OpenAI and returns tags + a group.

📍 Location: src/lib/openai.ts
copy from here

php

import OpenAI from 'openai'

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })

export async function generateTagsAndGroup(text: string) { const prompt = You are an assistant for organizing personal thoughts.

Given the following thought: "${text}"

Return:

- tags: a comma-separated list of relevant keywords

- group: one short word that categorizes the theme

Respond in this JSON format:

{ "tags": "tag1, tag2, tag3", "group": "category" }

const response = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: prompt }], temperature: 0.5, })

const content = response.choices[0].message.content

return JSON.parse(content || '{}') }

💬 Cursor Prompt

Paste this into a comment in Cursor:

copy from here

pgsql

// Create OpenAI function to send a thought and return tags and group as JSON

Right-click → Ask Cursor.

🔁 Step 2: Add Supabase Update Function

Now update the row with new tags + group.

📍 Location: src/lib/supabaseClient.ts

copy from here

csharp

import { supabase } from './supabaseClient'

export async function updateThoughtTags(id: string, tags: string, group: string) { const { error } = await supabase .from('thoughts') .update({ tags, group }) .eq('id', id) if (error) { console.error('Update failed:', error.message) } }

💬 Cursor Prompt

copy from here

pgsql

// Update Supabase row with new tags and group based on thought id

⚙️ Step 3: Chain It After Insert

Inside your submit logic:

csharp

const { data, error } = await supabase.from('thoughts').insert([...])

if (data && data[0]) { const id = data[0].id const text = data[0].text const { tags, group } = await generateTagsAndGroup(text) await updateThoughtTags(id, tags, group) }

🔐 Add Your OpenAI Key

In your .env.local:

ini

OPENAI_API_KEY=your-openai-key

✅ Enable Row Update Policy

Paste this into Supabase → SQL Editor:

pgsql

CREATE POLICY allow_anon_updates ON thoughts FOR UPDATE TO anon USING (true) WITH CHECK (true);

🧪 Test Example

Try inputting:

I keep putting off building my side projects and it's stressing me out.

Expected response:

json

{ "tags": "procrastination, stress, productivity, side projects", "group": "build" }

✅ Prompts Recap

Paste each of these into Cursor as a comment:

pgsql

- // Create OpenAI function to send a thought and return tags and group as JSON

- // Update Supabase row with new tags and group based on thought id

- // Add logic to call GPT after thought is saved

Then right-click → Ask Cursor.

🎉 Day 2 Complete

Your thoughts are now smart. Every time you save a thought, it gets auto-tagged and grouped without lifting a finger.

Next: voice input with real-time transcription.