Repository navigation
Expand file tree
/
Copy pathretrieveInfo.js
More file actions
181 lines (161 loc) · 6.09 KB
/
Copy pathretrieveInfo.js
File metadata and controls
181 lines (161 loc) · 6.09 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { openai, models, supabase } from "./config.js";
/**
* Generates an embedding vector for a query string
* @param {string} query - The user's query text
* @returns {Promise<number[]>} - The embedding vector (1536 dimensions)
*/
async function generateQueryEmbedding(query) {
const response = await openai.embeddings.create({
model: models.embedding,
input: query,
});
return response.data[0].embedding;
}
/**
* Retrieves relevant information from the database using similarity search
* @param {string} userQuery - The user's query text
* @param {number} matchCount - Maximum number of results to return (default: 5)
* @param {number} matchThreshold - Minimum similarity score (default: 0.1, lowered from 0.3)
* @param {object|null} metadataFilter - Optional metadata filter for narrowing results
* @returns {Promise<{data: Array|null, error: Error|null, queryEmbedding: number[], matchCount: number}>}
*/
async function retrieveInformation(
userQuery,
matchCount = 5,
matchThreshold = 0.3,
metadataFilter = null
) {
console.log(`\n${"═".repeat(60)}`);
console.log(`🔍 SEMANTIC SEARCH`);
console.log(`${"═".repeat(60)}`);
console.log(`📝 Query: "${userQuery}"`);
console.log(`⚙️ Settings: max_results=${matchCount}, threshold=${matchThreshold}`);
if (metadataFilter) {
console.log(`🏷️ Metadata filter: ${JSON.stringify(metadataFilter)}`);
}
console.log(`${"─".repeat(60)}\n`);
try {
// Generate embedding for the user query
console.log(`🧠 Generating query embedding...`);
const queryEmbedding = await generateQueryEmbedding(userQuery);
console.log(`✓ Embedding generated (${queryEmbedding.length} dimensions)\n`);
// Build RPC parameters
const rpcParams = {
query_embedding: queryEmbedding,
match_count: matchCount,
match_threshold: matchThreshold,
};
// Only add filter if provided
if (metadataFilter) {
rpcParams.filter = metadataFilter;
}
// Call the match_information function via Supabase RPC
console.log(`🔄 Searching database for similar content...\n`);
const { data, error } = await supabase.rpc("match_information", rpcParams);
if (error) {
console.error("✗ Error during similarity search:", error.message);
return { data: null, error, queryEmbedding, matchCount };
}
// Log results
if (data && data.length > 0) {
console.log(`${"─".repeat(60)}`);
console.log(`✅ RESULTS FOUND: ${data.length} matching chunk(s)`);
console.log(`${"─".repeat(60)}\n`);
data.forEach((result, index) => {
const similarity = (result.similarity * 100).toFixed(1);
const preview = result.content.substring(0, 100).replace(/\n/g, ' ');
console.log(`📄 Result ${index + 1}:`);
console.log(` Similarity: ${similarity}%`);
console.log(` Content: "${preview}..."`);
// Log key metadata if available
if (result.metadata) {
const meta = result.metadata;
if (meta.name) console.log(` Name: ${meta.name}`);
if (meta.location) console.log(` Location: ${meta.location}`);
if (meta.source_filename) console.log(` Source: ${meta.source_filename}`);
if (meta.chunk_number) console.log(` Chunk: ${meta.chunk_number}/${meta.total_chunks}`);
}
console.log('');
});
} else {
console.log(`${"─".repeat(60)}`);
console.log(`⚠️ NO RESULTS FOUND`);
console.log(`${"─".repeat(60)}`);
console.log(`\nTips to improve results:`);
console.log(` • Try lowering the match_threshold (currently ${matchThreshold})`);
console.log(` • Rephrase your query to be more similar to the stored content`);
console.log(` • Check if data has been ingested into the database\n`);
}
return {
data,
error,
queryEmbedding,
matchCount,
};
} catch (error) {
console.error("✗ Error generating embedding:", error.message);
return {
data: null,
error,
queryEmbedding: null,
matchCount,
};
}
}
/**
* Retrieves information filtered by a specific metadata field
* Useful for queries like "find all developers in Florida"
* @param {string} userQuery - The user's query text
* @param {string} field - The metadata field to filter by (e.g., "location")
* @param {string} value - The value to match (e.g., "Florida")
* @param {number} matchCount - Maximum number of results (default: 5)
* @returns {Promise<object>}
*/
async function retrieveByMetadataField(userQuery, field, value, matchCount = 5) {
const metadataFilter = { [field]: value };
return retrieveInformation(userQuery, matchCount, 0.0, metadataFilter);
}
/**
* Retrieves all information related to a specific person/name
* @param {string} name - The name to search for
* @param {number} matchCount - Maximum number of results (default: 10)
* @returns {Promise<object>}
*/
async function retrieveByName(name, matchCount = 10) {
const metadataFilter = { name: name };
return retrieveInformation(`Information about ${name}`, matchCount, 0.0, metadataFilter);
}
/**
* Gets a simple answer by combining retrieval with the most relevant result
* @param {string} question - The user's question
* @returns {Promise<{answer: string|null, context: object|null, error: Error|null}>}
*/
async function getAnswer(question) {
const result = await retrieveInformation(question, 3, 0.1);
if (result.error || !result.data || result.data.length === 0) {
return {
answer: null,
context: null,
error: result.error || new Error("No relevant information found"),
};
}
// Return the most relevant content as the answer context
const bestMatch = result.data[0];
return {
answer: bestMatch.content,
context: {
similarity: bestMatch.similarity,
metadata: bestMatch.metadata,
source: bestMatch.metadata?.source_filename || "unknown",
},
error: null,
};
}
// Export functions for external use
export {
generateQueryEmbedding,
retrieveInformation,
retrieveByMetadataField,
retrieveByName,
getAnswer
};