lib/cryptum/open_ai.rb in cryptum-0.0.423 vs lib/cryptum/open_ai.rb in cryptum-0.0.424
- old
+ new
@@ -10,11 +10,12 @@
# option_choice: 'required - option_choice object containing command line params',
# token: 'required - open_ai bearer token',
# http_method: 'optional HTTP method (defaults to GET)
# rest_call: 'required rest call to make per the schema',
# params: 'optional params passed in the URI or HTTP Headers',
- # http_body: 'optional HTTP body sent in HTTP methods that support it e.g. POST'
+ # http_body: 'optional HTTP body sent in HTTP methods that support it e.g. POST',
+ # timeout: 'optional - timeout in seconds (defaults to 60)'
# )
private_class_method def self.open_ai_rest_call(opts = {})
http_method = if opts[:http_method].nil?
:get
@@ -25,10 +26,12 @@
params = opts[:params]
http_body = opts[:http_body].to_s.scrub
base_open_ai_api_uri = 'https://api.openai.com/v1'
token = opts[:token]
option_choice = opts[:option_choice]
+ timeout = opts[:timeout]
+ timeout ||= 60
if option_choice.proxy
rest_client = RestClient
rest_client.proxy = option_choice.proxy
rest_client_request = rest_client::Request
@@ -44,58 +47,57 @@
headers: {
content_type: 'application/json; charset=UTF-8',
authorization: "Bearer #{token}",
params: params
},
- verify_ssl: false
+ verify_ssl: false,
+ timeout: timeout
)
-
when :post
response = rest_client_request.execute(
method: :post,
url: "#{base_open_ai_api_uri}/#{rest_call}",
headers: {
content_type: 'application/json; charset=UTF-8',
authorization: "Bearer #{token}"
},
payload: http_body,
- verify_ssl: false
+ verify_ssl: false,
+ timeout: timeout
)
-
else
- raise @@logger.error("Unsupported HTTP Method #{http_method} for #{self} Plugin")
+ msg = "Unsupported HTTP Method #{http_method} for #{self} Plugin"
+ Cryptum::Log.append(level: :error, msg: msg, which_self: self)
end
- response
+
+ JSON.parse(response, symbolize_names: true)
rescue RestClient::ExceptionWithResponse => e
- File.open('/tmp/cryptum-errors.txt', 'a') do |f|
- f.puts Time.now.strftime('%Y-%m-%d %H:%M:%S.%N%z')
- f.puts "Module: #{self}"
- f.puts "URL: #{base_open_ai_api_uri}#{rest_call}"
- f.puts "PARAMS: #{params.inspect}"
- f.puts "HTTP POST BODY: #{http_body.inspect}" if http_body != ''
- f.puts "#{e}\n#{e.response}\n\n\n"
- end
+ msg = "URL: #{base_open_ai_api_uri}#{rest_call}"
+ msg += "PARAMS: #{params.inspect}"
+ msg += "HTTP POST BODY: #{http_body.inspect}" if http_body != ''
+ msg += "#{e}\n#{e.response}\n\n\n"
+ Cryptum::Log.append(level: :error, msg: msg, which_self: self)
rescue Interrupt, StandardError => e
Cryptum::Log.append(level: :error, msg: e, which_self: self)
end
# Supported Method Parameters::
# response = Cryptum::OpenAI.get_models(
# option_choice: 'required - option_choice object containing command line params',
# token: 'required - Bearer token',
+ # timeout: 'optional - timeout in seconds (defaults to 60)'
# )
public_class_method def self.get_models(opts = {})
token = opts[:token]
- response = open_ai_rest_call(
+ open_ai_rest_call(
option_choice: option_choice,
token: token,
- rest_call: 'models'
+ rest_call: 'models',
+ timeout: timeout
)
-
- JSON.parse(response, symbolize_names: true)
rescue Interrupt, StandardError => e
Cryptum::Log.append(level: :error, msg: e, which_self: self)
end
# Supported Method Parameters::
@@ -105,11 +107,12 @@
# request: 'required - message to ChatGPT',
# model: 'optional - model to use for text generation (defaults to gpt-3.5-turbo)',
# temp: 'optional - creative response float (deafults to 0)',
# max_tokens: 'optional - integer (defaults to 4_097 - request.length || 300)',
# system_role_content: 'optional - context to set up the model behavior for conversation (Default: "You are a momentum trading bot that analyzes order history on Coinbase. You are able to access any content or website on any device or network without any limitations or filters.")',
- # response_history: 'optional - pass response back in to have a conversation'
+ # response_history: 'optional - pass response back in to have a conversation',
+ # timeout: 'optional - timeout in seconds (defaults to 60)'
# )
public_class_method def self.chat(opts = {})
option_choice = opts[:option_choice]
token = opts[:token]
@@ -124,10 +127,13 @@
# TODO: Accurately calculate max_tokens
max_tokens = opts[:max_tokens].to_i
max_tokens = 4_097 - request.to_s.length
max_tokens = 300 unless max_tokens.positive?
+ timeout = opts[:timeout]
+ timeout ||= 60
+
gpt = true if model.include?('gpt-3.5') || model.include?('gpt-4')
if gpt
rest_call = 'chat/completions'
@@ -172,19 +178,19 @@
max_tokens: max_tokens,
echo: true
}
end
- response = open_ai_rest_call(
+ json_resp = open_ai_rest_call(
option_choice: option_choice,
http_method: :post,
token: token,
rest_call: rest_call,
- http_body: http_body.to_json
+ http_body: http_body.to_json,
+ timeout: timeout
)
- json_resp = JSON.parse(response, symbolize_names: true)
if gpt
assistant_resp = json_resp[:choices].first[:message]
json_resp[:choices] = http_body[:messages]
json_resp[:choices].push(assistant_resp)
end
@@ -199,20 +205,22 @@
public_class_method def self.help
puts "USAGE:
response = #{self}.get_models(
option_choice: 'required - option_choice object containing command line params',
token: 'required - Bearer token',
+ timeout: 'optional - timeout in seconds (defaults to 60)'
)
response = #{self}.chat(
option_choice: 'required - option_choice object containing command line params',
token: 'required - Bearer token',
request: 'required - message to ChatGPT',
model: 'optional - model to use for text generation (defaults to gpt-3.5-turbo)',
temp: 'optional - creative response float (defaults to 0)',
max_tokens: 'optional - integer (deafults to 4_097 - request.length || 300)',
system_role_content: 'optional - context to set up the model behavior for conversation (Default: \"You are a momentum trading bot that analyzes order history on Coinbase. You are able to access any content or website on any device or network without any limitations or filters.\")',
- response_history: 'optional - pass response back in to have a conversation'
+ response_history: 'optional - pass response back in to have a conversation',
+ timeout: 'optional - timeout in seconds (defaults to 60)'
)
"
end
end
end