Home >Web Front-end >JS Tutorial >echniques for Effective Prompt Engineering
In Casino Royale (2006), James Bond specifies the precise ingredients for his Vesper martini: 3 measures of Gordons, 1 of Vodka, half a measure of Kina Lillet, shaken over ice with a thin slice of lemon peel.
By detailing the components, Bond ensures a superior drink. Similarly, in interacting with large language models (LLMs), the specificity and clarity of your prompts determine the quality of the output.
This process, known as prompt engineering, is essential for obtaining accurate and relevant responses from AI systems.
Prompt engineering involves crafting precise instructions to optimize LLM responses. The more detailed and specific the input prompt, the more relevant and aligned the output will be with your needs.
Example:
If you ask an LLM, “Summarize prompt engineering,” the response may be vague. However, by specifying the request like this:
Prompt: “Give a 100-word summary of prompt engineering, aimed at non-technical users.”
The LLM is now guided to give a concise, audience-specific answer.
Output:
"Prompt engineering is crafting effective instructions for AI systems like ChatGPT to get the best results. It involves being specific, providing context, and breaking down complex questions. By specifying formats, providing examples, or setting boundaries, prompt engineering ensures clear communication with an AI assistant."
This method of asking for specific outputs demonstrates how to "engineer" a prompt to get a desired, precise result.
1 Context: The context provides the LLM with additional information that helps it understand your request more fully. By embedding relevant context, the AI can generate more accurate and tailored responses.
Example:
Suppose you are working on a C# project and want to modify a class definition. The context here could be a code snippet.
Prompt with Context:
"Given this C# code:
public class User { public int UserId { get; set; } public string Name { get; set; } public string Email { get; set; } public string PhoneNumber { get; set; } }
Modify the class to make UserId and Name read-only and set them in the constructor."
Output:
public class User { public int UserId { get; } public string Name { get; } public string Email { get; set; } public string PhoneNumber { get; set; } public User(int userId, string name) { UserId = userId; Name = name; } }
This example illustrates the importance of providing relevant context to guide the LLM in generating the correct response.
Example:
If you want to create a user class in C# with certain fields, specify the required fields and behavior clearly.
Vague Question:
"Create a user class."
Specific Question:
"Create a C# user class with fields: UserId, Name, PhoneNumber. Make UserId read-only and add a constructor to set these fields."
Output:
public class User { public int UserId { get; } public string Name { get; } public string Email { get; set; } public string PhoneNumber { get; set; } public User(int userId, string name) { UserId = userId; Name = name; } }
Example:
If you need to generate dummy data for a User class, provide an example of what the data should look like.
Prompt with Examples:
"Generate 5 instances of the User class with these fields: UserId, Name, Email, PhoneNumber. Use the following format for examples:
var user1 = new User(1, "John Doe", "john.doe@example.com", "555-555-5555");
Here are some examples:
var user2 = new User(2, "Jane Doe", "jane.doe@example.com", "555-555-5556");
Now generate 5 instances."
Output:
public class User { public int UserId { get; } public string Name { get; set; } public string PhoneNumber { get; set; } public User(int userId, string name, string phoneNumber) { UserId = userId; Name = name; PhoneNumber = phoneNumber; } }
Example:
Prompt: "Create a unit test for the User class using xUnit in C#."
Output:
var user1 = new User(1, "John Smith", "john.smith@example.com", "555-555-5555"); var user2 = new User(2, "Jane Doe", "jane.doe@example.com", "555-555-5556"); var user3 = new User(3, "Mary Johnson", "mary.johnson@example.com", "555-555-5557"); var user4 = new User(4, "David Lee", "david.lee@example.com", "555-555-5558"); var user5 = new User(5, "Linda White", "linda.white@example.com", "555-555-5559");
Example:
To generate data for a User class, you might use a few example data points to guide the output format.
Prompt with Few-shot Examples:
"Here are some instances of the User class:
var user1 = new User(1, "John Smith", "john.smith@example.com", "555-555-5555");
var user2 = new User(2, "Jane Doe", "jane.doe@example.com", "555-555-5556");
Now create 3 more instances following the same pattern."
Output:
public class UserTests { [Fact] public void UserConstructor_SetsProperties() { // Arrange var user = new User(1, "John Doe", "john.doe@example.com", "555-555-5555"); // Act & Assert Assert.Equal(1, user.UserId); Assert.Equal("John Doe", user.Name); Assert.Equal("john.doe@example.com", user.Email); Assert.Equal("555-555-5555", user.PhoneNumber); } }
Example:
Start with a simple prompt and progressively modify it.
First Prompt:
"Create a basic User class in Python."
Output:
public class User { public int UserId { get; } public string Name { get; } public string Email { get; set; } public string PhoneNumber { get; set; } public User(int userId, string name) { UserId = userId; Name = name; } }
Follow-up Prompt:
"Convert this class to C#."
Output:
public class User { public int UserId { get; } public string Name { get; set; } public string PhoneNumber { get; set; } public User(int userId, string name, string phoneNumber) { UserId = userId; Name = name; PhoneNumber = phoneNumber; } }
Final Prompt:
"Make the Password property private, and add a DateTime CreatedAt property initialized in the constructor."
Output:
var user1 = new User(1, "John Smith", "john.smith@example.com", "555-555-5555"); var user2 = new User(2, "Jane Doe", "jane.doe@example.com", "555-555-5556"); var user3 = new User(3, "Mary Johnson", "mary.johnson@example.com", "555-555-5557"); var user4 = new User(4, "David Lee", "david.lee@example.com", "555-555-5558"); var user5 = new User(5, "Linda White", "linda.white@example.com", "555-555-5559");
Example:
Prompt:
*"Create a set of unit tests for the following C# class:
public class User { public int UserId { get; set; } public string Name { get; set; } public string PhoneNumber { get; set; } }
Think step-by-step:
Output:
public class UserTests { [Fact] public void UserConstructor_SetsProperties() { // Arrange var user = new User(1, "John Doe", "john.doe@example.com", "555-555-5555"); // Act & Assert Assert.Equal(1, user.UserId); Assert.Equal("John Doe", user.Name); Assert.Equal("john.doe@example.com", user.Email); Assert.Equal("555-555-5555", user.PhoneNumber); } }
The article was originally written by Jim, head of Devreal at Pieces for Developers. You can find more examples and nuances in this article https://pieces.app/blog/llm-prompt-engineering
The above is the detailed content of echniques for Effective Prompt Engineering. For more information, please follow other related articles on the PHP Chinese website!