Skip to content

OpenAI SDK

Chat Completions, Embeddings and Models through the OpenAI-compatible API.

Your region's gateway address: resolving…

Setup

Set the SDK's base_url to the gateway address + /v1 and use your Aitechestvo API key instead of the provider key. The gateway picks the provider by model alias and adds its credentials.

Python

Regular and streamed responses, tools (function calling) and response_format all work. Data is restored in the answer, including tool-call arguments.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://<gateway>/v1",
    api_key=os.environ["AITECH_API_KEY"],  # atk_… key from the console
)

stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Draft a reply to John Smith, john.smith@example.com"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Node.js / TypeScript

The same code as with OpenAI — only the address and key change.

import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://<gateway>/v1',
  apiKey: process.env.AITECH_API_KEY, // atk_… key from the console
});

const completion = await client.chat.completions.create({
  model: 'gpt-4o-mini',
  messages: [{ role: 'user', content: 'Summarize the contract with INN 7701234560' }],
});
console.log(completion.choices[0]?.message.content);

Embeddings

Texts sent to /v1/embeddings are pseudonymized first. By default, equal values get equal keyed pseudonyms within the organization, so vector search keeps working.

vectors = client.embeddings.create(
    model="text-embedding-3-small",
    input=["Customer John Smith asked about invoice 42"],
)

Response headers

X-Aitech-Request-Id — the log identifier; X-Aitech-Decision — the outcome (passed, masked, warned); X-Aitech-Warnings and X-Aitech-Entities — percent-encoded category keys (decode with decodeURIComponent).

Errors

Gateway errors use the OpenAI format with an aitech extension: for example 403 policy_blocked with error.aitech.block_reasons — the categories that blocked the request (category keys only, never the values). If masking is impossible, the request is rejected and never reaches the model.

{
  "error": {
    "message": "Request blocked by policy",
    "type": "permission_error",
    "code": "policy_blocked",
    "param": null,
    "aitech": {
      "request_id": "0b6f…",
      "block_reasons": ["CREDENTIAL"],
      "warn_reasons": []
    }
  }
}