nlkitai / nlbridge

Mozilla Public License 2.0
8 stars 3 forks source link

Second inputs using the Express.js middleware result in HTTP 400 responses #1

Open brianangulo opened 1 month ago

brianangulo commented 1 month ago

Everything works fine the first request after connecting my express.js backend to your frontend as per your instructions. But whenever I enter input into the chatbox for the second time it fails due to server responding with 400.

Below is the response:

{
    "status": "error",
    "message": "Invalid payload for action \"chat-stream\". payload.conversationHistory contains invalid items"
}

And here is the payload the front end is sending:

{
    "action": "chat-stream",
    "payload": {
        "message": "any",
        "conversationHistory": [
            {
                "role": "user",
                "message": "asda"
            },
            {
                "role": "assistant",
                "message": [
                    "Asda is a British",
                    " supermarket chain that is a",
                    " subsidiary of Walmart. It is one of",
                    " the largest retail chains in",
                    " the UK, selling groceries,",
                    " clothing, electronics, and other products."
                ]
            }
        ]
    }
}
gonzalezw commented 1 month ago

I just ran into this as well. There seems to be two issue with the nlbridge express middleware.

  1. The role value assistant is not recognized as valid in https://github.com/nlkitai/nlbridge/blob/c4f41bb5de0c66937e1a9473f78a725c02dce0df/packages/node/express/src/express/validators/chat/isValidConversationHistory.ts#L14.
  2. The message type is expect to be a string instead of the streaming chat string array.

For now to get around this I added an express middleware before the nlbridge middleware with

router.post(
  '/',
  function (
    req: Request<
      unknown,
      unknown,
      {
        payload: {
          conversationHistory: {
            role: 'ai' | 'assistant' | 'system' | 'user'
            message: string | string[]
          }[]
        }
      }
    >,
    _res: Response,
    next: NextFunction
  ) {
    req.body.payload = {
      ...req.body.payload,
      conversationHistory: req.body.payload.conversationHistory.map((chat) => ({
        ...chat,
        message: Array.isArray(chat.message) ? chat.message.join(' ') : chat.message,
        role: chat.role === 'assistant' ? 'ai' : chat.role,
      })),
    }
    next()
  }
)
brianangulo commented 1 month ago

Unsure is this is still an issue, I ended up using my own custom adapter