How can NumPy be used to efficiently calculate the shortest distance between a point and a line segment?

I had a query, I was generating code for my math assignment and got stuck on how to code a formula or an algorithm that calculates the shortest distance between a point and a line segment, and I used ChatGPT to generate this code:

I wanted a more efficient approach to this code using exclusively NumPY, can anyone help me out? I tried using ChatGPT but it didn’t give me code more efficient than this.