Skip to content

Instantly share code, notes, and snippets.

@jlomako
Created October 27, 2022 15:34
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
use GPT-3 in R with the OpenAI API
#####################################################
# 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