Build with AI

Ilias Papachristos
Google for Developers Europe
7 min readJun 4, 2024

It’s always great to have the opportunity to speak and write about the things you love!

As the Lead of the GDG Cloud Thessaloniki and as a Google Cloud Champion Innovator for AI/ML I had the pleasure to talk with our members about AI on Google Cloud.

The idea was to share my knowledge of LLMs and demystify terms like temperature, top-k, top-p, prompt design and engineering among others.

💡 Prompt Engineering Best Practises
Have you ever felt like you weren’t quite getting what you expected from a large language model (LLM)? Well, you’re not alone! But fear not, there’s a way to bridge the gap between your desires and the LLM’s responses: prompt engineering.

In simpler terms, prompt engineering is the art of crafting clear and concise instructions for the LLM. By taking control of how you “ask the question,” you can significantly improve the quality and accuracy of the results you receive.

This article will guide you through some key prompt engineering best practices to help you get the most out of your LLM interactions. Let’s dive in!

Express Yourself Clearly:

  • Keep it Short and Sweet: Avoid overly complex prompts. The clearer and more concise your instructions are, the easier it is for the LLM to understand your intent.
  • Specificity is Key: Don’t leave room for misinterpretation. Clearly define what you want the LLM to do and the information you expect in the response.
  • One Task at a Time: Break down complex requests into smaller, more manageable tasks. This helps the LLM focus on delivering exactly what you need.

Boost Accuracy and Safety:

  • The Power of Examples: Sometimes, showing is better than telling. Including relevant examples within your prompts can significantly improve the accuracy and direction of the LLM’s response.
  • Classification for Safety: When dealing with potentially risky prompts, consider reframing them as classification tasks. This can help guide the LLM towards safer and more appropriate outputs.

By following these simple tips, you’ll be well on your way to becoming a prompt engineering pro! Remember, the more effectively you communicate with your LLM, the better the results you’ll achieve. So, get out there and start crafting those perfect prompts!

💡 Be concise
❌ Don’t

prompt = "I want to open a shop to sell flowers. What do you think could be a good name for a flower shop that specializes in selling bouquets of dried flowers more than fresh flowers?"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)

✅ Do. To the point

prompt = "Suggest a name for a flower shop that sells bouquets of dried flowers"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)

💡 Be Specific
❌ Don’t. This is too generic

prompt = "Tell me about the Moon"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)

✅ Do. It’s specific

prompt = "Generate a list of ways that makes Earth unique compare to other planets of the solar system"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)

💡 One task at a time
❌ Don’t.

prompt = "What's the best way to boil an egg and why is the sky blue?"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)

✅ Do.

prompt = "What's the best way to boil an egg??"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)
prompt = "Why is the sky blue?"
print(generation_model.predict(prompt=prompt, max_output_tokens=256).text)

💡 Hallucinations
Large language models (LLMs) are impressive AI systems that have learned from massive amounts of data. They can generate text, translate languages, write different kinds of creative content, and answer your questions in informative ways. However, it’s important to remember that they’re still under development. Sometimes, LLMs might include information in their responses that isn’t quite accurate — like a creative writer getting a little carried away! We call these made-up bits “hallucinations.”

Great sense of humour 🥰

💡 Zero-shot, one-shot, few-shot prompting methods
Have you ever wondered how to get the most out of your large language model (LLM)? Well, wonder no more! One secret weapon for boosting response quality is the power of examples.

Think of your LLM as a super student who learns best by seeing things in action. By providing relevant examples in your prompts, you’re essentially giving your LLM a cheat sheet for understanding what kind of response you’re looking for. Just a few well-chosen examples (think one to five) can make a big difference!

But there’s a catch, too much information can backfire. Just like cramming for a test, overloading your LLM with too many examples can lead to it focusing too much on the specifics and missing the bigger picture. The key is to find the sweet spot: a curated selection that reflects the types of responses you want your LLM to generate.

Now, you might be thinking: “Isn’t this similar to how we train traditional models?”. The answer is absolutely! Just like any good teacher, you want to provide high-quality examples that represent the situations your LLM will encounter most often. Imagine training a classifier — you wouldn’t just show pictures of cats, you’d also include dogs, birds, and other animals to ensure it can tell the difference!

The final piece of the puzzle is choosing the right prompt technique. Here’s a simplified breakdown:

  • Zero-shot prompts: These prompts are open-ended, like asking a question. They’re great for sparking creative responses, but may not always be super specific.
I hope that you like my article 😎
  • One-shot and few-shot prompts: These prompts provide one or a few examples to guide your LLM. This helps it learn the desired response format and style, leading to more consistent and predictable results.
One-shot prompt
Few-shot prompt

Ultimately, the best prompt technique depends on your goals. If you’re looking for a burst of creativity, zero-shot might be your pick. But if you need consistent, reliable answers, one-shot or few-shot prompts are the way to go!

So, next time you’re interacting with your LLM, remember the power of examples. With a few well-chosen prompts, you can unlock its full potential and get the kind of responses you’ve always dreamed of!

💡 Generative Tasks to Classification Tasks
To reduce the output variability you can turn your generative tasks into classification tasks. The response of a generative task might be useful for brainstorming but it will be highly variable. The classification reduces the output variability and it will be easier to control.

Generative Task
Classification Task

💡 Temperature, Top-k, Top-p
In Google Cloud’s Vertex AI, temperature, top-k, and top-p are all parameters used during text generation by large language models (LLMs) to control the randomness and focus of the output. Here’s a breakdown of each:

Temperature:

  • Imagine temperature as a dial for randomness. A lower temperature results in more predictable and likely responses, favouring the most probable continuations based on the LLM’s training data.
  • Conversely, a higher temperature injects more randomness, allowing the LLM to explore less likely but potentially more creative options.

Top-k:

  • This parameter limits the options the LLM considers during generation. It essentially creates a shortlist of the k most probable next tokens (words) based on the current context.
  • A lower k value restricts the LLM to a smaller set of highly likely words, leading to more focused and safe outputs.
  • A higher k value provides the LLM with more options, allowing for a wider range of possibilities but potentially introducing some unexpected or nonsensical choices.

Top-p:

  • Similar to top-k, top-p filters the next token based on probability, but in a cumulative way. It considers all possible continuations and stops searching once the cumulative probability reaches a threshold (p).
  • A lower p-value restricts the LLM to focus on the most probable continuations, similar to a low temperature.
  • A higher p-value allows the LLM to explore a wider range of options with slightly lower probabilities, offering a balance between focus and creativity.

Choosing the Right Settings:

There’s no one-size-fits-all answer for these parameters. The ideal settings depend on your specific needs:

  • For factual or informative text generation: Use a lower temperature and potentially a lower top-k or top-p value to prioritise accuracy and focus.
  • For creative writing or generating different artistic text formats: Experiment with higher temperatures and potentially higher top-k or top-p values to encourage exploration and surprise.

Remember:

  • It’s generally recommended to only adjust one of temperature, top-k, or top-p at a time to avoid unintended consequences.
  • Google Cloud offers tools to experiment with these parameters and find the optimal settings for your specific use case.

I used the following example to get 5 meme ideas. I copied the first one and pasted it in the ImageFX.

ImageFX — photorealistic, detailed, photography

Follow me, and let’s connect on LinkedIn 😎

--

--

Ilias Papachristos
Google for Developers Europe

Full-Time Family Man, Retired Military Helicopter Pilot, Kendo Instructor, Google Cloud Champion Innovator AI/ML, Lead GDG Cloud Thessaloniki, WTM Ambassador