Class: OpenAI::Completion
- Inherits:
-
Object
- Object
- OpenAI::Completion
- Defined in:
- lib/monadic_chat/open_ai.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Instance Method Summary collapse
- #get_json(data) ⇒ Object
-
#initialize(access_token) ⇒ Completion
constructor
A new instance of Completion.
- #models ⇒ Object
- #run(params, research_mode: false, timeout_sec: 60, num_retrials: 1, &block) ⇒ Object
- #run_iteration(params, prompts, template, replace_key = "{{PROMPT}}", timeout_sec: 60, num_retrials: 0) ⇒ Object
Constructor Details
#initialize(access_token) ⇒ Completion
Returns a new instance of Completion.
156 157 158 |
# File 'lib/monadic_chat/open_ai.rb', line 156 def initialize(access_token) @access_token = access_token end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
154 155 156 |
# File 'lib/monadic_chat/open_ai.rb', line 154 def access_token @access_token end |
Instance Method Details
#get_json(data) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/monadic_chat/open_ai.rb', line 189 def get_json(data) case data when %r{<JSON>\n*(\{.+?\})\n*</JSON>}m json = Regexp.last_match(1).gsub(/\r\n?/, "\n").gsub(/\r\n/) { "\n" } res = JSON.parse(json) when /(\{.+\})/m json = Regexp.last_match(1).gsub(/\r\n?/, "\n").gsub(/\r\n/) { "\n" } res = JSON.parse(json) else res = data end res end |
#models ⇒ Object
160 161 162 |
# File 'lib/monadic_chat/open_ai.rb', line 160 def models OpenAI.models(@access_token) end |
#run(params, research_mode: false, timeout_sec: 60, num_retrials: 1, &block) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/monadic_chat/open_ai.rb', line 164 def run(params, research_mode: false, timeout_sec: 60, num_retrials: 1, &block) method = OpenAI.model_to_method(params["model"]) response = OpenAI.query(@access_token, "post", method, timeout_sec, params, &block) if response["error"] raise response["error"]["message"] elsif response["choices"][0]["finish_reason"] == "length" raise "finished because of length" end if research_mode get_json response["choices"][0]["text"] else response["choices"][0]["text"] end rescue StandardError => e case num_retrials when 0 raise e else run(params, research_mode: research_mode, timeout_sec: timeout_sec, num_retrials: num_retrials - 1, &block) end end |
#run_iteration(params, prompts, template, replace_key = "{{PROMPT}}", timeout_sec: 60, num_retrials: 0) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/monadic_chat/open_ai.rb', line 203 def run_iteration(params, prompts, template, replace_key = "{{PROMPT}}", timeout_sec: 60, num_retrials: 0) = TTY::ProgressBar.new("[:bar] :current/:total :total_byte :percent ET::elapsed ETA::eta", total: prompts.size, bar_format: :box) .start json = "" prompts.each do |prompt| params["prompt"] = template.sub(replace_key, prompt) res = run(params, timeout_sec: timeout_sec, num_retrials: num_retrials) json = JSON.pretty_generate(get_json(res)) .advance(1) template = template.sub(/JSON:\n+```json.+?```\n\n/m, "JSON:\n\n```json\n#{json}\n```\n\n") end .finish JSON.parse(json) end |