Integrating ChatGPT into article translation in AdminCP

Integrating ChatGPT into article translation in AdminCP

Daily short news for you
  • Hic, I've been really busy lately, and there have been so many changes that I haven't had time to write a longer post. Instead, I'm trying to maintain this short daily update for readers. Since it's short, it's much quicker to write.

    I'm currently tweaking the interface a bit while also adding a new feature. Can you guess what it is? Here's a hint: it has something to do with AI and Tiktok 😁

    » Read more
  • Currently on the 14-day trial of Windsurf, today is day seven, and I have some quick impressions as follows:

    First, the interface is a bit more customizable, giving a flatter and friendlier feel compared to the traditional VSCode.

    Second, the suggestions are super fast but a bit hasty. They’re not always accurate, yet they confidently offer several lines at once. So, not every tab tab is correct. However, it reads context well, better than Copilot.

    Third, the Chat/Edit feature is top-notch, very good, almost a perfect understanding, probably on par with Cursor, but I’m not entirely sure; that's just how it feels.

    Additionally, one annoyance is that sometimes it suggests but the tab doesn’t match, which makes it a hassle to delete.

    I wonder how it will be after the 14 days, so I will continue to update. But overall, it’s way better than Copilot.

    Oh! One more thing, the Vietnamese in this one is terrible. I have no idea why!?

    » Read more
  • smee.io is a simple way to create a webhook address and map it to the localhost address on your computer.

    $ npm install --global smee-client $ smee -u https://smee.io/eu4UoW8vrKSZtTB

    » Read more

Problem

Last week, I released a multilingual feature for the website. You know, every time I add a new feature, it requires additional time to operate. After implementing that feature, I spent a whole day copying the articles, putting them into ChatGPT for translation, then reinserting them into the English version of the articles, and editing the format if there were any mistakes... then publishing those articles. It sounds simple, but it's actually a nightmare for programmers. Oh, it turns out I'm working instead of lines of code.

Thinking this way, I need to do something to optimize this work, or at least not repeat it in a boring way. So, there's only one way left: finding a service provider that allows calling the ChatGPT API, integrating it into my admin panel, and writing code to directly translate Vietnamese articles into English. Hmm... but that might require some money.

Is there any other way?

Research

As we all know, ChatGPT allows us to use it for free on its website. Whatever you want to ask or search, just go there and chat, with no limitations except for slower response times during peak hours, when user traffic suddenly increases.

ChatGPT also provides an API service. Instead of going to their website, you can now directly call the API. This brings a lot of value because if ChatGPT is integrated into a system, many services will become "smarter" in the literal sense. Just imagine giving a command to the system, and some tasks are automatically completed, how convenient it is.

Regarding pricing, it varies depending on the model used. The following example is the pricing table for GPT-3.5 Turbo.

ChatGPT pricing table

With 4K context, the price for each 1K tokens (meaning the length of the question) is $0.0015. Tokens can be understood as "words" in their units, so 1K Tokens is equivalent to 1,000 words. Within that, 4K and 16K represent the knowledge of ChatGPT. This means that the larger the context, the deeper and wider ChatGPT's understanding will be.

If roughly calculating, one of my articles is about 1K - 1.5K words, how much time does it take to reach $1 by calling the API?

But the truth is ChatGPT API cannot be paid for in Vietnam, so the desire is just a desire. My initial idea was how to crawl the response data from the ChatGPT chat page. The term "Crawl" has existed for a long time and is suitable in cases where a service provider does not provide an API. However, the drawback of this method is its instability. Furthermore, ChatGPT also cleverly integrated Cloudflare Turnstile, a Captcha service that prevents crawling behavior from Cloudflare.

Am I at a dead end? I wandered on Github to find help from "colleagues" all over the world and accidentally discovered this repository transitive-bullshit/chatgpt-api.

By a quick look, we can see that it is a tool that allows us to interact with ChatGPT through CLI or Node.js. Its characteristic is that it requires providing an OPENAI_API_KEY, which is the key for OpenAI to calculate the cost of calling their API, but I don't have that!

But the "magic" appears at the end. There's a function called ChatGPTUnofficialProxyAPI that allows us to interact with ChatGPT through an Access Token (AC). AC is easy to get, you can retrieve it through the Dev Tools of your browser or simply access this URL https://chat.openai.com/api/auth/session to extract the information.

However, having AC alone is not enough. As mentioned at the beginning of the article, ChatGPT is also protected by Turnstile, so to use this AC, a Proxy Server needs to be present. Simply put, this server "bypasses" Turnstile and holds the AC to interact with ChatGPT on your behalf. The documentation lists two "generous" authors who have built free servers for people to use, but with some rate limits.

Of course, everything has its downsides. The author of the repository also considers removing ChatGPTUnofficialProxyAPI in the future due to various reasons, including stability and information security concerns.

But after considering it, I still decided to use this tool.

Process

If the library works as planned, I will create a local server on my machine to use for the automatic article translation feature, with the input being the content to be translated and the output being the translated content. One reason for not exposing this API is that I no longer have a server, all services have been pushed to Cloudflare in the article Completing the blog transfer to "Web is on the edge". Worker does not support this Node.js library, so it cannot be deployed. Moreover, the article writing time is not continuous, so there is no need to maintain this API server continuously.

There is one thing to note that ChatGPT does not respond with the complete answer in one go if it is too long. To continue, you need to click the "Continue" button if you're on the web. Luckily, the library supports the next syntax to request ChatGPT to continue the incomplete answer.

In the function to send messages, there are 2 options: conversationId and parentMessageId, which represent the ID of the conversation and the previous message, respectively. The reason for having parentMessageId is to help ChatGPT understand the previous context to provide a more accurate answer, while conversationId is used to group messages in the same conversation and avoid mixing them up. Therefore, I need to save parentMessageId each time ChatGPT responds so that the next message, it understands the previous context.

Then, I create a class ChatGPT with 2 methods: sendMessage and sendNext, representing one chat round and a request to continue the response if the answer is not complete. I use the fastify library to create a simple HTTP server. Finally, I integrate it into AdminCP like this.

![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/gJJzIumvCWo/0.jpg)

You can refer to the source code of my local server here tonghoai/chatgpt-translate.

Premium
Hello

5 profound lessons

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

Every product comes with stories. The success of others is an inspiration for many to follow. 5 lessons learned have changed me forever. How about you? Click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...
Scroll or click to go to the next page