🇯🇵 日本語/🇺🇸 English

Automated English Article Generation After Posting Articles

  • AI
  • Engineering

The content of this blog is managed with microCMS.

Since AI easily translates it into good English, I prepared a framework for posting English articles about six months ago and have been manually operating it ever since.

With about five minutes of work, the AI translation and copy-paste tasks are complete, but because this blog extensively uses microCMS repeated fields, it was somewhat cumbersome, so I decided to automate it.

Process Flow

It's not overly complicated, but the flow is roughly as follows.

Post an article on the Japanese API

Trigger GitHub Actions via Webhook

Run a script from GitHub Actions to retrieve the published Japanese article from microCMS and translate it using the ChatGPT API

Format the translated content and POST it to the English API

Deploy to Cloudflare Pages

Script

The script for the translation process and the English submission portion looks like the following.

import OpenAI from 'openai';
import { createClient } from 'microcms-js-sdk';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY
});

const client = createClient({
  serviceDomain: process.env.MICROCMS_SERVICE_DOMAIN,
  apiKey: process.env.MICROCMS_API_KEY
});

// Determine the content ID of the published article from the Webhook payload and retrieve its content
const ja = await client.getListDetail({
  endpoint: 'blog',
  contentId: process.env.CONTENT_ID
});

async function main() {
  // Translate the Japanese article to English
  const chatCompletion = await openai.chat.completions.create({
    messages: [
      {
        role: 'system',
        content:
          'Please translate the title, description, and body parts of the JSON data input by the user into English and respond in the original JSON format.'
      },
      { role: 'user', content: JSON.stringify(ja) }
    ],
    model: 'gpt-3.5-turbo-0125',
    response_format: {
      type: 'json_object'
    }
  });
  const en = JSON.parse(chatCompletion.choices[0]?.message.content);
  console.log(en);

  // Check if an article with the same content ID already exists on the English side
  const isExist = await client
    .getListDetail({
      endpoint: 'en-blog',
      contentId: en.id
    })
    .then(() => true)
    .catch(() => false);

  const content = {
    title: en.title,
    tags: en.tags.map((tag) => tag.id),
    ogimage: en.ogimage.url,
    body: en.body,
    description: en.description,
    books: en.books.map((book) => ({
      ...book,
      image: book.image.url
    })),
    author: en.author.id
  };

  // Update if an article with the same content ID already exists on the English side, create new if it doesn't
  if (isExist) {
    await client
      .update({
        endpoint: 'en-blog',
        contentId: en.id,
        content
      })
      .catch((err) => console.error(err));
    return;
  }

  await client
    .create({
      endpoint: 'en-blog',
      contentId: en.id,
      content
    })
    .catch((err) => console.error(err));
}

main();

It was nice that the ChatGPT API allows specifying the response format as JSON.

response_format: {
  type: 'json_object'
}

Cost

Depending on the length of the blog post, it can be around 1-5 yen.

If I don't have to do the copy-paste work for five minutes, it's definitely cheap.

Summary

It's important to gradually implement AI within a range that is useful to oneself.

By the way, if everything goes well, the English version of this article should be generated at the following URL.

Automated English Article Generation After Posting Articles | Kazuki Shibata
Since AI easily translates it into good English, I prepared a framework for posting English articles about six months ago and have been manually operating it ever since. With about five minutes of work, the AI translation and copy-paste tasks are complete, but because this blog extensively uses microCMS repeated fields, it was somewhat cumbersome, so I decided to automate it.
https://blog.shibe97.com/en/auto-translation
Kazuki Shibata X GitHub
microCMS Co-founder CEO / Designer and front-end engineer / Father of 2

Recommended