This guide outlines two key principles for writing effective prompts for large language models (LLMs). By following these guidelines, you can craft clear and structured prompts that help the model generate accurate and relevant responses.
Guidelines for Prompting
Note: These notes are based on the videos available on the OpenAI platform.
This guide outlines two key principles for writing effective prompts for large language models (LLMs).
Setting Up the Environment
Before working with LLMs, ensure the environment is properly configured.
Steps
- Load API Key and Libraries
Use the following code to load the necessary libraries and set up the OpenAI client:
1# import openai
2import os
3
4from openai import OpenAI
5from dotenv import load_dotenv, find_dotenv
6
7_= load_dotenv(find_dotenv())
8
9# openai.api_key = os.getenv("OPENAI_API_KEY")
10client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
- A helper function is created to get the model working
1def get_answer(prompt):
2 completion = client.chat.completions.create(
3 model="gpt-3.5-turbo",
4 messages=[
5 {"role": "user", "content": prompt}
6 ]
7 )
8 return completion.choices[0].message.content
Prompting Principles
To write effective prompts, follow these two principles:
Write Clear and Specific Prompts
Ensure the prompt is unambiguous and provides clear instructions to the model.Give the Model Time to Think
Use techniques like breaking down tasks or asking the model to reason step-by-step.
Tip: Use Delimiters
When writing prompts, use delimiters to indicate distinct parts of the input. Delimiters can be symbols like ..., <...>, or tags such as <tag>...</tag>.
Example: Correcting Spelling Mistakes
Input Paragraph with Errors
1paragraph_with_errors = """
2Ths is a smple paragraf with severl speling erors. It is meant to test the abilty of the model to corect these mistkes.
3"""
Prepare the Prompt
1prompt = f"Please correct the spelling mistakes in the following paragraph:\n\n{paragraph_with_errors}"
Run the Function
1response = get_answer(prompt)
2print(response)
Output
1This is a simple paragraph with several spelling errors. It is meant to test the ability of the model to correct these mistakes.
Conclusion
By following these guidelines, you can create structured and effective prompts that help large language models generate accurate and relevant responses. Clear and specific prompts, along with techniques to give the model time to think, are essential for maximizing the model’s capabilities and improving the quality of its outputs.






