Integrating ChatGPT into article translation in AdminCP

Integrating ChatGPT into article translation in AdminCP

Daily short news for you
  • I got an email from Windsurf today - they said I'm an active user so they're giving me early access to Windsurf Next. What's that?

    It's just the next version of Windsurf, they're improving a lot of things in it. After trying it out for a day, I think the Vietnamese support has gotten better and the suggestions are more accurate now. I'm not sure yet, I'll have to use it more to see, that's my initial impression.

    Oh, I'm still loyal to the free version 😆.

    » Read more
  • One thing I find a bit confusing is that Postman requires an internet connection to function, everyone. This has probably been the case for several years now; back then, many people complained about it, but it’s still the same, still needing the internet to work.

    They must think that IT folks always have internet access, which is why they added the requirement for a network connection just to be safe. Indeed, I am rarely or never affected since I always have internet while working. However, I believe many others find this issue extremely frustrating. Proof of that is that after this incident, quite a few open-source projects have been created to replace Postman.

    Among them, I see the most notable one is hoppscotch. The interface looks quite similar to Postman, but it seems "nicer" 😁

    » Read more
  • Have you ever tried reading the Google Analytics (GA) API documentation? The first time I read it, I was completely lost, not understanding what they were presenting, what the next steps should be... in general, the way it was presented was so convoluted that I couldn’t read it all at once to get immediate results.

    However, if you interact with it more, you'll find that it follows a certain standard. This means that the next time you read it, you'll know what to read first and what to read next, and then move on to the steps to get results, which makes you a bit more proficient. While I was fumbling around with the GA API, I discovered this website that helps me quickly test queries for results, and then I just need to copy the query and put it into the API. Quick and easy! 😅

    GA4 Query Explorer

    » 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

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, 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...