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.
client = openai.OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": 'How old is Europe?'}]
)
response
ChatCompletion(id='chatcmpl-8WjEPJ4InHoD9Tbmr2lnzUGc1CDhs', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='Europe is not a living entity and therefore does not have an age. However, the continent of Europe has a long history that dates back thousands of years. The earliest human settlements in Europe date back to around 45,000 years ago, while the modern concept of Europe emerged during the Renaissance period in the 14th century.', role='assistant', function_call=None, tool_calls=None))], created=1702811057, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=66, prompt_tokens=12, total_tokens=78))
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.
"""
client = openai.OpenAI()
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": message}]
)
return response.choices[0].message.content
print(prompt('Which countries are in central Europe?'))
The countries typically considered part of Central Europe are:
1. Austria
2. Czech Republic
3. Germany (parts of it, mainly eastern regions such as Saxony and Thuringia)
4. Hungary
5. Liechtenstein
6. Poland (some debate exists over whether it belongs to Central or Eastern Europe)
7. Slovakia
8. Slovenia
9. Switzerland (parts of it, mainly western regions such as Bern and Lucerne)
Note: The specific definition of Central Europe may vary, and some sources may include additional or different countries in this region.
print(prompt('Which parts of Switzerland are not central Europe?'))
Switzerland is located in Central Europe, so all parts of Switzerland are considered to be in central Europe.
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 in central Europe, so there are no parts of Switzerland that are not considered part of central Europe. The country is bordered by Germany to the north, France to the west, Italy to the south, and Austria and Liechtenstein to the east.