Introduction to OpenAI

Text Generation

Practical Applications

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:

  • Blog post creation
  • Summarization
  • Conversational agents for customer support
  • Code explanations and comments
  • Creative writing
  • Language translation

All snippets use the Chat Completion API for clarity and reproducibility.

Note

Adjust temperature (creativity) and max_tokens (length) to fine-tune your results. Lower temperature yields deterministic output, while higher values increase randomness.


Overview Table

Use CaseDescriptionExample Parameter Highlights
Blog Post GenerationDrafts articles or sectionstemperature=0.7, max_tokens=150
SummarizationCondenses long text into concise summariestemperature=0.3, max_tokens=100
Customer Support AgentAutomated, professional responses to inquiriestemperature=0.3, system prompt setup
Code Explanations & CommentsGenerates detailed code walkthroughstemperature=0.3, max_tokens=150
Creative WritingShort stories, poems, or dialoguetemperature=0.9, max_tokens=200
Language TranslationTranslates between specified languagesdefault temperature, max_tokens=60

1. Blog Post Generation

Generate full articles or individual sections by providing a clear prompt.

import openai

def 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.content

print(generate_blog_intro())

2. Text Summarization

Condense research papers, articles, or reports into concise summaries:

import openai

def 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.content

article_text = (
    "Blockchain technology is a decentralized digital ledger that records "
    "transactions across many computers to ensure security and transparency."
)

print(summarize_article(article_text))

3. Conversational Agent for Customer Support

Build a polite, professional support agent that handles common inquiries:

import openai

def 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.content

print(customer_support_response())

Warning

Never expose your API key in public repositories or client-side code. Use environment variables or a secrets manager.


4. Code Explanations and Comments

Automatically generate inline comments or detailed explanations for any code snippet:

import openai

def explain_code(code_snippet):
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{
            "role": "user",
            "content": f"Explain the following Python function:\n\n{code_snippet}"
        }],
        max_tokens=150,
        temperature=0.3
    )
    return response.choices[0].message.content

code_snippet = '''
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
'''

print(explain_code(code_snippet))

5. Creative Writing

Use GPT-4 to craft short stories, poems, or dialogues. Increase temperature for more imaginative output:

import openai

def 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.content

print(short_story())

6. Language Translation

Translate text between languages by specifying the source and target:

import openai

def 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.content

print(translate_text())

References

Watch Video

Watch video content

Previous
Prompt Engineering