Class: AIRspecWriter::AIProviders::ChatGPTGenerator
- Inherits:
-
Object
- Object
- AIRspecWriter::AIProviders::ChatGPTGenerator
- Defined in:
- lib/ai_rspec_writer/ai_providers/chatgpt_generator.rb
Overview
Generates RSpec tests using OpenAI’s ChatGPT API
Instance Method Summary collapse
- #generate_spec(file_content) ⇒ Object
-
#initialize(ec: nil, schema: nil) ⇒ ChatGPTGenerator
constructor
A new instance of ChatGPTGenerator.
Constructor Details
#initialize(ec: nil, schema: nil) ⇒ ChatGPTGenerator
Returns a new instance of ChatGPTGenerator.
12 13 14 15 16 |
# File 'lib/ai_rspec_writer/ai_providers/chatgpt_generator.rb', line 12 def initialize(ec: nil, schema: nil) @extra_comment = ec @schema = schema @client = OpenAI::Client.new(access_token: AiRspecWriter.configuration.chatgpt_api_key) end |
Instance Method Details
#generate_spec(file_content) ⇒ Object
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 |
# File 'lib/ai_rspec_writer/ai_providers/chatgpt_generator.rb', line 18 def generate_spec(file_content) my_schema = "check my table defination details: #{@schema}" if @schema # puts "Write RSpec tests for this Ruby code with all factory files and rubocop standards:\n\n#{file_content}\n\n#{@extra_comment}\n\n #{my_schema}" # Define loading spinner spinner = TTY::Spinner.new("[:spinner] Generating AI-powered RSpec tests".yellow, format: :dots) # Start the spinner spinner.auto_spin response = @client.chat( parameters: { model: AiRspecWriter.configuration.chatgpt_model, messages: [{ role: "user", content: "Write RSpec tests for this Ruby code with all factory files and rubocop standards:\n\n#{file_content}\n\n#{@extra_comment}\n\n #{my_schema}" }] } ) # Stop the spinner when AI response is ready spinner.success("✅ Done!") full_content = response.dig("choices", 0, "message", "content") # extract_ruby_code(full_content) full_content rescue StandardError => e spinner.error("(❌ An error occurred while calling ChatGPT AI: #{e.message})") exit 1 end |