> ## 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.

# Chat Completions

> This guide explains migrating to the modern chat completions API for enhanced conversational experiences with OpenAI models.

Unlock richer conversational experiences by switching from legacy completions endpoints to the modern `chat.completions.create` interface. This guide walks you through migrating your code, customizing your client, leveraging message roles, and fine-tuning parameters for GPT-3.5-Turbo, GPT-4, and beyond.

***

## Why Migrate from Legacy Endpoints

OpenAI’s older `completions.create` and `Completion.create` endpoints stopped receiving updates as of July 2023. The new chat completions API supports structured conversations with roles, function calls, and more control over model behavior.

<Callout icon="triangle-alert" color="#FF6B6B">
  Avoid using legacy calls: they no longer receive feature updates and may be removed in future releases.
</Callout>

### Legacy Usage Examples

```python theme={null}
# Legacy OpenAI Python SDK (no longer recommended)
from openai import OpenAI
client = OpenAI()

response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Write a tagline for an ice cream shop."
)
```

```python theme={null}
# Legacy openai-python (also deprecated)
import openai
response = openai.Completion.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Write a tagline for an ice cream shop."
)
```

***

## Modern Chat Completions

Switch to the chat API for richer, role-based interactions:

```python theme={null}
from openai import OpenAI

openai = OpenAI(api_key="sk-...")

response = openai.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Write a tagline for an ice cream shop."}]
)

print(response.choices[0].message.content)
```

***

## Naming Your OpenAI Client

Feel free to name your client object whatever you like. Here’s a standard pattern:

```python theme={null}
from openai import OpenAI

openai = OpenAI(api_key="sk-...")

def chat_comp(prompt):
    response = openai.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=250
    )
    return response.choices[0].message.content

print(chat_comp("How can I make more money?"))
```

Or with a custom client name:

```python theme={null}
from openai import OpenAI

RobotBestFriend = OpenAI(api_key="sk-...")

def chat_comp(prompt):
    response = RobotBestFriend.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        max_tokens=250
    )
    return response.choices[0].message.content

print(chat_comp("How can I make more money?"))
```

Console output:

```plaintext theme={null}
There are several strategies you can consider to increase your income. Here are some ideas:
1. **Negotiate Your Salary:** ...
2. **Acquire New Skills:** ...
3. **Side Gigs:** ...
4. **Investing:** ...
5. **Start a Business:** ...
6. **Monetize Hobbies:** ...
```

<Callout icon="lightbulb" color="#1CB2FE">
  Never commit your `api_key` to public repositories. Use environment variables or secret management tools.
</Callout>

***

## Roles in Chat Messages

The `messages` array defines each turn in a conversation. Supported roles:

| Role      | Purpose                                                   | Example                                                        |
| --------- | --------------------------------------------------------- | -------------------------------------------------------------- |
| system    | Sets global context, tone, or behavior for the assistant. | `"You are a helpful assistant."`                               |
| user      | End user’s input or question.                             | `"What's the weather today?"`                                  |
| assistant | Model’s prior responses in a multi-turn conversation.     | `"The weather is sunny and 75°F."`                             |
| function  | Allows the model to call a custom function you define.    | `"function_call": {"name": "get_weather", "arguments": {...}}` |

### JavaScript Example

```javascript theme={null}
const openai = new OpenAI({ apiKey: "sk-..." });

const response = await openai.chat.completions.create({
  model: "gpt-4",
  messages: [
    {
      role: "system",
      content: [
        {
          type: "text",
          text: "You are a friendly coding tutor who uses southern idioms."
        }
      ]
    },
    {
      role: "user",
      content: [
        {
          type: "text",
          text: "Are semicolons optional in JavaScript?"
        }
      ]
    }
  ]
});
console.log(response.choices[0].message.content);
```

***

## Adding a System Prompt in Python

Include both `system` and `user` messages to guide the model’s tone:

```python theme={null}
from openai import OpenAI

RobotBestFriend = OpenAI(api_key="sk-...")

def chat_comp(prompt):
    response = RobotBestFriend.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are a southern belle."},
            {"role": "user",   "content": prompt}
        ],
        max_tokens=250
    )
    return response.choices[0].message.content

print(chat_comp("What are some side hustles I can try?"))
```

Console output:

```bash theme={null}
There are several ways to bring a little more income if you set your mind to it, sugar...
```

<Callout icon="lightbulb" color="#1CB2FE">
  Place the `system` message before the `user` message to ensure the context is applied first.
</Callout>

***

## Exploring the `create` Signature

The `chat.completions.create` method supports numerous parameters for fine-tuning:

```python theme={null}
def create(
    *,
    model: str,
    messages: Iterable[ChatCompletionMessageParam],
    max_tokens: int | None = None,
    n: int | None = None,
    temperature: float | None = None,
    top_p: float | None = None,
    presence_penalty: float | None = None,
    frequency_penalty: float | None = None,
    logit_bias: dict[str, int] | None = None,
    functions: Iterable[Function] | None = None,
    function_call: FunctionCall | None = None,
    stream: bool | None = None,
    ...
):
    ...
```

Experiment with these options for customized responses:

* **temperature**: Controls randomness.
* **top\_p**: Nucleus sampling probability.
* **presence\_penalty** & **frequency\_penalty**: Encourage topic variety.
* **functions** & **function\_call**: Invoke your own code.

***

## Links and References

* [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat)
* [Kubernetes Basics](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)
* [Docker Hub](https://hub.docker.com/)
* [Terraform Registry](https://registry.terraform.io/)

<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/008e902f-5b14-4a71-8d0c-f0f42691e659" />
</CardGroup>
