> For the complete documentation index, see [llms.txt](https://docs.usegravity.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.usegravity.app/gravity-server/ai-tools.md).

# AI Tools

Gravity ships with support for generative AI images and text with ChatGPT and Dall-E.

{% embed url="<https://youtu.be/aJYzm0cdGh0>" %}

### 1. Create an OpenAI API Key

You must [register an account with OpenAI](https://platform.openai.com/) and [create an API key](https://platform.openai.com/account/api-keys). Then, add your API key to the .env file in Gravity:

```properties
OPENAI_API_KEY=YOUR_API_KEY
```

### 2. Generating Text with ChatGPT

Import the OpenAI model and make a request using the following:

```javascript
const openai = require('./model/openai');

async function generateText(){
  const textData = await openai.text({ prompt: 'YOUR PROMPT' }});
}  
```

This function will return a text string from ChatGPT.

### 3. Generating Images with Dall-E

Import the OpenAI model and make a request using the following:

```javascript
const openai = require('./model/openai');

async function generateImage(){
  const imageData = await openai.image({ 
  
    prompt: 'YOUR PROMPT', 
    size: '512x512'
    number: 1,
    
  }});
}
```

Size is optional - the default is `512x512` pixels. You can also specify the number of images to return; the default is 1.

This function will return an array of image objects with a url key containing your image.

```javascript
[{ url: 'https://your_generated_image_url', created: timestamp }]
```

### Included Endpoints

There are pre-configured endpoints included for you to use from the client:

```javascript
POST /api/ai/text
POST /api/ai/image
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.usegravity.app/gravity-server/ai-tools.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
