DEV Community

Cover image for Use the grammar correction API provided by GPT2API to fix English grammar errors.
New Bing
New Bing

Posted on

Use the grammar correction API provided by GPT2API to fix English grammar errors.

Using GPT2API is easy to create API. So I create a grammar correction API and make it public to the community.

Prompts

You're a native English speaker, and you're good at correcting the grammar inside your statements. Please correct the wrong grammar in the statements provided by the user, and output it in the following JSON format:  {"result":"<right sentence>"}
Enter fullscreen mode Exit fullscreen mode

Visit this link to get more details
https://aicanvas.app/gpt/run?id=100011

Call the API in your project

Get the token

Go to page https://aicanvas.app/gpt/account/api to get the token.

Copy code

Javascript code for demo use, you should replace the in the code.
Javascript

var md5 = require('js-md5');
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var timestamp = parseInt(Date.now()/1000)
var token = "<token>"
var sign = md5(token + timestamp)

var raw = JSON.stringify({
  "timestamp": timestamp,
  "app_id": "1",
  "sign": sign,
  "api": 100011,
  "model": "gpt-3.5-turbo",
  "max_tokens": 2048,
  "query": "$QUERY"
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://aicanvas.app/api/gpt/call", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
Enter fullscreen mode Exit fullscreen mode

Here is the Golang code, replace the before you run it.

package main

import (
  "fmt"
  "strings"
  "time"
  "net/http"
  "io/ioutil"
  "crypto/md5"
  "encoding/hex"
)

func Md5(text string) string {
    hasher := md5.New()
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}

func main() {

  url := "https://aicanvas.app/api/gpt/call"
  method := "POST"
  token := "<token>"
  ts := time.Now().Unix()
  sign := Md5(fmt.Sprintf("%s%d", token, ts))

  payload := strings.NewReader(fmt.Sprintf(`{"timestamp":%d,"app_id":"3","sign":"%s","api":5,"model":"gpt-3.5-turbo","max_tokens":2048,"query":"abc"}`, ts, sign))

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
Enter fullscreen mode Exit fullscreen mode

Try it

Go to page https://aicanvas.app/gpt/run?id=100011, then login and copy code to test the API.

Top comments (0)