In this article, we are going to see how to simply integrate our backend with Openai's gpt-3.5 turbo for Static Prompting and generating simple Responses from gpt.
we are going to make the backend for the simple code converter converting a piece of code from one language to another.
Requirements
VS Code Editor (for writing Backend)
Postman (for making requests to backend)
OPENAI API Personal key
Generating OPENAI API key :
At First, we are going to see how to generate our personal OpenAI API key, this key is used by Openai to Authenticate the user and for Usage and Billing purposes, Although Openai charges the usage, but for the first 5$ usage is free, so let us head with generating our key.
Goto Openai website ( OpenAI website click here ), move to the login section if you don't have account then create one with the help of a Google account.
After login process is done, click on the API section as shown in the image
After you clicking on API section, click on the right top corner of Account section and visit the "View API keys" as shown below
On Reaching View API Window, you will be able to create the key, by clicking on the "Create new secret Key" button, do not expose this Key Publically,as it will expired, instead save it somewhere so that you can use it multiple times.
while using "GitHub Repository" create a dotenv and store your key inside it or Otherwise our key will get exposed.
Now that you have created your Secret Key let us head for Creating a Backend.
Creating Our Backend for Code Converter:
Create a project directory in which , we can store all our files and folder
Goto terminal and inside your project directory, type "npm init -y",
this command will add the required package.json file inside your project directory
Now, let us move forward with creating index.js, where all our backend code will be there for the server and inside index.js ,let us install all the dependencies required.
Once again move to terminal and type "npm install express openai ",
if you are using git repository then you also need to install "dotenv",
for safe keeping the key inside it,here the index.js should look like
Explantion of code:
At first we install all the dependencies, the "cors" dependencies should only be install if you are backend and frontend for the same or otherwise you don't need to install it
Further, we have created a routing for converting the code and structured our request in such a manner that the request body should carries the two provided parameter "sourceCode" and the "targetLanguage" you want to convert your Code in JSON format.
Here we have used the Chat completions capability of gpt , in which you have to provide with to properties message and model you want to use
In the below, code we have provided message properties as role and content, the role is used for telling the gpt what your role is and the message is the prompt that you want to carry out operations such as you have to convert the code from sourceCode to targetLanguage,
Now, that we we have create our backend ,it is the time to run, to run the server goto terminal type "node index.js" and woolah! the server start and the in terminal you should see "server is listening at port 5000" in your terminal.
Let us proceed for testing and making request to the server
Using POSTMAN:
Postman is freely available software which can be used to test your api for making the request to the particular server and testing the api endpoint, or you can "thunderclient" as well which you need to install from VS Code.
Open Postman , move to workspace section and enter your server api endpoint in this case "http://localhost:5000/convert", make anke it as post Request and in body set it as JSON format as shown below in the image,
The above image represent what type of request you are sending and also what is your request body format in this it is JSON, as well request body structure as well.
After Clicking on send button we get our reponse from the gpt and the our server extract the response from it and send it back to Postman as we can see in this image,
Above you can see, we get response from the gpt as your code get converted to the "targetLanguage" which is "Python" from JavaScript "\n"
that it will be printed in next line.
Conclusion :
From the above article we can Interpret that for Static prompting , we can use gpt features of Chat Completions, that is Provided by OPENAI and we also can use different gpt engine such as "gpt-3.5-turbo" or we can use "da-vinci" engine, and can we able to integrate our backend as simple as possible and create our app
DO reply for further enhancement if you want to and feel free to try with different engine and different prompting.
please leave a comment if you have any questions or feedback.
I've learned this stuff on the internet as well, Here are the resources given and tried to simplify it from the user's perspective.