OpenAI API Chat Request

This is the basic code to make an API request of the OpenAI API with Python. Make sure to install the OpenAI Python Package.

pip3 install openai
import openai

client = openai.OpenAI(api_key='APIKEY',)

response = client.chat.completions.create(
    messages=[
      {"role": "system", "content": "You are a Journalist"},
      {"role": "assistant", "content": "Limit response to 25 words"},
      {"role": "user", "content": "what is a computer?"}
    ],
    model="gpt-3.5-turbo",
  )

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

Be the first to comment

Leave a Reply