Learn the two core principles of prompt engineering and how to apply them with practical tactics.
pip install groq
from groq import Groq
client = Groq(api_key="YOUR_GROQ_API_KEY") # or set env var GROQ_API_KEY
def get_completion(prompt, model="llama3-8b-8192"):
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0
)
return response.choices[0].message.content
💡 Groq models to use:
llama3-8b-8192,llama3-70b-8192,mixtral-8x7b-32768
Clear ≠ Short. Longer prompts often give better results.
Clearly separate input text from instructions to avoid prompt injection.
text = """You should express what you want a model to do by providing \
instructions that are as clear and specific as you can possibly make them."""
prompt = f"""
Summarize the text delimited by triple backticks into a single sentence.
"""
print(get_completion(prompt))
> Delimiters: ` ``` `, `"""`, `< >`, `---`, XML tags, section titles
---
#### Tactic 2 — Ask for Structured Output
prompt = """
Generate a list of 3 made-up book titles with authors and genres.