ChatGPT API R&D

What I’m trying to do:
Use the ChatGPT API to request an answer to a question

What I’ve tried and what’s not working:
I tried including the openai package that’s loaded in Anvil, however I think it’s an outdated library (0.18.01) which does not support chat. I think the newer library 0.23.01 does… so I’m just using requests to hit the gpt-3.5-turbo model directly.

For some reason the error I’m getting indicates that something in my code is wrong… as I see in the error that the api endpoint url is truncated. I have a more complete api url with all the parameters but since I’m getting this invalid url error, I’m iteratively trying to identify the problem so with the code below I’m simply trying to authenticate to the API

Code Sample:

url = "https://api.openai.com/v1/chat/completions -H \"Authorization: Bearer " + key + "\""

response = requests.request("GET", url)
  
remedy = json.loads(response.text)

that is returning this error message (I’ve x’d out my api key):

{‘error’: {‘message’: ‘Invalid URL (GET /v1/chat/completions -H “Authorization: Bearer sk-xxxxxxxxxxxxxxxx”)’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: None}}

I’m not a requests expert, but I would normally expect the headers to be passed in rather than encoded in the URL. Something like:

headers = {'Authorization': 'Bearer sk-xxx'}
url = "https://api.openai.com/v1/chat/completions"
response = requests.get(url, headers=headers)

@jshaffstall is right - see this section in the docs for the correct syntax :

request(url, [method="GET"], [data=None], [json=False], [headers=None], [username=None], [password=None], [timeout=None]) (more info)

Make an HTTP request to the specified URL.

  • url - The request will be made to this URL.
  • method - The HTTP method. Defaults to ‘GET’.
  • data - The data to send in the request body
  • json - If set to True, the response is parsed into Python objects (dicts/lists/etc), and ‘data’ is JSON-encoded before sending. If False, the response will be a Media object.
  • headers - A dict of strings to set HTTP headers
  • username - If specified, used to perform HTTP Basic authentication
  • password - If specified, used to perform HTTP Basic authentication
  • timeout - An int or float representing the amount of time, in seconds, to wait for a response. Default is 60 seconds.