AI for code analysis

Swarm enhances the code review process by integrating AI-driven functionality. By utilizing OpenAI, this feature on request generates explanations for all code diffs in a file, making it easier for developers to understand and review code changes.

By default, the minimum diff size threshold for AI to explain the diff is four characters, up to a maximum of 31000 characters. This is also the recommended threshold for AI to explain the diffs.

The configuration to integrate AI into Swarm is specified within an ai_review block in the SWARM_ROOT/data/config.php file, similar to the following example:

<?php
'ai_review' => array( // Please read Perforce's Generative AI policy before enabling this feature. // See https://www.perforce.com/generative-ai-policy 'enabled' => false, // set to true to enable the AI review feature 'ai_vendors' => array( 'openAIWithExplainCodeOnGPT4' => array( 'ai_vendor' => 'openAI', // name of the AI provider being used 'ai_package_id' => '1', // Ensure that it is a unique id and its value should not be modified 'ai_package_key' => 'openaiwithexplaincodeongpt4', // This is intended for future use when we // will support multiple models and have an // AI configuration page in the UI. 'ai_package_value' => 'Open AI With Explain Code On GPT4', // This is used to display the model // type in the summary of the AI // vendor's response. 'ai_model' => 'gpt-4', 'ai_package_type' => "Explain the following code:", // This is the prompt for the AI to follow. // You can modify the prompt for purposes other // than explaining the code, or to request // output in a specific language. 'api_key' => $SECRET_KEY, // Paste your copied OpenAI API key here 'ai_min_char_limit'=> 4, // Minimum number of characters required in the content to be submitted // to the AI vendor for analysis. Defaults to 4 characters. 'ai_max_char_limit'=> 31000, // The maximum number of characters that can be submitted to the // AI vendor for analysis. Defaults to 31000 characters. ), ), ),

By default, ai_review block is disabled. To enable AI-driven code explanations for a diff, set enabled to true.

How to set up OpenAI integration for Swarm

To integrate OpenAI with Swarm, follow these steps:

  1. Create an OpenAI account or log in to your an OpenAI account here:

    https://platform.openai.com/settings/organization/usage

  2. Once logged in, create a new project within the OpenAI platform.

  3. Generate a secret API key:

    1. Navigate to the project settings.

    2. Create a new secret key.

    3. Copy the generated key.

  4. Configure OpenAI in SWARM_ROOT/data/config.php file.

    Add the following example configuration to the ai_review block:

    <?php
    'ai_review' => array( // Please read Perforce's Generative AI policy before enabling this feature. // See https://www.perforce.com/generative-ai-policy 'enabled' => true, 'ai_vendors' => array( 'ai_model1' => array( 'ai_vendor' => 'openAI', // Name of the AI provider being used 'ai_package_id' => '1', // Ensure that it is a unique id and its value should not be modified 'ai_package_key' => 'openaiwithexplaincodeongpt4', // This is intended for future use when we // will support multiple models and have an // AI configuration page in the UI. 'ai_package_value' => 'Open AI With Explain Code On GPT4', // This is used to display the model // type in the summary of the AI // vendor's response. 'ai_model' => 'gpt-4', 'ai_package_type' => "Explain the following code:", // This is the prompt for the AI to follow. // You can modify the prompt for purposes other // than explaining the code, or to request // output in a specific language. 'api_key' => $SECRET_KEY, // Paste your copied OpenAI API key here 'ai_min_char_limit'=> 4, // Minimum number of characters required in the content to be submitted // to the AI vendor for analysis. Defaults to 4 characters. 'ai_max_char_limit'=> 31000, // The maximum number of characters that can be submitted to the // AI vendor for analysis. Defaults to 31000 characters. ), ), ),

    Replace $SECRET_KEY with the API key you copied earlier.

  5. Make sure your OpenAI account has enough tokens to use the API. If needed, recharge your account.

This setup will enable OpenAI's GPT-4 model to explain the diff code in your Swarm.