Generating Embeddings with OpenAI and Store in Postgres and Supabase
by Suroor Wijdan, Director
In this tutorial, we'll explore how to leverage the OpenAI API to generate embeddings for text and store them in a PostgreSQL database on Supabase. Embeddings are numerical representations of text that capture semantic information, making them valuable for various natural language processing tasks.
Prerequisites
Before you begin, ensure you have the following:
- OpenAI API Key: Sign up for OpenAI and obtain your API key here.
- Node.js and npm: Install Node.js and npm from nodejs.org.
- Supabase Account: Create a Supabase account here.
Step 1: Set Up OpenAI API
Install the necessary npm packages for OpenAI:
npm install openai
Create a JavaScript file (e.g., openaiEmbeddings.js) and add the following code:
// openaiEmbeddings.js
const openai = require('openai');
const openaiApiKey = 'YOUR_OPENAI_API_KEY';
openai.configure({
apiKey: openaiApiKey,
});
async function generateEmbedding(text) {
const response = await openai.Completions.create({
engine: 'text-davinci-002',
prompt: text,
max_tokens: 50, // Adjust as needed
});
return response.choices[0].text.trim();
}
// Example usage
const textData = "The future of web development in 2024";
generateEmbedding(textData)
.then(embedding => {
console.log('Generated Embedding:', embedding);
})
.catch(error => {
console.error('Error generating embedding:', error);
});
Replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key.
Step 2: Set Up Supabase
Install the Supabase JavaScript client:
npm install @supabase/supabase-js
Now, create another JavaScript file (e.g., supabaseStorage.js) and add the following code:
// supabaseStorage.js
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'YOUR_SUPABASE_URL';
const supabaseKey = 'YOUR_SUPABASE_KEY';
const supabase = createClient(supabaseUrl, supabaseKey);
async function insertEmbedding(textData, embeddingVector) {
const { data, error } = await supabase
.from('embeddings')
.upsert([
{
text_data: textData,
embedding_vector: embeddingVector,
},
], { onConflict: ['text_data'] });
if (error) {
console.error('Error inserting embedding:', error);
} else {
console.log('Embedding inserted successfully:', data);
}
}
// Example usage
const textData = "The future of web development in 2024";
const embeddingVector = await generateEmbedding(textData);
await insertEmbedding(textData, embeddingVector);
Replace 'YOUR_SUPABASE_URL' and 'YOUR_SUPABASE_KEY' with your actual Supabase project URL and key.
Step 3: Run the Scripts
Run the scripts in your terminal:
node openaiEmbeddings.js
node supabaseStorage.js
This will generate an embedding using the OpenAI API and store it in your Supabase database.
Congratulations! You've successfully generated embeddings with the OpenAI API and stored them in a PostgreSQL database on Supabase.
Feel free to integrate these scripts into your web applications or further customize them based on your specific requirements.
This tutorial provides a foundation for incorporating embeddings into your applications and leveraging the capabilities of OpenAI and Supabase for natural language processing tasks. Customize the code according to your use case and explore additional features provided by both OpenAI and Supabase for more advanced applications.