upstash / rag-chat

Batteries included SDK for RAG development.
https://upstash.com/docs/vector/sdks/rag-chat/gettingstarted
MIT License
163 stars 43 forks source link

Why does ragchat response returns a metadata array of 5 undefined? #49

Open enesakar opened 2 months ago

enesakar commented 2 months ago
{
  output: "I'm sorry, but I cannot answer that question based on the provided context and chat history.",
  isStream: false,
  metadata: [ undefined, undefined, undefined, undefined, undefined ],
}
umutbozdag commented 2 months ago

Hi @enesakar, I think I found the cause for it:

Problem When no relevant context is found, the retrieval process returns undefined metadata values in the final output.

Possible Fix In the Database class:

if (allValuesUndefined) {
  console.error("No answer found in the provided context.");
  return []; // Return empty array instead of object with empty metadata
}

In the ContextService class:

return {
  formattedContext: await traceable(
    (_context: PrepareChatResult) => formatFacts(_context.map(({ data }) => data)),
    {
      name: "Step: Format",
      metadata: { sessionId },
      run_type: "tool",
    }
  )(context),
  metadata: context.length > 0 ? context.map(({ metadata }) => metadata) as TMetadata[] : [],
};

Also we can update this place where we return metadata directly, like this: metadata: metadata.length > 0 ? metadata : [{}],

What do you think about it? @enesakar