Build an application that supports English writing practice using ChatGPT API and NextJS

Tram Ho

Hello all of you!

On the occasion of the new year, I would like to wish all readers of Viblo a happy and prosperous year, achieve much success in career and life!

Surely you all know ChatGPT, an AI bot that has emerged recently with the ability to chat and answer about almost any area of ​​life. Although there are many controversies about the right and wrong of data, it is undeniable that the great power of this tool as well as AI in helping to increase human productivity in many different professions such as programming, marketing, …

In this article, we will use its API to write a simple application that supports users in learning English, and more specifically, optimizing IELTS Writing and Speaking essay writing.

And of course, a lot of the code in this app was written by ChatGPT itself

Since OpenAI has not opened publicly to ChatGPT’s own API, I will use the Text Completion API with the same generate text feature as ChatGPT.

You can refer here.

OpenAI API

Features of this app include:

  • From essay type: IELTS Writing task 2 and user-entered topics, the application provides suggestions, creates sample essays
  • Correcting errors, suggesting sentences, explaining the meaning of words, etc., based on the text that the user entered and the essay title.

You can check the source code of the project here.

https://github.com/ngviethoang/ai-writing-assistant

Application demo.

Writing Assistant

Setting

Initialize NextJS project

Install libraries: OpenAI client, ChakraUI (UI framework)

Register OpenAI API key

Log in to your OpenAI account at https://platform.openai.com/

Create API Secret Key

Create .env file in project and save secret key

Add this .env file to the .gitignore file to avoid revealing the key when committing code

Create a prompt to communicate with the API

To communicate with the Text Completion API, we need to use queries (prompt). This is an important step to get the exact output you want. The terminology in NLP is prompt engineering .

For example, a sample prompt to create a sample outline for the article according to the topic of the IELTS Writing task:

Here we can define parameters that can be passed from the UI:

  • actor : an IELTS test taker with a band score of 8.0
  • question : IELTS Writing Task 2 question
  • content : the text entered by the user

Create a prompt constructor for querying the API based on the actor , question , content parameters.

Create API handler in NextJS

To create an API handler that handles query results from Text Completion, create an API route in the pages/api/prompt.ts

Parameters in Text Completion API used

  • model : use the latest and most powerful text-davinci-003 of the GPT-3 models
  • prompt : the query built in the previous step
  • temperature : determines the stability of the results, the higher the temperature, the more diverse the results will be
  • max_tokens : maximum number of tokens returned, can limit the number of tokens returned per prompt to reduce costs

Interface code

Next is the frontend for the application, I will write basic components like

  • Text editor to enter questions, article content
  • Buttons are used to call the API corresponding to functions such as creating article outlines, creating sample articles, correcting typos, comments, etc.
  • Component that displays the results returned from the API

Create components and layouts for pages using ChakraUI

Render buttons to generate prompts and suggestions from the API

Returns from the API:

This GPT API has a high cost, so we can use cache to save the results of previous queries.

Call API /api/prompt when clicking the above buttons to display the suggested result

Run the app

Application interface

Conclude

Through building this application, hopefully you have grasped how to integrate AI into your application to serve other use cases such as chatbots, tutors, PTs to schedule exercises, etc.

See you in the next posts!

Share the news now

Source : Viblo