Last updated on August 7, 2024
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 Old | Old | Recent | Bleeding Edge |
---|---|---|---|
BASIC | Perl | Swift | Kotlin |
Assembly | Pascal | TypeScript | Julia |
Fortran | PHP | Rust | Crystal |
Lisp | Python | Kotlin/Native | Racket |
COBOL | C | Julia (GPU) | Lua |
Algol | C++ | Go | Zig |
SNOBOL | Java | Dart | Nim |
RPG | Smalltalk | Elixir | Crystal (LLVM) |
Forth | Tcl | Groovy | Vlang |
Ada | SQL | Scala Native | Erlang |
Instructing ChatGPT, or any other LLM-based AI chatbot, to generate code is as simple as this:
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:
Try it here:
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:
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:
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:
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 -= 2The output will include the corrected while loop, which is changed to ensure the denominator variable stays positive:
For more nuanced errors, you may need to add more specifics: the error message, environment variables, included libraries, or variations in inputs.
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:
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:
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."
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:
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:
What if you want an AI chatbot to evaluate multiple files? Simply supply the filenames and paste the code for each:
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.
Most AI chatbots work with code, but some can even simulate server environments like databases, and web and email servers. Consider this 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:
From there, you can add columns, indexes, and more:
Add a column to the politicians table for the second-in-command and update it for each row.
The output might be something like:
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:
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:
Here are just some of the types of servers and APIs that one popular chatbot can emulate:
Very Old | Old | Recent | Bleeding Edge |
---|---|---|---|
Telnet | HTTP/1.0 | RESTful API | GraphQL API |
FTP | SMTP | SOAP API | GRPC API |
POP3 | LDAP | OAuth 2.0 | Docker API |
IMAP | DHCP | OpenAPI (Swagger) | Kubernetes API |
DNS | NTP | gRPC-web API | Istio API |
SNMP | RADIUS | MQTT API | Prometheus API |
Syslog | SFTP | WebSocket API | Envoy API |
TFTP | SSH | AWS API Gateway | Istio Mixer API |
NFS | XML-RPC | Azure API Management | OpenTracing API |
SMB | JSON-RPC | Google Cloud Endpoints | NATS Streaming API |
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:
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:
Try experimenting with other commands:
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:
A full discussion of using AI chatbots as virtual machines can be found at engraved.blog.
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:
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:
Contributed by Prompt Yes!, a company offering instructor-led courses in ChatGPT and Midjourney.