Skip to main content

Codegen

The mantis codegen command is a powerful tool that leverages AI to generate, modify, and self-correct code based on a given prompt and system context. It uses a loop of generation and validation to produce high-quality, syntactically correct code.

[!WARNING] The code generation feature is still in its infancy and under active development. The code generated through this feature is not production-ready, and should be considered as an initial draft and should be used with caution. We are continuously working to improve the accuracy and reliability of the generated code, and align it with user goals and best practices for production code. Always review, test, and validate any generated code before using it in a production environment.

Usage

mantis codegen

Parameters

  • --system-prompt <path_to_prompt_file>: Path to the file containing the system prompt. This file provides context and instructions for the AI model.
  • --code-dir <path_to_code_directory>: Directory where the code lives. This directory is used for both input and output.
  • --prompt <your_prompt>: The specific instruction or request for code generation.

How It Works

  1. Input: The command takes three main inputs:

    • A system prompt file that provides context and instructions for the AI model.
    • A code directory that serves as both the input and output location.
    • A user prompt specifying the desired code generation task.
  2. Generation: Based on the system prompt, existing code (if any), and the user prompt, the AI generates or modifies code in the specified directory.

  3. Validation: Currently, the command performs basic syntax validation on the generated code.

  4. Self-Correction: If validation fails, the AI attempts to correct the code based on the error feedback.

  5. Loop: Steps 2-4 are repeated until the code passes validation or a maximum number of attempts is reached.

Configuration

Mantis uses a configuration file to set up the AI backends for code generation. The configuration file is located at ~/.mantis/config.cue and uses the CUE language for configuration.

Currently, Mantis only supports OpenAI as a backend, but there are plans to extend support to other models in the future. Here's an example configuration:

backends: {
openai: {
type: "openai"
api_key: "<OPENAI_API_KEY>"
default_model: "gpt-4o"
url: "https://api.openai.com/v1"
}
}

Configuration Fields

  • type: Specifies the type of backend (currently only "openai" is supported)
  • api_key: Your OpenAI API key
  • default_model: The default model to use (e.g., "gpt-4o")
  • url: The API endpoint URL

Supported Models

While Mantis currently only supports OpenAI models, it's designed to be extensible. The configuration structure is inspired by the AIAC project, which supports multiple AI backends including OpenAI, Amazon Bedrock, and Ollama.

In the future, Mantis plans to extend support to various models and providers, potentially including:

  • OpenAI models (GPT-3.5, GPT-4, etc.)
  • Amazon Bedrock models
  • Ollama models
  • Other open-source or proprietary models

As Mantis evolves, the configuration file will be updated to support these additional backends and models.

Example

mantis codegen --system-prompt /path/to/mantis_prompts.md --code-dir ~/stripe_query --prompt "Create a product in Stripe"

This command will:

  • Use the system prompt from /path/to/mantis_prompts.md
  • Generate or modify code in the ~/stripe_query directory
  • Attempt to create code that configures a product in Stripe
  • Validate and self-correct the code until it's syntactically correct

Future Enhancements

In future versions, the validation step will be expanded to include more comprehensive checks, potentially including:

  • Semantic validation
  • Best practices adherence
  • Security checks
  • Performance optimization suggestions

These enhancements will further improve the quality and reliability of the generated code.

Notes

  • The code directory is used for both input and output. Existing code in this directory may be modified by the command.
  • Always review the generated code before using it in production environments.
  • The effectiveness of the code generation depends on the quality and specificity of both the system prompt and the user prompt.