Skip to content

Completions

The legacy text completion endpoint generates text from an input prompt.

Recommendation

For most conversational use cases, use the Chat API. It supports multi-turn conversations and provides more complete functionality.

Create a completion

http
POST https://api.nexusmodels.cn/v1/completions

Request parameters

ParameterTypeRequiredDescription
modelstringYesModel ID
promptstring/arrayYesInput prompt
suffixstringNoText appended after the generated content
max_tokensintegerNoMaximum number of generated tokens
temperaturenumberNoSampling temperature, usually between 0 and 2
top_pnumberNoNucleus sampling value, usually between 0 and 1
nintegerNoNumber of completion candidates to return
streambooleanNoEnables streaming output
logprobsintegerNoNumber of token log probabilities to return
echobooleanNoIncludes the original prompt in the response
stopstring/arrayNoSequence that stops generation
presence_penaltynumberNoPresence penalty
frequency_penaltynumberNoFrequency penalty
best_ofintegerNoGenerates multiple results on the server and returns the best one

Parameter compatibility

Supported parameters may vary by model. Unsupported parameters may be ignored or cause the request to fail.

cURL example

bash
curl -sS -X POST \
  'https://api.nexusmodels.cn/v1/completions' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_NEXUSMODELS_API_KEY' \
  -d '{
    "model": "YOUR_COMPLETION_MODEL",
    "prompt": "Write a short greeting:",
    "max_tokens": 50,
    "temperature": 0.7
  }'

Response

A successful request returns:

http
200 OK
json
{
  "id": "cmpl-abc123",
  "object": "text_completion",
  "created": 1788163200,
  "model": "YOUR_COMPLETION_MODEL",
  "choices": [
    {
      "text": "Hello! I hope you have a wonderful and productive day.",
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 15,
    "completion_tokens": 18,
    "total_tokens": 33
  }
}

Response fields

FieldDescription
idUnique identifier for the completion request
objectResponse object type
createdResponse creation timestamp
modelModel used for the request
choicesCompletion candidates generated by the model
choices[].textGenerated text
choices[].indexCandidate index
choices[].logprobsToken log probabilities
choices[].finish_reasonReason generation stopped
usageToken usage information for the request

Code completion example

This example requires a model that supports code completion.

bash
curl -sS -X POST \
  'https://api.nexusmodels.cn/v1/completions' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_NEXUSMODELS_API_KEY' \
  -d '{
    "model": "YOUR_COMPLETION_MODEL",
    "prompt": "// JavaScript: calculate the sum of all even numbers in an array\nfunction sumEvenNumbers(arr) {\n",
    "max_tokens": 100,
    "temperature": 0.2
  }'

Python example

Install the dependency:

bash
pip install requests

Call the API:

python
import os
import requests