A Python library for generating Java methods from unit tests using OpenAI's GPT model.
Project description
CoderAI
CoderAI is a Python library designed to generate Java methods from unit tests using OpenAI's GPT model. This tool is currently designed specifically for Java and is still in the development process. Future versions may include support for other languages.
Features
- Generates Java methods that satisfy given unit tests.
- Reads unit tests from a file and processes them to generate corresponding Java methods.
- Creates a project structure for the generated code.
- Runs Maven commands to compile and test the generated Java code.
Installation
pip install coder-ai
Usage
Example
Here's a basic example of how to use CoderAI:
- Ensure you have your unit tests written in a file, for example, example_code.
- Create a configuration file config.py with the necessary paths and API keys.
config.py
SRC_PATH = r'YOUR_SPRING_SRC_PATH'
PACKAGE_NAME = 'YOUR_PACKAGE_NAME_WILL_CREATED_INTO'
INPUT_FILENAME = 'YOUR_INPUT_FILENAME'
OPENAI_API_KEY = "YOUR_KEY"
MAVEN_HOME = r'YOUR_MAVEN/BIN_PATH'
PROJECT_DIR = r'YOUR_PROJECT_DIR'
WHOLE_PACKAGE_NAME = 'YOUR_MAIN_PACKAGE_NAME'
BASE_NAME = "YOUR_BASE_CLASS_NAME"
CLASS_NAME = 'YOUR_CLASS_NAME'
TEST_CLASS_NAME = 'YOUR_TEST_CLASS_NAME'
- Use the main script to generate the methods and run Maven commands.
main.py
import os
import config
from coderai.functions import *
from coderai.functions import CoderAI
def main():
base_dir = os.path.join(config.SRC_PATH, 'main', 'java', *config.PACKAGE_NAME.split('.'))
test_dir = os.path.join(config.SRC_PATH, 'test', 'java', *config.PACKAGE_NAME.split('.'))
project_name = config.PROJECT_NAME
input_filename = config.INPUT_FILENAME
unit_tests = read_from_file(input_filename)
method_code = generate_method_from_tests(config.OPENAI_API_KEY, unit_tests, language="Java")
project_path = create_project_structure(base_dir, project_name)
test_path = create_project_structure(test_dir, project_name)
import_package, real_codes = process_unit_tests(unit_tests)
method_code_lines = method_code.split('\n')
cleaned_method_code_lines = [line for line in method_code_lines if not line.startswith('package')]
cleaned_method_code = '\n'.join(cleaned_method_code_lines)
class_code = f"""
package {config.PACKAGE_NAME}.{project_name};
{import_package}
{cleaned_method_code}
"""
test_code = f"""
package {config.PACKAGE_NAME}.{project_name};
{unit_tests}
"""
write_to_file(
os.path.join(project_path, f"{config.CLASS_NAME}.java"),
class_code)
print(f"Generated project structure with class files in {project_path}")
write_to_file(
os.path.join(test_path, f"{config.TEST_CLASS_NAME}.java"),
test_code)
print(f"Generated project structure with test files in {test_path}")
if run_maven_commands(config.MAVEN_HOME, config.PROJECT_DIR):
print("Maven build and tests were successful.")
else:
print("Maven build or tests failed.")
if __name__ == "__main__":
main()
Functionality
- Reading Unit Tests: Reads unit tests from a specified file.
- Generating Methods: Uses OpenAI's GPT model to generate Java methods that satisfy the unit tests.
- Creating Project Structure: Creates the necessary directory structure for the Java project.
- Writing Code to Files: Writes the generated methods and unit tests to the appropriate Java files.
- Running Maven Commands: Compiles and tests the generated code using Maven.
Development
- CoderAI is currently in the development phase. Contributions and feedback are welcome.
Future Plans
- Add support for generating code in other programming languages.
- Improve the method generation logic and handle edge cases.
- Enhance the configuration options for more flexibility.
License
This project is licensed under the GNU GENERAL PUBLIC LICENSE - see the LICENSE file for details.
Contributing
- Fork the repository
- Create your feature branch (git checkout -b feature/AmazingFeature)
- Commit your changes (git commit -m 'Add some AmazingFeature')
- Push to the branch (git push origin feature/AmazingFeature)
- Open a Pull Request
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file coder_ai-1.0.1.tar.gz.
File metadata
- Download URL: coder_ai-1.0.1.tar.gz
- Upload date:
- Size: 15.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3706dd2653190adc96802353bc691c385bb5a140e793cae7fe536e88c3f10964
|
|
| MD5 |
483d4454e079e7be6887ff4c27b0ac5a
|
|
| BLAKE2b-256 |
5cfc8698270dc8773c68613bb7d1e49ee17214c1a0f127be25979e349118745d
|
File details
Details for the file coder_ai-1.0.1-py3-none-any.whl.
File metadata
- Download URL: coder_ai-1.0.1-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f03404acba0b08fffeca0a50d469a5d86fcc2c1d58ff801b3e2b10fbb398d3ba
|
|
| MD5 |
6ba5f243e1be306768cc85786c07c054
|
|
| BLAKE2b-256 |
5c2a68928902e83a63f6f930396a35ea718b31ba49315b2e963162fce2e25fbe
|