GPT-4o

Text Generation • OpenAI

openai/gpt-4o

  • Third-party
  • Zero data retention

GPT-4o is OpenAI’s multimodal flagship, accepting text and images and responding quickly across a wide range of tasks.

Model Info
Context Window ↗ 128,000 tokens
Terms and License link ↗
More information link ↗
Zero data retention Yes
Request formats Responses, Chat Completions
Pricing <ul><li>Input (per 1M tokens)$1.25</li><li>Output (per 1M tokens)$5.00</li><li>Cached input (per 1M tokens)$0.625</li></ul>

Usage

const response = await env.AI.run(
  'openai/gpt-4o',
  { messages: [{ content: 'What are the three laws of thermodynamics?', role: 'user' }] },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "openai/gpt-4o",
  "messages": [
    {
      "content": "What are the three laws of thermodynamics?",
      "role": "user"
    }
  ]
}'
The three laws of thermodynamics are fundamental principles that describe the behavior of energy and matter in a system. They are:

1. **The First Law of Thermodynamics (Law of Energy Conservation):**
   This law states that energy cannot be created or destroyed in an isolated system. The total energy of a system and its surroundings is conserved. It can only change forms (e.g., from kinetic energy to potential energy, or from mechanical energy to thermal energy). Mathematically, it is often expressed as:
   \[
   \Delta U = Q - W
   \]
   where \(\Delta U\) is the change in internal energy of the system, \(Q\) is the heat added to the system, and \(W\) is the work done by the system.

2. **The Second Law of Thermodynamics:**
   This law states that the total entropy of a closed system can never decrease over time. It also posits that energy, while conserved, tends to disperse or spread out, leading to an increase in disorder or entropy. In more practical terms, it implies that heat cannot spontaneously flow from a colder body to a hotter one and that all natural processes are irreversible. It establishes the concept of entropy as a measure of the energy dispersal within a system.

3. **The Third Law of Thermodynamics:**
   According to this law, as the temperature of a system approaches absolute zero, the entropy of the system approaches a minimum value, often considered to be zero for a perfect crystalline structure. This means that it becomes increasingly difficult to remove more energy from a system as it nears absolute zero, and practically, reaching absolute zero is impossible.

These three laws describe the fundamental behavior of energy transformations and form the groundwork for understanding thermodynamic processes.
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "annotations": [],
        "content": "The three laws of thermodynamics are fundamental principles that describe the behavior of energy and matter in a system. They are:\n\n1. **The First Law of Thermodynamics (Law of Energy Conservation):**\n   This law states that energy cannot be created or destroyed in an isolated system. The total energy of a system and its surroundings is conserved. It can only change forms (e.g., from kinetic energy to potential energy, or from mechanical energy to thermal energy). Mathematically, it is often expressed as:\n   \\[\n   \\Delta U = Q - W\n   \\]\n   where \\(\\Delta U\\) is the change in internal energy of the system, \\(Q\\) is the heat added to the system, and \\(W\\) is the work done by the system.\n\n2. **The Second Law of Thermodynamics:**\n   This law states that the total entropy of a closed system can never decrease over time. It also posits that energy, while conserved, tends to disperse or spread out, leading to an increase in disorder or entropy. In more practical terms, it implies that heat cannot spontaneously flow from a colder body to a hotter one and that all natural processes are irreversible. It establishes the concept of entropy as a measure of the energy dispersal within a system.\n\n3. **The Third Law of Thermodynamics:**\n   According to this law, as the temperature of a system approaches absolute zero, the entropy of the system approaches a minimum value, often considered to be zero for a perfect crystalline structure. This means that it becomes increasingly difficult to remove more energy from a system as it nears absolute zero, and practically, reaching absolute zero is impossible.\n\nThese three laws describe the fundamental behavior of energy transformations and form the groundwork for understanding thermodynamic processes.",
        "refusal": null,
        "role": "assistant"
      }
    }
  ],
  "created": 1777319820,
  "gatewayMetadata": {
    "keySource": "Unified"
  },
  "id": "chatcmpl-DZMMegPcIzmJH5Nn9qiWIzdRJqlSB",
  "model": "gpt-4o-2024-08-06",
  "object": "chat.completion",
  "service_tier": "default",
  "system_fingerprint": "fp_b0de34c6fc",
  "usage": {
    "completion_tokens": 360,
    "completion_tokens_details": {
      "accepted_prediction_tokens": 0,
      "audio_tokens": 0,
      "reasoning_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "prompt_tokens": 16,
    "prompt_tokens_details": {
      "audio_tokens": 0,
      "cached_tokens": 0
    },
    "total_tokens": 376
  }
}

Examples

<details>

<summary>With System Message — Using a system message to set context</summary>

const response = await env.AI.run(
  'openai/gpt-4o',
  {
    messages: [
      { content: 'You are a helpful coding assistant specializing in Python.', role: 'system' },
      { content: 'How do I read a JSON file in Python?', role: 'user' },
    ],
  },
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "openai/gpt-4o",
  "messages": [
    {
      "content": "You are a helpful coding assistant specializing in Python.",
      "role": "system"
    },
    {
      "content": "How do I read a JSON file in Python?",
      "role": "user"
    }
  ]
}'

``` To read a JSON file in Python, you typically use the built-injson` module, which provides utilities for parsing JSON formatted data. Here’s a step-by-step guide on how to do it:

  1. Import the json module: To work with JSON data, you need to import this module.

  2. Open the JSON file: Use the built-in open() function to open the file in read mode.

  3. Parse the JSON data: Use json.load() to parse the JSON data from the file into a Python data structure (usually a dictionary or a list).

Here is a simple example demonstrating these steps:

```python import json