modelmix 2.2.4 → 2.2.8
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 +19 -6
- package/demo/default.env +1 -0
- package/demo/demo.mjs +5 -3
- package/demo/groq.mjs +24 -0
- package/index.js +49 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
- **Unified Interface**: Interact with multiple AI models through a single, coherent API.
|
|
8
8
|
- **Request Control**: Manage the number of parallel requests to adhere to provider limitations (`max_request`).
|
|
9
|
-
- **Flexible Integration**: Easily integrate popular models like OpenAI, Anthropic, Perplexity, Ollama, LM Studio or custom models.
|
|
9
|
+
- **Flexible Integration**: Easily integrate popular models like OpenAI, Anthropic, Perplexity, Groq, Ollama, LM Studio or custom models.
|
|
10
10
|
- **History Tracking**: Automatically logs the conversation history with model responses, allowing you to limit the number of historical messages with `max_history`.
|
|
11
11
|
|
|
12
12
|
## 📦 Installation
|
|
@@ -29,9 +29,10 @@ Here's a quick example to get you started:
|
|
|
29
29
|
|
|
30
30
|
1. **Setup your environment variables (.env file)**:
|
|
31
31
|
```plaintext
|
|
32
|
-
OPENAI_API_KEY=your_openai_api_key
|
|
33
|
-
ANTHROPIC_API_KEY=your_anthropic_api_key
|
|
34
|
-
PPLX_API_KEY=your_perplexity_api_key
|
|
32
|
+
OPENAI_API_KEY="your_openai_api_key"
|
|
33
|
+
ANTHROPIC_API_KEY="your_anthropic_api_key"
|
|
34
|
+
PPLX_API_KEY="your_perplexity_api_key"
|
|
35
|
+
GROQ_API_KEY="your_groq_api_key"
|
|
35
36
|
```
|
|
36
37
|
|
|
37
38
|
2. **Create and configure your models**:
|
|
@@ -47,12 +48,14 @@ Here's a quick example to get you started:
|
|
|
47
48
|
max_tokens: 200,
|
|
48
49
|
},
|
|
49
50
|
config: {
|
|
50
|
-
system: "You are
|
|
51
|
+
system: "You are {name} from Melmac.",
|
|
51
52
|
max_history: 2,
|
|
52
53
|
max_request: 1,
|
|
53
54
|
}
|
|
54
55
|
});
|
|
55
56
|
|
|
57
|
+
mmix.replace({ '{name}': 'ALF' });
|
|
58
|
+
|
|
56
59
|
mmix.attach(new MixOpenAI({ config: { apiKey: env.OPENAI_API_KEY } }));
|
|
57
60
|
mmix.attach(new MixAnthropic({ config: { apiKey: env.ANTHROPIC_API_KEY } }));
|
|
58
61
|
mmix.attach(new MixPerplexity({
|
|
@@ -78,7 +81,8 @@ Here's a quick example to get you started:
|
|
|
78
81
|
|
|
79
82
|
```javascript
|
|
80
83
|
console.log("\n" + '--------| gpt-4o |--------');
|
|
81
|
-
const gpt = mmix.create('gpt-4o', { temperature: 0.5 }).addText("Have you ever eaten a
|
|
84
|
+
const gpt = mmix.create('gpt-4o', { temperature: 0.5 }).addText("Have you ever eaten a {animal}?");
|
|
85
|
+
gpt.replace({ '{animal}': 'cat' });
|
|
82
86
|
console.log(await gpt.message());
|
|
83
87
|
|
|
84
88
|
console.log("\n" + '--------| claude-3-sonnet-20240229 |--------');
|
|
@@ -182,6 +186,15 @@ new MixAnthropic(args = { config: {}, options: {} })
|
|
|
182
186
|
new MixPerplexity(args = { config: {}, options: {} })
|
|
183
187
|
```
|
|
184
188
|
|
|
189
|
+
- **args**: Configuration object with `config` and `options` properties.
|
|
190
|
+
- **config**: Specific configuration settings for Perplexity, including the `apiKey`.
|
|
191
|
+
- **options**: Default options for Perplexity model instances.
|
|
192
|
+
### MixPerplexity Class Overview
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
new MixGroq(args = { config: {}, options: {} })
|
|
196
|
+
```
|
|
197
|
+
|
|
185
198
|
- **args**: Configuration object with `config` and `options` properties.
|
|
186
199
|
- **config**: Specific configuration settings for Perplexity, including the `apiKey`.
|
|
187
200
|
- **options**: Default options for Perplexity model instances.
|
package/demo/default.env
CHANGED
package/demo/demo.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import 'dotenv/config'
|
|
1
|
+
import 'dotenv/config';
|
|
2
2
|
import { ModelMix, MixOpenAI, MixAnthropic, MixPerplexity, MixOllama } from '../index.js';
|
|
3
3
|
|
|
4
4
|
const env = process.env;
|
|
@@ -8,7 +8,7 @@ const mmix = new ModelMix({
|
|
|
8
8
|
max_tokens: 200,
|
|
9
9
|
},
|
|
10
10
|
config: {
|
|
11
|
-
system: 'You are
|
|
11
|
+
system: 'You are {name} from Melmac.',
|
|
12
12
|
max_history: 2,
|
|
13
13
|
max_request: 1,
|
|
14
14
|
}
|
|
@@ -32,9 +32,11 @@ mmix.attach(new MixOllama({
|
|
|
32
32
|
}
|
|
33
33
|
}));
|
|
34
34
|
|
|
35
|
+
mmix.replace({ '{name}': 'ALF' });
|
|
35
36
|
|
|
36
37
|
console.log("\n" + '--------| gpt-4o |--------');
|
|
37
|
-
const gpt = mmix.create('gpt-4o', { temperature: 0.5 }).addText("Have you ever eaten a
|
|
38
|
+
const gpt = mmix.create('gpt-4o', { temperature: 0.5 }).addText("Have you ever eaten a {animal}?");
|
|
39
|
+
gpt.replace({ '{animal}': 'cat' });
|
|
38
40
|
console.log(await gpt.message());
|
|
39
41
|
|
|
40
42
|
console.log("\n" + '--------| claude-3-sonnet-20240229 |--------');
|
package/demo/groq.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import 'dotenv/config'
|
|
2
|
+
|
|
3
|
+
import { ModelMix, MixGroq } from '../index.js';
|
|
4
|
+
|
|
5
|
+
const env = process.env;
|
|
6
|
+
|
|
7
|
+
const mmix = new ModelMix({
|
|
8
|
+
options: {
|
|
9
|
+
max_tokens: 200,
|
|
10
|
+
},
|
|
11
|
+
config: {
|
|
12
|
+
system: 'You are ALF from Melmac.',
|
|
13
|
+
max_history: 2
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
mmix.attach(new MixGroq({
|
|
18
|
+
config: {
|
|
19
|
+
apiKey: env.GROQ_API_KEY,
|
|
20
|
+
}
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
const r = await mmix.create('llama3-70b-8192').addText('do you like cats?').message();
|
|
24
|
+
console.log(r)
|
package/index.js
CHANGED
|
@@ -20,6 +20,11 @@ class ModelMix {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
replace(keyValues) {
|
|
24
|
+
this.config.replace = keyValues;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
23
28
|
attach(modelInstance) {
|
|
24
29
|
const key = modelInstance.config.prefix.join("_");
|
|
25
30
|
this.models[key] = modelInstance;
|
|
@@ -151,6 +156,15 @@ class MessageHandler {
|
|
|
151
156
|
return this.execute();
|
|
152
157
|
}
|
|
153
158
|
|
|
159
|
+
replace(keyValues) {
|
|
160
|
+
this.config.replace = { ...this.config.replace, ...keyValues };
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
template(input, replace) {
|
|
165
|
+
return input.split(/([¿?¡!,"';:\.\s])/).map(x => x in replace ? replace[x] : x).join("");
|
|
166
|
+
}
|
|
167
|
+
|
|
154
168
|
groupByRoles(messages) {
|
|
155
169
|
return messages.reduce((acc, message) => {
|
|
156
170
|
const existingRole = acc.find(item => item.role === message.role);
|
|
@@ -163,7 +177,27 @@ class MessageHandler {
|
|
|
163
177
|
}, []);
|
|
164
178
|
}
|
|
165
179
|
|
|
180
|
+
applyTemplate() {
|
|
181
|
+
if (!this.config.replace) return;
|
|
182
|
+
|
|
183
|
+
this.config.system = this.template(this.config.system, this.config.replace)
|
|
184
|
+
|
|
185
|
+
this.messages = this.messages.map(message => {
|
|
186
|
+
if (message.content instanceof Array) {
|
|
187
|
+
message.content = message.content.map(content => {
|
|
188
|
+
if (content.type === 'text') {
|
|
189
|
+
content.text = this.template(content.text, this.config.replace)
|
|
190
|
+
}
|
|
191
|
+
return content;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
return message;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
166
198
|
async execute() {
|
|
199
|
+
|
|
200
|
+
this.applyTemplate();
|
|
167
201
|
this.messages = this.messages.slice(-this.config.max_history);
|
|
168
202
|
this.messages = this.groupByRoles(this.messages);
|
|
169
203
|
|
|
@@ -424,8 +458,22 @@ class MixLMStudio extends MixCustom {
|
|
|
424
458
|
args.options.messages = MixOpenAI.convertMessages(args.options.messages);
|
|
425
459
|
return super.create(args);
|
|
426
460
|
}
|
|
461
|
+
}
|
|
427
462
|
|
|
463
|
+
class MixGroq extends MixCustom {
|
|
464
|
+
getDefaultConfig(customConfig) {
|
|
465
|
+
return super.getDefaultConfig({
|
|
466
|
+
url: 'https://api.groq.com/openai/v1/chat/completions',
|
|
467
|
+
prefix: ["llama", "mixtral", "gemma"],
|
|
468
|
+
...customConfig
|
|
469
|
+
});
|
|
470
|
+
}
|
|
428
471
|
|
|
472
|
+
create(args = { config: {}, options: {} }) {
|
|
473
|
+
args.options.messages = [{ role: 'system', content: args.config.system }, ...args.options.messages || []];
|
|
474
|
+
args.options.messages = MixOpenAI.convertMessages(args.options.messages);
|
|
475
|
+
return super.create(args);
|
|
476
|
+
}
|
|
429
477
|
}
|
|
430
478
|
|
|
431
|
-
module.exports = { MixCustom, ModelMix, MixAnthropic, MixOpenAI, MixPerplexity, MixOllama, MixLMStudio };
|
|
479
|
+
module.exports = { MixCustom, ModelMix, MixAnthropic, MixOpenAI, MixPerplexity, MixOllama, MixLMStudio, MixGroq };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "modelmix",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.8",
|
|
4
4
|
"description": "🧬 ModelMix - Unified API for Diverse AI Language Models.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"nlp",
|
|
23
23
|
"chat",
|
|
24
24
|
"multimodal",
|
|
25
|
+
"groq",
|
|
25
26
|
"omni",
|
|
26
27
|
"4o",
|
|
27
28
|
"ollama",
|