Module: PWN::AI::Gemini

Defined in:
lib/pwn/ai/gemini.rb

Overview

This plugin interacts with Google’s Gemini API (Generative Language). It provides methods to list models, generate completions, and chat, plus a native tool-calling adapter (‘chat_raw`) for PWN::AI::Agent::Loop.

API documentation: ai.google.dev/api Obtain an API key from aistudio.google.com/app/apikey

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <support@0dayinc.com>



423
424
425
426
427
# File 'lib/pwn/ai/gemini.rb', line 423

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <support@0dayinc.com>
  "
end

.chat(opts = {}) ⇒ Object

Supported Method Parameters

response = PWN::AI::Gemini.chat(

request: 'required - message to Gemini',
model: 'optional - model to use for text generation (defaults to PWN::Env[:ai][:gemini][:model])',
temp: 'optional - creative response float (defaults to PWN::Env[:ai][:gemini][:temp])',
system_role_content: 'optional - context to set up the model behavior for conversation (Default: PWN::Env[:ai][:gemini][:system_role_content])',
response_history: 'optional - pass response back in to have a conversation',
speak_answer: 'optional speak answer using PWN::Plugins::Voice.text_to_speech (Default: nil)',
timeout: 'optional timeout in seconds (defaults to 300)',
spinner: 'optional - display spinner (defaults to false)'

)



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/pwn/ai/gemini.rb', line 339

public_class_method def self.chat(opts = {})
  engine  = PWN::Env[:ai][:gemini]
  request = opts[:request]
  max_prompt_length = engine[:max_prompt_length] ||= 1_000_000
  request = request.to_s[0, ((max_prompt_length - 1) / 3.36).floor]

  model = opts[:model] ||= engine[:model]
  raise 'ERROR: Model is required.  Call #get_models method for details' if model.nil?

  temp = opts[:temp].to_f
  temp = engine[:temp].to_f.nonzero? || 1 if temp.zero?

  system_role_content = opts[:system_role_content] ||= engine[:system_role_content]
  system_role = { role: 'system', content: system_role_content }
  user_role   = { role: 'user',   content: request }

  response_history = opts[:response_history]
  response_history ||= { choices: [system_role] }

  # Build the OpenAI-shape messages array, then reuse the Gemini
  # translator so .chat and .chat_raw share one wire path.
  messages = [system_role]
  if response_history[:choices].length > 1
    response_history[:choices][1..].each do |msg|
      r = (msg[:role] || msg['role']).to_s
      next if r == 'system'

      messages.push(msg)
    end
  end
  messages.push(user_role)

  sys_str, contents = oa_messages_to_gemini(messages: messages)

  http_body = {
    contents: contents,
    generationConfig: { temperature: temp, maxOutputTokens: 8192 }
  }
  http_body[:systemInstruction] = { parts: [{ text: sys_str }] } if sys_str && !sys_str.empty?

  response = gemini_rest_call(
    http_method: :post,
    rest_call: "models/#{model}:generateContent",
    http_body: http_body,
    timeout: opts[:timeout],
    spinner: opts[:spinner]
  )

  json_resp = JSON.parse(response, symbolize_names: true)
  raise "Gemini API Error: #{json_resp[:error]}" if json_resp[:error]

  parts = Array(json_resp.dig(:candidates, 0, :content, :parts))
  assistant_content = parts.select { |p| p.key?(:text) }.map { |p| p[:text] }.join
  assistant_resp = { role: 'assistant', content: assistant_content }

  # Build choices for PWN compatibility: [system, ...history..., user, assistant]
  json_resp[:choices] = messages
  json_resp[:choices].push(assistant_resp)
  json_resp[:id] ||= "gemini_#{SecureRandom.hex(6)}"
  json_resp[:object] ||= 'chat.completion'
  json_resp[:model]  ||= model

  usage = json_resp[:usageMetadata] || {}
  json_resp[:usage] = {
    prompt_tokens: usage[:promptTokenCount] || 0,
    completion_tokens: usage[:candidatesTokenCount] || 0,
    total_tokens: usage[:totalTokenCount] ||
                  ((usage[:promptTokenCount] || 0) + (usage[:candidatesTokenCount] || 0))
  }

  if opts[:speak_answer]
    text_path = "/tmp/#{SecureRandom.hex}.pwn_voice"
    File.write(text_path, assistant_content)
    PWN::Plugins::Voice.text_to_speech(text_path: text_path)
    File.unlink(text_path)
  end

  json_resp
rescue StandardError => e
  raise e
end

.chat_raw(opts = {}) ⇒ Object

Supported Method Parameters

response = PWN::AI::Gemini.chat_raw(

messages: 'required - OpenAI-format messages array (system/user/assistant/tool)',
tools: 'optional - OpenAI tools array [{type:"function", function:{...}}]',
tool_choice: 'optional - "auto" | "none" | "required" | {type:"function", function:{name:..}}',
model: 'optional - overrides PWN::Env[:ai][:gemini][:model]',
temp: 'optional - temperature (defaults to PWN::Env[:ai][:gemini][:temp] || 1)',
max_tokens: 'optional - maxOutputTokens (defaults to 8192)',
timeout: 'optional - seconds (default 300)',
spinner: 'optional - display spinner (default false)'

)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/pwn/ai/gemini.rb', line 149

public_class_method def self.chat_raw(opts = {})
  engine   = PWN::Env[:ai][:gemini]
  messages = opts[:messages]
  raise 'ERROR: messages array is required' if messages.nil? || messages.empty?

  model = opts[:model] ||= engine[:model]
  raise 'ERROR: Model is required.  Call #get_models method for details' if model.nil?

  temp = opts[:temp].to_f
  temp = engine[:temp].to_f.nonzero? || 1 if temp.zero?

  system_str, contents = oa_messages_to_gemini(messages: messages)

  http_body = {
    contents: contents,
    generationConfig: {
      temperature: temp,
      maxOutputTokens: opts[:max_tokens] || 8192
    }
  }
  http_body[:systemInstruction] = { parts: [{ text: system_str }] } if system_str && !system_str.empty?

  if opts[:tools] && !opts[:tools].empty?
    http_body[:tools] = [{
      functionDeclarations: opts[:tools].map do |t|
        fn = t[:function] || t['function'] || t
        {
          name: fn[:name] || fn['name'],
          description: fn[:description] || fn['description'],
          parameters: fn[:parameters] || fn['parameters'] || { type: 'object', properties: {} }
        }
      end
    }]
    http_body[:toolConfig] = gemini_tool_config(choice: opts[:tool_choice]) if opts[:tool_choice]
  end

  response = gemini_rest_call(
    http_method: :post,
    rest_call: "models/#{model}:generateContent",
    http_body: http_body,
    timeout: opts[:timeout],
    spinner: opts[:spinner]
  )
  return nil if response.nil?

  json_resp = JSON.parse(response, symbolize_names: true)
  raise "Gemini API Error: #{json_resp[:error]}" if json_resp[:error]

  gemini_resp_to_oa(response: json_resp)
rescue StandardError => e
  raise e
end

.get_modelsObject

Supported Method Parameters

models = PWN::AI::Gemini.get_models



113
114
115
116
117
118
119
# File 'lib/pwn/ai/gemini.rb', line 113

public_class_method def self.get_models
  models = gemini_rest_call(rest_call: 'models')

  JSON.parse(models, symbolize_names: true)[:models]
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/pwn/ai/gemini.rb', line 431

public_class_method def self.help
  puts "USAGE:
    models = #{self}.get_models

    response = #{self}.chat(
      request: 'required - message to Gemini',
      model: 'optional - model to use for text generation (defaults to PWN::Env[:ai][:gemini][:model])',
      temp: 'optional - creative response float (defaults to PWN::Env[:ai][:gemini][:temp])',
      system_role_content: 'optional - context to set up the model behavior for conversation (Default: PWN::Env[:ai][:gemini][:system_role_content])',
      response_history: 'optional - pass response back in to have a conversation',
      speak_answer: 'optional speak answer using PWN::Plugins::Voice.text_to_speech (Default: nil)',
      timeout: 'optional - timeout in seconds (defaults to 300)',
      spinner: 'optional - display spinner (defaults to false)'
    )

    response = #{self}.chat_raw(
      messages: 'required - OpenAI-format messages array',
      tools: 'optional - OpenAI tools array',
      tool_choice: 'optional - auto | none | required | {function:{name:..}}',
      model: 'optional - overrides PWN::Env[:ai][:gemini][:model]'
    )

    #{self}.authors
  "
end