This tutorial introduces the learner to the ChatGPT API by walking through how to make an AI Coding Assistant.
Use this repository for the tutorial!
In order to use the ChatGPT API, you will need an openai account.
Navigate to the OpenAI API page and Click on Get Started. Create an account.
After creating your account, navigate to the API keys section.
Create your own key, or use the one provided to you.
On the root directory of your project, create an .env
file. Add the following contents:
OPENAI_API_KEY=<your api key here>
replacing <your api key here>
with your key.
Run:
npm init -y
npm install openai
npm install dotenv # allows your api key to be read
Add the following line in the root object within package.json
:
"type": "module"
This allows you to use the import syntax present on the following section.
Import the openai package using the following lines on a js file.
import OpenAI from "openai"
const openai = new OpenAI()
The above code is only an illustration--no need to use the above code for this tutorial.
For Exercise 1, we will run the setup and test our integration to the ChatGPT API. There will be no dynamic user input yet.
Complete the setup and create the following file:
import dotenv from "dotenv"
import OpenAI from "openai"
dotenv.config()
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
})
async function main() {
console.log("Assistant is typing...")
console.log()
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo",
})
console.log(completion)
}
main()
Name the above file assistant.js
.
Change the main property in package.json
to "main":"assistant.js"
.
Run the file using the following command:
node assistant.js
When running the test command, the output should be similar to the following output:
Assistant is typing...
{
id: 'chatcmpl-8UTWRUDVWX6IZXMTrTCqDv6H0DGC0',
object: 'chat.completion',
created: 1702274015,
model: 'gpt-3.5-turbo-0613',
choices: [ { index: 0, message: [Object], finish_reason: 'stop' } ],
usage: { prompt_tokens: 13, completion_tokens: 7, total_tokens: 20 },
system_fingerprint: null
}
Edit the console log such that the message from ChatGPT is the only response like so:
c11-chatgpt % node assistant.js
Assistant is typing...
How can I assist you today?
(Assistant output may vary)
Now that we are using the ChatGPT API, let's extend the app so that it takes user input. For Exercise 2, we will pass in code which we want ChatGPT to analyze.
We will be implementing user input using the cli.
Edit the code to add process.argv[2]
, which is the first input on the command line, as a message to send to ChatGPT.
Read the Chat Completions API to determine how to add a new message to your current completion.
{"role": "user", "content": process.argv[2]}
Your app should take user input, but not specifically as a coding assistant.
c11-chatgpt % node assistant.js "Can you give me the history of the Turing Test?"
Assistant is typing...
The Turing Test, proposed by the British mathematician and computer scientist Alan Turing...
(Assistant output may vary)
You can add or change system input to the chat in order to specify that you are looking for coding assistance.
Again, you may refer to Chat Completions API if you get stuck.
The app should take user input which it interprets as javascript code. If there is no error, the app should say just that. If there is a possible error, the app should respond with the solution.
c11-chatgpt % node assistant.js "function sayHelloWorld() { console.log('hello world') }; sayHelloWorld()"
Assistant is typing...
There is no error in the provided code.
(Assistant output may vary)
Passing in code as a string is only a small fraction of what you can do with the ChatGPT API as a coding assistant. Ideally, we want to be able to pass in file contents.
The purpose of Exercise 3 is to be able to extend our integration in order to take advantage of the ChatGPT API beyond what is possible with the ChatGPT UI.
Let's use node's fs
module to read the file URL you pass as input. Learn more using this documentation. Or, you know... Use ChatGPT to help you.
c11-chatgpt % node assistant.js sampleCode.js
Assistant is typing...
There is no error in the provided code.
(Assistant output may vary)
You may want to provide further context for your code. Implement the functionality below:
c11-chatgpt % node assistant2.js sampleCode.js "If there's no error, please let me know what I need to do to export the sayHelloWorld function to another file."
Assistant is typing...
To export the `sayHelloWorld` function to another file, you can use ...
(Assistant output may vary)
Refer to Chat Completions API if you get stuck.
Want an extra challenge? Train your model to talk like a caveman, and make caveman analogies. Learn more on the Fine Tuning API documention. Have fun!