@memberjunction/ai-openrouter 2.84.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 +177 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/models/openRouter.d.ts +9 -0
- package/dist/models/openRouter.d.ts.map +1 -0
- package/dist/models/openRouter.js +30 -0
- package/dist/models/openRouter.js.map +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
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.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @memberjunction/ai-openrouter
|
|
13
|
+
```
|
|
14
|
+
|
|
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
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Basic Text Generation
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
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
|
+
|
|
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
|
+
```
|
|
72
|
+
|
|
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'
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const languages = JSON.parse(result.data.OutputText);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Using Tools/Functions
|
|
89
|
+
|
|
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
|
+
```
|
|
116
|
+
|
|
117
|
+
## Supported Models
|
|
118
|
+
|
|
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
|
|
168
|
+
|
|
169
|
+
OpenRouter handles rate limiting across all providers. The package includes automatic retry logic for rate limit errors.
|
|
170
|
+
|
|
171
|
+
## Contributing
|
|
172
|
+
|
|
173
|
+
This package is part of the MemberJunction open-source project. Contributions are welcome!
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
MIT - See the MemberJunction repository for details.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
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);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OpenAILLM } from "@memberjunction/ai-openai";
|
|
2
|
+
/**
|
|
3
|
+
* OpenRouter implementation is just a sub-class of the OpenAILLM that overrides the base url
|
|
4
|
+
*/
|
|
5
|
+
export declare class OpenRouterLLM extends OpenAILLM {
|
|
6
|
+
constructor(apiKey: string);
|
|
7
|
+
}
|
|
8
|
+
export declare function LoadOpenRouterLLM(): void;
|
|
9
|
+
//# sourceMappingURL=openRouter.d.ts.map
|
|
@@ -0,0 +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;AAED,wBAAgB,iBAAiB,SAEhC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.LoadOpenRouterLLM = exports.OpenRouterLLM = void 0;
|
|
10
|
+
const ai_1 = require("@memberjunction/ai");
|
|
11
|
+
const global_1 = require("@memberjunction/global");
|
|
12
|
+
const ai_openai_1 = require("@memberjunction/ai-openai");
|
|
13
|
+
const __openRouterURL = 'https://openrouter.ai/api/v1';
|
|
14
|
+
/**
|
|
15
|
+
* OpenRouter implementation is just a sub-class of the OpenAILLM that overrides the base url
|
|
16
|
+
*/
|
|
17
|
+
let OpenRouterLLM = class OpenRouterLLM extends ai_openai_1.OpenAILLM {
|
|
18
|
+
constructor(apiKey) {
|
|
19
|
+
super(apiKey, __openRouterURL);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.OpenRouterLLM = OpenRouterLLM;
|
|
23
|
+
exports.OpenRouterLLM = OpenRouterLLM = __decorate([
|
|
24
|
+
(0, global_1.RegisterClass)(ai_1.BaseLLM, 'OpenRouterLLM')
|
|
25
|
+
], OpenRouterLLM);
|
|
26
|
+
function LoadOpenRouterLLM() {
|
|
27
|
+
// this does nothing but prevents the class from being removed by the tree shaker
|
|
28
|
+
}
|
|
29
|
+
exports.LoadOpenRouterLLM = LoadOpenRouterLLM;
|
|
30
|
+
//# sourceMappingURL=openRouter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openRouter.js","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAA6C;AAC7C,mDAAuD;AACvD,yDAAsD;AAEtD,MAAM,eAAe,GAAW,8BAA8B,CAAA;AAE9D;;GAEG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,qBAAS;IACxC,YAAY,MAAc;QACtB,KAAK,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACnC,CAAC;CACJ,CAAA;AAJY,sCAAa;wBAAb,aAAa;IADzB,IAAA,sBAAa,EAAC,YAAO,EAAE,eAAe,CAAC;GAC3B,aAAa,CAIzB;AAED,SAAgB,iBAAiB;IAC7B,iFAAiF;AACrF,CAAC;AAFD,8CAEC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@memberjunction/ai-openrouter",
|
|
3
|
+
"version": "2.84.0",
|
|
4
|
+
"description": "MemberJunction Wrapper for OpenRouter AI inference services",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"/dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "ts-node-dev src/index.ts",
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
|
+
},
|
|
15
|
+
"author": "MemberJunction.com",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"ts-node-dev": "^2.0.0",
|
|
19
|
+
"typescript": "^5.4.5"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@memberjunction/ai": "2.84.0",
|
|
23
|
+
"@memberjunction/ai-openai": "2.84.0",
|
|
24
|
+
"@memberjunction/global": "2.84.0"
|
|
25
|
+
}
|
|
26
|
+
}
|