> For the complete documentation index, see [llms.txt](https://emory.gitbook.io/conversational-ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emory.gitbook.io/conversational-ai/5.-lm-based-matching/5.2.-quickstart-with-gpt.md).

# 5.2. Quickstart with GPT

## API Key

Create an account for [OpenAI](https://platform.openai.com) and log in to your account.

Click your icon on the top-right corner and select "View API keys":

<figure><img src="https://1792735690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIngbebkg4x5sRiZcznOl%2Fuploads%2FN0X5ul3K18CoIGym82Ok%2Fopenai-menu.jpg?alt=media&amp;token=259e5616-1481-4bea-88f7-8723619e5b99" alt=""><figcaption><p>OpenAI account menu.</p></figcaption></figure>

Click the "+ Create new secret key" button and copy the API key:

<figure><img src="https://1792735690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIngbebkg4x5sRiZcznOl%2Fuploads%2FkSQnkg0UmvcjgoXuf8nU%2Fopenai-api-key.jpg?alt=media&amp;token=9a72781e-6aff-4979-bb6d-8eef20ac3f1b" alt=""><figcaption><p>OpenAI API key generator.</p></figcaption></figure>

{% hint style="info" %}
Make sure to save this key in a local file. If you close the dialog without saving, you cannot retrieve the key again, in which case, you have to create a new one.
{% endhint %}

Create a file `openai_api.txt` under the [`resources`](https://github.com/emory-courses/conversational-ai/tree/main/resources) directory and paste the API key to the file such that it contains only one line showing the key.

Add `openai_api.txt` to the [`.gitignore`](https://github.com/emory-courses/conversational-ai/blob/main/.gitignore) file:

```
.idea/
venv/
/resources/openai_api.txt
```

{% hint style="info" %}
Do not share this key with anyone or push it to any remote repository (including your private GitHub repository).
{% endhint %}

## Using GPT API

Open the terminal in PyCharm and install the OpenAI package:

```
(venv) $ pip install openai
```

Create a function called `api_key()` as follow:

{% code lineNumbers="true" %}

```python
import openai

PATH_API_KEY = 'resources/openai_api.txt'
openai.api_key_path = PATH_API_KEY
```

{% endcode %}

* `#4`: specifies the path of the file containing the OpenAI API key.

Retrieve a response by creating a [`ChatCompletition`](https://platform.openai.com/docs/guides/chat) module:

{% code lineNumbers="true" %}

```python
model = 'gpt-3.5-turbo'
content = 'Say something inspiring'
response = openai.ChatCompletion.create(
    model=model,
    messages=[{'role': 'user', 'content': content}]
)
```

{% endcode %}

* `#1`: the [GPT model](https://platform.openai.com/docs/models) to use.
* `#2`: the content to be sent to the GPT model.
* `#3`: creates the chat completion model and retrieves the response.
* `#5`: messages are stored in a list of dictionaries where each dictionary contains content from either the `user` or the `system`.

Print the type of the response and the response itself that is in the JSON format:

{% tabs %}
{% tab title="Output" %}
{% code lineNumbers="true" %}

```json
<class 'openai.openai_object.OpenAIObject'>
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "\n\n\"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle.\"",
        "role": "assistant"
      }
    }
  ],
  "created": 1678893623,
  "id": "chatcmpl-6uNDL6Qfh7MjxpLxH3NW7UPJXS3tN",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 26,
    "prompt_tokens": 10,
    "total_tokens": 36
  }
}
```

{% endcode %}

* `#1`: the response type.
* `#2`: the response in the JSON format.
  {% endtab %}
  {% endtabs %}

Print only the content from the output:

{% code lineNumbers="true" %}

```python
output = response['choices'][0]['message']['content'].strip()
print(output)
```

{% endcode %}

{% tabs %}
{% tab title="Output" %}
{% code overflow="wrap" %}

```
"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle."
```

{% endcode %}
{% endtab %}
{% endtabs %}
