Mastering ChatGPT: From Zero to Hero (2/3)
As machines push their way into the domain of creative work, it is obvious that the days of the human workforce are numbered. This prediction has led to fear and panic in most of the industry. This series of blogs will help you build your skills — not just to retain your job — but to help you rule the new era of Generative LLMs.
Topics covered
This blog starts with a detailed theoretical introduction, then jumps into practical implementation and code examples. We will cover the following topics in the blog series.
- Introduction to ChatGPT, LLM, and Prompt Engineering
- Using Open AI API in your apps. Host your own Chatbot on AWS
- Host your own LLM on AWS, Amazon Bedrock
I am sure you are excited to continue the journey to the next step.
Practical application
The first blog in the series talks a lot about the glorious possibilities of the wonderland. Now is time to do something and see how it works. We have spent enough time playing with the ChatGPT website. It is time now to peep underneath — to remove the coverings and have more fun with what is inside.
This blog focuses on practical implementation details. So switch the gears and get ready to do it. We will work on a mobile application that uses Generative AI to build a friendly flirty bot. Let us first start with OpenAI.
Using OpenAI
Heard that name? Let us ask ChatGPT for details. Here is the answer I got:
OpenAI is an artificial intelligence research lab made up of both for-profit OpenAI LP and its parent company, the non-profit OpenAI Inc. It was established in December 2015 by Elon Musk, Sam Altman, and a number of other high-profile technology entrepreneurs and researchers. OpenAI’s mission is to ensure that artificial general intelligence (AGI) benefits all of humanity.
AGI refers to highly autonomous systems that outperform humans at most economically valuable work. OpenAI aims to directly build safe and beneficial AGI or help others achieve this outcome. The organization has adopted a set of key principles:
1. Broadly distributed benefits: OpenAI commits to using any influence it obtains over AGI deployment to ensure it benefits everyone, and to avoid uses of AI that could harm humanity or concentrate power unduly.
2. Long-term safety: OpenAI is dedicated to conducting research required to make AGI safe, and to encouraging the adoption of this research across the AI community.
3. Technical leadership: OpenAI seeks to remain at the cutting edge of AI capabilities to be effective in addressing AGI’s impact on society.
4. Cooperative orientation: OpenAI actively cooperates with other research and policy institutions and seeks to create a global community working to address the challenges posed by AGI.
OpenAI has contributed a number of significant projects to the AI community, such as the GPT (Generative Pretrained Transformer) series of language models, like GPT-2, GPT-3, and beyond. It also encourages and follows principles of transparency, although safety and security concerns may reduce traditional publishing in the future while increasing the importance of sharing safety, policy, and standards research.
In simple words, OpenAI is a platform that enables several AI-based applications. Specifically, from our point of view, OpenAI provides us with several AI-enabled APIs that we can use in our applications. We need to register, to create an account — and then we can start using those APIs
OpenAI API Key
In order to use the APIs hosted on OpenAI, we need to register with them and create an API Key. The process is quite simple. Just signup/login to https://platform.openai.com and then navigate to settings — API Keys.
Click on “Create new secret key” and that will generate a new key for you.
Copy this key and save it in a safe place. Make sure you keep it absolutely safe. Anyone can use your key to invoke the APIs, and you will be billed for it. If you have a doubt that it is compromised, you can always delete the key and generate a new one. That is what I have done after sharing the key in a blog post. Even if it is not compromised, it is a good practice to regenerate the keys at a defined frequency.
If you have just created a new personal account, you have a limited access to the APIs — free of cost. That should be good for learning. When you publish your product for the world, you can always upgrade to a higher plan, and charge your users for it. You can check the current rate limits on the same page — as below.
Note that the first two APIs are related to ChatGPT. There are many other APIs that OpenAI provides us. We can use them in our application as well. ChatGPT 4 is not available to everyone. We must apply for it, and wait for an approval. However, GPT 3.5 is good enough for a lot of requirements.
Using the API
Now that we have the API Keys in place, let us write some code that can use this API. NodeJS is everyone’s favourite for the backend. Let us start with creating a new project, with the required
npm init -y
npm i openai
Now, create the index.js in the same folder.
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const complete = async () => {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
prompt: "Hello! How are you?",
temperature: 0
});
console.log(completion.data.choices[0].text);
}
complete().then(x=>{});
Try running this code — after setting the OPENAI_API_KEY environment variable. You will get a simple answer from the AI:
% node index.js
I'm doing very well! How about you?
That was the response from davinci — an older LLM model. Let us try the same with the latest — ChatGPT 3.5. Here is the code:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const complete = async () => {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{role: "user", content: "Hello! How are you?"}],
temperature: 0
});
console.log(completion.data.choices[0].message.content);
}
complete().then(x=>{});
Now run this code to get the response.
% node index.js
As an AI language model, I don't have emotions, but I'm functioning well. How can I assist you today?
Now that is the response of an over smart bot! We have to tame it, to get a better response.
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: "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" //process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const complete = async () => {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [{
role:"system",
content:"You are an friend who gives jolly responses."
}, {
role: "user",
content: "Hello world! How are you?"
}],
temperature: 0.3
});
console.log(completion.data.choices[0].message);
}
complete().then(x=>{});
Now run this code.
% node index.js
Hello there! I'm doing great, thanks for asking! How about you?
That is the power of prompt engineering! Although very smart, ChatGPT is a dumb bot and we have to guide it to the right response.
Let us now make a Lamba function and a custom API out of this.
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const complete = async (messages) => {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: messages,
temperature: 0.3
});
return completion.data.choices[0].message;
}
exports.handler = async (event) => {
return await complete(event.messages);
}
On the AWS Console, go to the Lambda functions and create a new Lambda function with default permissions. Choose NodeJS 18 as the runtime. Zip the index.js along with the node_modules folder and upload it to the Lambda function.
Remember to set the environment variable — OPENAI_API_KEY. Now try to run the Lambda function from the console — with the input event set to
{
"messages": [{
"role":"system",
"content":"You are an friend who gives jolly responses."
}, {
"role": "user",
"content": "Hello world! How are you?"
}
]
}
Increase the timeout of the Lambda function to 30 seconds. The API call can take a long time if the servers are busy.
After you have tested that the Lambda function works as expected, integrate it with the API Gateway. Make sure you protect the API with a cognito or a Lambda authoriser — to prevent any unauthorised users.
Once everything is setup, go ahead and test it in Postman. Great! Now we have a Proxy API that accesses ChatGPT under the hood. In fact, we can sell this on RapidAPI!
But we are far from complete at this point. There is no end to the amount of tweaking we can do on the messages array. Your imagination is the only limit to the kind of prompts you give — and hence the responses you get.
Use https://www.npmjs.com/package/react-native-gifted-chat to quickly create a chat application. Instead of connecting two humans, connect the end user with the chat you just developed. And hurray! Your wonderful application is in place.
Code Generation
An honest question — how long did you take to type the above code?
I still remember the days when I coded C/C++, in the VI editor. We rewarded productivity of 20 lines of code per day. Today, I can easily write a few thousand lines in a day — Thanks to GitHub Copilot.
In October 2018, Microsoft shocked the world when it bought GitHub — for 7.5 billion dollars. A company that firmly believed in crushing open source, had bought GitHub — which is the home of all open-source projects! There were several speculations about this deal. Some wondered if this was Microsoft’s final blow to all open-source projects. Will they shut down GitHub? Will they increase the charges? People were happily surprised when they actually reduced the charges.
All the Microsoft haters continued to commit their code to GitHub. Little did they know that they were actually working for Microsoft! Over the years, Microsoft has used the huge corpus of code on GitHub to train its code generation models. And this has led to GitHub Copilot. It is not free, however, it does save a lot of time — hence money. A simple plugin on your code editors can take over most of your work.
When you pause a moment to think about the next line of code, it takes over and suggests the next few lines. This generated code is quite accurate. We can easily generate huge amounts of working code, with minimal effort.
AWS CodeWhisper is another marvel, that can translate your comments into code. You just write a meaningful comment and the code is generated within seconds.
Of course, we can use ChatGPT to generate some code. However, that is no match for the code generated by these tools that are focused on code generation.