> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreflux.org/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI

> Client-mode REST API connector for OpenAI Responses, Embeddings, and Chat Completions — MQTT into LLM alarm classification and embeddings

Technical Preview

## Overview

A client-mode connector for a **curated** slice of the [OpenAI API](https://developers.openai.com/api/reference/) focused on IIoT / edge alarm text into LLMs:

| Catalog connector event | Entity / path | Notes |
| - | - | - |
| POST responses | `POST /v1/responses` | Responses API — classify alarm text; JSON schema / structured output |
| POST embeddings | `POST /v1/embeddings` | Create embeddings (e.g. `text-embedding-3-small`) |
| POST chat/completions | `POST /v1/chat/completions` | Chat Completions; optional `response_format` |
| (supporting) GET models | `GET /v1/models` | List models (auth smoke) |
| (supporting) GET model | `GET /v1/models/{model}` | Retrieve one model |

Default `BASE_ADDRESS` is `https://api.openai.com` (no trailing slash). Topics use `ai/openai/<object>/<verb>`.

See the base guide: [REST API Connectors](/latest/lot-language/routes/rest-api-routes).

> [!NOTE]
>
> **Auth:** `Authorization: Bearer &lt;OpenAI API key&gt;`. Prefer Hub secrets (`GET SECRET "OPENAI_API_KEY"`) mapped into MQTT `api_token`. Never commit real `sk-...` keys.

> [!NOTE]
>
> Factory live smoke is **blocked** until an OpenAI API key is supplied. Point secrets / MQTT JSON at your project before production use.

## Basic Syntax

```lot wrap theme={"theme":"css-variables","languages":{"custom":["/languages/lot.json"]}}
# Coreflux REST_API template — OpenAI
# Auth: Authorization Bearer {value.json.api_token}
# BASE_ADDRESS: https://api.openai.com

DEFINE ROUTE OpenAiRestApi WITH TYPE REST_API
    ADD REST_API_CONFIG
        WITH BASE_ADDRESS "https://api.openai.com"
        WITH ENABLE_CLIENT "true"
        WITH USE_SSL "true"
        WITH REQUEST_TIMEOUT '60'
        WITH DEFAULT_CONTENT_TYPE "application/json"
    ADD EVENT PostResponses
        WITH SOURCE_TOPIC "ai/openai/responses/post"
        WITH DESTINATION_TOPIC "ai/openai/responses/post/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1/responses"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "Authorization" "Bearer {value.json.api_token}"
        WITH BODY "{payload}"
    ADD EVENT PostEmbeddings
        WITH SOURCE_TOPIC "ai/openai/embeddings/post"
        WITH DESTINATION_TOPIC "ai/openai/embeddings/post/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1/embeddings"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "Authorization" "Bearer {value.json.api_token}"
        WITH BODY "{payload}"
    ADD EVENT PostChatCompletions
        WITH SOURCE_TOPIC "ai/openai/chat/completions/post"
        WITH DESTINATION_TOPIC "ai/openai/chat/completions/post/response"
        WITH METHOD "POST"
        WITH ENDPOINT "/v1/chat/completions"
        WITH HEADERS
            ADD "Accept" "application/json"
            ADD "Content-Type" "application/json"
            ADD "Authorization" "Bearer {value.json.api_token}"
        WITH BODY "{payload}"
```

Full deployable file: `OpenAiRestApi.lot` in the factory package (no `ADD METADATA`).

## Topics

| Event | Source topic | Destination topic |
| - | - | - |
| PostResponses | `ai/openai/responses/post` | `…/response` |
| PostEmbeddings | `ai/openai/embeddings/post` | `…/response` |
| PostChatCompletions | `ai/openai/chat/completions/post` | `…/response` |
| GetModels | `ai/openai/models/list` | `…/response` |
| GetModel | `ai/openai/models/get` | `…/response` |

## Configuration

1. Set `BASE_ADDRESS` to `https://api.openai.com`.
2. Store an OpenAI API key in Hub secrets (`OPENAI_API_KEY`); map into MQTT `api_token`.
3. For alarm classification: publish to `ai/openai/responses/post` with a Responses body using `text.format` JSON schema (or use Chat Completions with `response_format`).
4. For semantic search / similarity: publish to `ai/openai/embeddings/post` with a tiny `input` string and an embeddings model.
5. Use `GetModels` as a cheap auth smoke before production workloads.

## References

* [OpenAI API reference](https://developers.openai.com/api/reference/)
* [Responses](https://developers.openai.com/api/reference/resources/responses)
* [Embeddings](https://developers.openai.com/api/reference/resources/embeddings)
* [Chat Completions](https://developers.openai.com/api/reference/resources/chat)
* [Models](https://developers.openai.com/api/reference/resources/models)
* [Official OpenAPI](https://github.com/openai/openai-openapi)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.