LM Studio integration for P4 Code Review

Use LM Studio with P4 Code Review to generate AI-based explanations for code diffs using locally hosted AI models.

Unlike cloud AI providers, LM Studio runs models on your local machine and does not require an external API key.

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

Recommended models

LM Studio supports many local models. Recommended models include:

  • lmstudio-community/gemma-3-1B-it-qat-GGUF

  • lmstudio-community/Qwen3-1.7B-GGUF

  • lmstudio-community/DeepSeek-R1-Distill-Llama-8B-GGUF

  • lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGU

To integrate LM Studio with P4 Code Review

Complete the following steps:

  • Start LM Studio and load a model.

  • Configure AI in P4 Code Review.

Start LM Studio and load a model

  1. Download and install LM Studio.

  2. Open LM Studio.

  3. Download and load a supported model.

  4. Start the local server:

    • Enable the Local Server option.

    • Note the API endpoint URL (for example: http://localhost:1234/v1).

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 LM Studio 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 LM Studio from the dropdown.

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

    • AI Model: Enter the name of the AI model.

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

    • API key: Leave this blank. It is not required for LM Studio.

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

    • API Endpoint: Enter the LM Studio server URL. For example: http://localhost:1234/v1.

    • Prompts: (Optional) Change the prompts sent to LM Studio. 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.

LM Studio must be running and the local server enabled for AI features to work. If the server is not running, AI requests will fail.

Use config.php (Not recommended)

To configure LM Studio 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(
             // 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
             'data_retention_lifetime' => '150 days',  // Delete AI summary records that are older than the
    					  	  // value provided. By default, this value is 30 days.
    						  // Enter a value in 'days' or 'months'.
    						  // For example, '30 days' or '2 months'.
    						  // A cron job is required to remove the
    						  // AI summaries on a schedule.
    						  // See Set up a cron job to delete AI summaries.	
            'timeout' => 500, // Setting timeout for the request sent from p4 code review to AI vendor 
                              //  Timeout set in seconds
            'ai_vendors' => array(
                'ai_model1' => array(
                    'ai_vendor'              => 'lmStudioAI', // Name of the AI provider being used
                    'ai_package_id'          => '1', // Ensure this remains as '1'. Do not modify the ai_package_id.
                    'ai_package_key'         => 'LMStudioPackage', // This is intended for future  
                                                                   // use when we will support
                                                                   // multiple models and have an 
                                                                   // AI configuration page in the UI.
                    'ai_package_value'       => 'AI LM Studio', // This is used to display 
                                                                // the model type in the
                                                                // summary of the
                                                                // AI vendor's response
                    'ai_package_type'        => "Explain the following code:", // This is the prompt for the AI.
                                                                               // You can modify the prompt  for
                                                                               // purposes other than explaining
                                                                               // the code, or to request the
                                                                               // output in a specific language.
                    'api_end_point'          => 'http://{LMStudioRunningHost}/v1/chat/completions',
                                                 // Replace {LMStudioRunningHost} with your 
                                                 // LM Studio server IP address.
                    'ai_min_char_limit'      => 4, // Minimum number of characters required in the content
                                                   // 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.
                ),
            ),
        ),

LM Studio enables AI-powered code analysis in P4 Code Review without requiring external services or API keys. This is useful for environments where data must remain local.