As a software developer, I have been following the advancements in artificial intelligence with great interest. While I have seen AI perform a variety of tasks, from image recognition to natural language processing, one thing that really surprised me was its ability to generate code.
Code generation is the process of automatically creating code based on a set of input parameters. This is a difficult task for a machine to perform, as it requires a deep understanding of programming languages, algorithms, and logic. However, recent developments in AI have made it possible for machines to generate code that is both functional and efficient. By training a machine learning model on a large dataset of code, it can learn the patterns and structures that are commonly used in programming languages. This allows it to generate code that follows these patterns, resulting in code that is both syntactically correct and semantically meaningful.
GitHub copilot was one such AI and now OpenAI’s ChatGPT is another that can generate code. The beauty of these AIs is their ability to understand natural language. So developers can generate code in preferred programming language with a simple prompt. This makes it possible for non-technical users to specify the functionality they want, without having to know how to write code themselves.
What is possible today?
I asked ChatGPT to write code for different use cases. First started with something simple:
Then I asked it to Write a golang server to serve static files
package main
import (
"fmt"
"net/http"
)
func main() {
// Use the http.FileServer function to create a new file server
// that serves files from the specified directory
fs := http.FileServer(http.Dir("static"))
// Use the http.Handle function to register the file server as
// the handler for requests to the "/static" URL path
http.Handle("/static/", http.StripPrefix("/static/", fs))
// Use the http.ListenAndServe function to start the server
// listening on the specified port
fmt.Println("Starting server on port 8080")
http.ListenAndServe(":8080", nil)
}
The code comments are helpful to developer and then it also responds with how to run the code which I think is good if some beginner is trying to learn to code. I generated quite a few programs and although I did not run any of these I am pretty sure they will give expected result with little to no tweaking.
I went on to ask more complex tasks and asked it to generate Go code to convert Color image to Black & White and below is the result. It even knows what it needs to import and how to use those APIs!
package main
import (
"image"
"image/color"
"image/jpeg"
"os"
)
func main() {
// Open the input file
in, err := os.Open("input.jpg")
if err != nil {
panic(err)
}
defer in.Close()
// Decode the input image
img, err := jpeg.Decode(in)
if err != nil {
panic(err)
}
// Create a new image with the same dimensions as the input image
bw := image.NewGray(img.Bounds())
// Iterate over each pixel in the input image
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
// Get the color of the current pixel
c := img.At(x, y)
// Convert the color to grayscale
g := color.GrayModel.Convert(c).(color.Gray)
// Set the pixel in the output image to the grayscale color
bw.Set(x, y, g)
}
}
// Open the output file
out, err := os.Create("output.jpg")
if err != nil {
panic(err)
}
defer out.Close()
// Encode the output image as a JPEG and write it to the output file
err = jpeg.Encode(out, bw, &jpeg.Options{Quality: 100})
if err != nil {
panic(err)
}
}
It can generate code in quite a few languages and understand what it needs to do. For example, you can just ask it to create a CSS for a rounded button with light color and it will spit out the result that you can use in your code. Currently it seems that the capabilities of this AI are intentionally diminished for general public because when I asked it to generate a website for selling sustainable products it truncated it’s response after generating fairly complex CSS, content for website and links to products page One can only imagine how powerful it is behind the scenes.
So far I have seen people using it to generate
- iptables for firewall config
- complicated ffmpeg commands to stich images to create a video
- regex for given filter criteria
- docker commands
Someone just launched a website to share the prompts and response of ChatGPT, visit https://www.learngpt.com/.
Overall, the ability of AI to generate code was the last thing I was expecting using machine learning. Its ability to understand and generate code has the potential to revolutionize the way we develop software, making it more accessible and efficient.
The possibilities with this AI are unlimited. Finally I will leave you with the terms and conditions I asked it to generate.
AIs can help developers throughout the development life cycle of the products. I am excited to see what other developments in AI will emerge in the future.
80% of this blog post including title and cover image is generated by AI