> ## Documentation Index
> Fetch the complete documentation index at: https://docs.znapai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Text generation

***

## Request

<CodeGroup>
  ```shellscript cURL theme={null}
  curl --location 'https://api.znapai.com/v1/chat/completions' \
  --header 'Content-Type: application/json' \
  --header 'Authorization: Bearer $ZnapAI_API_KEY' \
  --header 'spend-logs-metadata: {"user_id": "user-123", "project_id": "proj_abc", "env": "production"}' \
  --data '{
      "model": "gpt-4o-mini",
      "messages": [
        {
          "role": "user",
          "content": "Hello!"
        }
      ]
    }'
  ```

  ```python OpenAI SDK (Python) theme={null}
  import openai
  client = openai.OpenAI(
      api_key="$ZnapAI_API_KEY",
      base_url="https://api.znapai.com/"
  )

  completion = client.chat.completions.create(
      model="gpt-4o-mini",
      messages = [
          {
              "role": "user",
              "content": "hello"
          }
      ]
  )

  print(completion)
  ```

  ```javascript OpenAI SDK (JS) theme={null}
  import OpenAI from "openai";
  const openai = new OpenAI({
    apiKey: "$ZnapAI_API_KEY",
    baseURL: "https://api.znapai.com/"
  });

  const completion = await openai.chat.completions.create({
      model: "gpt-4o-mini",
      messages: [
          {
              role: "user",
              content: "hello",
          },
      ]
  });

  console.log(completion);
  ```
</CodeGroup>

## Response

```json theme={null}
{
  "id": "chatcmpl-B8rLGf4crBdVhX7a13Ae37NI9h2Re",
  "created": 1741451370,
  "model": "gpt-4o-mini",
  "object": "chat.completion",
  "system_fingerprint": "fp_ded0d14823",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Hello! How can I assist you today?",
        "role": "assistant",
        "tool_calls": null,
        "function_call": null
      }
    }
  ],
  "usage": {
    "completion_tokens": 9,
    "prompt_tokens": 9,
    "total_tokens": 18,
    "completion_tokens_details": {
      "accepted_prediction_tokens": 0,
      "audio_tokens": 0,
      "reasoning_tokens": 0,
      "rejected_prediction_tokens": 0
    },
    "prompt_tokens_details": {
      "audio_tokens": 0,
      "cached_tokens": 0
    }
  },
  "service_tier": null,
  "prompt_filter_results": [
    {
      "prompt_index": 0,
      "content_filter_results": {
        "hate": {
          "filtered": false,
          "severity": "safe"
        },
        "jailbreak": {
          "filtered": false,
          "detected": false
        },
        "self_harm": {
          "filtered": false,
          "severity": "safe"
        },
        "sexual": {
          "filtered": false,
          "severity": "safe"
        },
        "violence": {
          "filtered": false,
          "severity": "safe"
        }
      }
    }
  ]
}
```

***

## Parameters

<ParamField path="spend-logs-metadata" type="header" required={false}>
  An optional header used for spend logging metadata, containing JSON properties like `user_id`, `project_id`, and `env`.
</ParamField>

<ParamField path="model" type="string" required>
  Name of the model to use (for example: gpt-4o-mini). To see the complete list of supported chat models, visit: [https://znapai.com/models/chat](https://znapai.com/models/chat)
</ParamField>

<ParamField path="messages" type="array" required>
  A list of messages comprising the conversation so far.

  <Expandable title="messages[]" defaultOpen="true">
    <ResponseField name="role" type="string" default="user" required>
      Role type

      * `user` - User message
      * `assistant` - AI response (for multi-turn)
      * `system` - System prompt
    </ResponseField>

    <ResponseField name="content" type="string" required>
      Message content

      Your question or message
    </ResponseField>
  </Expandable>

  Example:

  ```json theme={null}
  [{"role": "user", "content": "Hello, please introduce yourself"}]
  ```

  Advanced usage:

  Add system prompt (to define AI behavior):

  ```json theme={null}
  [
    {"role": "system", "content": "You are a professional Python tutor"},
    {"role": "user", "content": "How do I learn programming?"}
  ]
  ```
</ParamField>

<ParamField path="temperature" type="number" default="1.0">
  Controls output randomness, range 0-2

  * Lower values (e.g., 0.2) make output more deterministic
  * Higher values (e.g., 1.8) make output more random
</ParamField>

<ParamField path="top_p" type="number">
  An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top\_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
</ParamField>

<ParamField path="max_tokens" type="integer">
  Maximum number of tokens to generate

  Different models have different maximum limits, please refer to specific model documentation
</ParamField>

<ParamField path="frequency_penalty" type="number">
  Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
</ParamField>

<ParamField path="presence_penalty" type="number">
  Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
</ParamField>

<ParamField path="n" type="integer">
  How many chat completion choices to generate for each input message.
</ParamField>

<ParamField path="stream" type="boolean">
  If set, partial message deltas will be sent, like in ChatGPT.
</ParamField>

<ParamField path="stop" type="string | array">
  Up to 4 sequences where the API will stop generating further tokens.
</ParamField>

***

## Params to Avoid

| Param                 | Reason                                                                       |
| --------------------- | ---------------------------------------------------------------------------- |
| `stream_options`      | Not supported by non-OpenAI models                                           |
| `seed`                | Not supported by non-OpenAI models                                           |
| `logprobs`            | Not supported by non-OpenAI models                                           |
| `top_logprobs`        | Not supported by non-OpenAI models                                           |
| `logit_bias`          | Not supported by non-OpenAI models                                           |
| `spend-logs-metadata` | This metadata must be passed as an HTTP header, not in the JSON request body |
