Chat · OpenAI

Chat Completions

Generate conversational responses using OpenAI language models. Supports streaming, function calling, and multimodal inputs.

POST/v1/chat/completions

Supported Models

ModelProvider
gpt-4oOpenAI
gpt-4o-miniOpenAI
gpt-4.1OpenAI
gpt-4.1-miniOpenAI
gpt-4.1-nanoOpenAI
o4-miniOpenAI

Request

Body Parameters

modelstringrequired

Model ID to use for completion

messagesarrayrequired

Array of message objects with role and content

temperaturenumber

Sampling temperature (0-2)

Default: 1

max_tokensinteger

Maximum tokens to generate

top_pnumber

Nucleus sampling parameter

Default: 1

streamboolean

Enable server-sent events streaming

Default: false

stopstring | string[]

Stop sequences to halt generation

frequency_penaltynumber

Frequency penalty (-2 to 2)

Default: 0

presence_penaltynumber

Presence penalty (-2 to 2)

Default: 0

toolsarray

List of tools (functions) the model can call

tool_choicestring | object

Control tool selection behavior

Options: auto, none, required

response_formatobject

Force output format

Options: text, json_object

Message Object

Fields

rolestringrequired

Message role

Options: system, user, assistant, tool

contentstring | arrayrequired

Message text or multimodal content array

namestring

Optional participant name

tool_callsarray

Tool calls made by the assistant

tool_call_idstring

ID of tool call this message responds to

cURL
curl https://api.metriqual.com/v1/chat/completions \
  -H "Authorization: Bearer mql_your_key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "What is quantum computing?"}
    ],
    "temperature": 0.7,
    "max_tokens": 500
  }'
TypeScript SDK
import Metriqual from '@metriqual/sdk';

const mql = new Metriqual({ apiKey: 'mql_your_key' });

const response = await mql.chat.create({
  model: 'gpt-4o',
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'What is quantum computing?' }
  ],
  temperature: 0.7,
  max_tokens: 500
});

console.log(response.choices[0].message.content);
Python SDK
from metriqual import MQL

mql = MQL(api_key="mql_your_key")

response = mql.chat.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is quantum computing?"}
    ],
    temperature=0.7,
    max_tokens=500
)

print(response["choices"][0]["message"]["content"])

Response

Response Fields

idstring

Unique completion ID

objectstring

Always "chat.completion"

createdinteger

Unix timestamp

modelstring

Model used for completion

choicesarray

Array of completion choices

usageobject

Token usage statistics

200
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1705320000,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Quantum computing is a type of computation..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 150,
    "total_tokens": 175
  }
}

Streaming

Set stream: true to receive incremental responses via Server-Sent Events. Each chunk contains a delta with partial content.

cURL Streaming
curl https://api.metriqual.com/v1/chat/completions \
  -H "Authorization: Bearer mql_your_key" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "Tell me a story"}], "stream": true}'
TypeScript SDK — Streaming
// Async iterator
for await (const chunk of mql.chat.stream({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell me a story' }]
})) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}

// Or collect the full response
const { text } = await mql.chat.streamToCompletion({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Tell me a story' }]
});
console.log(text);
Python SDK — Streaming
# Iterator
for chunk in mql.chat.stream(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Tell me a story"}]
):
    delta = chunk["choices"][0]["delta"]
    print(delta.get("content", ""), end="")

# Or collect the full response
result = mql.chat.stream_to_completion(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Tell me a story"}]
)
print(result["text"])

Simple Completion Helper

Use the complete() method for a one-line call that returns just the text response.

TypeScript SDK
const text = await mql.chat.complete(
  [{ role: 'user', content: 'Explain gravity in one sentence' }],
  { model: 'gpt-4o-mini' }
);
console.log(text); // "Gravity is the force..."
Python SDK
text = mql.chat.complete(
    [{"role": "user", "content": "Explain gravity in one sentence"}],
    model="gpt-4o-mini"
)
print(text)  # "Gravity is the force..."

SDK Method Reference

MethodTypeScriptPython
Create completionmql.chat.create(request)mql.chat.create(**kwargs)
Stream chunksmql.chat.stream(request)mql.chat.stream(**kwargs)
Stream to textmql.chat.streamToCompletion(request)mql.chat.stream_to_completion(**kwargs)
Simple textmql.chat.complete(messages, opts?)mql.chat.complete(messages, **kwargs)