Create prompts to chat about code

The Vertex AI Codey APIs include the code chat API which supports multi-turn conversations that are specialized for code. Use the generative AI foundation model named codechat-bison to interact with the code chat API. This topic helps you learn how to create prompts to work with the codechat-bison model to have a chatbot conversation about code.

Use cases

Some common use cases for code chat are:

  • Debugging: Get help with debugging code that doesn't compile or that contains a bug.
  • Documentation: Get help with understanding code so you can document it accurately.
  • Learning: Get help with learning about code you're not very familiar with.

Supported model

The following model supports code chat tasks:

  • codechat-bison

How to submit a code chat prompt

You can submit a code chat prompt in the Generative AI Studio, as a REST command, or using Python.

When you submit a prompt, you need to specify values for the temperature and maximum output tokens. For more information, see Code chat model parameters.

Submit a prompt (console)

To run and test a code chat prompt, use the codechat-bison model in Generative AI Studio. For more information about Generative AI Studio, see Introduction to Generative AI Studio.

Do the following to use Generative AI Studio to submit a code chat prompt:

  1. Go to the Generative AI Studio page from the Vertex AI section in the Google Cloud console. Generative AI Studio
  2. On the Language card, click Open.
  3. On Start a conversation, click Code chat.
  4. In Model, select the model with the name that begins with codechat-bison. A three digit number after codechat-bison indicates the version number of the model. For example, codechat-bison@001 is the name of version one of the code chat model.
  5. Adjust Temperature or Token limit to experiment with how they affect the response. For more information, see Code chat model parameters.
  6. In Enter a prompt to begin a conversation, enter a prompt to start a conversation about code.
  7. Click Continue the conversation to submit the prompt to the chat.
  8. After you receive a response, repeat the previous two steps to continue the conversation.

Submit a prompt with curl (REST)

Do the following to use curl submit a code chat prompt:

  1. Log in to the Google Cloud CLI with your user account by doing one of the following:
    1. Run gcloud init or gcloud auth login
    2. Use Cloud Shell, which logs you into the gcloud CLI. You can check the currently active account by running gcloud auth list.
  2. Create a JSON file named request.json.
  3. Enter the following code in request.json.

    {
      "instances": {
        "messages": [
            {
              "author": "user",
              "content": "CONTENT",
            },
            {
              "author": "system",
              "content": "I am doing good. What Can I help you with in the coding world?",
            },
            {
              "author": "user",
              "content": "CONTENT",
            }
          ]
      },
      "parameters": {
        "temperature": TEMPERATURE,
        "maxOutputTokens": MAX_OUTPUT_TOKENS,
        "candidateCount": CANDIDATE_COUNT
      }
    }
    
  4. In response.json, replace CONTENT and specify your parameters for temperature, and maximum output tokens. For more information, see Code completion model parameters.

  5. In your shell, run the following command. Replace PROJECT_ID with your project ID.

    curl -X POST \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "Content-Type: application/json; charset=utf-8" \
        -d @request.json \
        "https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/publishers/google/models/codechat-bison:predict"
    

Example code chat prompt

You can use the code chat model to generate code. In the following example chat, the user requests a function that calculates the minimum of two numbers.

This is the user's first prompt:

This is the user's second prompt that results in code generation for a function:

Stream response from code chat model

To view sample code requests and responses using the REST API, see Examples using the streaming REST API.

To view sample code requests and responses using the Vertex AI SDK for Python, see Examples using Vertex AI SDK for Python for streaming.

What's next