Class: LLMs::APIs::GoogleGeminiAPI
- Inherits:
-
Object
- Object
- LLMs::APIs::GoogleGeminiAPI
- Defined in:
- lib/llms/apis/google_gemini_api.rb
Instance Method Summary collapse
- #generate_content(model_name, messages, params = {}) ⇒ Object
-
#initialize(api_key) ⇒ GoogleGeminiAPI
constructor
A new instance of GoogleGeminiAPI.
Constructor Details
#initialize(api_key) ⇒ GoogleGeminiAPI
Returns a new instance of GoogleGeminiAPI.
8 9 10 |
# File 'lib/llms/apis/google_gemini_api.rb', line 8 def initialize(api_key) @api_key = api_key end |
Instance Method Details
#generate_content(model_name, messages, params = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/llms/apis/google_gemini_api.rb', line 12 def generate_content(model_name, , params = {}) stream = params.delete(:stream) path = "/#{model_name}:#{!!stream ? 'streamGenerateContent?alt=sse&' : 'generateContent?'}key=#{@api_key}" uri = URI("https://generativelanguage.googleapis.com/v1beta/models#{path}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Post.new(uri) request['Content-Type'] = 'application/json' request.body = params.merge({ contents: }).to_json if stream http.request(request) do |response| if response.code.to_s == '200' response.read_body do |data| stream.call(data) end return nil ## return nil to indicate that there's no separate api response other than the stream else return JSON.parse(response.body) end end else response = http.request(request) JSON.parse(response.body) end end |