Skip to content
Gravity Logo / Docs Gravity home
Browse documentation
Documentation/Gravity Server

AI Tools

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

Watch the video

1. Create an OpenAI API Key

You must register an account with OpenAI and create an API key. Then, add your API key to the .env file in Gravity:

OPENAI_API_KEY=YOUR_API_KEY

2. Generating Text with ChatGPT

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

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:

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.

The helper returns the image API response’s data array.

Included Endpoints

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

POST /api/ai/text
POST /api/ai/image

The text endpoint accepts prompt. The image endpoint requires both prompt and size. Both return the result in the response’s data property.