Description
In generate
mode NLWeb returns the same answer twice.
Bug Report: Duplicate Responses from Ask Endpoint
Issue Description
When asking a question through the /ask
endpoint, the system returns the same response twice. The response data with format {"message_type": "nlws", "answer": "something"}
is being sent twice for a single request.
Steps to Reproduce
- Setup NLWeb + Import Data
- set the generate mode in index.html
chat_interface = new ChatInterface('', 'nlwebsearch', 'generate');
- Send a request to the
/ask
endpoint - Observe the response stream
Expected Behavior
The system should send a single response for each request, containing the answer and any relevant item descriptions.
Actual Behavior
The system sends two identical responses:
- An initial response with just the answer
- A second response with the same answer and item descriptions
Root Cause
In the synthesizeAnswer
method of the GenerateAnswer
class, the code was sending an initial message with just the answer before processing URLs:
# Create initial message with just the answer
message = {"message_type": "nlws", "answer": answer, "items": json_results}
logger.info("Sending initial answer")
await self.send_message(message)
Then, after processing URLs and item descriptions, it would send another message with the same answer plus the item descriptions.
Solution
The fix involved removing the initial message sending, ensuring that the response is sent exactly once in all cases:
- If there are no ranked answers, it sends a message with a default answer and empty items list
- If there are URLs and description tasks, it sends a message with the answer and item descriptions
- If there are no URLs found in the synthesis response, it sends a message with just the answer and empty items list
- If there's an error, it sends an error message
This ensures that each request receives exactly one response, eliminating the duplicate message issue.