Azure OpenAI integration for P4 Code Review

Use Azure OpenAI with P4 Code Review to generate AI-based explanations for code diffs and assist with code reviews.

To review the terms for using your own API keys, download the API Key Usage Agreement for Perforce P4 Code Review.

To integrate OpenAI with P4 Code Review, complete the following:

  • Generate API key

  • Configure AI in P4 Code Review

Generate API key

  1. Sign in to the Azure portal.

  2. Create or select an Azure OpenAI resource.

  3. Create or identify an Azure OpenAI deployment.

  4. Copy the Azure OpenAI API key.

  5. Record the following information:

    • API key.

    • Resource endpoint URL.

    • Deployment name.

    • API version.

Configure AI in P4 Code Review

There are two ways to configure AI in P4 Code Review:

  • Use AI configuration in System Information (Recommended)

  • Use ai_review block config.php (Not recommended)

Use AI configuration in System Information (Recommended)

This is the easiest way to set up Azure OpenAI in P4 Code Review

  1. In the top right corner of P4 Code Review, click System Information from the User ID dropdown menu.

  2. Click Configuration.

  3. Click on AI Configuration to expand the section.

  4. Turn on Enable AI.

  5. At the bottom of the page, in the AI Vendor section, click the Edit vendor configuration icon.

  6. Configure the following settings:

    • AI Vendor: Select Azure OpenAI from the dropdown.

    • AI Package Value: (optional) A display value for the AI package.

    • AI Model: Enter the name of the AI model. For example, gpt-4.

    • AI Package Type: Enter a value to identify the prompt type used in the AI response.

    • API key: Enter your OpenAI API key.

    • Character Limits: Enter the minimum and maximum characters required for AI processing.

    • API Endpoint: Enter the API endpoint URL.

    • Prompts: (Optional) Change the prompts sent to OpenAI. These prompts are for the AI Assistant for comments. You can update the following:

      • Improve Comment

      • Summarize Text

      • Fix Spelling and Grammar

      • Custom Prompt

  7. Click Update AI Vendor.

Use config.php (Not recommended)

To configure OpenAI using the config.php file, you must be a super user.

  1. To configure AI review integration for P4 Code Review, add the ai_review block configuration in SWARM_ROOT/data/config.php file, as in the following example:

    <?php
    'ai_review' => array(
        'enabled' => true,
        'ai_vendors' => array(
            'ai_model1' => array(
                'ai_vendor'         => 'azureOpenAI',
                'ai_package_value'  => 'Azure OpenAI',
                'ai_model'          => 'gpt-4o',
                'api_key'           => $SECRET_KEY,
                'api_end_point'     => 'https://<resource>.openai.azure.com/openai/deployments/<deployment-id>',
                'api_version'       => '2024-10-21',
                'ai_package_type'   => 'Explain the following code:',
                'ai_min_char_limit' => 4,
                'ai_max_char_limit' => 31000,
            ),
        ),
    ),
    

    Replace the following:

    • $SECRET_KEY with your Azure OpenAI API key..

    • <resource> with your Azure OpenAI resource name.

    • $SECRET_KEY with your Azure OpenAI deployment name.

  2. Save the file.

  3. Reload P4 Code Review

Ensure that your OpenAI account has enough tokens to use the API. If necessary, recharge your account.

How Azure OpenAI authentication works

Azure OpenAI uses a different authentication method than OpenAI:

  • P4 Code Review sends the API key using the api-key HTTP header.

  • P4 Code Review includes the api-version query parameter with each request.

  • P4 Code Review uses the deployment specified in the API Endpoint URL.

The API version defaults to 2024-10-21 if no value is configured.

Result

This configuration enables Azure OpenAI to generate AI-assisted code review summaries and explanations in P4 Code Review.