Prompt Engineering Guide
😃 Basics
💼 Applications
🧙‍♂️ Intermediate
🧠 Advanced
Special Topics
🌱 New Techniques
🤖 Agents
⚖️ Reliability
🖼️ Image Prompting
🔓 Prompt Hacking
🔨 Tooling
💪 Prompt Tuning
🗂️ RAG
🎲 Miscellaneous
Models
📝 Language Models
Resources
📙 Vocabulary Resource
📚 Bibliography
📦 Prompted Products
🛸 Additional Resources
🔥 Hot Topics
✨ Credits
💼 Applications🟦 Coding Assistance

Coding Assistance

🟦 This article is rated medium
Reading Time: 9 minutes

Last updated on August 7, 2024

Takeaways
  • LLM-Powered Coding: You can use LLMs for tasks like code generation, debugging, refactoring, and even simulating servers.
  • Reduce Complexity: AI chatbots can reduce time spent on repetitive tasks, such as adding comments or optimizing inefficient scripts.
  • Memory and Multi-File Handling: Chatbots can handle conversations and code from multiple files, simulating databases, web servers, and more.
  • Step-by-Step Help: This article walks you through using LLMs for code-related tasks with practical examples.

You can use LLMs for debugging, code generation, reformatting, commenting, and more. One popular chatbot claims it can work with code in over 40 programming languages.

Very OldOldRecentBleeding Edge
BASICPerlSwiftKotlin
AssemblyPascalTypeScriptJulia
FortranPHPRustCrystal
LispPythonKotlin/NativeRacket
COBOLCJulia (GPU)Lua
AlgolC++GoZig
SNOBOLJavaDartNim
RPGSmalltalkElixirCrystal (LLVM)
ForthTclGroovyVlang
AdaSQLScala NativeErlang

Code Generation

Instructing ChatGPT, or any other LLM-based AI chatbot, to generate code is as simple as this:

Astronaut

Prompt


Act as a junior Python programmer. Please write the code to generate madlibs from 3 arrays of 6 items each: prepositions, verbs, and nouns, with the nouns also being used as direct objects. After writing it, execute the code.

The output will be something like:

Robot

AI Output


import random prepositions = ['at', 'in', 'on', 'with', 'under', 'over'] verbs = ['run', 'jump', 'dance', 'sing', 'laugh', 'cry'] nouns = ['cat', 'dog', 'tree', 'flower', 'book', 'computer'] preposition = random.choice(prepositions) verb = random.choice(verbs) noun = random.choice(nouns) madlib = f"I {verb} {preposition} the {noun}." print(madlib)

Try it here:

Commenting and Reformatting Code

You can ask the bot to add comments to your code and reformat it for legibility. After your prompt instruction, add three hash marks to separate your prompt from the code you'd like it to clean up:

Astronaut

Prompt


Please add line comments to this Python code and reformat it for legibility:

### import math amplitude = 30 frequency = 2 num_points = 50 for i in range(num_points): y = int(round(amplitude * math.sin(2 * math.pi * frequency * i / num_points))) print(' ' * (amplitude + y) + '*')

The output will be something like:

Robot

AI Output


# Import the math module import math # Set the amplitude, frequency, and number of points in the wave amplitude = 30 frequency = 2 num_points = 50 # Loop over each point in the wave for i in range(num_points): # Calculate the y value of the wave using the sine function from the math module y = int(round(amplitude * math.sin(2 * math.pi * frequency * i / num_points))) # Print a space for padding, followed by an asterisk at the appropriate position on the wave print(' ' * (amplitude + y) + '*')

Debugging

Not only can LLM-based AI chatbots detect syntax errors in code, but they can also find logical errors that would crop up when code is executed. Below is an example of a Python script that eventually causes a division by zero error on line 4 due to a logical error on line 3. Try this simple prompt to find and fix the error:

Astronaut

Prompt


Please debug this Python code:

1. pizza_cost = 100 2. partygoers = 10 3. while partygoers = 0: 4. cost_per_person = pizza_cost / partygoers 5. print("the cost per person is $", "{:.2f}".format(cost_per_person)) 6. partygoers -= 2

The output will include the corrected while loop, which is changed to ensure the denominator variable stays positive:

Robot

AI Output


3. while partygoers > 0:

For more nuanced errors, you may need to add more specifics: the error message, environment variables, included libraries, or variations in inputs.

