🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@memberjunction/ai

Package Overview
Dependencies
Maintainers
12
Versions
447
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@memberjunction/ai

MemberJunction: AI - core components for abstracting LLMs and other AI model types that are usable anywhere without ANY other MJ dependencies past @memberjunction/global which itself has zero additional dependencies.

Source
npmnpm
Version
5.46.0
Version published
Weekly downloads
2.6K
-51.85%
Maintainers
12
Weekly downloads
 
Created
Source

Back to AI Framework Overview

@memberjunction/ai

Core abstractions and base classes for the MemberJunction AI Framework. This package defines provider-agnostic interfaces for Large Language Models, embeddings, image generation, audio, video, reranking, and more.

Zero MemberJunction dependencies beyond @memberjunction/global (which itself has no transitive dependencies). This package works in any TypeScript or JavaScript project -- no database, no metadata layer, no MJ runtime required.

Installation

npm install @memberjunction/ai

Base Classes

Every AI capability is represented by an abstract base class. Provider packages (OpenAI, Anthropic, Gemini, etc.) extend these to implement the actual API calls.

ClassPurposeKey Methods
BaseLLMChat completions (text generation)ChatCompletion(), ChatCompletions() (parallel batch), GetFileCapabilities()
BaseEmbeddingsText & multimodal embeddingsEmbedText(), EmbedTexts(), EmbedContent(), GetFileCapabilities()
BaseImageGeneratorImage generation, editing, variationsGenerateImage(), EditImage(), CreateVariation()
BaseAudioText-to-speech and speech-to-textTextToSpeech(), SpeechToText()
BaseVideoVideo generation from text/imagesGenerateVideo()
BaseRerankerDocument reranking for retrievalRerank()
BaseRealtimeModelLive, full-duplex, tool-calling realtime sessions (voice)StartSession(), CreateClientSession()

All inherit from BaseModel, which manages API key storage and provides the @RegisterClass integration point.

One additional realtime primitive lives here that is not a BaseModel capability: BaseRealtimeChannelServer — the server half of the realtime interactive-channel plugin contract (MJ: AI Agent Channels.ServerPluginClass), mirroring the client half (BaseRealtimeChannelClient in the Angular conversations package). Concrete plugins register via @RegisterClass(BaseRealtimeChannelServer, '<key>') and are resolved per session by RealtimeChannelServerHost in @memberjunction/ai-agents. See guides/REALTIME_CO_AGENTS_GUIDE.md §5.

Type Definitions

Chat Types

TypeDescription
ChatParamsFull parameter set for chat requests: messages, model, temperature, topP, topK, streaming, effort level, response format, and more
ChatResultCompletion result with choices, token usage, cost tracking, and timing
ChatMessageSingle message with role, content (text or multimodal blocks), and optional metadata
ChatMessageContentBlockMultimodal content: text, image (base64/URL), video, audio, or file
StreamingChatCallbacksCallbacks for real-time streaming: OnContent, OnComplete, OnError
ParallelChatCompletionsCallbacksCallbacks for batch parallel completions
ChatMessageRoleEnum: system, user, assistant

Embedding Types

TypeDescription
EmbedTextParams / EmbedTextResultSingle text embedding request and response
EmbedTextsParams / EmbedTextsResultBatch text embedding request and response
EmbedContentParams / EmbedContentResultMultimodal embedding request (text and/or interleaved media blocks fused into one vector) and response. EmbedContent() is non-abstract — it defaults to EmbedText for text-only content; multimodal providers override it. Supported media is declared per provider via GetFileCapabilities()

Other Types

