Few-Shot Classification

Classify text using a few labeled examples per class — no training step

You send a single `text` plus an `examples` map: each key is a class label, each value is a list of example strings for that class. The service embeds all examples, builds a per-class prototype, embeds the input, and returns the predicted label, a confidence value, and a label → score map of all classes. Synchronous. Note: the response field is `confidence`, not `score`. `all_scores` is an object (label → score), not an array of pairs.

textanalysisnlp

Overview

Features

Example-based classification

Provide 2-10 labeled example texts per class. The model generalises from the examples without any training step.

Prototype scoring

Examples are embedded, averaged into class centroids, and the input is matched by similarity.

Confidence + all scores

Returns the predicted label, a confidence value (0-1), and a label→score map for every class.

Multilingual

Backed by multilingual embeddings; examples and the input can be in any supported language.

Use Cases

Custom taxonomy

Classify into domain-specific categories with 2-5 examples each (e.g. ticket intents, product types).

Intent detection

Recognise user intents with example utterances per intent.

Evolving categories

Add or remove classes at any time by changing the examples map.

Input / Output

Input

text: string + examples: { label: [example1, example2, ...] }

JSON body

Output

label, confidence, all_scores (label → score map)

JSON

Specs

Latency
~1-3 s
Async
false
Rate Limit
Per API key
Max Input
Embedding-model dependent — typical limit is a few hundred tokens per text

Quickstart

Prerequisites

  • -A CN8 Gateway API key with text-few-shot in allowed_services

1. Classify with examples

text-few-shot

POST text + an examples map. The service embeds the examples, builds per-class prototypes, and classifies the input.

POST/v1/proxy/text-few-shot
{
  "text": "The pizza was delicious",
  "examples": {
    "food": ["Loved the pasta", "I enjoyed the burger"],
    "hospitality": ["The hotel room was clean", "Great service at the bar"]
  }
}

Response

{
  "status": "success",
  "data": {
    "label": "food",
    "confidence": 0.8891,
    "all_scores": {
      "food": 0.8891,
      "hospitality": 0.1109
    }
  }
}

examples is a DICT { label: [text, ...] } — sending a list of {text, label} objects returns 400. Response field is `confidence` (not `score`).

Few-Shot Classification

POSTsync

Classify a text using a few labeled examples per class. Examples are embedded, prototypes built, input matched by similarity.

/v1/proxy/text-few-shot

Pricing

Billed per request, independent of class count or example count.

ServiceUnitPrice
Few-Shot Classificationitem$0.006/request

Guides & Tips

Important Notes (verified against the live upstream)

  • -`examples` MUST be a dict of the form `{label: [text, text, ...]}`. Sending a list of
  • -`{text, label}` objects returns a 400 with `"Input should be a valid dictionary"`.
  • -Top-level field is `confidence`, not `score` — earlier docs had this wrong.
  • -`all_scores` is a dict (label → score), not a list of pairs.
  • -Cost is per request, regardless of how many classes / examples you send.

How it works

  • -All example texts and the input text are embedded with a multilingual sentence encoder.
  • -Per class, the example embeddings are averaged into a prototype (centroid).
  • -The input embedding is compared against each prototype by cosine similarity, then softmax-normalised.
  • -The highest-scoring class becomes `label`; its score is `confidence`; every score lives in `all_scores`.

How many examples per class?

  • -Minimum 2 per class, ideally 5–10. Diverse phrasings improve generalisation.
  • -More examples increase the encoding step but the per-request price is fixed.

FAQ

Q: Can I add a third class on the fly?

A: Yes — add another key to examples in the next request. There is no persisted classifier.

Q: What if all classes look unrelated to the input?

A: The model still picks one — check `confidence`. Treat low scores (e.g. < 0.5) as 'no confident match' in your application.

Q: Cross-language examples and inputs?

A: Supported — embeddings are multilingual. Mixed-language examples are fine.

Related Products

Changelog

1.2 (2026-04-29)

  • -Aligned to live upstream: response field is `confidence`, not `score`.
  • -Fixed `all_scores` shape: object (label → score), not array of pairs.
  • -Confirmed `examples` must be a dict (label → list of strings); list-of-dicts returns 400.
  • -Removed `mode` parameter from request — not exposed on the live endpoint.

1.1 (2026-02-23)

  • -Aligned with few_shot_classifier prose (now superseded).

1.0 (2026-01-26)

  • -Initial catalog.