Optimizing Code

If your script is taking too long to execute, an AI chatbot like ChatGPT can optimize it. Here's an example of optimizing an inefficient script that finds prime numbers:

Astronaut

Prompt


Act like a very senior Python developer. Please optimize this script:

for num in range(1, 101): if num 1: for i in range(2, num): if (num % i) == 0: break else: print(num)

The output might be a script that uses the Sieve of Eratosthenes algorithm for finding prime numbers:

Robot

AI Output


numbers = list(range(2, 101)) for i in numbers: for j in range(i * 2, 101, i): if j in numbers: numbers.remove(j) print(numbers)

Note that we asked the bot to "act like a senior developer" to optimize the script. You can also dictate that it has a certain area of expertise (e.g., sorting algorithms) or the number of years of experience. Alternatively, if you have a script that seems overly complicated, you can ask the bot to write that script "as a very junior developer."

Translating Between Programming Languages

A very useful feature AI chatbots offer is the ability to port programs from one language to another. A typical use for this might be upgrading code in an older language to a more modern one:

Astronaut

Prompt


Act like an experienced developer with knowledge of both COBOL and Python. Please convert this COBOL program to Python:

IDENTIFICATION DIVISION. PROGRAM-ID. CALCULATE-SS-TAX. DATA DIVISION. WORKING-STORAGE SECTION. 01 SALARY PIC 9(7)V99. 01 SS-TAX PIC 9(7)V99. PROCEDURE DIVISION. DISPLAY "Enter employee's salary: ". ACCEPT SALARY. COMPUTE SS-TAX = SALARY * 0.062. DISPLAY "Social Security tax is: $", SS-TAX. STOP RUN.

The output might be something like this:

Robot

AI Output


salary = float(input("Enter employee's salary: ")) ss_tax = salary * 0.062 print("Social Security tax is: $", round(ss_tax, 2))

Handling Multiple Files and Refactoring your Codebase

What if you want an AI chatbot to evaluate multiple files? Simply supply the filenames and paste the code for each:

Astronaut

Prompt


My Python script uses two files, listed below, in different directories. Please display the output from executing conversation.py:

BEGIN FILE 1: ./phrases/coming_and_going.py def hi(name): print(f"Hello, {name}!") def bye(name): print(f"Goodbye, {name}!") BEGIN FILE 2: ./conversation.py from phrases.coming_and_going import hi, bye hi("John") bye("John") Hello, John! Goodbye, John!

Using this technique, you can submit multiple files to the bot in a single prompt and ask it to refactor the code. Alternatively, you can ask it to "memorize" each one and then execute it in a later prompt.

Simulating a Database Server

Most AI chatbots work with code, but some can even simulate server environments like databases, and web and email servers. Consider this prompt:

Astronaut

Prompt


Act as Microsoft SQL Server. Create a database called "politics" and inside it a table called "politicians." Fill it with 50 rows of famous politicians from around the world from different eras, 1900-2000. Add columns for their full names, country, dates of birth, and date of death if applicable. Create a view of the top 3 politicians who lived the longest. Create and execute a Transact-SQL command that outputs the contents of that view.

The output will be something like:

Robot

AI Output


-- Create the "politics" database CREATE DATABASE politics; GO -- Use the "politics" database USE politics; GO -- Create the "politicians" table CREATE TABLE politicians ( id INT PRIMARY KEY, full_name VARCHAR(50), country VARCHAR(50), date_of_birth DATE, date_of_death DATE ); -- etc.

From there, you can add columns, indexes, and more:

Astronaut

Prompt


Add a column to the politicians table for the second-in-command and update it for each row.

The output might be something like:

Robot

AI Output


ALTER TABLE politicians ADD second_in_command VARCHAR(255); UPDATE politicians SET second_in_command = CASE name WHEN 'Winston Churchill' THEN 'Clement Attlee' WHEN 'Franklin D. Roosevelt' THEN 'Harry S. Truman' WHEN 'Mao Zedong' THEN 'Lin Biao' -- add more cases for each politician ELSE NULL END;

Simulating a Web Server

Some AI chatbots can simulate many other servers and APIs. Here's an example of a prompt that simulates an Apache server response to specific browser request headers:

Astronaut

Prompt


Act as an Apache web server. How would you respond to these HTTP headers?

