Class: BuilderApm::Doctor::BravoChatAi
- Inherits:
-
Object
- Object
- BuilderApm::Doctor::BravoChatAi
- Defined in:
- lib/builder_apm/doctor/bravo_chat_ai.rb
Constant Summary collapse
- MAX_RETRIES =
3
- TIMEOUT_IN_SECONDS =
equivalent to 5 minutes
300
Instance Method Summary collapse
- #chat(messages) ⇒ Object
-
#initialize(api_key) ⇒ BravoChatAi
constructor
A new instance of BravoChatAi.
- #role(ai_role = nil) ⇒ Object
- #temperature(temp = nil) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ BravoChatAi
Returns a new instance of BravoChatAi.
11 12 13 14 15 16 |
# File 'lib/builder_apm/doctor/bravo_chat_ai.rb', line 11 def initialize(api_key) @api_key = api_key @uri = URI.parse("https://models-gateway.builder.ai/api/v1/openai/deployments/gpt-35-turbo-16k-0613/chat/completions") @role = "You are to be my assistant" @temperature = 0.2 end |
Instance Method Details
#chat(messages) ⇒ Object
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 |
# File 'lib/builder_apm/doctor/bravo_chat_ai.rb', line 30 def chat() request = Net::HTTP::Post.new(@uri) request.content_type = "application/json" request["api-key"] = @api_key request.body = JSON.dump({ "temperature" => temperature, "messages" => () }) = { use_ssl: @uri.scheme == "https", } begin retries ||= 0 response = Net::HTTP.start(@uri.hostname, @uri.port, ) do |http| http.read_timeout = TIMEOUT_IN_SECONDS http.request(request) end rescue Net::ReadTimeout => e if retries < MAX_RETRIES retries += 1 retry else raise e end end begin JSON.parse(response.body)["choices"].first["message"]["content"] rescue => e puts e. response.body end end |
#role(ai_role = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/builder_apm/doctor/bravo_chat_ai.rb', line 18 def role(ai_role = nil) @role = ai_role unless ai_role.nil? @role end |
#temperature(temp = nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/builder_apm/doctor/bravo_chat_ai.rb', line 24 def temperature(temp = nil) @temperature = temp unless temp.nil? @temperature end |