Explore real-world use cases for text generation with OpenAI’s GPT-4, demonstrating prompt engineering and model parameters’ influence on output.
Explore real-world use cases for text generation with OpenAI’s GPT-4. Each example demonstrates how prompt engineering and model parameters influence the output. We cover:
Adjust temperature (creativity) and max_tokens (length) to fine-tune your results. Lower temperature yields deterministic output, while higher values increase randomness.
Generate full articles or individual sections by providing a clear prompt.
Copy
Ask AI
import openaidef generate_blog_intro(): response = openai.ChatCompletion.create( model="gpt-4", messages=[{ "role": "user", "content": "Write a blog post introduction about the benefits of remote work for companies and employees." }], max_tokens=150, temperature=0.7 ) return response.choices[0].message.contentprint(generate_blog_intro())
Condense research papers, articles, or reports into concise summaries:
Copy
Ask AI
import openaidef summarize_article(article_text): response = openai.ChatCompletion.create( model="gpt-4", messages=[{ "role": "user", "content": f"Summarize this article on blockchain technology:\n\n{article_text}" }], max_tokens=100, temperature=0.3 ) return response.choices[0].message.contentarticle_text = ( "Blockchain technology is a decentralized digital ledger that records " "transactions across many computers to ensure security and transparency.")print(summarize_article(article_text))
Build a polite, professional support agent that handles common inquiries:
Copy
Ask AI
import openaidef customer_support_response(): response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a helpful customer support agent."}, {"role": "user", "content": "A customer requests a refund for a defective product. Draft a professional response."} ], max_tokens=100, temperature=0.3 ) return response.choices[0].message.contentprint(customer_support_response())
Never expose your API key in public repositories or client-side code. Use environment variables or a secrets manager.
Use GPT-4 to craft short stories, poems, or dialogues. Increase temperature for more imaginative output:
Copy
Ask AI
import openaidef short_story(): response = openai.ChatCompletion.create( model="gpt-4", messages=[{ "role": "user", "content": "Write a short story about a robot learning to love music." }], max_tokens=200, temperature=0.9 ) return response.choices[0].message.contentprint(short_story())
Translate text between languages by specifying the source and target:
Copy
Ask AI
import openaidef translate_text(): response = openai.ChatCompletion.create( model="gpt-4", messages=[{ "role": "user", "content": "Translate this sentence from English to French: 'I hope you have a wonderful day.'" }], max_tokens=60 ) return response.choices[0].message.contentprint(translate_text())