const fs = require('fs'); const path = require('path'); const { PredictionServiceClient…
- User
const fs = require('fs'); const path = require('path');
const { PredictionServiceClient } = require('@google-cloud/aiplatform').v1; const { helpers } = require('@google-cloud/aiplatform');
const KMeans = require('ml-kmeans'); const PCA = require('ml-pca');
async function loadPainPoints() { console.log("Loading pain points..."); const data = fs.readFileSync('pain_points_results.json', 'utf8'); const results = JSON.parse(data);
const painPoints = [];
for (const demographic in results) { for (const environment in results[demographic]) { const points = results[demographic][environment].pain_points; points.forEach(point => { painPoints.push({ demographic, environment, point }); }); } } console.log(
Total pain points loaded: ${painPoints.length}); return painPoints; }async function getEmbeddings(painPoints) { console.log("Generating embeddings..."); const clientOptions = { apiEndpoint: 'us-central1-aiplatform.googleapis.com' }; const client = new PredictionServiceClient(clientOptions);
const project = 'project-id'; // TODO: Replace with your Google Cloud project ID const location = 'us-central1'; const model = 'text-embedding-004 ; // You can choose a different model if needed
const endpoint =
projects/${project}/locations/${location}/publishers/google/models/${model};const batchSize = 100; // Adjust based on API limits const embeddings = [];
for (let i = 0; i < painPoints.length; i += batchSize) { const batchPoints = painPoints.slice(i, i + batchSize);
codeconst instances = batchPoints.map(p => ({ content: p.point })); const request = { endpoint: endpoint, instances: instances.map(e => helpers.toValue(e)), }; try { const [response] = await client.predict(request); const predictions = response.predictions; predictions.forEach(prediction => { const embeddingsProto = prediction.structValue.fields.embeddings; const valuesProto = embeddingsProto.structValue.fields.values; const embeddingValues = valuesProto.listValue.values.map(v => v.numberValue); embeddings.push(embeddingValues); }); console.log(`Processed ${Math.min(i + batchSize, painPoints.length)} / ${painPoints.length} pain points`); } catch (error) { console.error(`Error processing batch starting at index ${i}: ${error.message}`); // Handle specific errors if needed } - Assistant