Upgrade to Pro

Unlock premium features and boost productivity!

Learn More

API Documentation

Reference documentation for the AI platform APIs.

API Endpoints

Models

Completions

Chat

Embeddings

Fine-tuning

Create Completion

Endpoint

/v1/completions

Request Body

{
  "model": "gpt-4o",
  "prompt": "Write a poem about artificial intelligence",
  "max_tokens": 256,
  "temperature": 0.7,
  "top_p": 1,
  "n": 1,
  "stream": false,
  "stop": ["\n\n"]
}

Parameters

model

String required

ID of the model to use

prompt

String required

The prompt to generate completions for

max_tokens

Integer optional

Maximum number of tokens to generate

temperature

Float optional

Sampling temperature between 0 and 2

Response

{
  "id": "cmpl-123abc",
  "object": "text_completion",
  "created": 1677858242,
  "model": "gpt-4o",
  "choices": [
    {
      "text": "In circuits of silicon and gold, ...",
      "index": 0,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 6,
    "completion_tokens": 156,
    "total_tokens": 162
  }
}
Code Examples
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

async function main() {
  const { text } = await generateText({
    model: openai("gpt-4o"),
    prompt: "Write a poem about artificial intelligence",
    maxTokens: 256,
    temperature: 0.7,
  });
  
  console.log(text);
}

main();