Skip to main content

A library that employs multiple-choice questions to guide LLM outputs by constraining unstable generated answers to one of the predefined options.

Project description

MCQ (Multiple-Choice Question)

A Python library that constrains LLM outputs to one of the predefined options through semantic matching. MCQ ensures LLM responses are classified into expected categories even when exact string matches aren't found.

Why MCQ?

Working with LLMs often requires classifying their responses into predefined categories, but:

  • LLMs don't always respond with exact matches to expected options
  • String matching alone is fragile and error-prone
  • Semantic similarity provides more robust classification
  • Error handling becomes complex when outputs don't match expectations

MCQ solves this by transparently handling both exact and semantic matches, making LLM response classification reliable and robust.

Use Cases

  • AI Agent Memory Management: Classify new information as "new", "duplicate", or "update"
  • Decision Trees: Route LLM responses through conditional branches in pipelines
  • Intent Classification: Map varied user expressions to predefined intents
  • Response Normalization: Standardize diverse LLM outputs to consistent formats
  • Multiple-Choice Testing: Evaluate LLM performance on multiple-choice questions

Installation

pip install mcq

Usage

from mcq import Client

# Initialize with your answer options
mcq = Client(choices=["new information", "duplicate information", "updated information"])

llm_answer = "This appears to be a new piece of data I haven't seen before"

# Classify an answer
result = mcq.classify(llm_answer)
print(result)  # "new information"

# Even inexact matches are classified correctly
result = mcq.classify("I already have this in my memory")
print(result)  # "duplicate information"

How It Works

  1. Initialization: Creates embeddings for all predefined answer options using OpenAI's embedding model
  2. Classification:
    • First attempts exact string matching (case-insensitive)
    • If no match, computes the embedding for the input answer
    • Calculates cosine similarity between the answer embedding and all option embeddings
    • Returns the option with the highest similarity score

This approach ensures fast classification with no runtime delay regardless of how many options you define, as options are pre-embedded during initialization.

Requirements

  • Python 3.10+
  • OpenAI API key set as an environment variable (OPENAI_API_KEY)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcq-1.2.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcq-1.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file mcq-1.2.0.tar.gz.

File metadata

  • Download URL: mcq-1.2.0.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Darwin/24.3.0

File hashes

Hashes for mcq-1.2.0.tar.gz
Algorithm Hash digest
SHA256 78e0f13ccc4b1d6fad5d6a8b6b8749cc89318ee9983a7531a9b3f28774af1b12
MD5 7ea2103f05751b36006953cfe36c9c62
BLAKE2b-256 e8357e32536961414695914aff8dfd087f5c2d017aa5ed361cd714bea7f9d7c7

See more details on using hashes here.

File details

Details for the file mcq-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: mcq-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.12.9 Darwin/24.3.0

File hashes

Hashes for mcq-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 02956ebb4c469ec4c1baf9ca63832d4f99e7719a87d6fd3db15c8b72b835cce5
MD5 3ce9fd4e920f5ca11fc873c04b46ec99
BLAKE2b-256 8c10b3929eb71c3d571bdefee0380f6822d046a9d23e73fce7d56d13200a4c83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page