> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Practical Applications

> 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:

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

All snippets use the [Chat Completion API](https://platform.openai.com/docs/guides/chat) for clarity and reproducibility.

<Callout icon="lightbulb" color="#1CB2FE">
  Adjust `temperature` (creativity) and `max_tokens` (length) to fine-tune your results. Lower `temperature` yields deterministic output, while higher values increase randomness.
</Callout>

***

## Overview Table

| Use Case                     | Description                                    | Example Parameter Highlights           |
| ---------------------------- | ---------------------------------------------- | -------------------------------------- |
| Blog Post Generation         | Drafts articles or sections                    | `temperature=0.7`, `max_tokens=150`    |
| Summarization                | Condenses long text into concise summaries     | `temperature=0.3`, `max_tokens=100`    |
| Customer Support Agent       | Automated, professional responses to inquiries | `temperature=0.3`, system prompt setup |
| Code Explanations & Comments | Generates detailed code walkthroughs           | `temperature=0.3`, `max_tokens=150`    |
| Creative Writing             | Short stories, poems, or dialogue              | `temperature=0.9`, `max_tokens=200`    |
| Language Translation         | Translates between specified languages         | default temperature, `max_tokens=60`   |

***

## 1. Blog Post Generation

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

```python theme={null}
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:

```python theme={null}
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:

```python theme={null}
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())
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Never expose your API key in public repositories or client-side code. Use environment variables or a secrets manager.
</Callout>

***

## 4. Code Explanations and Comments

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

```python theme={null}
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:

```python theme={null}
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:

```python theme={null}
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

* [OpenAI Chat Completion API](https://platform.openai.com/docs/guides/chat)
* [OpenAI Models](https://platform.openai.com/docs/models)
* [API Best Practices](https://platform.openai.com/docs/guides/rate-limits)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-openai/module/b6b7bec7-ed21-47d5-afbb-663df59f5e97/lesson/04f1a674-6fc4-47a3-a431-2ccfe3bf41ef" />
</CardGroup>
