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.

from openai import OpenAI

from IPython.core.magic import register_line_cell_magic
from IPython.display import display, Markdown

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. To make it more convenient we write a little helper function that allows us to retrieve the response to a message directly.

@register_line_cell_magic
def prompt(line:str, cell:str, model="gpt-3.5-turbo"):
    """A prompt helper function that sends a message to openAI
    and prints out the text response.
    """
    message = line + "\n" + cell
    client = OpenAI()
    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": message}]
    )
    text = response.choices[0].message.content
    display(Markdown(text))
%%prompt
Which countries are in central Europe?

The countries in Central Europe are:

  1. Austria

  2. Czech Republic

  3. Germany (some parts are considered part of Central Europe)

  4. Hungary

  5. Poland

  6. Slovakia

  7. Slovenia (sometimes considered part of Central Europe)

  8. Switzerland (some parts are considered part of Central Europe)

%%prompt
Which parts of Switzerland are not central Europe?

Switzerland is located in central Europe, so there are no parts of Switzerland that are not part of central Europe.

A comment on reproducibility#

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

%%prompt
Which parts of Switzerland are not central Europe?

The parts of Switzerland that are not considered central Europe are its overseas territories or colonies, as Switzerland does not have any. However, geographically speaking, Switzerland entirely lies within Central Europe.