I could use some help here, I’m using the following versions:
- NodeJS (v20.11.0)
- @pinecone-database/pinecone": “^3.0.2”
- OpenAI model text-embedding-ada-002
Error:
Error during embedding generation: TypeError: records.forEach is not a function
Code: embeddingController.js
const axios = require('axios');
const { Pinecone } = require('@pinecone-database/pinecone');
const { v4: uuidv4 } = require('uuid'); // Use UUID for unique IDs
(async () => {
const fetch = (await import('node-fetch')).default;
global.fetch = fetch; // Resolve fetch implementation warning
})();
// Initialize Pinecone client
const pinecone = new Pinecone({
apiKey: process.env.PINECONE_API_KEY,
});
exports.generateEmbedding = async (req, res) => {
const { text } = req.body;
try {
// Generate embedding from OpenAI API
const response = await axios.post(
'https://api.openai.com/v1/embeddings',
{
model: 'text-embedding-ada-002',
input: text,
},
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
}
);
const embeddings = response.data.data;
if (!Array.isArray(embeddings) || embeddings.length === 0) {
throw new Error('Invalid embedding response from OpenAI API');
}
const embedding = embeddings[0].embedding;
console.log('Length of the embedding:', embedding.length);
// Check if the embedding dimension matches the required length for Pinecone
if (embedding.length !== 1536) {
throw new Error('Embedding dimension mismatch: Expected 1536.');
}
// Assign a unique ID to the vector
const uniqueId = uuidv4();
// Construct the vector to be upserted
const vector = {
id: uniqueId,
values: embedding,
metadata: { text: text } // Store the original text as metadata
};
// Initialize the Pinecone index
const index = pinecone.Index('secret');
// Prepare the payload for upsert
const upsertPayload = {
vectors: [vector] // Pass the vector in an array as required by Pinecone
};
// Log the payload before upserting
console.log('Upsert payload:', upsertPayload);
// Perform the upsert operation
await index.upsert(upsertPayload);
res.status(200).send({ message: 'Embedding generated and stored successfully.', id: uniqueId });
} catch (error) {
console.error('Error during embedding generation:', error);
res.status(500).send(error.message);
}
};```