Skip to content

Instantly share code, notes, and snippets.

@ztangent
Created December 5, 2022 14:53
Show Gist options
  • Save ztangent/949ea51b3c9b87e513b03a62f4ded234 to your computer and use it in GitHub Desktop.
Save ztangent/949ea51b3c9b87e513b03a62f4ded234 to your computer and use it in GitHub Desktop.
Julia interface for the OpenAI's GPT-3 API
using HTTP, JSON3
"Call the OpenAI JSON API and return the results."
function gpt3_api_call(
prompt, n_completions::Int=1;
endpoint::String = "https://api.openai.com/v1/completions",
api_key::String = get(ENV, "OPENAI_API_KEY", ""),
organization::String = get(ENV, "OPENAI_ORGANIZATION", ""),
n_retries::Int = 10,
model::String = "text-davinci-003",
temperature::Real = 1.0,
max_tokens::Int = 1024,
logprobs::Union{Nothing,Int} = 0,
echo::Bool = false,
stop::Union{Nothing,String} = nothing,
verbose::Bool = false,
logit_bias::Union{Dict,Nothing} = nothing,
options... # Other options
)
# Construct HTTP request headers and body
headers = ["Content-Type" => "application/json",
"Authorization" => "Bearer $api_key",
"OpenAI-Organization" => organization]
body = Dict{String,Any}(
"prompt" => prompt,
"n" => n_completions,
"model" => model,
"temperature" => temperature,
"max_tokens" => max_tokens,
"logprobs" => logprobs,
"echo" => echo,
"stop" => stop,
options...
)
if !isnothing(logit_bias)
body["logit_bias"] = logit_bias
end
body = JSON3.write(body)
# Post request with exponential backoff
if verbose println("Posting HTTP request...") end
delays = ExponentialBackOff(n=n_retries, first_delay=0.5, max_delay=60.0,
factor=2.0, jitter=0.1)
request = Base.retry(delays=delays,
check=(_, e) -> HTTP.RetryRequest.isrecoverable(e)) do
HTTP.post(endpoint, headers, body, retry=false)
end
response = request()
return JSON3.read(response.body)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment