Created
May 15, 2023 14:33
-
-
Save dserodio/97268844bd448e311da5fec96a3d45af to your computer and use it in GitHub Desktop.
OpenAI bolierplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Boilerplate for Python apps using OpenAI API. | |
# From DeepLearning.ai's "ChatGPT Prompt Engineering for Developers" mini-course | |
# First, these dependencies need to be installed: | |
# | |
# pip install openai python-dotenv | |
import openai | |
import os | |
from dotenv import load_dotenv, find_dotenv | |
_ = load_dotenv(find_dotenv()) # read local .env file | |
openai.api_key = os.getenv('OPENAI_API_KEY') | |
def get_completion(prompt, model="gpt-3.5-turbo"): | |
messages = [{"role": "user", "content": prompt}] | |
response = openai.ChatCompletion.create( | |
model=model, | |
messages=messages, | |
temperature=0, # this is the degree of randomness of the model's output | |
) | |
return response.choices[0].message["content"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment