Member Junction
    Preparing search index...

    Module @memberjunction/ai-openai - v6.1.0

    Back to AI Framework Overview | All Providers

    @memberjunction/ai-openai

    MemberJunction AI provider for OpenAI. Implements BaseLLM, BaseEmbeddings, BaseImageGenerator, and BaseAudio from @memberjunction/ai. This is the foundational LLM provider in MemberJunction -- many other providers (Groq, Cerebras, Fireworks, OpenRouter, LMStudio, xAI) extend this package since they use OpenAI-compatible APIs.

    graph TD
        A["OpenAILLM
    (Provider)"] -->|extends| B["BaseLLM
    (@memberjunction/ai)"] C["OpenAIEmbedding
    (Provider)"] -->|extends| D["BaseEmbeddings
    (@memberjunction/ai)"] A -->|wraps| E["OpenAI SDK
    (openai npm)"] C -->|wraps| E A -->|provides| F["Chat + Streaming"] A -->|provides| G["Thinking Extraction"] A -->|provides| H["JSON / Response
    Format Control"] B -->|registered via| I["@RegisterClass"] D -->|registered via| I subgraph Subclasses["OpenAI-Compatible Subclasses"] J["GroqLLM"] K["CerebrasLLM"] L["FireworksLLM"] M["OpenRouterLLM"] N["LMStudioLLM"] O["xAILLM"] end J -->|extends| A K -->|extends| A L -->|extends| A M -->|extends| A N -->|extends| A O -->|extends| A style A fill:#7c5295,stroke:#563a6b,color:#fff style C fill:#7c5295,stroke:#563a6b,color:#fff style B fill:#2d6a9f,stroke:#1a4971,color:#fff style D fill:#2d6a9f,stroke:#1a4971,color:#fff style E fill:#2d8659,stroke:#1a5c3a,color:#fff style F fill:#b8762f,stroke:#8a5722,color:#fff style G fill:#b8762f,stroke:#8a5722,color:#fff style H fill:#b8762f,stroke:#8a5722,color:#fff style I fill:#b8762f,stroke:#8a5722,color:#fff
    • Chat Completions: Full support for GPT-4.1, GPT-4o, o1, o3, o4-mini, and other OpenAI models
    • Streaming: Real-time response streaming with chunk processing
    • Thinking/Reasoning: Extraction of thinking content from reasoning model responses
    • Embeddings: Text embedding generation via text-embedding-3-small/large and other models
    • Image Generation: DALL-E integration via BaseImageGenerator
    • Audio: Text-to-speech and speech-to-text via BaseAudio
    • Multimodal Input: Support for text, image, audio, and file content in messages
    • Response Formats: JSON mode, text, and structured output controls
    • Effort Level: Maps MJ effort levels to OpenAI reasoning effort parameters
    • Error Analysis: Integrated error analysis via ErrorAnalyzer
    • Extensible Base: Designed as the foundation for any OpenAI-compatible provider
    npm install @memberjunction/ai-openai
    
    import { OpenAILLM } from "@memberjunction/ai-openai";

    const llm = new OpenAILLM("your-openai-api-key");

    const result = await llm.ChatCompletion({
    model: "gpt-4.1",
    messages: [
    { role: "system", content: "You are a helpful assistant." },
    { role: "user", content: "Explain quantum computing." },
    ],
    temperature: 0.7,
    maxOutputTokens: 1000,
    });

    if (result.success) {
    console.log(result.data.choices[0].message.content);
    }
    const result = await llm.ChatCompletion({
    model: "gpt-4.1",
    messages: [{ role: "user", content: "Write a detailed essay." }],
    streaming: true,
    streamingCallbacks: {
    OnContent: (content) => process.stdout.write(content),
    OnComplete: (result) => console.log("\nDone!"),
    },
    });
    import { OpenAIEmbedding } from "@memberjunction/ai-openai";

    const embedder = new OpenAIEmbedding("your-openai-api-key");

    const result = await embedder.EmbedText({
    text: "Sample text for embedding",
    model: "text-embedding-3-small",
    });

    console.log(`Dimensions: ${result.vector.length}`);
    Parameter Supported Notes
    temperature Yes 0.0 - 2.0
    maxOutputTokens Yes Maximum tokens to generate
    topP Yes Nucleus sampling
    frequencyPenalty Yes -2.0 to 2.0
    presencePenalty Yes -2.0 to 2.0
    seed Yes Deterministic outputs
    stopSequences Yes Custom stop sequences
    responseFormat Yes JSON, text modes
    streaming Yes Real-time streaming
    effortLevel Yes Maps to reasoning_effort
    topK No Not supported by OpenAI
    minP No Not supported by OpenAI

    This provider is designed as a base class for any OpenAI-compatible API. Override the base URL to point to a different service:

    import { OpenAILLM } from "@memberjunction/ai-openai";
    import { RegisterClass } from "@memberjunction/global";
    import { BaseLLM } from "@memberjunction/ai";
    import OpenAI from "openai";

    @RegisterClass(BaseLLM, "MyProviderLLM")
    export class MyProviderLLM extends OpenAILLM {
    constructor(apiKey: string) {
    super(apiKey);
    this._openai = new OpenAI({
    apiKey,
    baseURL: "https://api.my-provider.com/v1",
    });
    }
    }
    • OpenAILLM -- Registered via @RegisterClass(BaseLLM, OpenAILLM)
    • OpenAIEmbedding -- Registered via @RegisterClass(BaseEmbeddings, OpenAIEmbedding)
    • @memberjunction/ai - Core AI abstractions
    • @memberjunction/global - Class registration
    • openai - Official OpenAI SDK

    OpenAIRealtime / OpenAIRealtimeSession implement the OpenAI GA Realtime API wire protocol ONCE, parameterized by an OpenAIRealtimeProfile (transcription model, turn detection, config timing, live-reconfigure support, GA feature gates, effort mapping). OpenAI-compatible providers subclass this driver with their own profile instead of cloning it — xAI Grok Voice (@memberjunction/ai-xai) via the same SDK socket, and self-hosted HuggingFace (@memberjunction/ai-huggingface) via the exported RawRealtimeWebSocketConnection adapter (raw WS speaking OpenAI frames → IOpenAIRealtimeConnection, with send-buffering until open and SDK-mirroring dual error channels).

    MJ-idiomatic keys are extracted (ExtractRealtimeFeatures), translated to provider-native fields only when the profile confirms support, and always scrubbed so raw keys never reach a provider:

    Key Meaning
    effortLevel MJ-normalized effort (ChatParams.effortLevel vocabulary: numeric 1–100 or named). Mapped per provider via the profile's mapEffortLevel seam — OpenAI: quintiles over minimal/low/medium/high/xhigh (MapEffortLevelToOpenAIRealtime).
    reasoningEffort Provider-native effort literal — explicit override, wins over effortLevel.
    parallelToolCalls parallel_tool_calls.
    mcpTools Remote MCP server tools appended to session.tools. No approval UX exists yet — approval requests are AUTO-DENIED (the model voices the refusal); declare servers with require_approval: 'never'.
    inputTranscriptionModel Per-session ASR override (falls back to the profile's model; natively-transcribing profiles send no block).
    voice / disableAutoResponse Output voice / meeting-mode gating (as before).
    endpoint, sampleRate, proxyBaseUrl MJ-side transport settings — always scrubbed.

    Protected wire fields: type, instructions, and tools can never be overridden through the bag (scrubbed with a diag log); audio remains the one documented override channel. Residual provider-native keys (tool_choice, output_modalities, …) spread into the session payload identically on both topologies (server-bridged session.update and the client-direct minted SessionConfig).

    • WaitForConfigApplied() resolves once the initial config is on the socket (deferred to session.created on OpenAI). A 15s readiness deadline (configReadinessTimeoutMs, overridable) rejects awaiting callers on a silent endpoint WITHOUT cancelling the deferred apply.
    • response.done usage surfaces per-modality token detail (RealtimeUsage.InputTokenDetails/OutputTokenDetails: text/audio/image/cached) — required for multi-channel cost attribution (audio-in bills ~8× text-in on GPT Realtime 2.1).
    • Capabilities.CanReconfigureTurnMode is profile-gated (supportsLiveReconfigure); Reconfigure no-ops on profiles that declare no support.

    OpenAILiveRealtime (@RegisterClass(BaseRealtimeModel, 'OpenAILiveRealtime')) implements the server model driver for the OpenAI Live API (gpt-live-1):

    • Endpoint: https://api.openai.com/v1/realtime/calls WebRTC SDP exchange and session minting.
    • Client Session Minting: Generates client session configurations for browser WebRTC direct connection via OpenAILiveClient.
    • Wire Event Compliance: Enforces OpenAI Live wire protocol — tool outputs are delivered as response.item.create (type function_call_output), followed by response.create coordinated by RealtimeToolBatchBarrier. Does not emit conversation.item.create (which is rejected by OpenAI Live).
    • Tool Projection & Execution: Direct actions and subagent delegations (invoke-target-agent) are projected directly into the Live session tools schema, allowing the model to invoke direct tools and target agents concurrently.

    Classes

    OpenAIAudioGenerator
    OpenAIEmbedding
    OpenAIImageGenerator
    OpenAILiveRealtime
    OpenAILiveSession
    OpenAILLM
    OpenAIRealtime
    OpenAIRealtimeSession
    RawRealtimeWebSocketConnection

    Interfaces

    ILiveWebSocketLike
    IOpenAIRealtimeConnection
    LiveServerEvent
    OpenAILiveSessionOptions
    OpenAIRealtimeProfile

    Type Aliases

    LiveClientEvent
    OpenAIReasoningEffort
    RealtimeReasoningEffort

    Variables

    EmbeddingModels
    OPENAI_REALTIME_PROFILE
    OPENAI_REASONING_EFFORTS

    Functions

    ExtractRealtimeFeatures
    MapEffortLevelToOpenAIRealtime
    MapNormalizedTurnDetection
    MapUsageModalityDetail