> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mage.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Using AI (artificial intelligence)

> Use AI to perform various actions in Mage.

<Frame>
  <p align="center">
    <img alt="Using artificial intelligence" src="https://steamuserimages-a.akamaihd.net/ugc/813376249149384255/EC3222795DC2961599F7FC0B69E0241C8EC7293E/?imw=5000&imh=5000&ima=fit&impolicy=Letterbox&imcolor=%23000000&letterbox=false" />
  </p>
</Frame>

Mage uses AI to help you build data pipelines faster— with our OpenAI integration, you can automate the tedious parts of pipeline development and focus on the fun stuff.

<Note>You will need to add an OpenAI API key to your project before you can use AI for various actions.</Note>

We currently support the following AI features:

* Generate pipelines
* Generate blocks
* Document code
  * Block documentation
  * Block comments
  * Pipeline documentation
  * Simultaneously document *all* blocks in a pipeline

<p align="center">
  <img src="https://mage-ai.github.io/assets/overview/ai-demo.gif" />
</p>

To see examples of how to use AI in Mage, check out our [AI guides](/guides/ai/overview).

## Setup

You need to add an OpenAI API key to your project before you can use AI for various actions.

<Note>
  Read [OpenAI’s documentation](https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key)
  to get your API key.
</Note>

Once you have your OpenAI API key, go to project settings (click the "wizard" in the top right > Workspace > Preferences) and enter the OpenAI API key under the section labeled <b>OpenAI</b>.

## Generate pipeline using AI

When creating a new pipeline, select the option labeled <b>Using AI</b>.

Then, type the description of what the pipeline should do.

For example:

*Load data from an API,
then clean the column names,
and finally export the dataframe to PostgreSQL.*

<img alt="Generate pipeline using AI" src="https://mage-ai.github.io/assets/guides/ai/generate-pipeline.png" />

## Generate block using AI

<Note>
  You must turn on the feature named `add_new_block_v2`
  in your project settings. (click the "wizard" in the top right > Workspace > Preferences)
</Note>

When adding a new block, type in the description of what you want the block to do.
In the autocomplete dropdown list, select the 1st option with a label starting with

<b>Generate block using AI: ...</b>

<img alt="Generate block using AI" src="https://mage-ai.github.io/assets/guides/ai/generate-block.png" />

## Document code using AI

<img alt="Add documentation using AI" src="https://mage-ai.github.io/assets/guides/ai/document.png" />

### Add documentation for a block

On the edit pipeline page in the top right corner of a block, click the AI actions icon.
Select the option labeled <b>Document block</b>.

### Add documentation for a pipeline and all its blocks

On the edit pipeline page in the top right corner of a block, click the AI actions icon.
Select the option labeled <b>Document pipeline and all blocks</b>.

### Add comments in a block

On the edit pipeline page in the top right corner of a block, click the AI actions icon.
Select the option labeled <b>Add comments in code</b>.
