Tuesday, April 15, 2025

A Beginner's Guide to Prompt Engineering 2025


Did you know that AI understands and responds better when you ask the right way? Today, I'm going to teach you what you need to know about Large Language Models (LLMs) and how to interact with them using prompt engineering. Prompts are what empower LLMs to make us more productive in our work. When you understand the power of prompting and its limitations, you can achieve even more, faster.

Let's dive right in!

Understanding Large Language Models (LLMs)

Before we get into prompt engineering, we should understand more of what LLMs are under the hood. Large language models are a type of AI that are trained on large (hence the name) amounts of text data to understand and generate human-like language. They work by predicting the next word in a sentence based on the context of the words that came before it, making their responses relevant and coherent to humans that use them. You can think of it as an ultra-smart autocomplete.

There are three things you really need to know to understand LLMs: context, tokens, and limitations.

  • Context: Think of context as the surrounding information that helps an LLM understand what you're talking about. Just like in a conversation with a friend, the more context you have, the more the conversation will make sense. Context provides the necessary background for the LLM to generate relevant and accurate responses. It's about giving the AI enough information to grasp the essence of what you're asking.

  • Tokens: In the world of LLMs, text is broken down into units called tokens. A token can be a word, a part of a word, or even a single letter. The AI model processes these tokens to generate responses. The number of tokens you use with an LLM can impact its response. Too few tokens, and it may lack context; too many, and it might get overwhelmed. Each LLM has a specific token limit that you need to be aware of.

  • Limitations: While LLMs are powerful, they aren't all-powerful. They don't truly understand language like humans do. They rely on patterns and probabilities that were present in the data they trained on. We won't get into the training side of things in this episode, but hopefully, this quickly explains why we need a diverse and broad training set for the best responses. Models are never perfect, so they sometimes provide incorrect or nonsensical answers, sometimes called "hallucinations."

The Power of Prompts

You can work with an LLM's power and limitations by combining everything into a prompt. A prompt gives the model context via tokens and works around the model's potential limitations so that the model can give you a response.

For example, if you prompt an LLM with, "Write a JavaScript function to calculate the factorial of a number," it will use its training to give you a function that accomplishes that task. Depending on how the model was trained, it may understand your prompt differently and output different code. This is why you may see different outputs from various models out in the world, like OpenAI's GPT, Anthropic's Claude, Google's Gemini, and so on. These are all trained differently and understand and output differently as a result.

What is Prompt Engineering?

So now we know what a prompt is, but what is prompt engineering? Honestly, it's just a term for crafting prompts. Just like how clear instructions can help a friend complete a task exactly how you want it, a well-crafted prompt can help an LLM understand and deliver what you're looking for. Good prompt engineering can drastically improve the quality and relevance of the outputs you get from an LLM. It's about being intentional and thoughtful in how you communicate with the AI.

Key Components of Effective Prompting

An effective prompt is clear and precise. Ambiguity can confuse the model, so don't forget that an effective prompt also gives enough context. You want to provide enough background information to get the job done without overwhelming the model with unnecessary detail. And don't forget, you can always iterate and refine your prompts. If you ask for something and you don't get what you expect, tweak your language.

Let's look at an example. Imagine you're using GitHub Copilot and you say, "Write a function that will square numbers in a list." At first glance, this sentence isn't too bad. But let me ask you:

  • What language should the function be written in?

  • Do you want to include negative numbers?

  • Will the input ever have non-numbers?

  • Should it affect the given list or return a new list?

Let's refine our prompt a bit. So we can say, "Write a Python function that takes a list of integers and returns a new list where each number is squared, excluding any negative numbers."

Do you see the difference? This prompt is clear and specific about the language we want to use, what the function should do, what constraints there are, and the expected input type. By giving GitHub Copilot more context, the output will be more relevant to exactly what we want from it.

In summary, prompt engineering is really about being a good communicator with the computer, very similar to coding. By crafting your prompts thoughtfully, you can harness the full power of tools like GitHub Copilot to make your experience smoother and more efficient.

Troubleshooting Common Prompting Issues

Now, as you're working with GitHub Copilot and other LLM tools, you may run into times where you're not getting the output you hoped for. The problem isn't always because you're not specific enough. Here are some things you might be running into and how you can improve them:

  • Prompt Confusion: Sometimes we mix multiple requests or aren't clear enough, which can lead to confusing outputs. For example, we might highlight something in VS Code and say, "Fix the errors in this code and optimize it." Is the AI supposed to fix the errors or optimize it first? What should it optimize for – speed, memory, readability?

    • Solution: Break it down. Separate your asks in the prompt. "First, fix the errors in the code snippet. Then, optimize the fixed code for better performance." Building out a prompt iteratively will make it more likely that you get the final result that you want because each step of the way has been thought through.

  • Token Limits: We've discussed that tokens are units of words or partial words that a model can handle. It's important to remember that there's a limit to how many tokens a given model can handle at once. If your prompt is too long or the expected output is very extensive, the LLM might start "hallucinating," cut itself off and only give you a partial response, or just fail entirely.

    • Solution: You want to keep your prompts concise. Similarly to before, you will want to iterate on smaller parts but also only provide necessary context. Does the LLM need your entire code file to work, or can it just use a few lines of code in a certain function? Instead of asking for it to generate an entire application, could you ask it to make each component step by step?

  • Unstated Assumptions: We often assume that the LLM knows more than it does. If you say, "Add authentication to my app," does it know what your app even does? Does it know which technologies you might want to use?

    • Solution: When doing a prompt like this, you need to explicitly state your requirements, outline your specific needs, mention best practices if needed, and be ready to iterate again with edge cases and restraints. By stating all your requirements, you help ensure that the LLM doesn't overlook critical aspects of your request when it generates the output.

Conclusion

We've gone over a lot in this section. Prompt engineering is an art and a science, and you'll get better with practice. It's about understanding how LLMs work, what they need, and how to communicate effectively with them.

As a quick overview, when you're working with GitHub Copilot or any other LLM tool, you use tokens to give the model context, given any limitations it might have. Your prompts should be clear, concise, and precise for the best results, providing as much context as the model might need. You should break down your prompts into smaller chunks if there are multiple tasks to be completed and iterate from there. You should be specific about your requirements and needs so that the model understands the constraints of what it should provide for you.

Thanks so much for reading! Remember to sign up for GitHub Copilot if you haven't already done so and get started for free.

0 comments:

Post a Comment