top of page
Search
Writer's pictureKeith Carter

How to Setup AWS API to Run ChatGPT and Avoid Errors from ssl and urllib3 Dependencies

TLDR:

Problem:

When you try to import openai in AWS Lambda, it errors with ssl needs to be v1.1.1 or import boto3 to retrieve the API key, it errors with can’t import DEFAULT_CIPHER required urllib3 v2.02


FIX:

1. Using AWS Cloud9, create a python 3.7 SAM and upgrade to 3.10 (click here for instructions)

2. Create a python environment with urllib3==1.26 in the python env run “pip3.10 install urllib3==1.26” (not with the quotes)

3. Create an AWS Lambda layer with the python environment (click here)

4. This chatgpt code (click here) will work now (as of May 13, 2023)


Details:

It was a sunny Saturday afternoon that started off with a inspiration let's to setup AWS API to run ChatGPT and replace a function call from Wix backend. A quick test on the local PC showed that by running but no JS function we could set up a listener and then call that listener then that listener would go ahead and call the ChatGPT function. So we knew from documentation that we could do the same using the AWS API gateway, Lambda function etc.


Motivations:

1. Running back end functions on Wix has limitations the main being that you can't use it in other formats like connecting up to telegram or line. By creating the API call on AWS will you be able to use the same API for other user interfaces. We also have more control and be able to take advantage of the much larger infrastructure of AWS.

2. We get more error handling and performance information on the OpenAI api call

3. We can switch to other AIs like BARD or ERNIE when available or compare responses during research

4. We got a solid code repository and version control


At first it was easy.


To setup an API on AWS takes just 3 steps (its dirty and not secure but we can start here just to see if the juice is worth the squeeze)


  1. Open Lambda


Keep Python 3.10 it will come in handy later (I know, I started with 3.8, I never like the newest version of python...)


Keep the default create new role with basic Lambda permissions

Click the create button


We'll test this code first:



Create a new test and run it



Enter EventName, anything you want

Click save



There we go, it works! Now lets connect it to an API Gateway


Back that the top choose "Add Trigger




You can use create a new API

keep the default HTTP API

and Security Open


Click Add


Now we have our API Gateway attached. Let's Test!




Copy this endpoint to a cmd prompt and run "CURL https... "




It works! If you click on the link it will open in the web browser

Or you could use the CURL command in the command prompt


It feels like we are seconds away from moving our javascript wix openai call to python on AWS. Afterall, chatgpt can convert the code for you!


Let's test, can I import the "request" library that I need which is equivalent to the node-fetch library in js.


I won't change anything, except add in the import just to see if I will be able to?

Hahaha! Nope!



Well, surely there is a function amazon provides! How about boto.vendored as per StackExchange (click here)



6 year old issue... ok.


What to do? Maybe use urllib3


Well, that imports!



and its successfully tests


OK! now let's get some data! Long story short, this doesn't work well... because you still need to import openai. As soon as we do that we get another error



Simple, follow these instructions to add openai to a layer (click here) Great walk through of adding python packages. But it still doesn't work because if you use python3.7 to 3.9 the openssl version is 1.0.2 but it now needs to be 1.1.1 so you need to recompile python with the upgraded version of ssl. These instruction here work great (click here)


What doesn't work great is starting up a python3.10 environment in Cloud9!!! It actually errors, so you have to start a 3.7SAM and upgrade to 3.10 (at least 30 mins)


OK, once we add the layer to our function we a almost good to go. Right?


Nope, you don't want to expose your API Key so we'll want some code to retrieve it. Introducing import Boto3 and



To get past this I had to go back to the python3.10 env and pip3.10 install urllib3==1.26 and redploy the updated layer.


That fixed it and we are finally of to the races!


And what did we learn... chatGPT is not replacing programmers anytime soon...




Comments


bottom of page