Coder Social home page Coder Social logo

c11-chatgpt's Introduction

AI Coding Assistant

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!

Setup

Creating an Account

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.

Getting an API Key

After creating your account, navigate to the API keys section.

Create your own key, or use the one provided to you.

Saving your API Key

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.

Initialize and Install OpenAI

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.

Using the import

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.

[Back to top]

Exercise 1

For Exercise 1, we will run the setup and test our integration to the ChatGPT API. There will be no dynamic user input yet.

File contents

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()

Additional Setup

Name the above file assistant.js. Change the main property in package.json to "main":"assistant.js".

Checkpoint 1.1

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
}

Refining output / Checkpoint 1.2

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)

[Back to top]

Exercise 2

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.

Implementing user input

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.

Solution Hint

{"role": "user", "content": process.argv[2]}

Checkpoint 2.1

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)

Adding system input

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.

Checkpoint 2.2

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)

[Back to top]

Exercise 3

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.

Passing in sample code using fs

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.

Checkpoint 3.1

c11-chatgpt % node assistant.js sampleCode.js
Assistant is typing...

There is no error in the provided code.

(Assistant output may vary)

Bringing user input back / Checkpoint 3.2

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.

Extra Spicy Challenge

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!

[Back to top]

Further Reading

c11-chatgpt's People

Contributors

elvinlimpin avatar

Stargazers

Izabella Ribeiro avatar maaltech avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.