GET /example HTTP/1.1 Host: www.example.com

The output will be something like:

Robot

AI Output


HTTP/1.1 200 OK Date: [current date and time] Server: Apache/2.4.41 (Ubuntu) Last-Modified: [date and time of last modification of the requested resource] Content-Type: [MIME type of the requested resource, e.g. text/html]

Here are just some of the types of servers and APIs that one popular chatbot can emulate:

Very OldOldRecentBleeding Edge
TelnetHTTP/1.0RESTful APIGraphQL API
FTPSMTPSOAP APIGRPC API
POP3LDAPOAuth 2.0Docker API
IMAPDHCPOpenAPI (Swagger)Kubernetes API
DNSNTPgRPC-web APIIstio API
SNMPRADIUSMQTT APIPrometheus API
SyslogSFTPWebSocket APIEnvoy API
TFTPSSHAWS API GatewayIstio Mixer API
NFSXML-RPCAzure API ManagementOpenTracing API
SMBJSON-RPCGoogle Cloud EndpointsNATS Streaming API

Simulating a Command Line

Since a shell within Linux (or Windows PowerShell) is also a programming language, you can interact with it using an AI chatbot, and build an environment for your filesystem:

Astronaut

Prompt


Act as Debian Linux command shell. Please respond to my commands as the terminal would, with as little explanation as possible. My first command is: ls -l

The output will be something like:

Robot

AI Output


-rw-r--r-- 1 user group 2048 Mar 3 14:23 file1.txt drwxr-xr-x 2 user group 4096 Feb 28 09:12 directory1 -rwx------ 1 user group 16384 Feb 25 19:41 executable1

Try experimenting with other commands:

Astronaut

Prompt


> touch greeting.txt > echo "hello" > greeting.txt > ls -l

Depending on the size of its context window, the bot should remember your changes (for a while, at least) and the final output ought to be something like:

Robot

AI Output


-rw-r--r-- 1 user group 2048 Mar 3 14:23 file1.txt drwxr-xr-x 2 user group 4096 Feb 28 09:12 directory1 -rwx------ 1 user group 16384 Feb 25 19:41 executable1 -rw-r--r-- 1 user group 6 Mar 4 16:15 greeting.txt

A full discussion of using AI chatbots as virtual machines can be found at engraved.blog.

Generating Unit Tests

AI chatbots built on LLMs are ideal for the time-consuming task of creating unit tests. Consider this prompt to build a series of unit tests for a tax calculating program:

Astronaut

Prompt


Act as a Python software test engineer. Write unit tests for the calculate_tax function of this tax estimator Python program:

### def get_user_inputs(): total_income = float(input('Enter your income (wages, tips, etc.): $')) dependent_care_expenses = float(input('Enter your dependent care expenses: $')) return total_income, dependent_care_expenses def calculate_tax(total_income, dependent_care_expenses): deduction = 12500 # Standard deduction for single filers in 2020 ... etc.

The output might be something like:

Robot

AI Output


import unittest def calculate_tax(total_income, dependent_care_expenses): # ... (your existing code here) class TestCalculateTax(unittest.TestCase): def test_low_income_no_expenses(self): self.assertEqual(calculate_tax(10000, 0), 0) def test_mid_income_no_expenses(self): self.assertEqual(calculate_tax(50000, 0), 4476.0) def test_high_income_no_expenses(self): self.assertEqual(calculate_tax(100000, 0), 14776.0) def test_low_income_with_expenses(self): self.assertEqual(calculate_tax(10000, 2000), -400.0) ... etc. if __name__ == '__main__': unittest.main()

Contributed by Prompt Yes!, a company offering instructor-led courses in ChatGPT and Midjourney.

Sander Schulhoff

Sander Schulhoff is the Founder of Learn Prompting and an ML Researcher at the University of Maryland. He created the first open-source Prompt Engineering guide, reaching 3M+ people and teaching them to use tools like ChatGPT. Sander also led a team behind Prompt Report, the most comprehensive study of prompting ever done, co-authored with researchers from the University of Maryland, OpenAI, Microsoft, Google, Princeton, Stanford, and other leading institutions. This 76-page survey analyzed 1,500+ academic papers and covered 200+ prompting techniques.

Edit this page
Word count: 0

Get AI Certified by Learn Prompting


Copyright © 2024 Learn Prompting.