How to give clear instructions to an AI
Quick answer
To give clear instructions to an AI, use precise and unambiguous
prompt language, provide relevant context, and include examples when possible. Specify the desired format or constraints explicitly to guide the AI's response.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python package and set your API key as an environment variable to authenticate requests.
pip install openai Step by step
Use clear, explicit instructions in your prompt. Include context and specify output format. Here is a complete example using gpt-4o model:
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
"You are a helpful assistant."
" Please summarize the following text in 3 bullet points, each under 20 words:\n"
"Text: 'Artificial intelligence is transforming industries by automating tasks, improving decision-making, and enabling new innovations.'
"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
print(response.choices[0].message.content) output
- AI automates tasks across industries. - Enhances decision-making processes. - Enables innovative new solutions.
Common variations
You can adapt instructions for different models or use streaming for real-time output. For example, use claude-3-5-sonnet-20241022 with Anthropic SDK or add examples to your prompt for clarity.
import anthropic
import os
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
system_msg = "You are a helpful assistant."
user_msg = (
"Summarize the text below in 3 concise bullet points:\n"
"Text: 'AI improves efficiency, accuracy, and innovation in many fields.'"
)
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=200,
system=system_msg,
messages=[{"role": "user", "content": user_msg}]
)
print(response.content[0].text) output
- AI boosts efficiency. - Enhances accuracy. - Drives innovation across fields.
Troubleshooting
If the AI output is vague or off-topic, refine your prompt by adding more context, specifying output format, or providing examples. Avoid ambiguous terms and keep instructions concise but detailed.
Key Takeaways
- Use explicit, unambiguous language in your
promptto guide the AI clearly. - Provide context and specify output format or constraints to improve response relevance.
- Include examples in your instructions to demonstrate the desired output style or structure.