How I used chatGPT to re-write Soldirac in nest.js

How I used chatGPT to re-write Soldirac in nest.js

Everyone in Tech has probably heard about chatGPT. Its a state of the art NLP model by openAI that is exceptionally good at QnA tasks.

One of the greatest feats achieved by chatGPT is that it has the ability to code. It has deep understanding of code and what it does.

Goal: To convert existing back-end of soldirac.com from vanilla node.js to nest.js and typescript

Asking chatGPT if it can convert code

I started out asking chatGPT if it can convert my code base to nest.js

asking chatgpt if it can convert my code to nest js

It doesn't know anything about my existing code base. It gave me some instructions on how to setup a nest project.

So I followed the instructions and created a new project.

Asking chatGPT to convert given code

Soldirac's back-end has already been written in JavaScript, and has some "modular" structure built into it.

chatGPT has no idea about my project structure, or what even soldirac is.

I took a file (module) from my existing JavaScript implementation and simply asked it to re-write this file in nest.js:

asking chatGPT to re-write BTCPay module

The above code snippet is the BTCPay.js module from soldirac. This module handles all communication with the BTCPay Server hosted by Soldirac.

If you read the code, it is essentially sending API requests to an endpoint where btcpay server is hosted. It is also using some keys and other values from config to send those requests, and then finally returns the result.

ChatGPT's response:

chatGPT re-writes the entire module in nest.js.

  • no where in my implementation it tells that its a file for BTCPay, however chatGPT automatically creates a nest.js service and names it BTCPayService, which is accurate.
  • chatGPT ignores useless comments in original code and doesn't re-writes them in its implementation.
  • chatGPT creates a syntactically correct code. All strings, types and library specific syntax are correct.
  • even remembers the names of config variables such as btcpay-legacy-api-key and uses them correctly.

If you notice chatGPT used the same axios library. However it is standard to use nest.js abstracted HttpService instead. So I asked chatGPT as a follow up if it can use HttpService instead of axios:

and boom! chatGPT automatically imports and injects an instance of HttpService and replaces the code to use the syntax or HttpService and not axios.

However this is not enough, if you've worked with nest.js you know that there is a better way to handle config in nest.js, ideally we would create a ConfigService that handles all config variables. In my original implementation I am reading config as a json file and exporting as a variable.

So I asked chatGPT to incorporate this request. I asked it to use a config module instead of using app_config:

chatgpt creates a config module and uses it in BTCPayService it created

ChatGPT automatically created a Config based on what variable it knows are being used. There are more variables, but chatGPT doesn't know about it, because it hasn't seen the entire code base. It has only seen that single file.

It then uses this ConfigService in the BTCPayService it had created.

chatGPT then injects the ConfigService and uses const apiKey = this.configService.get('btcpay-legacy-api-key'); to read config variables.

Notice that chatGPT isn't perfect. It forgot that I had asked it to use HttpService before and didn't include that change this time. Also it didn't give the full code.

Building the App from scratch

I later asked how I can build this project from scratch. It gave me instructions on how to create a project and what files to create:

I, then, ask it how I can include the BTCPayService it created:

It gives instruction on what files to create in what folder and what the code should be.

chatGPT is learning about your code base as it helps you build it. The more it builds, the more it will be able to connect different parts of it.

Using chatGPT as an assistant

chatGPT isn't perfect, but its very good. I used this method and asked it to convert other modules in my code such as LnpayModule, DatabaseModule, CommonModule, etc.

and chatGPT learned to connect this modules as time went on.

chatGPT writes the ProblemService

chatGPT wrote the ProblemService which handles everything with posting questions on Soldirac.com, notice how it able to automatically create other services like UserService.

Conclusion

ChatGPT isn't perfect, but it is evidently a massive boost in a developer's productivity. Compared to GitHub Co-pilot, chatGPT genuinely understands code and is able to connect to the dev-ops sides of coding.

To fully convert the back-end to nest.js it required more effort. There were few nits and pieces that chatGPT understandably got wrong that I had to manually fix.

However it got 80% of what my goal was. So even though its not perfect, its an awesome assistant.

Personal Thoughts on its future

I believe chatGPT is capable of doing the entire task on its own. What it misses is the understanding of the entire code base, which is fed to chatGPT in iterations.

If we can access chatGPT and fine-tune it on a code-base we want to work on, chatGPT will fully understand your code-base, and will be able to answer almost any question around it.

Be it converting to nest.js, asking it what a code snippet does or ask it to further optimize a section. It will be able to do it all, once it has been fine tuned on your code-base.

This means chatGPT is very close to being a self-reliant programmer.

This is a massive business opportunity. A service that takes your code-base, and provides fine tuned instance of chatGPT. That service can replace a lot dev work in coming years.