Created
October 27, 2022 15:34
-
-
Save jlomako/d5e230398c9242727e85d9652e5de5d3 to your computer and use it in GitHub Desktop.
use GPT-3 in R with the OpenAI API
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
##################################################### | |
# Use GPT-3 in R with the OpenAI API. You need to install the reticulate package. | |
# Additionally, an API key is required that must be saved in a separate file, called .openaikey | |
# Get your API key here: https://openai.com/api/ | |
##################################################### | |
# install.packages("reticulate") # run only once | |
library(reticulate) | |
# create python env | |
conda_create(envname = "gpt3", packages = "openai", python_version = "3.10") | |
use_condaenv("gpt3") | |
# import openai | |
openai <- import("openai") | |
# read api key from file | |
openai$api_key <- scan(".openaikey", what="character", sep=NULL) | |
# set parameters | |
response <- openai$Completion$create( | |
model = "text-davinci-002", # or try: "text-ada-001" | |
prompt = "Write a 20 word message to a loved one", | |
temperature = 0.9, # randomness, the higher the weirder | |
max_tokens = 30L, # needs integer | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
# get response | |
cat(response$choices[[1]]$text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment