@memberjunction/ai-openrouter 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +56 -149
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,10 +1,39 @@
1
- # OpenRouter Provider for MemberJunction
2
-
3
- This package provides integration with OpenRouter's API for accessing multiple AI models through a unified interface within the MemberJunction framework.
1
+ # @memberjunction/ai-openrouter
2
+
3
+ MemberJunction AI provider for OpenRouter, a unified API gateway that provides access to 100+ AI models from multiple providers (OpenAI, Anthropic, Google, Meta, Mistral, and more) through a single endpoint.
4
+
5
+ ## Architecture
6
+
7
+ ```mermaid
8
+ graph TD
9
+ A["OpenRouterLLM<br/>(Provider)"] -->|extends| B["OpenAILLM<br/>(@memberjunction/ai-openai)"]
10
+ B -->|extends| C["BaseLLM<br/>(@memberjunction/ai)"]
11
+ A -->|overrides base URL| D["OpenRouter API<br/>(openrouter.ai/api/v1)"]
12
+ D -->|routes to| E["OpenAI"]
13
+ D -->|routes to| F["Anthropic"]
14
+ D -->|routes to| G["Google"]
15
+ D -->|routes to| H["Meta / Mistral / Others"]
16
+ C -->|registered via| I["@RegisterClass"]
17
+
18
+ style A fill:#7c5295,stroke:#563a6b,color:#fff
19
+ style B fill:#2d6a9f,stroke:#1a4971,color:#fff
20
+ style C fill:#2d6a9f,stroke:#1a4971,color:#fff
21
+ style D fill:#2d8659,stroke:#1a5c3a,color:#fff
22
+ style E fill:#b8762f,stroke:#8a5722,color:#fff
23
+ style F fill:#b8762f,stroke:#8a5722,color:#fff
24
+ style G fill:#b8762f,stroke:#8a5722,color:#fff
25
+ style H fill:#b8762f,stroke:#8a5722,color:#fff
26
+ style I fill:#b8762f,stroke:#8a5722,color:#fff
27
+ ```
4
28
 
5
- ## Overview
29
+ ## Features
6
30
 
7
- The OpenRouter provider implements MemberJunction's AI framework interfaces, allowing seamless access to 100+ AI models from various providers (OpenAI, Anthropic, Google, Meta, etc.) through OpenRouter's unified API.
31
+ - **Multi-Model Access**: Access models from OpenAI, Anthropic, Google, Meta, Mistral, xAI, and many more
32
+ - **Automatic Routing**: OpenRouter handles provider selection and failover
33
+ - **OpenAI Compatible**: Inherits all features from the OpenAI provider
34
+ - **Streaming**: Full streaming support for real-time responses
35
+ - **Thinking/Reasoning**: Thinking block extraction for reasoning models
36
+ - **Response Formats**: JSON mode support for compatible models
8
37
 
9
38
  ## Installation
10
39
 
@@ -12,166 +41,44 @@ The OpenRouter provider implements MemberJunction's AI framework interfaces, all
12
41
  npm install @memberjunction/ai-openrouter
13
42
  ```
14
43
 
15
- ## Configuration
16
-
17
- Configure the OpenRouter provider in your MemberJunction setup:
18
-
19
- ```typescript
20
- import { OpenRouterLLM } from '@memberjunction/ai-openrouter';
21
-
22
- // The provider will automatically use the API key from AIModelAPIConnection
23
- // configuration in the database
24
- ```
25
-
26
- ## Features
27
-
28
- - **Multi-Model Support**: Access models from OpenAI, Anthropic, Google, Meta, Mistral, and more
29
- - **Automatic Routing**: OpenRouter automatically routes requests to the best available provider
30
- - **Response Format Support**: JSON mode support for compatible models
31
- - **Streaming**: Full streaming support for real-time responses
32
- - **Tool/Function Calling**: Support for models with function calling capabilities
33
- - **Comprehensive Error Handling**: Detailed error messages and retry logic
34
-
35
44
  ## Usage
36
45
 
37
- ### Basic Text Generation
38
-
39
46
  ```typescript
40
47
  import { OpenRouterLLM } from '@memberjunction/ai-openrouter';
