AI Assistant

Code Faster with JetBrains AI in PyCharm

PyCharm 2024.3 comes with many improvements to JetBrains AI to help you code faster. I’m going to walk you through some of these updates in this blog post. 

Natural language inline AI prompt

You can now use JetBrains AI by typing straight into your editor in natural language without opening the AI Assistant tool window. If you use either IntelliJ IDEA or PyCharm, you might already be familiar with natural language AI prompts, but let me walk you through the process. 

If you’re typing in the gutter you can start typing your request straight into the editor, and then press Tab. Here’s an example of one such request:

write a script to capture a date input from a user and print it out prefixed by a message stating that their birthday is on that date.

You can then iterate on the initial input by clicking on the purple block in the gutter or by pressing ⌘\ or Ctrl+\ and pressing Enter:

add error handling so that when a birthday is in the future, we dont accept it

You can use  ⌘\ or Ctrl+\ to keep iterating until you’re happy with the result. For example, we can use the prompt:

print out the day of the week as well as their birthday date

And then: 

change the format of day_of_week to short

This feature is available for Python, JavaScript, TypeScript, JSON, and YAML files.

Let’s look at some more examples. We can get JetBrains AI Assistant to help us generate new code with a prompt like this:

Write code that lists the latest polls, shows poll details, handles voting, updates votes, and displays poll results, ensuring only published polls are accessible.

Or add some error handling to our code:

Add edge case handling to this code

Remember, context is everything. Where you start your natural language prompt is important, as PyCharm uses the placement of your caret to figure out the context. You don’t need to prefix your query with a ? or $ if you start typing in the gutter because the context is the file, but if your caret is indented, you’ll need to start your query with the ? or $ character so PyCharm knows you’re crafting a natural language query.

In this example, we want to refactor existing code, so we need to prefix our query with the ? character:

?create a dedicated function for printing the schedule and remove the code from here

Try JetBrains AI for free

Running code in the Python console

We know that JetBrains AI can generate code for you, but now you can run that code in the Python console without leaving the AI Assistant tool window by clicking the green run arrow.

For example, let’s say you have the following prompt:

Create a python script that asks for a birthday date in standard format yyy-MM-dd then converts it and prints it back out in a written format such as 22nd January 1991 

You can now click the green run arrow on the top-right of the code snippet to run it in your Python console:

Even more features

In addition to the new functionality for natural language and code completion for PyCharm highlighted above, there are several other improvements to JetBrains AI. 

Faster code completion

We have introduced a new model for faster cloud-based completion with AI Assistant which is showing very promising results.

Faster documentation

If documentation isn’t your thing, you can now hand off writing your Python docstrings to JetBrains AI. If you type either single or double quotes to enter a docstring and then press Return, you’ll see a prompt that says Generate with AI Assistant. Click that prompt and let JetBrains AI generate the documentation for you:

Help at your fingertips

We all need a little help now and again, and we can get JetBrains AI to help us here too. We’ve added a /docs prompt to the JetBrains AI tool window. This prompt will query the PyCharm documentation to save you from switching out of the context you’re working in!

Ability to choose your LLM

For AI Chat, you can now select a different LLM from the drop-down menu in the chat window itself. There are lots of options for you to choose from:

More context in Jupyter notebooks

We’ve also improved how JetBrains AI works for data scientists. JetBrains AI now recognizes DataFrames and variables in your notebook. You can prefix your DataFrame or variable with # so that JetBrains AI considers it as part of the context. 

Summary

JetBrains AI is available inside PyCharm, right where you need it. This release brings many improvements, from writing in natural language inside the editor and running AI-generated Python snippets in the console to generating documentation. 

Remember, if you’re in the gutter, you can start typing in natural language and then press Tab to get AI Assistant to generate the code. If you’re inside a method or function, you need to prefix your natural language query with either ? or $. You can then iterate on the generated code as many times as you like as you build out your new functionality and explore further.

Try JetBrains AI for free

image description