Use prompts to explain, refactor, and suggest changes to your code
This functionality relies on the AI Assistant plugin that requires an additional license.
For more information, refer to JetBrains AI Service licensing and Enable AI Assistant plugin.
Use pre-written prompts to explain code, find problems, and refactor your code. PyCharm provides project-specific context, such as the languages and technologies used in your project.
You can also define your own custom prompts and add them to the AI Actions menu.
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press AltEnter.
Select AI Actions and then Explain Code.
The AI Assistant tool window will open to provide you with an explanation.
Click
Attached elements to see the list of files that provided the necessary context for generating the answer.
Click
Explain DataFrame in the upper-right corner of the output cell.
The AI Assistant tool window will open to provide the detailed description of the dataframe.
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press AltEnter.
Select AI Actions and then Suggest Refactoring.
The AI chat will open to offer you refactoring suggestions.
In the field with the refactored code, click See Diff and Apply to open the diff viewer. To skip the diff viewing step, expand the list next to See Diff and Apply and select Apply Immediately.
In the diff viewer, use Unified or Two-Side view to review the suggested changes.
If you like the result, click
Accept in the gutter to move the selected AI-generated code snippets to the source file, or click Accept all to fully replace the originally selected code fragment. Otherwise, close the diff viewer to skip the suggested refactoring.
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press AltEnter.
Select AI Actions and then Find Problems.
The AI chat will open to show the potential issues you may want to look into.
For Python runtime errors shown in the console, PyCharm shows inlay hints, allowing you to get AI explanations for those errors.
Click Explain with AI in the console.
The AI Assistant tool window will open to give you an explanation of the error and suggest a fix.
If you want to use the suggested fix, click
in the field with the refactored code to put the AI-generated code into the editor.
Select a code fragment and right-click it to open the context menu.
Alternatively, select a code fragment and press AltEnter.
Select AI Actions and choose the Django intention.
- Create Django View For Model
AI Assistant suggests the code of a class-based view for the selected model along with the steps required to implement it in your project.
This action is available only for Django models.
- Create Django Admin For Model
AI Assistant suggests the code required to register the selected model in the Django admin interface along with other necessary steps.
You can add your own prompts to the prompts library and use them via the AI Actions menu.
Do one of the following:
Right-click anywhere in the editor to open the context menu, then go to AI Actions | Add Your Prompts.
Press AltEnter, select AI Actions, and click Add your prompts.
Press CtrlAlt0S to open the IDE settings and then select Tools | AI Assistant | User Prompts Library.
Click
to create a new prompt.
In the text field, write the prompt.
If needed, click the
$SELECTION
variable to add a Markdown-formatted code block with current code selection and language name to the new prompt.Edit the new prompt name.
Select the first checkbox if you want AI Assistant to wait for you to make additional input in the chat after invoking the prompt.
Keep the second checkbox if you want your new prompt to be listed in the AI Actions menu.
Click Apply.
Once you create a prompt, you can edit or delete it at any time.
note
If you have written a prompt while using the AI Assistant chat, you can save the current prompt by clicking
in the chat.
Thanks for your feedback!