Prompting chatGPT#

In this notebook we will send basic prompts to chatGPT and receive answers. We will write a small prompt helper function that makes it more accessible.

import openai

A basic prompt requires to define the model we will be using as well as the role we have and the content/message we would like to be responded to.

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": 'How old is Europe?'}]
)
response
<OpenAIObject chat.completion id=chatcmpl-7DdKaCE0JvcvRjlyhUGbAmEEKXoPg at 0x258aa0a1810> JSON: {
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Europe has a long and complex history, with evidence of human presence dating back to around 45,000 BCE. The specific age of Europe as a distinct cultural and geographical entity is difficult to determine, as it has evolved over time through various empires, kingdoms, and political alliances. However, some historians place the beginning of modern Europe as the fall of the Roman Empire in 476 CE.",
        "role": "assistant"
      }
    }
  ],
  "created": 1683483808,
  "id": "chatcmpl-7DdKaCE0JvcvRjlyhUGbAmEEKXoPg",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 79,
    "prompt_tokens": 13,
    "total_tokens": 92
  }
}

To make it more convenient we write a little helper function that allows us to retrieve the response to a message directly.

def prompt(message:str, model="gpt-3.5-turbo"):
    """A prompt helper function that sends a message to openAI
    and returns only the text response.
    """
    response = openai.ChatCompletion.create(
        model=model,
        messages=[{"role": "user", "content": message}]
    )
    return response['choices'][0]['message']['content']
print(prompt('Which countries are in central Europe?'))
The countries in central Europe are:

1. Austria
2. Czech Republic
3. Germany (some parts)
4. Hungary
5. Liechtenstein
6. Poland (some parts)
7. Slovakia
8. Slovenia
9. Switzerland (some parts)
print(prompt('Which parts of Switzerland are not central Europe?'))
As an AI language model, I can't make subjective judgment calls on which parts of Switzerland are or are not considered central Europe. However, geographically speaking, Switzerland is located in the heart of Europe and is often considered a central European country.

A comment on reproducibilty#

Note that prompt responses may not be very reproducible. You can execute the same prompt twice and receive different responses.

print(prompt('Which parts of Switzerland are not central Europe?'))
Switzerland is located entirely in central Europe. Therefore, there are no parts of Switzerland that are not in central Europe.