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.3.tar.gz (15.2 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.3-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coder_ai-1.0.3.tar.gz
  • Upload date:
  • Size: 15.2 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.3.tar.gz
Algorithm Hash digest
SHA256 d36d4f056eb7711349f2bde3cfd743bb91f520514efa0ff52b7d2346dd3deeaf
MD5 6ca561e62c59a451506ffdd69a21064b
BLAKE2b-256 e3d84b5b844cc15fc94e90becfa93fe1e5aeeee9ff8a87bce6654de0f2354801

See more details on using hashes here.

File details

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

File metadata

  • Download URL: coder_ai-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.1 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 870223da3a9d2ebd2d13f54ee455f73aa7b5c10adfc89acb1409f49832b16e93
MD5 1a10161b6d5a2061f62a01a74940e5b6
BLAKE2b-256 1eaaf38a9806fe5452cd225fd88335b51d45ff8a64154e2af0ce280d6111699a

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