Here is how you can call OpenAI’s ChatGPT API, given that you have an API key. Follow these instructions to get one.
import openai
openai.api_key = ''
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
This will print out something like the following:
<OpenAIObject chat.completion id=chatcmpl-xxx at xxx> JSON: {
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1689160553,
"model": "gpt-3.5-turbo-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 53,
"completion_tokens": 17,
"total_tokens": 70
}
}
Leave a Reply
You must be logged in to post a comment.