Create prompts for code completion

The Vertex AI Codey APIs include the code completion API, which supports code suggestions based on code that's recently written. Use the generative AI foundation model named code-gecko to interact with the code completion API. This topic helps you learn how to create prompts to work with the code-gecko model to create code completion suggestions.

Use cases

Use the code completion API to integrate the code-gecko model and its code completion capability into an IDE. Some common use cases for code completion are:

  • Write code faster: Use the code-gecko model to write code faster by taking advantage of code suggested for you.

  • Minimize bugs in code: Use code suggestions that you know are syntactically correct to avoid errors. Code completion helps you minimize the risk of accidentally introducing bugs that can occur when you write code.

Supported model

The following model supports code completion tasks:

  • Code Gecko

Example code completion prompts

Use the following examples to learn how to design code completion prompts.

Prompt to complete a code function

You can use the code completion model to complete a code definition. The following prompt generates code that completes a partially typed code definition:

def reverse_string(s):
  
"""
   :type s: str
   :rtype: str
   """
  

Prompt to complete a test function

You can use the code completion model to complete a test function. The following prompt generates code that completes a test function. The function in the response, test_empty_input_string, tests the reverse_string function.

def reverse_string(s):
    return s[::-1]
def test_empty_input_string()
  
-> None:
    assert reverse_string("") == ""
def test_one_character_string() -> None:
    assert reverse_string("a") == "a"
def test_two_character_string() -> None:
  

What's next