@memberjunction/ai-openrouter 3.4.0 → 4.1.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.
- package/README.md +56 -149
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -17
- package/dist/index.js.map +1 -1
- package/dist/models/openRouter.d.ts +0 -1
- package/dist/models/openRouter.d.ts.map +1 -1
- package/dist/models/openRouter.js +11 -14
- package/dist/models/openRouter.js.map +1 -1
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -1,10 +1,39 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
##
|
|
29
|
+
## Features
|
|
6
30
|
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
58
|
+
console.log(result.data.choices[0].message.content);
|
|
86
59
|
```
|
|
87
60
|
|
|
88
|
-
|
|
61
|
+
## How It Works
|
|
89
62
|
|
|
90
|
-
|
|
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
|
-
|
|
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
|
-
##
|
|
76
|
+
## Class Registration
|
|
172
77
|
|
|
173
|
-
|
|
78
|
+
Registered as `OpenRouterLLM` via `@RegisterClass(BaseLLM, 'OpenRouterLLM')`.
|
|
174
79
|
|
|
175
|
-
##
|
|
80
|
+
## Dependencies
|
|
176
81
|
|
|
177
|
-
|
|
82
|
+
- `@memberjunction/ai` - Core AI abstractions
|
|
83
|
+
- `@memberjunction/ai-openai` - OpenAI provider (parent class)
|
|
84
|
+
- `@memberjunction/global` - Class registration
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './models/openRouter';
|
|
1
|
+
export * from './models/openRouter.js';
|
|
2
2
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./models/openRouter"), exports);
|
|
1
|
+
export * from './models/openRouter.js';
|
|
18
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openRouter.d.ts","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAItD;;GAEG;AACH,qBACa,aAAc,SAAQ,SAAS;gBAC5B,MAAM,EAAE,MAAM;CAG7B
|
|
1
|
+
{"version":3,"file":"openRouter.d.ts","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAItD;;GAEG;AACH,qBACa,aAAc,SAAQ,SAAS;gBAC5B,MAAM,EAAE,MAAM;CAG7B"}
|
|
@@ -1,30 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
6
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { BaseLLM } from "@memberjunction/ai";
|
|
11
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
12
|
+
import { OpenAILLM } from "@memberjunction/ai-openai";
|
|
13
13
|
const __openRouterURL = 'https://openrouter.ai/api/v1';
|
|
14
14
|
/**
|
|
15
15
|
* OpenRouter implementation is just a sub-class of the OpenAILLM that overrides the base url
|
|
16
16
|
*/
|
|
17
|
-
let OpenRouterLLM = class OpenRouterLLM extends
|
|
17
|
+
let OpenRouterLLM = class OpenRouterLLM extends OpenAILLM {
|
|
18
18
|
constructor(apiKey) {
|
|
19
19
|
super(apiKey, __openRouterURL);
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
(
|
|
22
|
+
OpenRouterLLM = __decorate([
|
|
23
|
+
RegisterClass(BaseLLM, 'OpenRouterLLM'),
|
|
24
|
+
__metadata("design:paramtypes", [String])
|
|
25
25
|
], OpenRouterLLM);
|
|
26
|
-
|
|
27
|
-
// this does nothing but prevents the class from being removed by the tree shaker
|
|
28
|
-
}
|
|
29
|
-
exports.LoadOpenRouterLLM = LoadOpenRouterLLM;
|
|
26
|
+
export { OpenRouterLLM };
|
|
30
27
|
//# sourceMappingURL=openRouter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openRouter.js","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"openRouter.js","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAEtD,MAAM,eAAe,GAAW,8BAA8B,CAAA;AAE9D;;GAEG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,SAAS;IACxC,YAAY,MAAc;QACtB,KAAK,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACnC,CAAC;CACJ,CAAA;AAJY,aAAa;IADzB,aAAa,CAAC,OAAO,EAAE,eAAe,CAAC;;GAC3B,aAAa,CAIzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-openrouter",
|
|
3
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "4.1.0",
|
|
4
5
|
"description": "MemberJunction Wrapper for OpenRouter AI inference services",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
@@ -9,19 +10,19 @@
|
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
11
12
|
"start": "ts-node-dev src/index.ts",
|
|
12
|
-
"build": "tsc",
|
|
13
|
+
"build": "tsc && tsc-alias -f",
|
|
13
14
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
15
|
},
|
|
15
16
|
"author": "MemberJunction.com",
|
|
16
17
|
"license": "ISC",
|
|
17
18
|
"devDependencies": {
|
|
18
19
|
"ts-node-dev": "^2.0.0",
|
|
19
|
-
"typescript": "^5.
|
|
20
|
+
"typescript": "^5.9.3"
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@memberjunction/ai": "
|
|
23
|
-
"@memberjunction/ai-openai": "
|
|
24
|
-
"@memberjunction/global": "
|
|
23
|
+
"@memberjunction/ai": "4.1.0",
|
|
24
|
+
"@memberjunction/ai-openai": "4.1.0",
|
|
25
|
+
"@memberjunction/global": "4.1.0"
|
|
25
26
|
},
|
|
26
27
|
"repository": {
|
|
27
28
|
"type": "git",
|