JiangWeixian / actions-article-helper

Check issue article grammar with ChatGPT
MIT License
0 stars 0 forks source link

test #1

Open JiangWeixian opened 1 year ago

JiangWeixian commented 1 year ago

My blog site is built on top of GitHub issues. When I create or edit my issue on GitHub, next.js ISR will recreate my blog site in the background. Additionally, ChatGPT is an excellent AI assistant tool that can be used for many purposes, such as a search engine and language translator. As a non-native English speaker, I plan to add ChatGPT to my article writing workflow this weekend to check my articles' grammar like a native English speaker.

TL;DR

If you write articles on GitHub issues like me, you can use the https://github.com/JiangWeixian/actions-article-helper action in your blog repository.

name: "article helper"
on:
  issues:
    types:
      - "opened"
      - "edited"

jobs:
  debug:
    runs-on: ubuntu-latest
    steps:
    - name: checkout
      uses: actions/checkout@v2
    - name: setup node.js
      uses: actions/setup-node@v2
      with:
        node-version: 16
    - name: article helper
      uses: jiangweixian/actions-article-helper@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

DEMO:

image

ChatGPT Open API

Lucky, ChatGPT provide open-api. First you should create API_KEY on OpenAI platform https://platform.openai.com/account/api-keys.

import { ChatGPTAPI } from 'chatgpt'

export const createChatGPTAPI = (apiKey: string) => {
  return new ChatGPTAPI({
    apiKey, // add you open-ai API_KEY here
  })
}

package chatgpt is a request client wrapper the basic open-ai endpoint.

Now, you can send you article content(from issue body) to ChatGPT, let ChatGPT to check grammar and rewrite it.

const client = createChatGPTAPI(apiKey)
client.sendMessage(content)

By default, you can't send article content like Hey, chatgpt help me check {{ content }} grammar. Maybe it will not work as you expect.

To make ChatGPT act as native English speaker, you need prompts. prompts like language(like JavaScript or Rust) that ChatGPT will understand better.

Theres are lots of prompts you can play with from https://github.com/f/awesome-chatgpt-prompts.

In order to make ChatGPT work as English native. Article content will send with the follow prompts, just replace "istanbulu cok seviyom burada olmak cok guzel" with real article content.

I want you to act as an English translator, spelling corrector and improver. I will speak to you in any language and you will detect the language, translate it and answer in the corrected and improved version of my text, in English. I want you to replace my simplified A0-level words and sentences with more beautiful and elegant, upper level English words and sentences. Keep the meaning same, but make them more literary. I want you to only reply the correction, the improvements and nothing else, do not write explanations. My markdown format article is "istanbulu cok seviyom burada olmak cok guzel"

const client = createChatGPTAPI(apiKey)
const result = await chatgptApi.sendMessage(article, {
  stream: true,
  // response original markdown format
  promptPrefix: 'Respond markdown format.<|im_end|>\n',
})

The main(hardest) part is complete, now we go futurter, and make it interaction with github actions.

Github Actions

First, get issue content

const issue = await octokit.rest.issues.get(params)

Then create comment under issue

await octokit.rest.issues.createComment({
  ...,
  body: result.text, // result is ChatGPT respsonse result.
})

That works great, but not perfect. Before issue publish, issue body will edit mutiple times. I don't wan't create too much comments, I need to find previous comment, and update it.

const comments = await octokit.rest.issues.listComments(params)
// find comments by bot<github_actioins> start with `<!--article-helper-->`
const comment = findComment(comments.data)
debug('find comment %o', comment)
if (comment) {
  await octokit.rest.issues.updateComment({
    owner,
    repo,
    comment_id: comment?.id,
    body: withLeadPrefix(`${result.text} ${Date.now()}`),
  })
} else {
  // create comment
}
github-actions[bot] commented 1 year ago

hello world

github-actions[bot] commented 1 year ago

Mon Mar 20 2023 08:57:49 GMT+0000 (Coordinated Universal Time)

Summary

The article explains how to use ChatGPT, an AI assistant tool, for grammar correction in article writing. It also describes how to integrate it with GitHub issues and actions. The author highlights the importance of incorporating this tool for non-native English speakers' writing workflows.

Article

My blog website is constructed atop GitHub issues. Whenever I generate or modify an issue on GitHub, next.js ISR automatically regenerates my website in the background. Moreover, ChatGPT is an exceptional AI assistant tool that can serve various purposes, like a search engine and language translator. As a non-native speaker of English, I intend to incorporate ChatGPT into my article writing procedure this weekend to ensure my grammar matches that of a native English speaker.

TL;DR

If you write articles on GitHub issues like me, you may utilize the https://github.com/JiangWeixian/actions-article-helper action in your blog repository.

DEMO:

image

ChatGPT Open API

Fortunately, ChatGPT provides an open API. Firstly, you should create an API_KEY on the OpenAI platform at https://platform.openai.com/account/api-keys.

The package chatgpt serves as a request client wrapper for the basic open-ai endpoint.

Now, you can submit your article content (from issue body) to ChatGPT, allowing it to examine and rephrase it.

By default, you cannot send article content such as Hey, chatgpt help me check {{ content }} grammar. It may not function as anticipated.

To make ChatGPT operate as a native English speaker, you require prompts. These prompts are akin to languages (like JavaScript or Rust) that ChatGPT comprehends better.

Numerous prompts are available for experimentation at https://github.com/f/awesome-chatgpt-prompts.

To ensure ChatGPT operates as a native English speaker, the article content will be sent with the accompanying prompts, with "istanbulu cok seviyom burada olmak cok guzel" replaced with the actual article content.

The main and most difficult portion is complete. Now we may proceed further and enable it to interact with GitHub actions.

Github Actions

Firstly, retrieve the issue content.

Then, create a comment beneath the issue.

This works well, but not perfectly. Before publication, the issue body will undergo multiple revisions. I do not want to generate excessive comments; therefore, I need to locate the previous comment and update it.