Now you can access the data·续

</details>

<details>

<summary>Creative Writing — Longer completion for creative output</summary>

const response = await env.AI.run(
  'openai/gpt-4o-mini',
  {
    max_completion_tokens: 8192,
    messages: [
      {
        content: 'Write a short story opening about a detective finding an unusual clue.',
        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-mini",
  "max_completion_tokens": 8192,
  "messages": [
    {
      "content": "Write a short story opening about a detective finding an unusual clue.",
      "role": "user"
    }
  ]
}'
Detective Iris Kline stood in the dimly lit study of the late Vincent Hawthorne, an eccentric author known for his best-selling thrillers and reclusive nature. The smell of old books and the faint hint of cigar smoke lingered in the air, wrapping around her like a shroud as she scanned the cluttered room for anything that might shed light on the enigmatic man's death. The police had ruled it a heart attack, but Iris wasn’t convinced. A writer whose life revolved around crafting intricate plots wouldn't simply drop dead without a shred of foreshadowing.

She crouched beside a mahogany desk strewn with yellowed manuscripts and coffee-stained pages, her fingers brushing against the surface. That’s when she spotted it—a glint of silver poking out from beneath a loose floorboard. Curious, she pried it open, her nails scraping against the wood until she could retrieve the object: a small, intricately designed key.

Flipping it over in her palm, she noticed a curious engraving on its barrel—a compass rose, with eight tiny arrows radiating from the center. It was both beautiful and unsettling, like something straight out of one of Hawthorne's novels. But it was the cryptic inscription beneath it that gave her pause, a single word: “NAVIGATOR.”

Iris felt the weight of the key shift in her hand, a sense of urgency rising within her. Whatever this key unlocked, it was clear it held secrets far beyond the reclusive author’s final chapter. She slid the key into her pocket, the quiet thrill of an unraveling mystery beginning to take root in her mind. What could Vincent Hawthorne have been hiding? And where would this unusual clue lead her next?
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "annotations": [],
        "content": "Detective Iris Kline stood in the dimly lit study of the late Vincent Hawthorne, an eccentric author known for his best-selling thrillers and reclusive nature. The smell of old books and the faint hint of cigar smoke lingered in the air, wrapping around her like a shroud as she scanned the cluttered room for anything that might shed light on the enigmatic man's death. The police had ruled it a heart attack, but Iris wasn’t convinced. A writer whose life revolved around crafting intricate plots wouldn't simply drop dead without a shred of foreshadowing.\n\nShe crouched beside a mahogany desk strewn with yellowed manuscripts and coffee-stained pages, her fingers brushing against the surface. That’s when she spotted it—a glint of silver poking out from beneath a loose floorboard. Curious, she pried it open, her nails scraping against the wood until she could retrieve the object: a small, intricately designed key. \n\nFlipping it over in her palm, she noticed a curious engraving on its barrel—a compass rose, with eight tiny arrows radiating from the center. It was both beautiful and unsettling, like something straight out of one of Hawthorne's novels. But it was the cryptic inscription beneath it that gave her pause, a single word: “NAVIGATOR.” \n\nIris felt the weight of the key shift in her hand, a sense of urgency rising within her. Whatever this key unlocked, it was clear it held secrets far beyond the reclusive author’s final chapter. She slid the key into her pocket, the quiet thrill of an unraveling mystery beginning to take root in her mind. What could Vincent Hawthorne have been hiding? And where would this unusual clue lead her next?",
        "refusal": null,
        "role": "assistant"
      }
    }
  ],
  "created": 1777319560,
  "gatewayMetadata": {
    "keySource": "Unified"
  },
  "id": "chatcmpl-DZMISg6vI8hQtXDR8mDZ4ED3dwWUk",
  "model": "gpt-4o-mini-2024-07-18",
  "object": "chat.completion",
  "service_tier": "default",
  "system_fingerprint": "fp_370ba29939",
  "usage": {
    "completion_tokens": 352,
    "completion_tokens_details": {
      "accepted_prediction_tokens": 0,
      "audio_tokens": 0,
      "reasoning_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "prompt_tokens": 20,
    "prompt_tokens_details": {
      "audio_tokens": 0,
      "cached_tokens": 0
    },
    "total_tokens": 372
  }
}

</details>

<details>

<summary>Streaming Response — Enable streaming for real-time output</summary>

const response = await env.AI.run(
  'openai/gpt-4o-mini',
  {
    messages: [{ content: 'Explain the concept of recursion with a simple example.', role: 'user' }],
    stream: true,
    stream_options: { include_usage: true },
  },
)
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-mini",
  "messages": [
    {
      "content": "Explain the concept of recursion with a simple example.",
      "role": "user"
    }
  ],
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}'

```` Recursion is a programming concept where a function calls itself in order to solve a problem. It typically involves a base case that stops the recursion and a recursive case that breaks the problem down into smaller subproblems.

Example: Factorial

A classic example of recursion is calculating the factorial of a number, denoted as n!. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. The recursive definition is:

  • Base Case: 0! = 1 (the factorial of 0 is 1)
  • Recursive Case: n! = n * (n - 1)! for n > 0

Here’s a simple implementation of this concept in Python:

```python def factorial(n): # Base case if n == 0: return 1 # Recursive case else: return n * factorial(n - 1)