What is Prompt Engineering?

What is Prompt Engineering?

·

4 min read

Today, we want to share with you a new way of developing code that has been gaining traction lately, sometimes referred to as : prompt engineering.

First invented by Github Copilot, prompt engineering is the process of using tools like chatGPT or Copilot to generate code snippets based on a given prompt.

It allows us to quickly prototype and iterate on different ideas, saving us time and effort in the development process.

For those who are not familiar with chatGPT or Github Co-pilot, they are state-of-the-art language models that can generate human-like text based on a given prompt. It has been a popular way to generate code snippets that can be built upon and incorporated into projects.

Here's how prompt engineering works

We start by providing chatGPT with a brief description of the code snippet we need, along with any relevant details or constraints.

For example, if we need a function to calculate the average of a list of numbers, we might provide chatGPT with the following prompt:

"Write a function in Python that takes a list of numbers as an argument and returns the average of those numbers."

Based on this prompt, chatGPT generates a code snippet that looks something like this:

def calculate_average(numbers):
    total = 0
    for num in numbers:
        total += num
    return total / len(numbers)

As you can see, chatGPT was able to generate a complete, functional code snippet based on just a few sentences of input. From here, we can build upon this snippet and customize it to fit our specific needs.

Here is an example of using prompt engineering with chatGPT to generate a starting point for a Node.js API in JavaScript:

"Write a Node.js API in JavaScript that has routes for creating, reading, updating, and deleting resources. Use Express as the web framework and MongoDB as the database. The API should include a way to authenticate users using JWT and handle form data using Multer."

The snippet we get is then a full boiler plate for this API:

const express = require('express');
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const multer = require('multer');

const app = express();

// Connect to MongoDB
mongoose.connect('mongodb://localhost/my-api', {
  useNewUrlParser: true,
  useUnifiedTopology: true
});

// Use Multer to handle form data
app.use(multer().none());

// Middleware to authenticate users using JWT
app.use((req, res, next) => {
  const token = req.header('x-auth-token');
  if (!token) {
    return res.status(401).send('Access denied. No token provided.');
  }
  try {
    const decoded = jwt.verify(token, 'my-secret-key');
    req.user = decoded;
    next();
  } catch (ex) {
    res.status(400).send('Invalid token.');
  }
});

// Routes for creating, reading, updating, and deleting resources
app.post('/resources', (req, res) => {

// Create a new resource
});

app.get('/resources', (req, res) => {

// Get a list of all resources
});

app.get('/resources/:id', (req, res) => {

// Get a single resource by ID
});

app.put('/resources/:id', (req, res) => {

// Update a resource by ID
});

app.delete('/resources/:id', (req, res) => {

// Delete a resource by ID
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Listening on port ${port}...`);
});

This snippet includes the basic setup for an API using Express and MongoDB, as well as routes for creating, reading, updating, and deleting resources. It also includes middleware to authenticate users using JWT and handle form data using Multer.

Of course, this is just a starting point, and we would still need to add the logic for each route and potentially make other modifications to fit our specific needs. But it should give us a good foundation to work from and save you some time in setting up the basic structure of our API.

Quickly prototype with prompt engineering

This approach can be extremely helpful when working on both complex projects and simple ones, as it allows us to quickly prototype and iterate on different ideas without having to start from scratch every time.

It's also great for when you’re stuck and need a little bit of inspiration to get moving again.

So if you, like us, are looking for a new way to streamline the code development process, we highly recommend giving prompt engineering a try.

GitHub Copilot is available as a free trial, and ChatGPT is currently in open beta and free to use. If you use Replit, you can also try out their variant of this, Replit Ghostwriter.