Class: Omamori::AIAnalysisEngine::GeminiClient
- Inherits:
-
Object
- Object
- Omamori::AIAnalysisEngine::GeminiClient
- Defined in:
- lib/omamori/ai_analysis_engine/gemini_client.rb
Instance Method Summary collapse
- #analyze(prompt, json_schema, model: "gemini-1.5-pro-latest") ⇒ Object
-
#initialize(api_key) ⇒ GeminiClient
constructor
A new instance of GeminiClient.
Constructor Details
#initialize(api_key) ⇒ GeminiClient
Returns a new instance of GeminiClient.
8 9 10 11 |
# File 'lib/omamori/ai_analysis_engine/gemini_client.rb', line 8 def initialize(api_key) @api_key = api_key || ENV["GEMINI_API_KEY"] @client = nil # Initialize client later end |
Instance Method Details
#analyze(prompt, json_schema, model: "gemini-1.5-pro-latest") ⇒ Object
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 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/omamori/ai_analysis_engine/gemini_client.rb', line 13 def analyze(prompt, json_schema, model: "gemini-1.5-pro-latest") # Ensure the client is initialized client begin response = @client.generate_content( prompt, model: model, response_schema: json_schema, # Use response_schema for Structured Output temperature: 0.0 ) # Debug: Inspect the response object # puts "Debug: response object: #{response.inspect}" # puts "Debug: response methods: #{response.methods.sort}" # Extract and parse JSON from the response text json_string = response.text.gsub(/\A```json\n|```\z/, '').strip begin parsed_response = JSON.parse(json_string) # Validate the parsed output structure if parsed_response.is_a?(Hash) && parsed_response.key?('security_risks') # Return the parsed JSON data parsed_response else puts "Warning: Unexpected AI analysis response structure." puts "Raw response text: #{response.text}" nil # Return nil if the structure is unexpected end rescue JSON::ParserError puts "Warning: Failed to parse response text as JSON." puts "Raw response text: #{response.text}" nil # Or handle the error appropriately end rescue Faraday::Error => e puts "API Error: #{e.message}" puts "Response body: #{e.response[:body]}" if e.response nil # Handle API errors rescue => e puts "An unexpected error occurred during API call: #{e.message}" nil # Handle other errors end end |