require "option_parser" require "pretty_print" require "openai" require "./zone" require "./parsers/string" require "./builders/string" require "./builders/openai_chat" class Storyteller def initialize() end def self.start(argv) input_file = STDIN input_file_path = "" output_file = STDOUT output_file_path = "" past_characters_limit = 1000 use_color = true make_request = true verbose = false parser = OptionParser.parse do |parser| parser.banner = "Usage: storyteller [options]" parser.on("-i FILE", "--input=FILE", "Path to input file") do |file| input_file_path = file end parser.on("-v", "--verbose", "Be verbose (cumulative)") do verbose = true end parser.on("--dry-run", "Don't call the API") do make_request = false end parser.on("-n", "--no-color", "Disable color output") do use_color = false end parser.on("-o FILE", "--output=FILE", "Path to output file") do |file| use_color = false output_file_path = file end parser.on("-h", "--help", "Show this help") do puts parser exit end end # Create Storyteller instance storyteller = Storyteller.new() # Read file and initialize zones if !input_file_path.empty? # puts "d: Using input file #{input_file_path}" input_file = File.open(input_file_path) end prompt = storyteller.read_file(input_file) input_file.close # Build GPT-3 request prompt = storyteller.complete(prompt, make_request, verbose) exit 0 if !make_request if !output_file_path.empty? # puts "d: Using output file #{input_file_path}" output_file = File.open(output_file_path, "w") end storyteller.write_file(output_file, prompt, use_color) output_file.close end def complete(prompt : Prompt, make_request : Bool, verbose : Bool) builder = OpenAIChatBuilder.new(verbose: verbose) messages = builder.build(prompt) return prompt if !make_request openai = OpenAI::Client.new(access_token: ENV.fetch("OPENAI_API_KEY")) result = openai.chat( "gpt-3.5-turbo", messages, { "temperature" => 0.82, "presence_penalty" => 1, "frequency_penalty" => 1, "max_tokens" => 256 } ) prompt.present_zone.content << "\n" + result.choices.first["message"]["content"] + "\n" prompt end def read_file(input_file : IO::FileDescriptor) content = input_file.gets_to_end # puts "d: building parser" parser = StringParser.new # puts "d: parsing" prompt = parser.parse(content) # pp prompt end def write_file(output_file : IO::FileDescriptor, prompt : Prompt, use_color : Bool) # STDERR.puts "d: building builder" builder = StringBuilder.new(use_color) # STDERR.puts "d: building" text = builder.build(prompt) output_file.write_string(text.to_slice) end def display_completion(completion : String) # Code pour afficher la complétion end end Storyteller.start(ARGV)