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

# Text Completion API

> Generate text using the OpenAI Legacy Completions compatible format.

# Text Completion API

The Legacy Completions API uses `prompt` as input and is suitable for businesses still using the old OpenAI SDK or simple text completion workflows. For new conversational scenarios, it is recommended to use the [chat completions API](./chat-completions-non-stream).

## Request Body

<ParamField body="model" type="string" required>
  Model name.
</ParamField>

<ParamField body="prompt" type="string | array<string>" required>
  Input prompt.
</ParamField>

<ParamField body="stream" type="boolean">
  Whether to enable streaming output.
</ParamField>

<ParamField body="max_tokens" type="integer">
  Maximum number of tokens to generate.
</ParamField>

<ParamField body="temperature" type="number">
  Sampling temperature.
</ParamField>

<ParamField body="top_p" type="number">
  Nucleus sampling parameter.
</ParamField>

<ParamField body="stop" type="string | array<string>">
  Stop sequences.
</ParamField>

## Request Example

<RequestExample>
  ```bash theme={null}
  curl -X POST https://octopusx.ai/v1/completions \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-3.5-turbo-instruct",
      "prompt": "把下面一句话改写得更正式：这个接口挺好用。",
      "max_tokens": 120,
      "temperature": 0.3
    }'
  ```
</RequestExample>

## Response Example

<ResponseExample>
  ```json 200 theme={null}
  {
    "id": "cmpl_abc123",
    "object": "text_completion",
    "created": 1735689600,
    "model": "gpt-3.5-turbo-instruct",
    "choices": [
      {
        "index": 0,
        "text": "该接口具备良好的易用性。",
        "finish_reason": "stop"
      }
    ],
    "usage": {
      "prompt_tokens": 22,
      "completion_tokens": 10,
      "total_tokens": 32
    }
  }
  ```

  ```json 400 theme={null}
  {
    "error": {
      "message": "field model is required",
      "type": "invalid_request_error",
      "param": "",
      "code": "invalid_request"
    }
  }
  ```

  ```json 401 theme={null}
  {
    "error": {
      "message": "Invalid API key provided",
      "type": "invalid_request_error",
      "param": "",
      "code": "invalid_api_key"
    }
  }
  ```

  ```json 402 theme={null}
  {
    "error": {
      "message": "User quota insufficient",
      "type": "new_api_error",
      "param": "",
      "code": "insufficient_user_quota"
    }
  }
  ```

  ```json 429 theme={null}
  {
    "error": {
      "message": "The current request rate is too high, please try again later",
      "type": "new_api_error",
      "param": "",
      "code": "too_many_requests"
    }
  }
  ```

  ```json 500 theme={null}
  {
    "error": {
      "message": "bad response body",
      "type": "new_api_error",
      "param": "",
      "code": "bad_response_body"
    }
  }
  ```
</ResponseExample>

## Related APIs

* [Chat Completions API (non-streaming by default)](./chat-completions-non-stream)
* [Model List](./models)
