-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai.rb
115 lines (107 loc) · 2.93 KB
/
openai.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
class Openai
class << self
OPENAI_API_KEY = ENV.fetch("OPENAI_API_KEY", nil).freeze
DEFAULT_MAX_TOKENS = 16.freeze
DEFAULT_TEMP = 0.6.freeze
DEFAULT_TOP_P = 1.freeze
DEFAULT_STOP = nil.freeze
def complete(
model: "text-davinci-003",
prompt: nil,
suffix: nil,
logprobs: nil,
echo: false,
best_of: 1,
max_tokens: DEFAULT_MAX_TOKENS,
temperature: DEFAULT_TEMP,
top_p: DEFAULT_TOP_P,
n: 1, # rubocop:disable Naming/MethodParameterName
stream: false,
stop: DEFAULT_STOP,
presence_penalty: 0,
frequency_penalty: 0,
logit_bias: {},
user: ""
)
resp = HTTP.headers({
"Content-Type": "application/json",
"Authorization": "Bearer #{OPENAI_API_KEY}"
}).post("https://api.openai.com/v1/completions", json: {
model: model,
prompt: prompt,
suffix: suffix,
logprobs: logprobs,
echo: echo,
best_of: best_of,
max_tokens: max_tokens,
temperature: temperature,
top_p: top_p,
n: n,
stream: stream,
stop: stop,
presence_penalty: presence_penalty,
frequency_penalty: frequency_penalty,
logit_bias: logit_bias,
user: user,
})
data = resp.parse.deep_symbolize_keys
[data, resp.status]
end
def chat(
model: "gpt-3.5-turbo",
messages: [],
max_tokens: DEFAULT_MAX_TOKENS,
temperature: DEFAULT_TEMP,
top_p: DEFAULT_TOP_P,
n: 1, # rubocop:disable Naming/MethodParameterName
stream: false,
stop: DEFAULT_STOP,
presence_penalty: 0,
frequency_penalty: 0,
logit_bias: {},
user: ""
)
resp = HTTP.headers({
"Content-Type": "application/json",
"Authorization": "Bearer #{OPENAI_API_KEY}"
}).post("https://api.openai.com/v1/chat/completions", json: {
model: model,
messages: messages,
max_tokens: max_tokens,
temperature: temperature,
top_p: top_p,
n: n,
stream: stream,
stop: stop,
presence_penalty: presence_penalty,
frequency_penalty: frequency_penalty,
logit_bias: logit_bias,
user: user,
})
data = resp.parse.deep_symbolize_keys
[data, resp.status]
end
def edit(
model: "text-davinci-edit-001",
input: "",
instruction: "",
n: 1, # rubocop:disable Naming/MethodParameterName
temperature: DEFAULT_TEMP,
top_p: DEFAULT_TOP_P
)
resp = HTTP.headers({
"Content-Type": "application/json",
"Authorization": "Bearer #{OPENAI_API_KEY}"
}).post("https://api.openai.com/v1/edits", json: {
model: model,
input: input,
instruction: instruction,
n: n,
temperature: temperature,
top_p: top_p,
})
data = resp.parse.deep_symbolize_keys
[data, resp.status]
end
end
end