How to prompt AI to improve writing
Quick answer
Use clear, specific prompts with examples and desired style instructions to guide
gpt-4o or similar models in improving writing. Include context, tone, and format requirements to get precise, polished output.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 for secure access.
pip install openai>=1.0 Step by step
Use the gpt-4o model with a prompt that includes the original text and clear instructions to improve it. Specify style, tone, and focus areas like grammar or clarity.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
original_text = "The quick brown fox jump over the lazy dog."
prompt = (
"Improve the following sentence for grammar and clarity, making it more engaging:\n" +
original_text
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
improved_text = response.choices[0].message.content
print("Improved text:", improved_text) output
Improved text: The quick brown fox jumps over the lazy dog, adding a lively touch to the scene.
Common variations
You can customize prompts to focus on style (formal, casual), length (concise, detailed), or specific improvements (tone, vocabulary). Use claude-3-5-sonnet-20241022 for advanced writing style refinement.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
original_text = "She dont like the movie."
prompt = (
"Rewrite the sentence to be grammatically correct and more expressive:\n" +
original_text
)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=100,
system="You are a helpful writing assistant.",
messages=[{"role": "user", "content": prompt}]
)
print("Improved text:", message.content[0].text) output
Improved text: She doesn't like the movie and found it quite disappointing.
Troubleshooting
If the AI output is too vague or off-topic, refine your prompt by adding explicit instructions and examples. Avoid ambiguous terms and specify the desired tone and style clearly.
Key Takeaways
- Use explicit, detailed prompts specifying what to improve (grammar, tone, clarity).
- Include the original text and desired style or tone in your prompt for best results.
- Test different models like
gpt-4oorclaude-3-5-sonnet-20241022for varied writing improvements. - Refine prompts iteratively if output is vague or off-topic.
- Always secure your API key using environment variables in code.