Class: Luogu::ChatLLM
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#plugin ⇒ Object
readonly
Returns the value of attribute plugin.
Class Method Summary collapse
Instance Method Summary collapse
- #ask(message) ⇒ Object
- #chat(user_message) ⇒ Object
-
#initialize(file, history_path = '.', plugin_file_path = nil) ⇒ ChatLLM
constructor
A new instance of ChatLLM.
- #playload(messages) ⇒ Object
- #provider ⇒ Object
- #push_history(user_message, assistant_message) ⇒ Object
- #push_row_history(user_message, assistant_message) ⇒ Object
- #puts(message) ⇒ Object
- #request(messages) ⇒ Object
- #run ⇒ Object
- #run_plugin(method_name, &block) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(file, history_path = '.', plugin_file_path = nil) ⇒ ChatLLM
Returns a new instance of ChatLLM.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/luogu/chatllm.rb', line 20 def initialize(file, history_path='.', plugin_file_path=nil) @plugin_file_path = plugin_file_path || file.sub(File.extname(file), ".plugin.rb") if File.exist?(@plugin_file_path) @plugin = Plugin.new(@plugin_file_path).load() else @plugin = Plugin.new(@plugin_file_path) end @history_path = history_path @prompt_file = file @prompt = PromptParser.new(file) @row_history = [] @histories = HistoryQueue.new provider.history_limit @request_params = provider.parameter_model.call @context = OpenStruct.new run_plugin :setup end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
17 18 19 |
# File 'lib/luogu/chatllm.rb', line 17 def context @context end |
#plugin ⇒ Object (readonly)
Returns the value of attribute plugin.
18 19 20 |
# File 'lib/luogu/chatllm.rb', line 18 def plugin @plugin end |
Class Method Details
.save(history, file_path) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/luogu/chatllm.rb', line 169 def save(history, file_path) text = "" role_map = {"user" => "@u", "assistant" => "@a", "system" => "@s"} history.each do |item| text += role_map[item[:role]] text += "\n" text += item[:content] text += "\n\n" end FileUtils.mkdir_p(File.dirname(file_path)) File.open(file_path, 'w') do |f| f.write(text) end puts "已经保存文件到 #{file_path}" end |
Instance Method Details
#ask(message) ⇒ Object
110 111 112 |
# File 'lib/luogu/chatllm.rb', line 110 def ask() TTY::Prompt.new.ask() end |
#chat(user_message) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/luogu/chatllm.rb', line 63 def chat() @context.user_input = run_plugin :before_input do |context| = context.user_input end = (@prompt.render + @histories.to_a) << { role: "user", content: } run_plugin :after_input do = @context. end = request() self.push_row_history(, ) if @prompt.ruby_code puts "执行文档中的callback" instance_eval @prompt.ruby_code, @prompt.file_path, @prompt.ruby_code_line elsif @plugin.before_save_history_proc @context.user_input = @context. = run_plugin :before_save_history else puts "执行默认的历史记录" self.push_history(, ) end run_plugin :after_save_history end |
#playload(messages) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/luogu/chatllm.rb', line 153 def playload() .each do || time = Benchmark.measure do puts "test: #{}" self.puts self.chat() end puts "test: #{} 执行时间为 #{time.real} 秒" end now = Time.now.to_i file_name = File.basename(@prompt_file, ".*") self.class.save @row_history, File.join(@history_path, "#{file_name}-#{now}.row_history.md") self.class.save @histories.to_a, File.join(@history_path, "#{file_name}-#{now}.history.md") end |
#provider ⇒ Object
44 45 46 |
# File 'lib/luogu/chatllm.rb', line 44 def provider config.provider end |
#push_history(user_message, assistant_message) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/luogu/chatllm.rb', line 100 def push_history(, ) @histories.enqueue({ role: "user", content: }) @histories.enqueue({ role: "assistant", content: }) if @plugin.after_save_history_proc @context.user_input = @context. = @plugin.after_save_history_proc.call(self, @context) end end |
#push_row_history(user_message, assistant_message) ⇒ Object
95 96 97 98 |
# File 'lib/luogu/chatllm.rb', line 95 def push_row_history(, ) @row_history << {role: "user", content: } @row_history << {role: "assistant", content: } end |
#puts(message) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/luogu/chatllm.rb', line 114 def puts() @_puts_method ||= if system("which glow > /dev/null 2>&1") require 'shellwords' -> () { system("echo #{Shellwords.escape()} | glow -") } else -> () { puts } end @_puts_method.call end |
#request(messages) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/luogu/chatllm.rb', line 48 def request() @request_params. = @context.request_params = @request_params run_plugin :before_request response = provider.request.call(@request_params.to_h) unless response.code == 200 logger.error response.body.to_s raise RequestError end @context.response = response run_plugin :after_request provider.parse.call(response) end |
#run ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/luogu/chatllm.rb', line 126 def run loop do # 从命令行读取输入 input = self.ask("请输入你的指令>").cover_chinese # 根据用户输入执行相应的操作 case input when "save" file_name = File.basename(@prompt_file, ".*") self.class.save @row_history, File.join(@history_path, "#{file_name}.row_history.md") self.class.save @histories.to_a, File.join(@history_path, "#{file_name}.history.md") when "row history" p @row_history when "history" p @histories.to_a when "exit" puts "再见!" break else time = Benchmark.measure do self.puts self.chat(input) end puts "input: #{input} 执行时间为 #{time.real} 秒" end end end |
#run_plugin(method_name, &block) ⇒ Object
40 41 42 |
# File 'lib/luogu/chatllm.rb', line 40 def run_plugin(method_name, &block) plugin.run method_name: method_name, llm: self, context: @context, &block end |