python-openaiHow to use OpenAI's ChatGPT API in Python?
OpenAI's ChatGPT API is a natural language processing (NLP) model that can be used to generate human-like conversations. It can be used in Python with the OpenAI SDK.
Example code
import openai
openai.api_key = "YOUR_API_KEY"
response = openai.Completion.create(
engine="davinci",
prompt="Hello, how are you?",
temperature=0.7,
max_tokens=50,
)
print(response.choices[0].text)
Output example
I'm doing great, thank you!
Code explanation
import openai
: imports the OpenAI SDK.openai.api_key = "YOUR_API_KEY"
: sets the API key for authentication.response = openai.Completion.create(...)
: creates a completion object with the given parameters.temperature=0.7
: sets the temperature of the response, which controls the randomness of the response.max_tokens=50
: sets the maximum number of tokens in the response.print(response.choices[0].text)
: prints the text of the first choice in the response.