Schema-aware function calling with logits-based selection and constrained JSON generation.
This project was created as part of the 42 curriculum by wehan.
Call Me Maybe is a function-calling system that converts natural language prompts into structured JSON function calls using a small language model.
The project combines:
- LLM-based function selection
- schema-driven parameter extraction
- constrained decoding
- state-machine JSON generation
- JSON validation
to produce valid structured output.
Example:
{
"prompt": "What is the sum of 2 and 3?",
"name": "fn_add_numbers",
"parameters": {
"a": 2.0,
"b": 3.0
}
}- LLM logits-based function selection
- schema-aware argument extraction
- constrained JSON decoding
- deterministic state-machine generation
- valid JSON output
- graceful error handling
- support for:
- numbers
- integers
- booleans
- strings
- enums
- regex-based substitutions
.
├── data/
│ └── input/
│ ├── function_calling_tests.json
│ └── functions_definition.json
│
├── llm_sdk/
│ └── ...
│
├── src/
│ ├── __init__.py
│ ├── __main__.py
│ ├── decoder.py
│ ├── extracter.py
│ ├── main.py
│ ├── models.py
│ ├── parser.py
│ ├── selector.py
│ └── utils.py
│
├── .gitignore
├── Makefile
├── pyproject.toml
├── README.md
└── uv.lock
Install dependencies with:
uv syncThe llm_sdk/ directory must exist at the repository root.
Run with default paths:
uv run python -m srcRun with custom files:
uv run python -m src \
--functions_definition data/input/functions_definition.json \
--input data/input/function_calling_tests.json \
--output data/output/function_calling_results.jsonThe selector builds a prompt containing:
- available functions
- descriptions
- user request
The model performs one forward pass and scores candidate functions using logits from the first distinguishing token after the shared fn prefix.
Softmax converts scores into probabilities.
Low-confidence prompts return:
{
"name": "",
"parameters": {}
}Arguments are extracted according to the selected schema.
Supported parameter types:
numberintegerstringbooleanenum
Regex-based parsing is used for:
- numbers
- quoted strings
- substitutions
- booleans
- regex patterns
JSON generation is guided by a deterministic state machine.
Only tokens matching the expected JSON fragment are allowed during decoding.
Token filtering is constrained by deterministic expected-text generation rather than free-form sampling.
This produces structurally valid JSON output.
Generated outputs are validated with:
json.loads()Pydantic
and written using:
json.dump()
The implementation is optimized for runtime stability using:
- one forward pass per prompt
- first-token function scoring
- valid-token caching
- regex extraction
Accuracy: 11/11 test cases passing (100%)
- Function selection: 100%
- Number extraction: 100%
- String extraction: 100%
- Regex generation: 100%
- JSON validity: 100%
Complexity:
- Linear in input size
- Single model load
- Lightweight per-prompt inference
Reliability:
- Structurally valid JSON output via deterministic state-machine generation and constrained token selection.
Key implementation choices include:
- using logits-based function selection instead of prompt-only matching
- combining deterministic state-machine generation with constrained token filtering
- using schema-aware parameter extraction for different parameter types
- validating structured outputs with Pydantic and json.loads()
Main implementation challenges included:
- maintaining valid JSON structure during constrained decoding
- handling tokenizer vocabulary boundaries during token filtering
- balancing deterministic generation with model-based scoring
Testing with:
- arithmetic prompts
- negative and decimal numbers
- regex substitutions
- enum values
- booleans
- empty strings
- malformed files
- ambiguous prompts
Run linting:
make lintRun strict linting:
make lint-strictmake install
make run
make debug
make clean
make lint
make lint-strictAI tools were used for:
- architecture discussion
- edge-case analysis
- README refinement
- testing ideas
All code was manually reviewed, adapted, and understood by the author.
- LLM Introduction (CN): https://www.bilibili.com/video/BV11nSjB2ErQ/
- ML Introduction (CN): https://www.bilibili.com/video/BV1SjDsBLEzN/
- Logits: https://en.wikipedia.org/wiki/Logit
- UTF-8: https://en.wikipedia.org/wiki/UTF-8
- Softmax: https://en.wikipedia.org/wiki/Softmax_function
If uv sync fails because of limited /home storage, redirect the uv cache and virtual environment to /goinfre:
export UV_CACHE_DIR=/goinfre/$USER/uv_cache
export HF_HOME=/goinfre/$USER/hf_cache
export PIP_CACHE_DIR=/goinfre/$USER/pip_cache
export TMPDIR=/goinfre/$USER/tmp
export UV_PROJECT_ENVIRONMENT=/goinfre/$USER/venvs/python-callmemaybe
export UV_LINK_MODE=copy
uv sync --python 3.11Project code is available under the MIT License. Third-party assets retain their original licenses and attribution requirements; this license does not relicense third-party media, datasets, model weights, or trademarks.