MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.01k stars 21.01k forks source link

Python Quickstart sample does not work #122121

Closed userr2232 closed 2 weeks ago

userr2232 commented 2 weeks ago

Hello, I'm using Python 3.9.6 on an Intel Mac. Trying to get started with Groundedness detection, but the sample provided in the docs is giving me an error (I redacted my endpoint from the logs):

Traceback (most recent call last): File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 872, in _get_hostport port = int(host[i+1:]) ValueError: invalid literal for int() with base 10: 'detectGroundedness?api-version=2024-02-15-preview' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/userr2232/Documents/misc/VizBot/test.py", line 4, in <module> conn = http.client.HTTPSConnection("<endpoint>/contentsafety/text:detectGroundedness?api-version=2024-02-15-preview") File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 1384, in __init__ super(HTTPSConnection, self).__init__(host, port, timeout, File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 834, in __init__ (self.host, self.port) = self._get_hostport(host, port) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 877, in _get_hostport raise InvalidURL("nonnumeric port: '%s'" % host[i+1:]) http.client.InvalidURL: nonnumeric port: 'detectGroundedness?api-version=2024-02-15-preview'

Then, when I instantiate HTTPSConnection with port=80, I get another error:

Traceback (most recent call last): File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/encodings/idna.py", line 167, in encode raise UnicodeError("label too long") UnicodeError: label too long The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/userr2232/Documents/misc/VizBot/test.py", line 21, in <module> conn.request("POST", "/contentsafety/text:detectGroundedness?api-version=2024-02-15-preview", payload, headers) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 1257, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 1303, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 1252, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 1012, in _send_output self.send(msg) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 952, in send self.connect() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 1419, in connect super().connect() File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py", line 923, in connect self.sock = self._create_connection( File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/socket.py", line 822, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/socket.py", line 953, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): UnicodeError: encoding with 'idna' codec failed (UnicodeError: label too long)

It seems to be related to Python having a hard limit of 64 characters for the first part of the url, which doesn't make much sense to me, since my host (endpoint) is only 48 characters (including https and .com)

Is there any work around to this? Thank you


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

PesalaPavan commented 2 weeks ago

@userr2232 Thanks for your feedback! We will investigate and update as appropriate.

Naveenommi-MSFT commented 2 weeks ago

@userr2232 If you need help with this, please log a case here, I'd recommend working closer with our support team via an Azure support request.

sakisrokanas commented 2 weeks ago

Remove the "https://" part and also the "/contentsafety/text:detectGroundedness?api-version=2024-02-15-preview" from the endpoint in "conn = http.client.HTTPSConnection" line and it will work. It's a python thing.

Naveenommi-MSFT commented 2 weeks ago

@sakisrokanas Thank you for your response.

@userr2232 If there are any further questions regarding the documentation, please tag me in your reply and we will be happy to continue the conversation.

Naveenommi-MSFT commented 2 weeks ago

@userr2232 We are going to close this thread as resolved but if there are any further questions regarding the documentation, please tag me in your reply and we will be happy to continue the conversation.

Please-close

userr2232 commented 2 weeks ago

Hi @Naveenommi-MSFT, the solution provided does not work for me. I am getting the error message below. I also updated my Python version to 3.11.9, but that hasn't helped either.

Traceback (most recent call last): File "/Users/userr2232/Documents/misc/VizBot/test.py", line 25, in <module> conn.request("POST", "/contentsafety/text:detectGroundedness?api-version=2024-02-15-preview", payload, headers) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1303, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1349, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1298, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1058, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 996, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 1468, in connect super().connect() File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/http/client.py", line 962, in connect self.sock = self._create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 827, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py", line 962, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ socket.gaierror: [Errno 8] nodename nor servname provided, or not known has context menu

For reference, this is how my code after following the suggestion above:

import http.client
import json

endpoint = "https://<redacted>.cognitiveservices.azure.com/"
endpoint_nohttps = endpoint.replace("https://", "")
subscription_key = '<redacted>'

conn = http.client.HTTPSConnection(endpoint_nohttps)
payload = json.dumps({
"domain": "Generic",
"task": "QnA",
"qna": {
   "query": "How much does she currently get paid per hour at the bank?"
},
"text": "12/hour",
"groundingSources": [
   "I'm 21 years old and I need to make a decision about the next two years of my life. Within a week. I currently work for a bank that requires strict sales goals to meet. IF they aren't met three times (three months) you're canned. They pay me 10/hour and it's not unheard of to get a raise in 6ish months. The issue is, **I'm not a salesperson**. That's not my personality. I'm amazing at customer service, I have the most positive customer service \"reports\" done about me in the short time I've worked here. A coworker asked \"do you ask for people to fill these out? you have a ton\". That being said, I have a job opportunity at Chase Bank as a part time teller. What makes this decision so hard is that at my current job, I get 40 hours and Chase could only offer me 20 hours/week. Drive time to my current job is also 21 miles **one way** while Chase is literally 1.8 miles from my house, allowing me to go home for lunch. I do have an apartment and an awesome roommate that I know wont be late on his portion of rent, so paying bills with 20hours a week isn't the issue. It's the spending money and being broke all the time.\n\nI previously worked at Wal-Mart and took home just about 400 dollars every other week. So I know i can survive on this income. I just don't know whether I should go for Chase as I could definitely see myself having a career there. I'm a math major likely going to become an actuary, so Chase could provide excellent opportunities for me **eventually**."
],
"reasoning": False
})
headers = {
'Ocp-Apim-Subscription-Key': subscription_key,
'Content-Type': 'application/json'
}
conn.request("POST", "/contentsafety/text:detectGroundedness?api-version=2024-02-15-preview", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Naveenommi-MSFT commented 2 weeks ago

@userr2232

Could please try this below python version which is mentioned in Azure documentation. and also try to Remove the "https://" part and also the "/contentsafety/text:detectGroundedness?api-version=2024-02-15-preview" from the endpoint in "conn = http.client.HTTPSConnection" line and it will work. It's a python thing. https://www.python.org/downloads/

If still there issue, I'd recommend working closer with our support team via an Azure support request.