# MiniGPT client v1.0 ``` #!/usr/bin/env ruby # frozen_string_literal: true # # MiniGPT client v1.0 # # gem install --user-install ruby-openai # require "openai" class OpenAIClient def initialize(api_key) @client = OpenAI::Client.new(access_token: api_key) @conversation_history = [] end def chat_loop puts "MiniGPT v1.0" loop do print "\n> " user_input = gets.chomp response_thread = Thread.new { process_response(user_input) } rotating_animation(response_thread) response_thread.join # Reset conversation history @conversation_history = [] end end def update_conversation(new_message) @conversation_history << new_message end def chat_wrapper(model:, new_message:) update_conversation(new_message) parameters = { model: model, messages: @conversation_history, temperature: 0.2 } begin response = @client.chat(parameters: parameters) return handle_response(response) if response handle_error("No response received from the assistant.") rescue StandardError => e handle_error("An error occurred: #{ e.message }") end end def rotating_animation(response_thread) animation_chars = ["/", "-", "\\", "|"] index = 0 while response_thread.alive? print "\r#{ animation_chars[index] }" index = (index + 1) % animation_chars.length sleep(0.1) end end def process_response(user_input) response = chat_wrapper(model: "gpt-4", new_message: { role: "user", content: user_input }) if response puts "\r#{ response }" else puts "\rAn error occurred while processing your request." end end def handle_response(response) assistant_message = response.dig("choices", -1, "message", "content") @conversation_history << { role: "assistant", content: assistant_message } assistant_message end def handle_error(error_message) puts "\n#{ error_message }" nil end end api_key = ENV["OPENAI_API_KEY"] client = OpenAIClient.new(api_key) client.chat_loop ```