Text generation

Learn how to generate text from a prompt.

This API provides simple endpoints to leverage a large language model for generating text from a prompt. The models are trained on vast amounts of data to understand both multimedia inputs and natural language instructions. With carefully crafted prompts, the API can generate a wide range of outputs, including code, mathematical equations, structured JSON data, or rich human-like prose.

Quickstart

To generate text, use the chat completions endpoint. You can access the endpoint via a REST API call using an HTTP client or by using one of the available SDKs, such as the OpenAI SDK shown in the examples below.

Create a Human-Like Response to a Prompt

Using JavaScript:

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
        { role: "developer", content: "You are a helpful assistant." },
        { 
            role: "user", 
            content: "Write a haiku about recursion in programming."
        }
    ],
});

console.log(completion.choices[0].message);
from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "developer", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Write a haiku about recursion in programming."}
    ]
)

print(completion.choices[0].message)
curl "https://api.myhispreadnlp.com/v1/chat/completions" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $HISPREAD_API_KEY" \
    -d '{
        "model": "gpt-4o",
        "messages": [
            {
                "role": "developer",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Write a haiku about recursion in programming."
            }
        ]
    }'

Describe the Contents of an Image

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
        {
            role: "user",
            content: [
                { type: "text", text: "What's in this image?" },
                {
                    type: "image_url",
                    image_url: {
                        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
                    }
                }
            ]
        }
    ],
});

console.log(completion.choices[0].message);
from openai import OpenAI

client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
                    }
                }
            ]
        }
    ]
)

print(completion.choices[0].message)
curl "https://api.myhispreadnlp.com/v1/chat/completions" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $HISPREAD_API_KEY" \
    -d '{
        "model": "gpt-4o",
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "What is in this image?"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
                        }
                    }
                ]
            }
        ]
    }'

Generate JSON Data Based on a JSON Schema

In this example, the model extracts an email address from a text input into a JSON format that meets a specified schema.

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o-2024-08-06",
    messages: [
        { role: "developer", content: "Extract email addresses into JSON data." },
        { role: "user", content: "Feeling stuck? Send a message to [email protected]." }
    ],
    response_format: {
        type: "json_schema",
        json_schema: {
            name: "email_schema",
            schema: {
                type: "object",
                properties: {
                    email: {
                        description: "The email address that appears in the input",
                        type: "string"
                    }
                },
                additionalProperties: false
            }
        }
    }
});

console.log(completion.choices[0].message.content);
from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4o-2024-08-06",
    messages=[
        {"role": "developer", "content": "Extract email addresses into JSON data."},
        {"role": "user", "content": "Feeling stuck? Send a message to [email protected]."}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "email_schema",
            "schema": {
                "type": "object",
                "properties": {
                    "email": {
                        "description": "The email address that appears in the input",
                        "type": "string"
                    }
                },
                "additionalProperties": False
            }
        }
    }
)

print(response.choices[0].message.content)
curl "https://api.myhispreadnlp.com/v1/chat/completions" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $HISPREAD_API_KEY" \
    -d '{
        "model": "gpt-4o",
        "messages": [
            {
                "role": "developer",
                "content": "Extract email addresses into JSON data."
            },
            {
                "role": "user",
                "content": "Feeling stuck? Send a message to [email protected]."
            }
        ],
        "response_format": {
            "type": "json_schema",
            "json_schema": {
                "name": "email_schema",
                "schema": {
                    "type": "object",
                    "properties": {
                        "email": {
                            "description": "The email address that appears in the input",
                            "type": "string"
                        }
                    },
                    "additionalProperties": false
                }
            }
        }
    }'

Building Prompts

Crafting effective prompts is key to achieving accurate and useful responses from the model. These can include not only clear instructions but additional context or examples to guide the model’s output.

User Messages

User messages provide the primary instruction for the task at hand. They are analogous to messages a user might type in during a chat session:

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Write a haiku about programming."
        }
      ]
    }
  ]
});

Developer Messages

Developer messages give high-priority instructions and set the context for all subsequent messages. These messages dictate how the model should behave during the session:

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    {
      "role": "developer",
      "content": [
        {
          "type": "text",
          "text": `
            You are a helpful assistant that answers programming 
            questions in a friendly tone reminiscent of a southern belle 
            from the southeast United States.
          `
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Are semicolons optional in JavaScript?"
        }
      ]
    }
  ]
});

Assistant Messages

Assistant messages act as examples or previous responses from the model. Including these as context builds a conversation or provides a template for how the model should reply. For example:

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { "role": "user", "content": [{ "type": "text", "text": "knock knock." }] },
    { "role": "assistant", "content": [{ "type": "text", "text": "Who's there?" }] },
    { "role": "user", "content": [{ "type": "text", "text": "Orange." }] }
  ]
});

Conversations and Context

Although every text generation request is stateless, you can create multi-turn conversations by including prior messages in the request. As more context is added to a conversation, be mindful of token usage. Both input tokens (your prompt) and output tokens (generated response) are counted against the model's limit. Tools like the tiktoken library can help manage your token usage.


Optimizing Model Outputs

To optimize outputs for accuracy, cost, and latency consider the following:

• Accuracy: Include all necessary details and context. Sometimes additional data or retrieval-augmented generation (RAG) techniques can help.
• Cost: Use fewer tokens or select a smaller model version when appropriate. • Latency: Optimize prompt structure and code parallelism to achieve faster responses.

By following these guidelines, you can utilize this API effectively through the endpoint at api.myhispreadnlp.com with the HISPREAD_API_KEY. This setup combines the familiarity of the SDK methods with enhanced capabilities for customized tasks.