Class: LLM::Gemini
- Defined in:
- lib/llm/providers/gemini.rb,
lib/llm/providers/gemini/audio.rb,
lib/llm/providers/gemini/files.rb,
lib/llm/providers/gemini/format.rb,
lib/llm/providers/gemini/images.rb,
lib/llm/providers/gemini/models.rb,
lib/llm/providers/gemini/error_handler.rb,
lib/llm/providers/gemini/stream_parser.rb
Overview
The Gemini class implements a provider for Gemini. The Gemini provider can accept multiple inputs (text, images, audio, and video). The inputs can be provided inline via the prompt for files under 20MB or via the Gemini Files API for files that are over 20MB.
Defined Under Namespace
Modules: Response Classes: Audio, Files, Images, Models
Constant Summary collapse
- HOST =
"generativelanguage.googleapis.com"
Instance Method Summary collapse
-
#web_search(query:) ⇒ LLM::Response
A convenience method for performing a web search using the Google Search tool.
-
#embed(input, model: "text-embedding-004", **params) ⇒ LLM::Response
Provides an embedding.
-
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API.
-
#audio ⇒ LLM::Gemini::Audio
Provides an interface to Gemini’s audio API.
-
#images ⇒ see LLM::Gemini::Images
Provides an interface to Gemini’s image generation API.
-
#files ⇒ LLM::Gemini::Files
Provides an interface to Gemini’s file management API.
-
#models ⇒ LLM::Gemini::Models
Provides an interface to Gemini’s models API.
-
#assistant_role ⇒ String
Returns the role of the assistant in the conversation.
-
#default_model ⇒ String
Returns the default model for chat completions.
-
#tools ⇒ String => LLM::Tool
-
#initialize ⇒ Gemini
constructor
A new instance of Gemini.
Methods inherited from Provider
#chat, #chat!, clients, #inspect, #moderations, mutex, #respond, #respond!, #responses, #schema, #tool, #vector_stores, #with
Constructor Details
Instance Method Details
#web_search(query:) ⇒ LLM::Response
A convenience method for performing a web search using the Google Search tool.
149 150 151 152 |
# File 'lib/llm/providers/gemini.rb', line 149 def web_search(query:) complete(query, tools: [tools[:google_search]]) .extend(LLM::Gemini::Response::WebSearch) end |
#embed(input, model: "text-embedding-004", **params) ⇒ LLM::Response
Provides an embedding
49 50 51 52 53 54 55 56 |
# File 'lib/llm/providers/gemini.rb', line 49 def (input, model: "text-embedding-004", **params) model = model.respond_to?(:id) ? model.id : model path = ["/v1beta/models/#{model}", "embedContent?key=#{@key}"].join(":") req = Net::HTTP::Post.new(path, headers) req.body = JSON.dump({content: {parts: [{text: input}]}}) res = execute(request: req) LLM::Response.new(res).extend(LLM::Gemini::Response::Embedding) end |
#complete(prompt, params = {}) ⇒ LLM::Response
Provides an interface to the chat completions API
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/llm/providers/gemini.rb', line 68 def complete(prompt, params = {}) params = {role: :user, model: default_model}.merge!(params) params = [params, format_schema(params), format_tools(params)].inject({}, &:merge!).compact role, model, stream = [:role, :model, :stream].map { params.delete(_1) } action = stream ? "streamGenerateContent?key=#{@key}&alt=sse" : "generateContent?key=#{@key}" model.respond_to?(:id) ? model.id : model path = ["/v1beta/models/#{model}", action].join(":") req = Net::HTTP::Post.new(path, headers) = [*(params.delete(:messages) || []), LLM::Message.new(role, prompt)] body = JSON.dump({contents: format()}.merge!(params)) set_body_stream(req, StringIO.new(body)) res = execute(request: req, stream:) LLM::Response.new(res).extend(LLM::Gemini::Response::Completion) end |
#audio ⇒ LLM::Gemini::Audio
Provides an interface to Gemini’s audio API
87 88 89 |
# File 'lib/llm/providers/gemini.rb', line 87 def audio LLM::Gemini::Audio.new(self) end |
#images ⇒ see LLM::Gemini::Images
Provides an interface to Gemini’s image generation API
95 96 97 |
# File 'lib/llm/providers/gemini.rb', line 95 def images LLM::Gemini::Images.new(self) end |
#files ⇒ LLM::Gemini::Files
Provides an interface to Gemini’s file management API
103 104 105 |
# File 'lib/llm/providers/gemini.rb', line 103 def files LLM::Gemini::Files.new(self) end |
#models ⇒ LLM::Gemini::Models
Provides an interface to Gemini’s models API
111 112 113 |
# File 'lib/llm/providers/gemini.rb', line 111 def models LLM::Gemini::Models.new(self) end |
#assistant_role ⇒ String
Returns the role of the assistant in the conversation. Usually “assistant” or “model”
117 118 119 |
# File 'lib/llm/providers/gemini.rb', line 117 def assistant_role "model" end |
#default_model ⇒ String
Returns the default model for chat completions
125 126 127 |
# File 'lib/llm/providers/gemini.rb', line 125 def default_model "gemini-2.5-flash" end |
#tools ⇒ String => LLM::Tool
This method includes certain tools that require configuration through a set of options that are easier to set through the LLM::Provider#tool method.
136 137 138 139 140 141 142 |
# File 'lib/llm/providers/gemini.rb', line 136 def tools { google_search: tool(:google_search), code_execution: tool(:code_execution), url_context: tool(:url_context) } end |