Chat Completion

In this section, we will guide you on how to use the OpenAI compatible Chat Completion API to generate human-like conversational responses.

The Chat Completion API allows you to interact with a wide range of language models in a conversational manner. You can use models from various providers, including but not limited to: GPT-4, Claude, Gemini, Llama, etc.

This versatility enables you to choose the most suitable model for your specific use case, whether you need advanced reasoning capabilities, specialized knowledge, or cost-effective solutions.

With this API, you can send a series of messages as input and receive a model-generated message as output, creating dynamic and context-aware conversations across a broad spectrum of applications.


POSThttps://api.model.box/v1/chat/completion

Chat completions

This endpoint creates a model response for the given chat conversation.

Authentication

To use the Chat Completion API, you must authenticate your requests using your API key. See Authentication for more information.

Required attributes

  • Name
    messages
    Type
    array
    Description

    A list of messages comprising the conversation so far.

  • Name
    model
    Type
    string
    Description

    ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.

Optional attributes

  • Name
    frequency_penalty
    Type
    number
    Description

    Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far.

  • Name
    logit_bias
    Type
    map
    Description

    Modify the likelihood of specified tokens appearing in the completion.

  • Name
    logprobs
    Type
    boolean
    Description

    Whether to return log probabilities of the output tokens or not.

  • Name
    max_tokens
    Type
    integer
    Description

    The maximum number of tokens that can be generated in the chat completion.

  • Name
    n
    Type
    integer
    Description

    How many chat completion choices to generate for each input message.

  • Name
    presence_penalty
    Type
    number
    Description

    Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far.

  • Name
    response_format
    Type
    object
    Description

    An object specifying the format that the model must output.

  • Name
    seed
    Type
    integer
    Description

    If specified, our system will make a best effort to sample deterministically.

  • Name
    stop
    Type
    string / array
    Description

    Up to 4 sequences where the API will stop generating further tokens.

  • Name
    stream
    Type
    boolean
    Description

    If set, partial message deltas will be sent, like in ChatGPT.

  • Name
    temperature
    Type
    number
    Description

    What sampling temperature to use, between 0 and 2.

  • Name
    top_p
    Type
    number
    Description

    An alternative to sampling with temperature, called nucleus sampling.

  • Name
    tools
    Type
    array
    Description

    A list of tools the model may call. Currently, only functions are supported as a tool.

  • Name
    tool_choice
    Type
    string / object
    Description

    Controls which (if any) tool is called by the model.

  • Name
    user
    Type
    string
    Description

    A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.

Request

POST
/v1/chat/completions
curl https://api.model.box/v1/chat/completions \
  -H "Authorization: Bearer $MODELBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Response

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-4o-mini-0613",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Hello! How can I assist you today?"
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}