Loading...
Loading...
Instructions for using the ModelMix Node.js library to interact with multiple AI LLM providers through a unified interface. Use when integrating AI models (OpenAI, Anthropic, Google, Groq, Perplexity, Grok, etc.), chaining models with fallback, getting structured JSON from LLMs, adding MCP tools, streaming responses, or managing multi-provider AI workflows in Node.js.
npx skill4agent add clasen/modelmix modelmixmessage()json()lastRawnpm install modelmiximport { ModelMix } from 'modelmix';// Static factory (preferred)
const model = ModelMix.new();
// With global options
const model = ModelMix.new({
options: { max_tokens: 4096, temperature: 0.7 },
config: {
system: "You are a helpful assistant.",
max_history: 5,
debug: 0, // 0=silent, 1=minimal, 2=summary, 3=full (no truncate), 4=verbose
roundRobin: false // false=fallback, true=rotate models
}
});const model = ModelMix.new()
.sonnet46() // primary
.gpt52() // fallback 1
.gemini3flash() // fallback 2
.addText("Hello!")sonnet45gpt5minigemini3flashgpt52gpt51gpt5gpt5minigpt5nanogpt41gpt41minigpt41nanoopus46opus45sonnet46sonnet45haiku45haiku35thinkgemini3progemini3flashgemini25progemini25flashgrok4grok41sonarsonarProscoutmaverickqwen3kimiK2deepseekR1gptOssminimaxM21deepseekV32GLM47mix.methodName(){ options, config }const answer = await ModelMix.new()
.gpt5mini()
.addText("What is the capital of France?")
.message();const result = await ModelMix.new()
.gpt5mini()
.addText("Name and capital of 3 South American countries.")
.json(
{ countries: [{ name: "", capital: "" }] }, // schema example
{ countries: [{ name: "country name", capital: "in uppercase" }] }, // descriptions
{ addNote: true } // options
);
// result.countries → [{ name: "Brazil", capital: "BRASILIA" }, ...]json()json(schemaExample, schemaDescription?, { addSchema, addExample, addNote }?)const result = await model.json(
{ name: 'martin', age: 22, sex: 'Male' },
{
name: { description: 'Name of the actor', required: false },
age: 'Age of the actor', // string still works
sex: { description: 'Gender', enum: ['Male', 'Female', null] }
}
);descriptionrequiredtruefalseenumnulldefault{ out: [...] }const result = await model.json([{ name: 'martin' }]);
// result is an array: [{ name: "Martin" }, { name: "Carlos" }, ...]await ModelMix.new()
.gpt5mini()
.addText("Tell me a story.")
.stream(({ delta, message }) => {
process.stdout.write(delta);
});const raw = await ModelMix.new()
.sonnet45think()
.addText("Solve this step by step: 2+2*3")
.raw();
// raw.message, raw.think, raw.tokens, raw.toolCalls, raw.responsemessage()json()lastRawmessage()json()block()stream()lastRawraw()const model = ModelMix.new().gpt5mini().addText("Hello!");
const text = await model.message();
console.log(model.lastRaw.tokens);
// { input: 122, output: 86, total: 541, cost: 0.000319 }
console.log(model.lastRaw.think); // reasoning content (if available)
console.log(model.lastRaw.response); // raw API responseconst model = ModelMix.new().sonnet45();
model.addImage('./photo.jpg'); // from file
model.addImageFromUrl('https://example.com/img.png'); // from URL
model.addText('Describe this image.');
const description = await model.message();const model = ModelMix.new().gpt5mini();
model.setSystemFromFile('./prompts/system.md');
model.addTextFromFile('./prompts/task.md');
model.replace({
'{role}': 'data analyst',
'{language}': 'Spanish'
});
model.replaceKeyFromFile('{code}', './src/utils.js');
console.log(await model.message());const pool = ModelMix.new({ config: { roundRobin: true } })
.gpt5mini()
.sonnet45()
.gemini3flash();
// Each call rotates to the next model
const r1 = await pool.new().addText("Request 1").message();
const r2 = await pool.new().addText("Request 2").message();const model = ModelMix.new({ config: { max_history: 10 } }).gpt5nano();
model.setSystem('You are an assistant. Today is ' + new Date().toISOString());
await model.addMCP('@modelcontextprotocol/server-brave-search');
model.addText('Use Internet: What is the latest news about AI?');
console.log(await model.message());BRAVE_API_KEY.envconst model = ModelMix.new({ config: { max_history: 10 } }).gpt5mini();
model.addTool({
name: "get_weather",
description: "Get weather for a city",
inputSchema: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"]
}
}, async ({ city }) => {
return `The weather in ${city} is sunny, 25C`;
});
model.addText("What's the weather in Tokyo?");
console.log(await model.message());const model = ModelMix.new({
config: {
bottleneck: {
maxConcurrent: 4,
minTime: 1000
}
}
}).gpt5mini();const model = ModelMix.new({
config: { debug: 2 } // 0=silent, 1=minimal, 2=summary, 3=full (no truncate), 4=verbose
}).gpt5mini();DEBUG=ModelMix* node script.js// These use providers with free quotas (OpenRouter, Groq, Cerebras)
const model = ModelMix.new()
.gptOss()
.kimiK2()
.deepseekR1()
.hermes3()
.addText("What is the capital of France?");
console.log(await model.message());const chat = ModelMix.new({ config: { max_history: 10 } }).gpt5mini();
chat.addText("My name is Martin.");
await chat.message();
chat.addText("What's my name?");
const reply = await chat.message(); // "Martin"package.jsonmodelmixnpm installModelMix.new()new ModelMix().envdotenv/configprocess.loadEnvFile()addTool()max_history.json(){ description, required, enum, default }.message().raw()thinksonnet45think(){key}require{ ModelMix }MixOpenAIMixAnthropicMixGoogleMixPerplexityMixGroqMixTogetherMixGrokMixOpenRouterMixOllamaMixLMStudioMixCustomMixCerebrasMixFireworksMixMiniMax| Method | Returns | Description |
|---|---|---|
| | Add user message |
| | Add user message from file |
| | Set system prompt |
| | Set system prompt from file |
| | Add image from file |
| | Add image from URL or data URI |
| | Set placeholder replacements |
| | Replace placeholder with file content |
| | Get text response |
| | Get structured JSON. Descriptions support descriptor objects |
| | Full response |
| | Full response from last |
| | Stream response |
| | Extract code block from response |
| | Add MCP server tools |
| | Register custom local tool |
| | Register multiple tools |
| | Remove a tool |
| | List registered tools |
| | Clone instance sharing models |
| | Attach custom provider |