Make money with Oziconnect referral program

In this tutorial, you will learn how to build a spell checker inside a cloud function using ChatGPT.

OpenAI’s large-scale language model ChapGPT is much more than just a chat interface. This is a powerful tool that can be used for a variety of tasks such as translation, code generation, and even spell checking as explained below. ChatGPT provides a simple and highly effective way to add AI language analysis and generation capabilities to your projects through a REST API.

All code for this tutorial can be found on GitHub.

table of contents

Cloud function

The code for the cloud function is as follows:


export async function spellcheck( body :  body: string ) 

    
    const  textToCheck  = < textToCheck: string >JSON.parse(body);

    

    
    return 
        statusCode: 200,
        body: JSON.stringify(...)
    ;

This Typescript function becomes a handler for AWS Lambda, accepting HTTP requests as input and returning an HTTP response. In the above example, body Get the fields of an incoming HTTP request, parse it to JSON, and read the properties. textToCheck From the request body.

openai package

To implement spell checker functionality, send: textToCheck Migrate to OpenAI and ask the AI ​​model to fix spelling mistakes. To easily do this, you can use the openai package on NPM. This package is maintained by OpenAI as a convenience Javascript/Typescript wrapper for the OpenAI REST API. It includes all the Typescript types you need and makes calling ChatGPT easy.

Install the openai package as follows:

npm install --save openai

Then you can import and create an instance of. OpenAI Use the class in your function handler to pass the OpenAI API key. In this example, it is stored in an environment variable called in this example. OPENAI_KEY. (Once you sign up for OpenAI, you can find your API key in your user settings.)


import OpenAI from "openai";

export async function spellcheck( body :  body: string ) 

    const  textToCheck :  textToCheck: string  = JSON.parse(body);

    
    const openai = new OpenAI( apiKey: process.env.OPENAI_KEY );

    

    return 
        statusCode: 200,
        body: JSON.stringify(...)
    ;

sample text

Finally, you’ll need sample text with misspellings for testing. There’s no better place to get sample text than by contacting ChatGPT itself.

This text is a good test for a spell checker, as it contains obvious misspellings such as “essense”, as well as more complex grammatical errors such as “princip”.leinstead of “princip”Al”. Errors like this test the spell checker beyond simply looking for words that aren’t in the dictionary. principle and major Since both are valid English words, the spell checker must use the context in which they occur to correctly detect this mistake. A real test!

text in, text out

Easiest way to find misspellings textToCheck The input is to create a prompt that asks ChatGPT to run a spell check and return a corrected version. Later in this tutorial, we’ll explore more powerful ways to get additional data from the OpenAI API, but for now, this simple approach is a good first iteration.

This requires two prompts. The first one, user prompt This tells ChatGPT to check for spelling mistakes.

Please correct the spelling and grammar mistakes in the following text.

Also, system prompt This will cause the model to return only modified text.

You are a copy editor who corrects a piece of text, and you always reply with just the corrected text, no explanation or other writing.

System prompts are useful for giving your model an initial context and instructing it to behave in a certain way in response to all subsequent user prompts. The system prompt here tells you to return to ChatGPT. only Use corrected text and don’t decorate it with descriptions or other leading text.

You can test your system and user prompts in OpenAI Playground.

OpenAI Playground

API calls include openai.chat.completions.create(...) method of OpenAI Calls the class instantiated above and returns a response message.

To put it all together, the code below sends these two prompts along with the input text. openai.chat.completions.create(...) OpenAI API endpoint. Note also that we are specifying the model to use as . gpt-3.5-turbo. You can use any OpenAI model for this, including GPT-4.


import OpenAI from "openai";

export async function spellcheck( body :  body: string ) 

    const  textToCheck :  textToCheck: string  = JSON.parse(body);

    
    const openai = new OpenAI( apiKey: process.env.OPENAI_KEY );

    const userPrompt = 'Correct the spelling and grammatical errors in the following text:nn';

    const gptResponse = await openai.chat.completions.create(
        model: "gpt-3.5-turbo",
        messages: [
            
                role: "system",
                content: "You are a copy editor that corrects pieces of text, you always reply with just the corrected text, no explanations or other description"
            ,
            
                role: "user",
                content: userPrompt + textToCheck
            
        ]
    );

    
    const correctedText = gptResponse.choices[0].message.content;

    return 
        statusCode: 200,
        body: correctedText
    ;

Text input, JSON output

So far, you’ve created an AWS Lambda cloud function that sends text to ChatGPT and returns a corrected version of the text with misspellings removed.but openai With packages, you can do even more. Wouldn’t it be nice if your functions could return structured data that actually lists the substitutions made within the text? That would make it much easier to integrate this cloud functionality with front-end user interfaces. It gets easier.

Fortunately, OpenAI provides an API feature that allows you to do just this: function calls.

function call This is a feature that exists in Several An OpenAI model that allows ChatGPT to respond with structured JSON rather than simple messages. You can receive more useful and predictable JSON responses from your API by instructing your AI model to call a function and specifying the details of the function it can call, including all arguments.

To use function calls, functions An array of chat completion creation options. Here we are telling ChatGPT that the function has been called. makeCorrections exists and can be called with one argument . replacements:

