-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rb
More file actions
29 lines (25 loc) · 774 Bytes
/
Copy pathclient.rb
File metadata and controls
29 lines (25 loc) · 774 Bytes
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
class Client
BASE_URL = 'https://api.openai.com'
def initialize(api_key: ENV.fetch('OPENAI_API_KEY'))
@api_key = api_key
end
# https://platform.openai.com/docs/api-reference/chat/create
def chat(messages, temperature: 0.7, model: "gpt-3.5-turbo")
params = {
messages: messages,
temperature: temperature,
model: model
}
connection.post("v1/chat/completions", params)
end
private
def connection
@connection ||= Faraday.new(BASE_URL) do |conn|
conn.request :json
conn.request :authorization, 'Bearer', @api_key
conn.response :json, parser_options: { symbolize_names: true }
conn.response :logger, nil, { headers: true, bodies: true }
conn.adapter Faraday.default_adapter
end
end
end