41
- import { GetAIAPIKey } from '@memberjunction/ai';
42
-
43
- const llm = new OpenRouterLLM();
44
- const apiKey = await GetAIAPIKey('OpenRouter');
45
-
46
- const result = await llm.InvokeModel({
47
- apiKey,
48
- modelName: 'anthropic/claude-3.5-sonnet',
49
- messages: [
50
- { role: 'user', content: 'Explain quantum computing in simple terms' }
51
- ]
52
- });
53
-
54
- console.log(result.data.OutputText);
55
- ```
56
-
57
- ### Streaming Responses
58
48
 
59
- ```typescript
60
- const result = await llm.InvokeModel({
61
- apiKey,
62
- modelName: 'openai/gpt-4-turbo',
63
- messages: [
64
- { role: 'user', content: 'Write a story about a robot' }
65
- ],
66
- streaming: true,
67
- streamCallback: (token) => {
68
- process.stdout.write(token);
69
- }
70
- });
71
- ```
49
+ const llm = new OpenRouterLLM('your-openrouter-api-key');
72
50
 
73
- ### JSON Response Format
74
-
75
- ```typescript
76
- const result = await llm.InvokeModel({
77
- apiKey,
78
- modelName: 'anthropic/claude-3.5-sonnet',
79
- messages: [
80
- { role: 'user', content: 'List 3 programming languages as JSON' }
81
- ],
82
- responseFormat: 'json_object'
51
+ const result = await llm.ChatCompletion({
52
+ model: 'anthropic/claude-sonnet-4-20250514',
53
+ messages: [
54
+ { role: 'user', content: 'Explain quantum computing in simple terms.' }
55
+ ]
83
56
  });
84
57
 
85
- const languages = JSON.parse(result.data.OutputText);
58
+ console.log(result.data.choices[0].message.content);
86
59
  ```
87
60
 
88
- ### Using Tools/Functions
61
+ ## How It Works
89
62
 
90
- ```typescript
91
- const tools = [{
92
- type: 'function',
93
- function: {
94
- name: 'get_weather',
95
- description: 'Get the current weather',
96
- parameters: {
97
- type: 'object',
98
- properties: {
99
- location: { type: 'string', description: 'City name' }
100
- },
101
- required: ['location']
102
- }
103
- }
104
- }];
105
-
106
- const result = await llm.InvokeModel({
107
- apiKey,
108
- modelName: 'openai/gpt-4-turbo',
109
- messages: [
110
- { role: 'user', content: 'What\'s the weather in New York?' }
111
- ],
112
- tools,
113
- toolChoice: 'auto'
114
- });
115
- ```
63
+ `OpenRouterLLM` is a thin subclass of `OpenAILLM` that redirects all API calls to OpenRouter's endpoint at `https://openrouter.ai/api/v1`. Since OpenRouter implements an OpenAI-compatible API, all chat, streaming, and parameter handling logic is inherited from the OpenAI provider.
116
64
 
117
65
  ## Supported Models
118
66
 
119
- OpenRouter provides access to models from:
120
-
121
- - **OpenAI**: GPT-4, GPT-3.5, o1-preview, o1-mini
122
- - **Anthropic**: Claude 3.5 Sonnet, Claude 3 Opus/Haiku
123
- - **Google**: Gemini Pro, Gemini Flash
124
- - **Meta**: Llama 3.1 (8B, 70B, 405B)
125
- - **Mistral**: Mistral Large, Mixtral
126
- - **xAI**: Grok
127
- - **And many more...**
128
-
129
- See [OpenRouter's model list](https://openrouter.ai/models) for complete details.
130
-
131
- ## Model Parameters
132
-
133
- The following parameters can be configured:
134
-
135
- - `temperature`: Controls randomness (0.0 to 2.0)
136
- - `maxTokens`: Maximum tokens to generate
137
- - `topP`: Nucleus sampling parameter
138
- - `topK`: Top-k sampling parameter
139
- - `frequencyPenalty`: Penalize frequent tokens
140
- - `presencePenalty`: Penalize tokens based on presence
141
- - `stopSequences`: Sequences that stop generation
142
- - `seed`: For deterministic outputs (when supported)
143
-
144
- ## Error Handling
145
-
146
- The provider includes comprehensive error handling:
147
-
148
- ```typescript
149
- try {
150
- const result = await llm.InvokeModel({...});
151
- if (!result.success) {
152
- console.error('Model invocation failed:', result.errorMessage);
153
- }
154
- } catch (error) {
155
- console.error('Unexpected error:', error);
156
- }
157
- ```
158
-
159
- ## API Key Management
160
-
161
- API keys are managed through MemberJunction's AIModelAPIConnection system. Configure your OpenRouter API key in the database:
162
-
163
- 1. Add an AIModelAPIConnection record for OpenRouter
164
- 2. Set the APIKey field to your OpenRouter API key
165
- 3. The provider will automatically retrieve and use this key
166
-
167
- ## Rate Limiting
67
+ OpenRouter provides access to models from many providers. Use the provider-prefixed model names:
168
68
 
169
- OpenRouter handles rate limiting across all providers. The package includes automatic retry logic for rate limit errors.
69
+ - `openai/gpt-4`, `openai/gpt-4-turbo`
70
+ - `anthropic/claude-sonnet-4-20250514`, `anthropic/claude-3-opus`
71
+ - `google/gemini-pro`, `google/gemini-flash`
72
+ - `meta-llama/llama-3.1-70b`
73
+ - `mistralai/mistral-large`
74
+ - And 100+ more at [openrouter.ai/models](https://openrouter.ai/models)
170
75
 
171
- ## Contributing
76
+ ## Class Registration
172
77
 
173
- This package is part of the MemberJunction open-source project. Contributions are welcome!
78
+ Registered as `OpenRouterLLM` via `@RegisterClass(BaseLLM, 'OpenRouterLLM')`.
174
79
 
175
- ## License
80
+ ## Dependencies
176
81
 
177
- MIT - See the MemberJunction repository for details.
82
+ - `@memberjunction/ai` - Core AI abstractions
83
+ - `@memberjunction/ai-openai` - OpenAI provider (parent class)
84
+ - `@memberjunction/global` - Class registration
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@memberjunction/ai-openrouter",
3
3
  "type": "module",
4
- "version": "4.0.0",
4
+ "version": "4.2.0",
5
5
  "description": "MemberJunction Wrapper for OpenRouter AI inference services",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -20,9 +20,9 @@
20
20
  "typescript": "^5.9.3"
21
21
  },
22
22
  "dependencies": {
23
- "@memberjunction/ai": "4.0.0",
24
- "@memberjunction/ai-openai": "4.0.0",
25
- "@memberjunction/global": "4.0.0"
23
+ "@memberjunction/ai": "4.2.0",
24
+ "@memberjunction/ai-openai": "4.2.0",
25
+ "@memberjunction/global": "4.2.0"
26
26
  },
27
27
  "repository": {
28
28
  "type": "git",