Code Generation API, based on GPT

What is Code Generation and Why Use GPT?

Code generation is about creating source code, in a specific programming language, from simple human instructions. For example, a human can ask an AI to create a blue button in HTML, and the AI will return the proper HTML code for that.

Example: do you want to help your employees request the database while they know nothing about SQL? Thanks to code generation, they can now simply ask what they want in human language. Let's say you want to ask the following: Fetch three employees from the Employee table. The AI model will return the following:

SELECT * FROM Employee ORDER BY last_name DESC LIMIT 3;

The above examples are simple examples but it is possible to perform much more complex code generation thanks to GPT-J.

GPT-J and GPT-NeoX are the most advanced open-source NLP models as of this writing, and they are the best alternatives to GPT-3 Codex for code generation. These models are so big that they can adapt to many situations, and look like a real programmer. For advanced use cases, it is possible to fine-tune GPT (train it with your own data), which is a great way to perform more advanced code generation that is perfectly tailored to your language or application.

Source code generation

Why Use Code Generation?

Programming is at the heart of many things today, but few people know how to code. Besides, developers themselves are constantly looking for ways to improve their productivity. Here are a couple of examples on how code generation can help:

Database Querying

Most of the valuable data is located in relational databases today, but few people know how to use SQL to get the results they want. Simply ask AI the SQL query you want to perform and it will generate it for you.

Mock Up Creation

Creating a quick mock-up with HTML and CSS is now much easier thanks to code generation. Anyone from a marketing department is able to create such a mock-up thanks to AI.

Focus on Complex Logic Only

Developers often spend time writing repetitive code that does not involve too much complex logic. This part of their work can now be offloaded to AI.

Speed Up Tests Creation

Writing unit tests and integration tests is a pain, but it's critical to the quality of an application. It is now possible to let AI write the tests for you so you can focus on something more important.

Performing Code Generation with GPT

In order to make the most of GPT, it is crucial to have in mind the so-called few-shot learning technique (see more here): by giving only a couple of examples to the AI, it is possible to dramatically improve the relevancy of the results, without even training a dedicated AI.

Sometimes, few-shot learning is not enough (for example if your existing code base is very specific). In that case, the best solution is to fine-tune (train) GPT with your own data see more here.

Building an inference API for code generation based on GPT is a necessary step as soon a you want to use code generation in production. But building such an API is hard... First because you need to code the API (easy part) but also because you need to build a highly available, fast, and scalable infrastructure to serve your models behind the hood (hardest part). It is especially hard for machine learning models as they consume a lot of resources (memory, disk space, CPU, GPU...).

Such an API is interesting because it is completely decoupled from the rest of your stack (microservice architecture), so you can easily scale it independently, and you can access it using any programming language. Most machine learning frameworks are developed in Python, but it's likely that you want to access them from other languages like Javascript, Go, Ruby...

NLP Cloud's Code Generation API

NLP Cloud proposes a code generation API based on GPT that gives you the opportunity to perform code generation out of the box, with breathtaking results. If the base GPT model is not enough, you can also fine-tune/train GPT on NLP Cloud and automatically deploy the new model to production with only one click.

For more details, see our documentation about text generation with GPT here. Also see our few-shot learning example dedicated to code generation here. And easily test GPT code generation on our playground.