Also some feedback, a bit more technical, since I was trying to see how it works, more of a suggestion I suppose
It looks like you're looping through the documents and asking it for known tags, right? ({str(db.current_library.tags)}.)
I don't know if I would do this through a chat completion and a chat response, there are special functions for keyword-like searching, like embeddings. It's a lot faster, and also probably way cheaper, since you're paying barely anything for embeddings compared to chat tokens