Skip to main content

needle-openai

CI Docs PyPI Python License: MIT

An OpenAI-compatible HTTP API in front of Needle 2 — Cactus Compute's 45M-parameter model for tool calling, device use and structured extraction. Point any OpenAI client at it and get function calls back from a 14MB model that runs on CPU in ~100MB of RAM.

📖 Full documentation

from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:8000/v1", api_key="not-needed")

response = client.chat.completions.create(
    model="needle-2",
    messages=[{"role": "user", "content": "what's it like in Lagos right now?"}],
    tools=[
        {
            "type": "function",
            "function": {
                "name": "get_weather",
                "description": "Get the current weather for a city.",
                "parameters": {
                    "type": "object",
                    "properties": {"city": {"type": "string"}},
                    "required": ["city"],
                },
            },
        }
    ],
)
print(response.choices[0].message.tool_calls[0].function)
# Function(arguments='{"city": "Lagos"}', name='get_weather')

[!IMPORTANT] Needle 2 is not a chat model. It emits tool calls or nothing at all — it has no free-form generation head. Read the fidelity notes before building on this; they list every place the OpenAI mapping is lossy and the two failure modes worth guarding against.

Quickstart

docker run -d -p 8000:8000 -v needle-cache:/cache ghcr.io/sirmmo/needle-openai:latest
curl -s localhost:8000/health

Or from a clone:

docker compose up -d

Or as a Python package (needs Python ≥3.10):

pip install needle-openai
pip install --no-deps cactus-needle==2.0.3   # see "Why --no-deps" in the docs
needle-openai --port 8000

First start downloads the native engine and weights from HuggingFace into /cache — a few tens of MB, ~6s on a warm connection, cached across restarts.

Runnable examples: examples/openai_client.py and examples/curl.sh.

What works

Tool calling tools / functions, parallel calls, the full agent loop.
Structured extraction response_format with a JSON Schema → typed objects.
Streaming SSE chunks, synthesized from the finished result.
Confidence gating Calibrated 0–1 score on every response via x_needle.
Tuned weights Serve a fine-tuned .cact with NEEDLE_WEIGHTS.
Auth Optional bearer token; /health stays open.

Endpoints

Endpoint Notes
POST /v1/chat/completions Tools, streaming, structured output.
POST /v1/completions Legacy text completion.
GET /v1/models, GET /v1/models/{id} Model discovery.
POST /v1/needle/extract Native extraction: text + JSON Schema → object.
POST /v1/needle/complete Passthrough returning the engine's raw response.
GET /health Readiness, queue depth, token-count mode.

Full details in the API reference.

Key things to know

A short version of the fidelity notes:

  • No free-form text. When no tool is called, the model's reasoning trace is returned as content. Asked "hello, who are you?" it replies "No tool available to query location or contact info."
  • Deterministic. temperature, top_p, seed and n are accepted, ignored, and reported in x_needle.warnings.
  • One request at a time per container. The native engine is a single global session that segfaults under concurrent use, so everything serializes through one thread. 24 concurrent requests complete correctly in ~4.2s; scale by running more containers.
  • Long inputs truncate silently and return a confident-looking wrong answer with confidence: 0.0. Gate on confidence.
  • response_format: {"type": "json_object"} returns 400 — Needle needs an explicit schema.

Development

pip install -r requirements.txt pytest httpx ruff
pytest                    # 57 tests, ~1s, no model download (fake engine)
docker compose up -d      # live suite against the real engine
NEEDLE_TEST_BASE_URL=http://127.0.0.1:8000 pytest -m live

See Development and CONTRIBUTING.md.

License

MIT — see LICENSE. Needle 2 and its weights are licensed by Cactus Compute.

Download files

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

Source Distribution

needle_openai-0.1.0.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

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

needle_openai-0.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file needle_openai-0.1.0.tar.gz.

File metadata

  • Download URL: needle_openai-0.1.0.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for needle_openai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f128fae51bbdd878437aea9dc11c97d9069fab8855dbff2adeeded7b091f0c68
MD5 3648f2a58c83e851f5361e495ae47736
BLAKE2b-256 4291be3d2bbd91a1b7215905493988d6fe8fdb9e75aad597667fb0ad7a2a2428

See more details on using hashes here.

Provenance

The following attestation bundles were made for needle_openai-0.1.0.tar.gz:

Publisher: release.yml on sirmmo/needle-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file needle_openai-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: needle_openai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for needle_openai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b386d1632602fc2772040c31eb0673dea1916902134c37611d5e1675a20bd22
MD5 75f99ae92ec862ac19de8ff9d5c0bfb7
BLAKE2b-256 60360dc8e276ceb9f7c7e58b6231b3c99fe2fddcf38bf8bb0b258032998d30b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for needle_openai-0.1.0-py3-none-any.whl:

Publisher: release.yml on sirmmo/needle-openai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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