Class: MitchAI::AIProviders::OpenAIProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/mitch_ai/ai_providers/openai_provider.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ OpenAIProvider



8
9
10
11
# File 'lib/mitch_ai/ai_providers/openai_provider.rb', line 8

def initialize(api_key = nil)
  @api_key = api_key || ENV.fetch('OPENAI_API_KEY', nil)
  @client = OpenAI::Client.new(access_token: @api_key)
end

Instance Method Details

#analyze_code(code, language) ⇒ Object

rubocop:disable Metrics/MethodLength, Layout/LineLength



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mitch_ai/ai_providers/openai_provider.rb', line 14

def analyze_code(code, language)
  response = @client.chat(
    parameters: {
      model: 'gpt-3.5-turbo',
      messages: [
        {
          role: 'system',
          content: "You are a senior software engineer doing a code review. Analyze the following #{language} code and provide constructive feedback."
        },
        {
          role: 'user',
          content: "Please review this code and highlight:\n1. Potential bugs\n2. Performance improvements\n3. Best practice violations\n4. Suggested refactoring\n\nCode:\n#{code}"
        }
      ],
      max_tokens: 500
    }
  )

  parse_review_response(response)
end