Class: GptFunction
- Inherits:
-
Object
- Object
- GptFunction
- Defined in:
- lib/gpt-function.rb,
lib/gpt_function/file.rb,
lib/gpt_function/batch.rb,
lib/gpt_function/storage.rb,
lib/gpt_function/version.rb,
lib/gpt_function/simple_queue.rb
Defined Under Namespace
Modules: Storage Classes: Batch, Error, File, SimpleQueue
Constant Summary collapse
- VERSION =
"0.8.0"
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.model ⇒ Object
Returns the value of attribute model.
Class Method Summary collapse
Instance Method Summary collapse
- #batch(inputs, post_processor_class) ⇒ Object
- #call(input) ⇒ Object
-
#initialize(prompt, examples = [], temperature = 0) ⇒ GptFunction
constructor
A new instance of GptFunction.
- #to_request_body(input) ⇒ Object
Constructor Details
#initialize(prompt, examples = [], temperature = 0) ⇒ GptFunction
Returns a new instance of GptFunction.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gpt-function.rb', line 29 def initialize(prompt, examples = [], temperature = 0) @temperature = temperature @messages = [ { role: "system", content: "#{prompt}\n Note: The response format is always a JSON with the key output like this:{output: ...}" }, *examples.flat_map do |example| [ { role: "user", content: example[0] }, { role: "assistant", content: { output: example[1] }.to_json } ] end ] end |
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
20 21 22 |
# File 'lib/gpt-function.rb', line 20 def api_key @api_key end |
.model ⇒ Object
Returns the value of attribute model.
20 21 22 |
# File 'lib/gpt-function.rb', line 20 def model @model end |
Class Method Details
.configure(api_key:, model:, batch_storage: GptFunction::SimpleQueue.new) ⇒ Object
22 23 24 25 26 |
# File 'lib/gpt-function.rb', line 22 def configure(api_key:, model:, batch_storage: GptFunction::SimpleQueue.new) @api_key = api_key @model = model GptFunction::Storage.batch_storage = batch_storage end |
Instance Method Details
#batch(inputs, post_processor_class) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/gpt-function.rb', line 81 def batch(inputs, post_processor_class) file_content = inputs.map.with_index do |input, index| { "custom_id": "request-#{index + 1}", "method": "POST", "url": "/v1/chat/completions", "body": to_request_body(input) } end batch_instance = Batch.new(GptFunction.api_key) batch_id = batch_instance.request(file_content) puts "Batch created with ID: #{batch_id}" # 創建 BatchRequest 並啟動 ProcessBatchJob batch_request = BatchRequest.create( batch_id: batch_id, status: 'created', total_request_counts: inputs.size, completed_request_counts: 0, post_processor_class: post_processor_class.to_s ) ProcessBatchJob.perform_later(batch_request.id) end |
#call(input) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/gpt-function.rb', line 51 def call(input) # 使用類別級別的變量來發送請求 response = send_request(input) body = response.body.force_encoding("UTF-8") json = JSON.parse(body) # 處理可能的錯誤回應 raise StandardError, json.dig("error", "message") if json.dig("error", "code") # 處理正常的回應 JSON.parse(json.dig("choices", 0, "message", "content"))["output"] end |
#to_request_body(input) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/gpt-function.rb', line 63 def to_request_body(input) { model: GptFunction.model, response_format: { type: "json_object" }, seed: 42, messages: [ *@messages, { "role": "user", "content": input } ], temperature: @temperature } end |