generated from trywilco/Anythink-Market-Public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
83 lines (67 loc) · 2.4 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const openai = require('openai');
const couchbase = require('couchbase');
require('dotenv').config();
const useLocalEmbedding = process.env.USE_LOCAL_EMBEDDING === 'true';
let openaiclient = null;
if (!useLocalEmbedding) {
// Initialize OpenAI client only if local embedding is not being used
openaiclient = new openai.OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}
async function generateQueryEmbedding(query) {
if (useLocalEmbedding) {
throw new Error('Local embedding mode is enabled, but no local embedding function is provided here.');
}
if (!openaiclient) {
throw new Error('OpenAI client is not initialized.');
}
const response = await openaiclient.embeddings.create({
model: 'text-embedding-ada-002',
input: query,
});
return response.data[0].embedding;
}
let cluster;
async function init() {
if (!cluster) {
cluster = await couchbase.connect(process.env.COUCHBASE_URL, {
username: process.env.COUCHBASE_USERNAME,
password: process.env.COUCHBASE_PASSWORD,
configProfile: "wanDevelopment",
});
}
return cluster;
}
async function storeEmbedding(content, id) {
try {
console.log(`Generating embedding for ${id}...`);
let embedding;
if (useLocalEmbedding) {
throw new Error('Local embedding mode is enabled, but storeEmbedding function is not set up for local embedding.');
} else {
embedding = await generateQueryEmbedding(content);
}
console.log(`Embedding generated for ${id}.`);
console.log(`Initializing Couchbase connection for ${id}...`);
const cluster = await init();
console.log(`Couchbase initialized for ${id}.`);
const bucket = cluster.bucket(process.env.COUCHBASE_BUCKET);
const collection = bucket.defaultCollection();
let parsedContent;
try {
parsedContent = JSON.parse(content);
} catch (parseErr) {
console.error(`Invalid JSON in file ${id}:`, parseErr);
throw new Error(`Invalid JSON in file ${id}: ${parseErr.message}`);
}
const document = { ...parsedContent, embedding };
const docId = `embedding::${id}`;
console.log(`Storing embedding for ${docId}...`);
await collection.upsert(docId, document);
console.log(`Embedding stored for ${docId}.`);
return { docId, embedding };
} catch (err) {
console.error(`Error in storeEmbedding for ${id}:`, err);
throw err;
}
}
module.exports = { generateQueryEmbedding, storeEmbedding };