const gptResponse = await openai.chat.completions.create({
    model: "gpt-3.5-turbo-0613",
    messages: [ ... ],
    functions: [
        {
            name: "makeCorrections",
            description: "Makes spelling or grammar corrections to a body of text",
            parameters: {
                type: "object",
                properties: {
                    replacements: 
                        type: "array",
                        description: "Array of corrections",
                        items: 
                            type: "object",
                            properties: 
                                changeFrom: 
                                    type: "string",
                                    description: "The word or phrase to change"
                                ,
                                changeTo: 
                                    type: "string",
                                    description: "The new word or phrase to replace it with"
                                ,
                                reason: 
                                    type: "string",
                                    description: "The reason this change is being made",
                                    enum: ["Grammar", "Spelling"]
                                                                    
                            
                        
                    
                }
            }
        }
    ],    });

The description of the function and all arguments is important here. ChatGPT doesn’t have access to the code, so everything we know about the functions is contained in the descriptions we provide.of parameters Properties describe the signature of the functions that ChatGPT can call and describe the data structure of the arguments according to JSON Schema.

The above function has one argument. replacementsIt conforms to the following TypeScript types:

type ReplacementsArgType = 
    changeFrom: string,
    changeTo: string,
    reason: "Grammar" []

Defining this type in your JSON Schema ensures that the JSON returned by ChatGPT conforms to this predictable shape. JSON.parse() To deserialize this into an object of this type:

const args = <ReplacementsArgType>JSON.parse(responseChoice.message.function_call!.arguments);

put everything together

This is the final code for your AWS Lambda function. Call ChatGPT, list Some corrections to the text.

There are a few more things to note here. As mentioned earlier, only a few OpenAI models support function calls. One of these models is gpt-3.5-turbo-0613So this is specified in the call to the completion endpoint.I also added function_call: name: 'makeCorrections' On the phone. This property tells the model to return the arguments needed to call it. makeCorrections Indicates that this function is not expected to return chat messages.

import OpenAI from "openai";
import  APIGatewayEvent  from "aws-lambda";

type ReplacementsArgType =  "Spelling"
[]

export async function main( body :  body: string ) {

    const  textToCheck :  textToCheck: string  = JSON.parse(body);

    const openai = new OpenAI( apiKey: process.env.OPENAI_KEY );

    const prompt = 'Correct the spelling and grammatical errors in the following text:nn';

    
    const gptResponse = await openai.chat.completions.create({
        model: "gpt-3.5-turbo-0613",
        messages: [
            
                role: "user",
                content: prompt + textToCheck
            
        ],
        functions: [
            {
                name: "makeCorrections",
                description: "Makes spelling or grammar corrections to a body of text",
                parameters: {
                    type: "object",
                    properties: {
                        replacements: 
                            type: "array",
                            description: "Array of corrections",
                            items: 
                                type: "object",
                                properties: 
                                    changeFrom: 
                                        type: "string",
                                        description: "The word or phrase to change"
                                    ,
                                    changeTo: 
                                        type: "string",
                                        description: "The new word or phrase to replace it with"
                                    ,
                                    reason: 
                                        type: "string",
                                        description: "The reason this change is being made",
                                        enum: ["Grammar", "Spelling"]
                                                                        
                                
                            
                        
                    }
                }
            }
        ],
        function_call:  name: 'makeCorrections' 
    });

    const [responseChoice] = gptResponse.choices;

    
    const args = <ReplacementsArgType>JSON.parse(responseChoice.message.function_call!.arguments);

    return 
        statusCode: 200,
        body: JSON.stringify(args)
    ;
}

This function is deployed to AWS Lambda and can be invoked over HTTP using the following request body.


    "textToCheck": "Thier journey to the nearby city was quite the experience. If you could of seen the way people reacted when they first saw the castle, it was as if they were taken back to a era long forgotten. The architecture, with it's grand spires and ancient motifs, captured the essense of a time when knights roamed the land. The principle reason for visiting, however, was the art exhibition showcasing several peices from renowned artists of the past."

The list of fixes is returned as a JSON array like this:

[
  
    "changeFrom": "Thier",
    "changeTo": "Their",
    "reason": "Spelling"
  ,
  
    "changeFrom": "could of",
    "changeTo": "could have",
    "reason": "Grammar"
  ,
  
    "changeFrom": "a",
    "changeTo": "an",
    "reason": "Grammar"
  ,

]

conclusion

By leveraging OpenAI APIs and cloud capabilities, you can create applications that not only identify spelling mistakes, but also understand context and capture complex grammatical nuances that typical spell checkers often miss. Although this tutorial provides the basics, the potential applications of ChatGPT in language analysis and correction are vast. As AI continues to evolve, so will the capabilities of such tools.

All code for this tutorial can be found on GitHub.

Make money with Oziconnect referral program
Make money with Oziconnect referral program
Make money with Oziconnect referral program
Make money with Oziconnect referral program
84512

About Us

We are a leading IT agency in Lagos, Nigeria, providing IT consulting and custom software development services. We offer a wide range of IT solutions across software development, web and mobile application development, blockchain development services, digital marketing, and branding.

Contact Us

25B Lagos-Abekouta Expressway Lagos

info@ozitechgroup.com

Phone: (234) 907 155 5545

@2023 OzitechGroup – All Right Reserved.