Having this code:
async function searchInPinecone(indexName, prompt, userId) {
const embeddings = new OpenAIEmbeddings({ apiKey: openAiApiKey });
const index = client.Index(indexName);
// Get the embedding of the prompt
const queryEmbedding = await embeddings.embedQuery(prompt);
// Log the UserId
console.log('UserId used in the search:', userId);
try {
// Perform the search in Pinecone with the filter by UserId
const queryResponse = await index.query({
vector: queryEmbedding,
topK: 20, // Number of closest results to return
includeMetadata: true,
filter: {
UserId: userId.toLowerCase() // Filter to return only results with the provided UserId
}
});
// Log the complete query response
console.log('Query response:', queryResponse);
// Log the metadata used in the search
console.log('Metadata used in the search:', queryResponse.matches.map(match => match.metadata));
// Check if any matches were found
if (queryResponse.matches.length === 0) {
console.log('No matches found for the provided UserId.');
return ['No context found'];
}
// Sort results by the value of {i} in the ID `${doc.id}_chunk_${i}`
const sortedMatches = queryResponse.matches.sort((a, b) => {
const aChunkIndex = parseInt(a.id.split('_chunk_').pop());
const bChunkIndex = parseInt(b.id.split('_chunk_').pop());
return aChunkIndex - bChunkIndex; // Sort by the numeric value of {i}
});
// Extract the relevant context from the sorted results
const fullContext = sortedMatches.map(match => match.metadata ? match.metadata.content : 'No metadata').join('\n\n');
// Split the full context into manageable-sized chunks
const contextChunks = splitContext(fullContext);
return contextChunks.length > 0 ? contextChunks : ['No context found']; // Return the chunks or a default context if empty
} catch (error) {
console.error('Error searching in Pinecone:', error.message);
throw new Error(`Pinecone search failed: ${error.message}`);
}
}
It doesn’t filter well and I don’t know what else I can do. Searching at the docs, I thought that It was like that, but It seems to be not right?? I’m getting this at the logs: Query response: { matches: , namespace: ‘’, usage: { readUnits: 5 } } UserId used in the search: {whatever} I’m creating the metadata like this: return { id:
${doc.id}chunk${i}, values: vector, metadata: { content: chunk, UserId: userId.toLowerCase() } };
That thing is doing it well, I have “content” and “UserId” as it should be, so I don’t know why It is not filtering!! Please Help ):