TypeDescription
ImageGenerationParams / ImageGenerationResultImage generation parameters and results
SummarizeParams / SummarizeResultText summarization
ClassifyParams / ClassifyResultText classification
RerankParams / RerankResultDocument reranking
ModelUsageToken counts and cost tracking (prompt tokens, completion tokens, total cost, currency)
BaseResultCommon result base with success flag, timing, and error info
FileCapabilitiesDeclares which non-text inputs a provider accepts: SupportedMimeTypes (e.g. image/png, audio/mp3, supports image/* wildcards), MaxFileSize, MaxFilesPerRequest, HasFileAPI. Returned by GetFileCapabilities() on BaseLLM (file inputs to chat) and BaseEmbeddings (media inputs to EmbedContent); null means text-only

Utilities

ExportDescription
AIAPIKeys / GetAIAPIKey()API key resolution from environment variables (AI_VENDOR_API_KEY__<DRIVER>) with optional runtime overrides
ErrorAnalyzerClassifies provider errors into structured types with severity, retry hints, and failover recommendations
AIErrorInfo / AIErrorTypeStructured error types: rate limit, authentication, context length, content filter, etc.
serializeMessageContent() / deserializeMessageContent()Content block serialization for database storage
parseBase64DataUrl() / createBase64DataUrl()Base64 data URL utilities

Usage Examples

Chat Completion

import { ChatParams, ChatMessageRole } from "@memberjunction/ai";
import { OpenAILLM } from "@memberjunction/ai-openai";

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

const result = await llm.ChatCompletion({
    model: "gpt-4.1",
    messages: [
        { role: ChatMessageRole.system, content: "You are a helpful assistant." },
        { role: ChatMessageRole.user, content: "What is the capital of France?" },
    ],
    temperature: 0.7,
    maxOutputTokens: 500,
});

console.log(result.data.choices[0].message.content);

Streaming

await llm.ChatCompletion({
    model: "gpt-4.1",
    messages: [{ role: ChatMessageRole.user, content: "Explain quantum computing." }],
    streaming: true,
    streamingCallbacks: {
        OnContent: (chunk, isComplete) => process.stdout.write(chunk),
        OnComplete: (result) => console.log("\nDone!"),
        OnError: (error) => console.error("Stream error:", error),
    },
});

Parallel Completions

const paramSets = [
    { ...base, temperature: 0.3 },
    { ...base, temperature: 0.7 },
    { ...base, temperature: 1.0 },
];

const results = await llm.ChatCompletions(paramSets, {
    OnCompletion: (result, index) => console.log(`Completion ${index} done`),
    OnAllCompleted: (results) => console.log(`All ${results.length} complete`),
});

Multimodal Content

const result = await llm.ChatCompletion({
    model: "gpt-4.1",
    messages: [{
        role: ChatMessageRole.user,
        content: [
            { type: "text", content: "What is in this image?" },
            { type: "image_url", content: "data:image/png;base64,..." },
        ],
    }],
});

Text Embeddings

import { OpenAIEmbedding } from "@memberjunction/ai-openai";

const embedder = new OpenAIEmbedding("your-api-key");
const result = await embedder.EmbedText({
    model: "text-embedding-3-small",
    text: "Sample text to embed",
});
console.log(`Dimensions: ${result.vector.length}`);

API Key Resolution

import { GetAIAPIKey } from "@memberjunction/ai";

// Reads AI_VENDOR_API_KEY__OPENAILLM from environment
const key = GetAIAPIKey("OpenAILLM");

// With runtime override
const key2 = GetAIAPIKey("AnthropicLLM", [
    { driverClass: "AnthropicLLM", apiKey: "sk-ant-..." },
]);

Implementing a New Provider

Extend the base class for the capability you want to support:

import { BaseLLM, ChatParams, ChatResult, ClassifyParams, ClassifyResult, SummarizeParams, SummarizeResult } from "@memberjunction/ai";
import { RegisterClass } from "@memberjunction/global";

@RegisterClass(BaseLLM, "MyProviderLLM")
export class MyProviderLLM extends BaseLLM {
    // Required: implement non-streaming chat
    protected async nonStreamingChatCompletion(params: ChatParams): Promise<ChatResult> {
        // Your API call here
    }

    // Optional: implement text classification
    public async ClassifyText(params: ClassifyParams): Promise<ClassifyResult> { /* ... */ }

    // Optional: implement summarization
    public async SummarizeText(params: SummarizeParams): Promise<SummarizeResult> { /* ... */ }

    // Optional: enable streaming by overriding these
    public get SupportsStreaming(): boolean { return true; }
    protected async createStreamingRequest(params: ChatParams): Promise<AsyncIterable<unknown>> { /* ... */ }
    protected processStreamingChunk(chunk: unknown): { content: string } { /* ... */ }
    protected finalizeStreamingResponse(content: string, lastChunk: unknown, usage: unknown): ChatResult { /* ... */ }
}

See the full provider list for working examples across 25+ implementations.

Dependencies

PackagePurpose
@memberjunction/globalClass factory and global utilities (zero transitive dependencies)
dotenvEnvironment variable loading
rxjsReactive extensions (internal use)
  • AI Framework Overview -- Architecture, provider matrix, and quick start
  • Providers -- All 25+ provider implementations
  • Prompts -- MJ-integrated prompt template engine
  • Agents -- Agent execution framework

FAQs

Package last updated on 09 Jul 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts