Skip to main content

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:

  1. Ensure you have your unit tests written in a file, for example, example_code.
  2. 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'
  1. 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 v3 (GPLv3) - see the LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. 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

coder_ai-1.0.2.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coder_ai-1.0.2-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file coder_ai-1.0.2.tar.gz.

File metadata

  • Download URL: coder_ai-1.0.2.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

Hashes for coder_ai-1.0.2.tar.gz
Algorithm Hash digest
SHA256 78e996bded8d4b4883c1527bd7b2b65011222c3d911722ca296de1babb016c2f
MD5 d0818f6cc6ff0e86617a44baf5bb29d0
BLAKE2b-256 d00deb1c131ff82c5c2ff9c9a0963cfc8c7593480c43d10c66336c54d8828785

See more details on using hashes here.

File details

Details for the file coder_ai-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: coder_ai-1.0.2-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

Hashes for coder_ai-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5396fca0893e4da0f8e2e187aec4b0e196e4bf3e6e65eb91e1551de4ad8f6b4b
MD5 5f2cb9ac03182b5c78f098a89159dcf9
BLAKE2b-256 b70bcbfab1cad8991088f018f37fb50aa36ebea2f0777c7641c4b3acc5df01db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page