MemberJunction AI Framework
A unified, provider-agnostic AI abstraction layer for TypeScript. Write your AI logic once; swap providers, models, and capabilities without changing application code.
The framework spans 47 packages covering LLMs, embeddings, image generation, audio synthesis, video generation, reranking, vector operations, agent orchestration, and protocol servers — with integrations for 25+ AI services from OpenAI and Anthropic to local inference engines like Ollama and LM Studio.
Two Ways to Use It
Section titled “Two Ways to Use It”Standalone (No MemberJunction Required)
Section titled “Standalone (No MemberJunction Required)”Install @memberjunction/ai (the Core package) plus any provider. Zero database dependencies. Works in any TypeScript or JavaScript project.
import { ChatParams, ChatMessageRole } from "@memberjunction/ai";import { AnthropicLLM } from "@memberjunction/ai-anthropic";
const llm = new AnthropicLLM(process.env.ANTHROPIC_API_KEY!);const result = await llm.ChatCompletion({ model: "claude-sonnet-4-20250514", messages: [{ role: ChatMessageRole.user, content: "Explain quantum tunneling." }],});console.log(result.data.choices[0].message.content);That is the entire setup. No configuration files, no database, no metadata — just types and a provider.
MJ-Integrated (Full Platform)
Section titled “MJ-Integrated (Full Platform)”When running inside MemberJunction, the AI framework gains metadata-driven model selection, prompt template management with hierarchical composition, cost tracking, agent orchestration, and vector synchronization — all configured through the MJ database and entity system.
import { AIPromptRunner } from "@memberjunction/ai-prompts";import { AIPromptParams } from "@memberjunction/ai-core-plus";
// Model, provider, and prompt template resolved from MJ metadataconst runner = new AIPromptRunner();const params = new AIPromptParams();params.promptName = "Summarize Document";params.data = { content: documentText };
const result = await runner.ExecutePrompt(params);Architecture
Section titled “Architecture”graph TB
subgraph Application["Application Layer"]
APP["Your Code"]
end
subgraph Core["@memberjunction/ai (Zero MJ Dependencies)"]
BM["BaseLLM"]
BE["BaseEmbeddings"]
BIG["BaseImageGenerator"]
BA["BaseAudio"]
BV["BaseVideo"]
BR["BaseReranker"]
TYPES["Chat/Embed/Image Types"]
ERR["ErrorAnalyzer"]
end
subgraph Providers["Provider Implementations"]
direction LR
P_OAI["OpenAI"]
P_ANT["Anthropic"]
P_GEM["Gemini"]
P_MIS["Mistral"]
P_GRQ["Groq"]
P_BED["Bedrock"]
P_MORE["...20+ more"]
end
subgraph MJLayer["MJ-Integrated Layer (Requires MJ Runtime)"]
ENG["AI Engine"]
PRM["Prompt Runner"]
AGT["Agent Framework"]
VEC["Vector Sync"]
MCP["MCP Server"]
A2A["A2A Server"]
end
APP --> Core
APP --> MJLayer
Core --> Providers
MJLayer --> Core
style Core fill:#1a365d,stroke:#2b6cb0,color:#fff
style Providers fill:#2d6a9f,stroke:#4299e1,color:#fff
style MJLayer fill:#553c9a,stroke:#805ad5,color:#fff
style Application fill:#2d8659,stroke:#48bb78,color:#fff
Provider Capabilities
Section titled “Provider Capabilities”Each provider implements one or more capability interfaces from @memberjunction/ai. The table below shows what each provider supports.
| Provider | npm Package | LLM | Embeddings | Image Gen | Audio | Video | Reranking |
|---|---|---|---|---|---|---|---|
| OpenAI | @memberjunction/ai-openai | x | x | x | x | ||
| Anthropic | @memberjunction/ai-anthropic | x | |||||
| Google Gemini | @memberjunction/ai-gemini | x | x | x | |||
| Mistral | @memberjunction/ai-mistral | x | x | ||||
| Groq | @memberjunction/ai-groq | x | |||||
| xAI (Grok) | @memberjunction/ai-xai | x | |||||
| Azure AI | @memberjunction/ai-azure | x | x | ||||
| Amazon Bedrock | @memberjunction/ai-bedrock | x | x | ||||
| Google Vertex | @memberjunction/ai-vertex | x | |||||
| Fireworks | @memberjunction/ai-fireworks | x | |||||
| OpenRouter | @memberjunction/ai-openrouter | x | |||||
| Cerebras | @memberjunction/ai-cerebras | x | |||||
| MiniMax | @memberjunction/ai-minimax | x | |||||
| Zhipu (Z.AI) | @memberjunction/ai-zhipu | x | |||||
| Ollama | @memberjunction/ai-ollama | x | x | ||||
| LM Studio | @memberjunction/ai-lmstudio | x | |||||
| BettyBot | @memberjunction/ai-betty-bot | x | |||||
| Black Forest Labs | @memberjunction/ai-blackforestlabs | x | |||||
| ElevenLabs | @memberjunction/ai-elevenlabs | x | |||||
| HeyGen | @memberjunction/ai-heygen | x | |||||
| Cohere | @memberjunction/ai-cohere | x | x | ||||
| Local Embeddings | @memberjunction/ai-local-embeddings | x | |||||
| Pinecone | @memberjunction/ai-vectors-pinecone | ||||||
| Rex (rasa.io) | @memberjunction/ai-recommendations-rex |
Pinecone and Rex implement vector database and recommendation interfaces respectively, not the base AI model interfaces.
Package Directory
Section titled “Package Directory”Core Abstractions
Section titled “Core Abstractions”| Package | npm | Standalone | Description |
|---|---|---|---|
| Core | @memberjunction/ai | Yes | Base classes (BaseLLM, BaseEmbeddings, BaseImageGenerator, BaseAudio, BaseVideo, BaseReranker), type definitions, error analysis, API key management |
| CorePlus | @memberjunction/ai-core-plus | Extended AI types that reference MJ entity concepts; shared by server and client | |
| BaseAIEngine | @memberjunction/ai-engine-base | Yes | Base engine class with extended types and data caching |
| Reranker | @memberjunction/ai-reranker | AI reranker service with LLM-based two-stage retrieval |
Orchestration and Engines
Section titled “Orchestration and Engines”| Package | npm | Standalone | Description |
|---|---|---|---|
| Engine | @memberjunction/aiengine | AI orchestration engine — automatic execution of Entity AI Actions using configured models | |
| Prompts | @memberjunction/ai-prompts | Prompt execution engine with hierarchical template composition, system placeholders, parallel runs, and output validation | |
| Agents | @memberjunction/ai-agents | Agent execution and management with metadata-driven agent types, sub-agent delegation, and client tool invocation for browser-side UI operations |
Agent Management
Section titled “Agent Management”| Package | npm | Standalone | Description |
|---|---|---|---|
| AgentManager/core | @memberjunction/ai-agent-manager | Core interfaces, types, and AgentSpec for agent metadata management | |
| AgentManager/actions | @memberjunction/ai-agent-manager-actions | Agent management actions (create, update, list, deactivate, export) |
Protocol Servers
Section titled “Protocol Servers”| Package | npm | Standalone | Description |
|---|---|---|---|
| MCPServer | @memberjunction/ai-mcp-server | Model Context Protocol server exposing MJ entity CRUD and agent execution to MCP-compatible clients | |
| MCPClient | @memberjunction/ai-mcp-client | MCP client for consuming external MCP tool servers | |
| A2AServer | @memberjunction/a2aserver | Agent-to-Agent (A2A) protocol server for cross-system agent interoperability |
Vector Operations
Section titled “Vector Operations”| Package | npm | Standalone | Description |
|---|---|---|---|
| Vectors/Core | @memberjunction/ai-vectors | Core vector operations and abstractions for entity vectorization | |
| Vectors/Database | @memberjunction/ai-vectordb | Vector database abstraction layer (index management, query operations) | |
| Vectors/Sync | @memberjunction/ai-vector-sync | Synchronization between MJ entities and vector databases | |
| Vectors/Dupe | @memberjunction/ai-vector-dupe | Duplicate record detection using vector similarity | |
| Vectors/Memory | @memberjunction/ai-vectors-memory | In-memory vector utilities |
Recommendations
Section titled “Recommendations”| Package | npm | Standalone | Description |
|---|---|---|---|
| Recommendations/Engine | @memberjunction/ai-recommendations | Provider-agnostic recommendation engine with run tracking and entity integration |
Computer Use
Section titled “Computer Use”| Package | npm | Standalone | Description |
|---|---|---|---|
| ComputerUse | @memberjunction/computer-use | Yes | Vision-to-action browser automation engine for LLM-driven web interactions |
| MJComputerUse | @memberjunction/computer-use-engine | MJ-integrated computer use engine with entity-backed configuration |
| Package | npm | Standalone | Description |
|---|---|---|---|
| AICLI | @memberjunction/ai-cli | AI agent, prompt, and action execution CLI integrated with the MJ CLI |
Provider Implementations
Section titled “Provider Implementations”All 25 provider packages are listed in the Providers README. The meta-package @memberjunction/ai-provider-bundle imports all standard providers to prevent tree-shaking in bundled applications.
Quick Start
Section titled “Quick Start”1. Install
Section titled “1. Install”# Core + one provider (pick any from the table above)npm install @memberjunction/ai @memberjunction/ai-openai2. Set Your API Key
Section titled “2. Set Your API Key”export AI_VENDOR_API_KEY__OPENAILLM=sk-...Or pass the key directly when constructing the provider.
3. Chat
Section titled “3. Chat”import { ChatParams, ChatMessageRole } from "@memberjunction/ai";import { OpenAILLM } from "@memberjunction/ai-openai";
const llm = new OpenAILLM(process.env.AI_VENDOR_API_KEY__OPENAILLM!);
const result = await llm.ChatCompletion({ model: "gpt-4.1", messages: [{ role: ChatMessageRole.user, content: "Hello, world!" }],});
console.log(result.data.choices[0].message.content);4. Stream
Section titled “4. Stream”await llm.ChatCompletion({ model: "gpt-4.1", messages: [{ role: ChatMessageRole.user, content: "Write a haiku about TypeScript." }], streaming: true, streamingCallbacks: { OnContent: (chunk) => process.stdout.write(chunk), OnComplete: () => console.log(), },});5. Embed
Section titled “5. Embed”import { OpenAIEmbedding } from "@memberjunction/ai-openai";
const embedder = new OpenAIEmbedding(process.env.AI_VENDOR_API_KEY__OPENAILLM!);const result = await embedder.EmbedText({ text: "Semantic search is powerful", model: "text-embedding-3-small",});console.log(`Vector dimensions: ${result.vector.length}`);Switching Providers
Section titled “Switching Providers”Because all providers implement the same BaseLLM interface, switching is a one-line change:
// Before: OpenAIimport { OpenAILLM } from "@memberjunction/ai-openai";const llm = new OpenAILLM(openaiKey);
// After: Anthropicimport { AnthropicLLM } from "@memberjunction/ai-anthropic";const llm = new AnthropicLLM(anthropicKey);
// After: Local (Ollama)import { OllamaLLM } from "@memberjunction/ai-ollama";const llm = new OllamaLLM(""); // No API key neededYour ChatCompletion() calls, streaming callbacks, and error handling remain identical.
Creating a New Provider
Section titled “Creating a New Provider”Extend the appropriate base class and register it with the class factory:
import { BaseLLM, ChatParams, ChatResult } from "@memberjunction/ai";import { RegisterClass } from "@memberjunction/global";
@RegisterClass(BaseLLM, "MyCustomLLM")export class MyCustomLLM extends BaseLLM { protected async nonStreamingChatCompletion(params: ChatParams): Promise<ChatResult> { // Call your provider API here }
// For streaming, override SupportsStreaming + streaming hooks}If your provider uses an OpenAI-compatible API, extend OpenAILLM instead and override the base URL. See the Groq or xAI providers for examples.
API Key Management
Section titled “API Key Management”The framework resolves API keys through environment variables using the convention:
AI_VENDOR_API_KEY__<DRIVER_CLASS>For example:
AI_VENDOR_API_KEY__OPENAILLMfor OpenAIAI_VENDOR_API_KEY__ANTHROPICLLMfor AnthropicAI_VENDOR_API_KEY__GEMINILLMfor Google Gemini
You can also pass keys directly to provider constructors or supply runtime overrides via GetAIAPIKey().
Error Handling
Section titled “Error Handling”All providers use a unified ErrorAnalyzer that classifies failures into structured error types:
const result = await llm.ChatCompletion(params);if (!result.success) { console.error(result.errorMessage); // result.exception contains the original error // ErrorAnalyzer provides: errorType, severity, isRetryable, failoverRecommended}Error types include rate limiting, authentication failures, context length exceeded, content filtering, and more — enabling intelligent retry and failover logic.
Further Reading
Section titled “Further Reading”- Core Package README — Full API reference for base classes, types, and utilities
- Providers README — Complete provider listing with categories
- Prompts Package — Hierarchical prompt template system
- Agents Package — Agent framework and sub-agent delegation
- MCP Server — Expose MJ capabilities via Model Context Protocol
- A2A Server — Agent-to-Agent interoperability
- Computer Use — Vision-driven browser automation
- MemberJunction Documentation — Full platform documentation
License
Section titled “License”See the MemberJunction repository root for license details.