ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
PE
command-coding · 2 min read

prompt engineering for code

Prompt engineering is the art of crafting precise and effective natural language commands to elicit high-quality code from Large Language Models (LLMs). When…

Prompt engineering is the art of crafting precise and effective natural language commands to elicit high-quality code from Large Language Models (LLMs). When working with an APIary platform, founders can issue commands in English, and the system generates relevant code snippets. This wiki page explores the techniques of prompt engineering for code, providing actionable guidance on how to phrase commands effectively.

Specificity is Key

When crafting a command, it's essential to be specific about what you want to achieve. Avoid vague or open-ended requests that might confuse the LLM. Instead, focus on precise outcomes and requirements. For example:

Bad Command

Create an API endpoint for user authentication.

This command is too broad and may result in a generic implementation.

Good Command

Implement a RESTful API endpoint at `/users/login` that accepts `username` and `password` parameters, validates them against the database, and returns a JWT token upon successful login.

The revised command specifies the exact requirements and structure of the API endpoint.

Examples are Essential

Providing concrete examples can help the LLM understand the context and produce more accurate code. Use specific scenarios or use cases to illustrate the desired behavior. For instance:

Bad Command

Create a function that calculates the average of an array.

This command lacks context and may result in a generic implementation.

Good Command

Implement a JavaScript function called `calculateAverage` that takes an array of numbers as input, ignores any non-numeric values, and returns the mean of the remaining elements. For example, if the input is `[1, 2, 'hello', 3]`, the output should be `2`.

The revised command includes a specific example to guide the LLM's implementation.

Structure Matters

When crafting a command, consider the structure and organization of the code it will produce. Use clear headings, section dividers, and concise descriptions to facilitate the LLM's understanding. For instance:

Bad Command

Create a script that does this:
- Connects to a database
- Retrieves data
- Performs some calculations
- Outputs the result

This command lacks structure and may confuse the LLM.

Good Command

**Database Connection**
Implement a Python script that connects to a PostgreSQL database using `psycopg2`. The connection details are as follows:

* Host: `localhost`
* Database: `mydatabase`
* Username: `myuser`
* Password: `mypassword`

**Data Retrieval**
Use the connected database cursor to execute a SQL query retrieving all rows from the `users` table.

**Calculations and Output**
Perform some calculations on the retrieved data using NumPy, then output the result in JSON format.

The revised command includes clear headings, section dividers, and concise descriptions to guide the LLM's implementation.

Related/Sources

By applying these techniques of prompt engineering, founders can craft effective natural language commands that elicit high-quality code from LLMs on the APIary platform. Remember to emphasize specificity, provide concrete examples, and structure your commands clearly for optimal results.

Frequently asked
What is prompt engineering for code about?
Prompt engineering is the art of crafting precise and effective natural language commands to elicit high-quality code from Large Language Models (LLMs). When…
What should you know about specificity is Key?
When crafting a command, it's essential to be specific about what you want to achieve. Avoid vague or open-ended requests that might confuse the LLM. Instead, focus on precise outcomes and requirements. For example:
What should you know about bad Command?
This command is too broad and may result in a generic implementation.
What should you know about good Command?
The revised command specifies the exact requirements and structure of the API endpoint.
What should you know about examples are Essential?
Providing concrete examples can help the LLM understand the context and produce more accurate code. Use specific scenarios or use cases to illustrate the desired behavior. For instance:
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room