Refactor a chat message processing function | OpenAI Coding Question