Back to docs

Recipe

Sentiment analysis with LLMs

Classifying free-text feedback into positive, negative, or neutral is one of the cleanest wins for an LLM-backed pipeline. With Meridian's azure/model-router alias you get adaptive routing across reasoning-capable models, so cheap reviews stay cheap and tricky ones get the horsepower they need. This recipe walks through a production pattern in three steps.

1. Frame the task as structured output

Ask the model for JSON, not prose. A schema with a label enum and a confidence score collapses post-processing to a single parse. Constrain the label set explicitly — three buckets is almost always enough, and adding a fourth like mixed tends to dilute the signal more than it helps.

2. Batch and cache aggressively

Send 10-50 inputs per request and hash the payload. Repeated reviews are common in real datasets and a content-addressed cache cuts spend without changing accuracy. The router will pick a small model when the batch is uniform and escalate when the inputs are linguistically diverse.

3. Evaluate against a held-out set

Hand-label 200 examples and treat them as a regression test. Re-run on every prompt change. Track precision per class, not just overall accuracy — neutral is usually the class that drifts first.

Example: classify restaurant reviews
import { Meridian } from "@meridian/sdk";

const client = new Meridian({ apiKey: process.env.MERIDIAN_API_KEY });

const reviews = [
  "The pasta was perfectly al dente and the sauce was rich.",
  "Service was slow and the bread arrived cold.",
  "Best tiramisu I have had outside of Rome — worth the wait."
];

const result = await client.chat.completions.create({
  model: "azure/model-router",
  messages: [
    {
      role: "system",
      content: "Classify each review as positive, negative, or neutral. Return JSON."
    },
    { role: "user", content: JSON.stringify(reviews) }
  ],
  response_format: { type: "json_object" }
});

console.log(result.choices[0].message.content);