Class: OpenAI::Chat
- Inherits:
-
Object
- Object
- OpenAI::Chat
- Defined in:
- lib/openai/chat.rb,
lib/openai/chat/version.rb
Defined Under Namespace
Classes: InputClassificationError
Constant Summary collapse
- VERSION =
"0.0.6"
Instance Attribute Summary collapse
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#model ⇒ Object
Returns the value of attribute model.
-
#schema ⇒ Object
Returns the value of attribute schema.
Instance Method Summary collapse
- #assistant(content) ⇒ Object
- #assistant! ⇒ Object
-
#initialize(api_token: nil) ⇒ Chat
constructor
A new instance of Chat.
- #inspect ⇒ Object
- #system(content) ⇒ Object
- #user(content, image: nil, images: nil) ⇒ Object
Constructor Details
#initialize(api_token: nil) ⇒ Chat
Returns a new instance of Chat.
9 10 11 12 13 |
# File 'lib/openai/chat.rb', line 9 def initialize(api_token: nil) @api_token = api_token || ENV.fetch("OPENAI_TOKEN") @messages = [] @model = "gpt-4.1-nano" end |
Instance Attribute Details
#messages ⇒ Object
Returns the value of attribute messages.
7 8 9 |
# File 'lib/openai/chat.rb', line 7 def @messages end |
#model ⇒ Object
Returns the value of attribute model.
7 8 9 |
# File 'lib/openai/chat.rb', line 7 def model @model end |
#schema ⇒ Object
Returns the value of attribute schema.
7 8 9 |
# File 'lib/openai/chat.rb', line 7 def schema @schema end |
Instance Method Details
#assistant(content) ⇒ Object
93 94 95 |
# File 'lib/openai/chat.rb', line 93 def assistant(content) .push({role: "assistant", content: content}) end |
#assistant! ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/openai/chat.rb', line 97 def assistant! request_headers_hash = { "Authorization" => "Bearer #{@api_token}", "content-type" => "application/json" } response_format = if schema.nil? { "type" => "text" } else { "type" => "json_schema", "json_schema" => JSON.parse(schema) } end request_body_hash = { "model" => model, "response_format" => response_format, "messages" => } request_body_json = JSON.generate(request_body_hash) uri = URI("https://api.openai.com/v1/chat/completions") raw_response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| request = Net::HTTP::Post.new(uri, request_headers_hash) request.body = request_body_json http.request(request) end parsed_response = JSON.parse(raw_response.body) content = parsed_response.fetch("choices").at(0).fetch("message").fetch("content") .push({role: "assistant", content: content}) schema.nil? ? content : JSON.parse(content) end |
#inspect ⇒ Object
138 139 140 |
# File 'lib/openai/chat.rb', line 138 def inspect "#<#{self.class.name} @messages=#{.inspect} @model=#{@model.inspect} @schema=#{@schema.inspect}>" end |
#system(content) ⇒ Object
15 16 17 |
# File 'lib/openai/chat.rb', line 15 def system(content) .push({role: "system", content: content}) end |
#user(content, image: nil, images: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 |
# File 'lib/openai/chat.rb', line 19 def user(content, image: nil, images: nil) if content.is_a?(Array) processed_content = content.map do |item| if item.key?("image") || item.key?(:image) image_value = item.fetch("image") { item.fetch(:image) } { type: "image_url", image_url: { url: process_image(image_value) } } elsif item.key?("text") || item.key?(:text) text_value = item.fetch("text") { item.fetch(:text) } { type: "text", text: text_value } else item end end .push( { role: "user", content: processed_content } ) elsif image.nil? && images.nil? .push( { role: "user", content: content } ) else text_and_images_array = [ { type: "text", text: content } ] if images && !images.empty? images_array = images.map do |image| { type: "image_url", image_url: { url: process_image(image) } } end text_and_images_array += images_array else text_and_images_array.push( { type: "image_url", image_url: { url: process_image(image) } } ) end .push( { role: "user", content: text_and_images_array } ) end end |