npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@memberjunction/ai

v5.24.0

Published

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.

Readme

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.

| Class | Purpose | Key Methods | |-------|---------|-------------| | BaseLLM | Chat completions (text generation) | ChatCompletion(), ChatCompletions() (parallel batch) | | BaseEmbeddings | Text-to-vector embeddings | EmbedText(), EmbedTexts() | | BaseImageGenerator | Image generation, editing, variations | GenerateImage(), EditImage(), CreateVariation() | | BaseAudio | Text-to-speech and speech-to-text | TextToSpeech(), SpeechToText() | | BaseVideo | Video generation from text/images | GenerateVideo() | | BaseReranker | Document reranking for retrieval | Rerank() |

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

Type Definitions

Chat Types

| Type | Description | |------|-------------| | ChatParams | Full parameter set for chat requests: messages, model, temperature, topP, topK, streaming, effort level, response format, and more | | ChatResult | Completion result with choices, token usage, cost tracking, and timing | | ChatMessage | Single message with role, content (text or multimodal blocks), and optional metadata | | ChatMessageContentBlock | Multimodal content: text, image (base64/URL), video, audio, or file | | StreamingChatCallbacks | Callbacks for real-time streaming: OnContent, OnComplete, OnError | | ParallelChatCompletionsCallbacks | Callbacks for batch parallel completions | | ChatMessageRole | Enum: system, user, assistant |

Embedding Types

| Type | Description | |------|-------------| | EmbedTextParams / EmbedTextResult | Single text embedding request and response | | EmbedTextsParams / EmbedTextsResult | Batch text embedding request and response |

Other Types

| Type | Description | |------|-------------| | ImageGenerationParams / ImageGenerationResult | Image generation parameters and results | | SummarizeParams / SummarizeResult | Text summarization | | ClassifyParams / ClassifyResult | Text classification | | RerankParams / RerankResult | Document reranking | | ModelUsage | Token counts and cost tracking (prompt tokens, completion tokens, total cost, currency) | | BaseResult | Common result base with success flag, timing, and error info |

Utilities

| Export | Description | |--------|-------------| | AIAPIKeys / GetAIAPIKey() | API key resolution from environment variables (AI_VENDOR_API_KEY__<DRIVER>) with optional runtime overrides | | ErrorAnalyzer | Classifies provider errors into structured types with severity, retry hints, and failover recommendations | | AIErrorInfo / AIErrorType | Structured 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

| Package | Purpose | |---------|---------| | @memberjunction/global | Class factory and global utilities (zero transitive dependencies) | | dotenv | Environment variable loading | | rxjs | Reactive extensions (internal use) |

Related Packages