In the past I've struggled with getting a model deployment's endpoint to work. Here are the steps I take when I get the Error code: 401 - {'statusCode': 401, 'message': 'Unauthorized.:
- Match AI Studio Chat Endpoint - I'm making an assumption your model is deployed through Azure's AI studio. If that is the case you can use the Chat Playground to provide an example of working code using your endpoint. There is an option to "show code". Within the show code option you can select your programming language of choice. If you're looking for REST call's, I would use the curl language selector and make sure my values match.
- Debug Env Variables - Env variables are tricky. I would ensure that the ENV variables I'm using in my API call are as expected.
echo $AZURE_OPENAI_API_KEY
echo $AZURE_OPENAI_ENDPOINT
echo $OPENAI_API_TYPE
echo $OPENAI_API_VERSION
If you've configured your ENV variables correctly. The ENV variables should match what you see in the AI Studio Chat Playground
- API Call to Test Service - Lastly I'd test using something other than the Script to test the service. I'd use a Postman or similar service to test that when I hit the API endpoint I'm getting the expected result.
Let me know if this helps or you need further clarification.
-Max