Class: Katalyst::GoogleApis::Gemini::GenerateContentService
- Inherits:
-
Object
- Object
- Katalyst::GoogleApis::Gemini::GenerateContentService
- Defined in:
- app/services/katalyst/google_apis/gemini/generate_content_service.rb
Overview
Use Gemini GPT to generate a response to a prompt.
Instance Attribute Summary collapse
-
#content_text ⇒ Object
readonly
Returns the value of attribute content_text.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #call(payload:) ⇒ Object
- #content_json ⇒ Object
-
#initialize(credentials:, model:, parent:, attempt:, retries:, jitter:) ⇒ GenerateContentService
constructor
A new instance of GenerateContentService.
- #inspect ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(credentials:, model:, parent:, attempt:, retries:, jitter:) ⇒ GenerateContentService
Returns a new instance of GenerateContentService.
17 18 19 20 21 22 23 24 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 17 def initialize(credentials:, model:, parent:, attempt:, retries:, jitter:) @credentials = credentials @model = model @parent = parent @attempt = attempt @retries = retries @jitter = jitter end |
Instance Attribute Details
#content_text ⇒ Object (readonly)
Returns the value of attribute content_text.
8 9 10 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 8 def content_text @content_text end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 8 def error @error end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
8 9 10 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 8 def response @response end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
8 9 10 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 8 def result @result end |
Class Method Details
.call(parent:, model:, payload:, credentials: Katalyst::GoogleApis.credentials, retries: 5, jitter: 1_000) ⇒ Object
10 11 12 13 14 15 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 10 def self.call(parent:, model:, payload:, credentials: Katalyst::GoogleApis.credentials, retries: 5, jitter: 1_000) new(parent:, model:, credentials:, attempt: 0, retries:, jitter:).call(payload:) end |
Instance Method Details
#call(payload:) ⇒ Object
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 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 26 def call(payload:) @response = Curl.post(url, payload.to_json) do |http| http.headers["Content-Type"] = "application/json; UTF-8" @credentials.apply!(http.headers) end if %r{^application/json}.match?(@response.content_type) @result = JSON.parse(response.body, symbolize_names: true) else raise GoogleApis::Error.new( code: response.response_code, status: Rack::Utils::HTTP_STATUS_CODES[response.response_code], message: "Unexpected HTTP response received (#{response.response_code}, #{@response.content_type})", ) end if result[:error].present? raise GoogleApis::Error.new(**result[:error]) else @content_text = result.dig(:candidates, 0, :content, :parts, 0, :text) end self rescue StandardError => e @error = e retry?(e) ? retry : raise ensure report_error end |
#content_json ⇒ Object
60 61 62 63 64 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 60 def content_json return @content_json if instance_variable_defined?(:@content_json) @content_json = JSON.parse(content_text, symbolize_names: true) end |
#inspect ⇒ Object
66 67 68 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 66 def inspect "#<#{self.class.name} result: #{@result.inspect} error: #{@error.inspect}>" end |
#success? ⇒ Boolean
56 57 58 |
# File 'app/services/katalyst/google_apis/gemini/generate_content_service.rb', line 56 def success? result.present? && content_text.present? end |