Description
Describe the bug
A clear and concise description of what the bug is. include full error messages.
I am trying to implement RAG reranking within my firebase functions application using Genkit, following this article:
https://firebase.google.com/docs/genkit/rag
I consistently hit this error:
Reranking failed with correct format: TypeError: Cannot read properties of undefined (reading 'map')
at rerank (/Users/richardmorgan/Documents/dev/neuro/mvp/functions/node_modules/@genkit-ai/ai/lib/reranker.js:123:29)
To Reproduce
Steps to reproduce the behavior. Code samples.
here is an example of my code:
try {
console.log("Starting reranking process...");
// Check if docs is valid
if (!docs || !Array.isArray(docs) || docs.length === 0) {
console.error("docs is invalid:", docs);
throw new Error("Invalid docs array for reranking");
}
// Log the structure of the first document to debug
console.log("First doc structure:", JSON.stringify(docs[0], null, 2));
// Create a simpler query
const queryText = aiPrompt.trim().substring(0, 500);
// Limit length to be safe
console.log("Using query text:", queryText);
// Try reranking with direct document passing
const rerankedDocs = await ai.rerank({
reranker: semanticRanker512,
query: queryText, // Try with plain text instead of Document object
documents: docs, // Pass the retrieved docs directly
});
console.log("Reranking successful!");
console.log("Number of reranked docs:", rerankedDocs.length);
// Check the structure of the reranked docs
if (rerankedDocs.length > 0) {
console.log("First reranked doc structure:",
JSON.stringify(rerankedDocs[0], null, 2));
}
// Include the reranked docs in the final response
res.status(200).json({
data: {
success: true,
message: "Mood assessment submitted successfully",
timestamp: data.timestamp,
aiRecommendations: aiResponse,
aiDocs: docs,
rerankedDocs: rerankedDocs,
},
status: "success",
});
} catch (error) {
console.error("Detailed reranking error:", error);
// Try another approach with explicit document conversion
try {
console.log("Attempting alternative reranking approach...");
// Convert docs to a simpler format that the reranker might accept
const simpleDocs = docs.map((doc) => {
return {
id: doc.metadata?.id ||
`doc_${Math.random().toString(36).substring(7)}`,
content: [{
text: typeof doc.content === "string" ?
doc.content :
JSON.stringify(doc.content),
}],
};
});
console.log("Created simplified docs:", simpleDocs.length);
// Try reranking with the simplified docs
const altRerankedDocs = await ai.rerank({
reranker: semanticRanker512,
query: aiPrompt,
documents: simpleDocs,
});
console.log("Alternative reranking successful!");
// Include these alternative results
res.status(200).json({
data: {
success: true,
message: "Mood assessment submitted successfully",
timestamp: data.timestamp,
aiRecommendations: aiResponse,
aiDocs: docs,
rerankedDocs: altRerankedDocs,
},
status: "success",
});
} catch (secondError) {
console.error("Both reranking approaches failed:", secondError);
// Return response without the reranked docs
res.status(200).json({
data: {
success: true,
message: "Mood assessment submitted success (reranking failed)",
timestamp: data.timestamp,
aiRecommendations: aiResponse,
aiDocs: docs,
},
status: "success",
});
}
}
Expected behavior
A clear and concise description of what you expected to happen.
I expect a response containing a ranked array of docs with scores.
Screenshots
If applicable, add screenshots to help explain your problem.
Runtime (please complete the following information):
- OS: [e.g. Linux, MacOS]
- Version [e.g. 22]
MacOS 12.5
** Node version
- run
node --version
at paste here
v22.2.0
Additional context
Add any other context about the problem here.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status