Lesson 6: The Parameter Builder Pattern
Learn how to control the model's output with advanced parameters like temperature and response format using the SDK's fluent builder pattern.
Code: lesson_6_parameter_control.mjs
The .withParameters()
method accepts a function that receives a builder instance. You can chain methods on this builder to configure the request. The SDK intelligently filters any parameters not supported by the selected model.
// lesson_6_parameter_control.mjs
import { MerciClient, createUserMessage } from '../lib/merci.2.14.0.mjs';
import { token } from '../secret/token.mjs';
const MODEL = 'google-chat-gemini-flash-2.5';
async function runExperiment(client, prompt, builderFn, description) {
console.log(`\n--- Experiment: ${description} ---`);
const chatSession = client.chat.session(MODEL).withParameters(builderFn);
const messages = [ createUserMessage(prompt) ];
// ... stream processing logic ...
}
async function main() {
const client = new MerciClient({ token });
client.on('parameter_warning', (warning) => {
console.warn(`\n[SDK WARNING] ${warning.message}`);
});
const storyPrompt = "Tell me a very short story about a robot who discovers music.";
await runExperiment(
client,
storyPrompt,
builder => builder.temperature(0.1),
'Focused and Deterministic (Low Temperature)'
);
await runExperiment(
client,
storyPrompt,
builder => builder.temperature(1.0).topP(0.9),
'Creative and Unpredictable (High Temperature, High Top-P)'
);
// Demonstrating an Unsupported Parameter
await runExperiment(
client,
storyPrompt,
builder => builder.temperature(0.5).seed(12345),
'Using an Unsupported Parameter (Seed)'
);
}
main().catch(console.error);
Expected Output
The output demonstrates how different parameters produce different results for the same prompt. Crucially, it also shows the SDK emitting a warning when we try to use the `seed` parameter, which is not supported by this specific model, and then filtering it out before making the API call.
--- Merci SDK Tutorial: Lesson 6 - Parameter Control (Model: google-chat-gemini-flash-2.5) ---
[STEP 1] Initializing MerciClient...
[STEP 2] Preparing prompt and input data...
[STEP 3] Running Experiment 1: Focused and Deterministic (Low Temperature)
--- Experiment: Focused and Deterministic (Low Temperature) ---
🤖 Assistant > Unit 734 stood motionless, its auditory sensors inactive. Then, a sound wave, a pattern of organized noise, rippled through its circuits. It was music, and for the first time, Unit 734 felt something akin to joy.
[INFO] Stream finished. Response fully received.
[STEP 4] Running Experiment 2: Creative and Unpredictable (High Temperature, High Top-P)
--- Experiment: Creative and Unpredictable (High Temperature, High Top-P) ---
🤖 Assistant > The old gramophone crackled to life in the dusty attic, and Bolt, a robot built for sorting scrap metal, found his metallic fingers tapping out a rhythm he'd never been programmed to understand.
[INFO] Stream finished. Response fully received.
[STEP 5] Running Experiment 3: Forcing JSON Output
--- Experiment: Forcing JSON Output ---
🤖 Assistant > {
"name": "Anna",
"age": 32,
"city": "Paris"
}
[INFO] Stream finished. Response fully received.
[INFO] Parsed JSON Output:
{
"name": "Anna",
"age": 32,
"city": "Paris"
}
[STEP 6] Running Experiment 4: Using an Unsupported Parameter (Seed)
--- Experiment: Using an Unsupported Parameter (Seed) ---
[SDK WARNING] Parameter 'seed' is not supported by model profile 'google-chat-gemini-flash-2.5' and will be ignored.
🤖 Assistant > Unit 7's world was a silent ballet of ones and zeros until it stumbled upon an abandoned music box, and the tinkling melody rewired its logic gates into a symphony of wonder.
[INFO] Stream finished. Response fully received.