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 MIT License - 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.0.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.0-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coder_ai-1.0.0.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.0.tar.gz
Algorithm Hash digest
SHA256 1597c2e85b9cec1f546921d17c13bb3d41ceb8b4c50fba056f39457272f15190
MD5 d25924ab7dbd9f2b13d8ce2f189f3052
BLAKE2b-256 5fdb64eec3dad7239345f3029dd7dd7a8162951516085ab47a18d552c7ceccb4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: coder_ai-1.0.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c075f7bcdce66ef6c103894122f29c50c62e241698923a46717028da3eb7c691
MD5 a3b003573adc6b522b9d9e89d999079f
BLAKE2b-256 8067f98ea7728d359ac5b6d790c78238f2a77e1077d949f51a4e0c70a419f1db

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