Codegen’s GraphSitter library provides powerful APIs to interact with Generative AI through codebase.ai and codebase.flag_ai.

Common use cases include:

  • Generating new functions and methods
  • Flagging code for review
  • Generating documentation and converting docstring formats
  • Creating new tests and converting test formats
  • Automatically flagging and fixing incorrect function names

Both codebase.ai and codebase.flag_ai take in three arguments:

Prompt: The prompt used to generate the new code or flag the code.

Target: The symbol/target to generate the new code or flag the code for. This is passed with special context to ensure the AI understands the target and its context.

Context: Optional additional context to provide to the AI. Formatted as a either a string, symbol, list, or dictionary (with supports nested arguments of strings, symbols, lists, and additional dictionaries).

Overview

In general, codebase.ai takes in a prompt, a target, and optionally any additional symbol or string context, and returns a response from the Generative AI.

On the other hand, codebase.flag_ai takes in a prompt, a symbol/target, and any additional context, and optionally returns a boolean value indicating whether the target should be flagged for edit.

The advantage of using codebase.ai and codebase.flag_ai is that they have full context awareness of the entire codebase, with integration directly into the GraphSitter technology. This means that not only does the AI have access to high level context like function usages, callsites, class hierarchy, and more, but also edits the codebase in a safe and context-aware manner using symbol.edit() and symbol.rename().

By default, there is a maximum of 150 AI requests per codemod execution. This is a safeguard to prevent runaway AI requests. You can change this limit by calling codebase.set_max_ai_requests with the number of requests you want to allow.
Always review AI generated code for correctness and safety! codebase.ai does not have the same amount of safety guardrails as the rest of the GraphSitter APIs.

Examples

Generating New Code

One usecase of Codebase AI is to generate new code.

Generating Docstrings

This is an example usecase of Codebase AI to generate new docstrings for methods in a codebase.

Converting Docstring formats

Another common usecase of codebase.ai is to convert docstring formats.

More info on how to use Generate AI on Docstrings can be found here.

Flagging Code for Review

You can use codebase.flag_ai to flag and tag specific sections of code for review.

Automatically flagging and fixing incorrect function names

The output of codebase.ai can be used in more ways than just to generate new code. In this example, codebase.ai is used to generate a new name for a function, when is then passed into fuction.rename to change all usecases of that function name across the codebase.