Chat Completion API

Core interface for creating conversational AI applications, supporting multi-turn conversations, system prompts, function calling, and other advanced features

POSTStable VersionStreaming Support

Endpoint

POSThttps://api.n1n.ai/v1/chat/completions

Request Headers

HeaderValueDescription
Content-Typeapplication/jsonRequest body format
AuthorizationBearer YOUR_API_KEYAuthentication token

Request Parameters

ParameterTypeRequiredDescription
modelstringModel ID, e.g. gpt-3.5-turbo, gpt-4
messagesarrayArray of chat messages containing conversation history
temperaturenumberNoSampling temperature, range 0-2, default 1. Higher values make output more random
max_tokensintegerNoMaximum number of tokens to generate
streambooleanNoEnable streaming response, default false
top_pnumberNoNucleus sampling parameter, range 0-1, default 1
nintegerNoNumber of responses to generate, default 1
stoparrayNoStop sequences to halt generation, up to 4
presence_penaltynumberNoPresence penalty, range -2 to 2, default 0
frequency_penaltynumberNoFrequency penalty, range -2 to 2, default 0

Message Format

Each message object in the messages array includes the following fields:

FieldTypeDescription
rolestringRole type: system,user,assistant
contentstringMessage content
namestring(Optional) Sender name
Message Array Example
[
  {
    "role": "system",
    "content": "You are a helpful AI assistant, skilled at answering various questions."
  },
  {
    "role": "user",
    "content": "What isMachine Learning? "
  },
  {
    "role": "assistant",
    "content": "Machine Learning is a branch of Artificial Intelligence..."
  },
  {
    "role": "user",
    "content": "Can you give me a simple example?"
  }
]

Request Example

Basic Request

cURL
curl https://api.n1n.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [
      {
        "role": "user",
        "content": "Hello, how's the weather today?"
      }
    ],
    "temperature": 0.7
  }'

Streaming Response Request

Python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.n1n.ai/v1"
)

# Streaming response
stream = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Write a poem about spring"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="")

Response Format

Success Response

{
  "id": "chatcmpl-123456",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "As an AI assistant, I cannot access real-time weather information. I suggest you check weather forecast websites or apps..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 13,
    "completion_tokens": 28,
    "total_tokens": 41
  }
}

Streaming Response Format

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"content":"As"},"finish_reason":null}]}

data: {"id":"chatcmpl-123","object":"chat.completion.chunk","created":1677652288,"model":"gpt-3.5-turbo","choices":[{"index":0,"delta":{"content":"AI"},"finish_reason":null}]}

data: [DONE]

Error Handling

401 Unauthorized

Invalid or missing API key

{"error": {"message": "Invalid API key", "type": "invalid_request_error", "code": "invalid_api_key"}}

429 Too Many Requests

Request frequency exceeds limit

{"error": {"message": "Rate limit exceeded", "type": "rate_limit_error", "code": "rate_limit_exceeded"}}

400 Bad Request

Request parameters error

{"error": {"message": "Invalid model specified", "type": "invalid_request_error", "code": "model_not_found"}}

Best Practices

Recommended Practices

  • Set temperature parameter appropriately: use 0.7-1.0 for creative tasks, 0-0.3 for precise tasks
  • Use system messages to define AI's role and behavior guidelines
  • Preserve conversation history to maintain context continuity
  • Use max_tokens to limit response length and control costs
  • Use streaming responses for long conversations to improve user experience
  • Implement error retry mechanism to improve service stability