Class: Dotcodegen::TestCodeGenerator
- Inherits:
-
Object
- Object
- Dotcodegen::TestCodeGenerator
- Defined in:
- lib/dotcodegen/test_code_generator.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#file_to_test_path ⇒ Object
readonly
Returns the value of attribute file_to_test_path.
-
#openai_key ⇒ Object
readonly
Returns the value of attribute openai_key.
-
#openai_org_id ⇒ Object
readonly
Returns the value of attribute openai_org_id.
Instance Method Summary collapse
- #generate_test_code ⇒ Object
-
#initialize(config:, file_to_test_path:, openai_key:, openai_org_id: nil) ⇒ TestCodeGenerator
constructor
A new instance of TestCodeGenerator.
- #openai_client ⇒ Object
-
#test_file_content ⇒ Object
rubocop:enable Metrics/MethodLength.
- #test_instructions ⇒ Object
-
#test_prompt ⇒ Object
rubocop:disable Metrics/MethodLength.
- #test_prompt_text ⇒ Object
Constructor Details
#initialize(config:, file_to_test_path:, openai_key:, openai_org_id: nil) ⇒ TestCodeGenerator
Returns a new instance of TestCodeGenerator.
10 11 12 13 14 15 |
# File 'lib/dotcodegen/test_code_generator.rb', line 10 def initialize(config:, file_to_test_path:, openai_key:, openai_org_id: nil) @config = config @file_to_test_path = file_to_test_path @openai_key = openai_key @openai_org_id = openai_org_id end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/dotcodegen/test_code_generator.rb', line 8 def config @config end |
#file_to_test_path ⇒ Object (readonly)
Returns the value of attribute file_to_test_path.
8 9 10 |
# File 'lib/dotcodegen/test_code_generator.rb', line 8 def file_to_test_path @file_to_test_path end |
#openai_key ⇒ Object (readonly)
Returns the value of attribute openai_key.
8 9 10 |
# File 'lib/dotcodegen/test_code_generator.rb', line 8 def openai_key @openai_key end |
#openai_org_id ⇒ Object (readonly)
Returns the value of attribute openai_org_id.
8 9 10 |
# File 'lib/dotcodegen/test_code_generator.rb', line 8 def openai_org_id @openai_org_id end |
Instance Method Details
#generate_test_code ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/dotcodegen/test_code_generator.rb', line 17 def generate_test_code response = openai_client.chat( parameters: { model: 'gpt-4-turbo-preview', messages: [{ role: 'user', content: test_prompt_text }], # Required. temperature: 0.7 } ) FormatOutput.format(response.dig('choices', 0, 'message', 'content')) end |
#openai_client ⇒ Object
58 59 60 61 62 |
# File 'lib/dotcodegen/test_code_generator.rb', line 58 def openai_client = { access_token: openai_key } [:organization_id] = openai_org_id unless openai_org_id.nil? @openai_client ||= OpenAI::Client.new() end |
#test_file_content ⇒ Object
rubocop:enable Metrics/MethodLength
50 51 52 |
# File 'lib/dotcodegen/test_code_generator.rb', line 50 def test_file_content File.open(file_to_test_path).read end |
#test_instructions ⇒ Object
54 55 56 |
# File 'lib/dotcodegen/test_code_generator.rb', line 54 def test_instructions config['content'] end |
#test_prompt ⇒ Object
rubocop:disable Metrics/MethodLength
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dotcodegen/test_code_generator.rb', line 33 def test_prompt [ 'You are an expert programmer. You have been given a task to write a test file for a given file following some instructions.', 'This is the file you want to test:', '--start--', test_file_content, '--end--', 'Here are the instructions on how to write the test file:', '--start--', test_instructions, '--end--', "Your answer will be directly written in the file you want to test. Don't include any explanation or comments in your answer that isn't code.", 'You can use the comment syntax to write comments in your answer.' ].join("\n") end |
#test_prompt_text ⇒ Object
28 29 30 |
# File 'lib/dotcodegen/test_code_generator.rb', line 28 def test_prompt_text [{ "type": 'text', "text": test_prompt }] end |