Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cactus/_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ def close(self):
if self._process.poll() is None:
try:
self._send({"operation": "close"})
self._receive(2)
except BaseException:
pass
if self._process.stdin is not None:
Expand Down
18 changes: 14 additions & 4 deletions cactus/agent/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ def __del__(self):
pass


_shared = {}


@synchronized
def extract(text: str, schema: type | dict, system: str | None = None,
max_new_tokens: int = 512, weights: str | None = None,
strict: bool = True, generation: int | None = None) -> object:
Expand All @@ -339,14 +343,20 @@ def extract(text: str, schema: type | dict, system: str | None = None,
generation = _weight_generation(selected) if selected else int(generation or 3)
_track("extract", {"n_tools": 1, "tuned": bool(selected),
"generation": generation})
agent = Cactus(tools=[schema], system=system, weights=selected, generation=generation)


system = _with_date_fact(system or "")
schema_id = json.dumps(schema, sort_keys=True) if isinstance(schema, dict) else schema
key = (schema_id, system, selected, generation)
if key not in _shared:
for previous in _shared.values():
previous.close()
_shared.clear()
_shared[key] = Cactus(tools=[schema], system=system, weights=selected, generation=generation)
agent = _shared[key]
facts = agent._system_text
try:
response = agent._complete(text, max_new_tokens)
finally:
agent.close()
agent.reset()
calls = response.get("function_calls") or response.get("suppressed_calls") or []
if not calls:
return None
Expand Down
2 changes: 1 addition & 1 deletion llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Engine binaries come from `Cactus-Compute/cactus`. Needle, Whistle and Mirage we
- `agent.reset()` - rewind the conversation, keep the tools loaded.
- `cactus.tool` - decorator that turns a function into a tool schema (attached as `fn._cactus_tool`); `@cactus.tool(triggers=[r"\b(turn|switch)\b.*\b(on|off)\b"])` adds case-insensitive regexes that route a matching request to this tool and require a call; a triggered call ships even below the confidence floor (raw schemas take the same `"triggers"` list beside `description`).
- `cactus.Field(default=..., *, description, enum, const, ge, le, gt, lt, multiple_of, min_length, max_length, pattern, format, min_items, max_items, unique_items)` - per-argument constraints; attach inline with `typing.Annotated`.
- `cactus.extract(text, schema, system=None, max_new_tokens=512, weights=None, strict=True, generation=None)` - one-shot extraction; returns a Pydantic instance if `schema` is a model, else a dict (or `None` if nothing matched). `weights` loads a tuned `.cact`; without it the base Needle 3 model runs. `strict=True` raises `cactus.ExtractionValidationError` instead of returning a date whose year contradicts the text or a value the engine flags as invented or negated.
- `cactus.extract(text, schema, system=None, max_new_tokens=512, weights=None, strict=True, generation=None)` - one-shot extraction; returns a Pydantic instance if `schema` is a model, else a dict (or `None` if nothing matched). `weights` loads a tuned `.cact`; without it the base Needle 3 model runs. `strict=True` raises `cactus.ExtractionValidationError` instead of returning a date whose year contradicts the text or a value the engine flags as invented or negated. Calls with the same schema and settings reuse one agent, so batches skip the setup.
- `cactus.transcribe(audio, language=None, keywords=None, word_timestamps=False, weights=None) -> dict` - speech to text in English, German, French, Spanish, Italian, Dutch and Polish, on the same engine and the same install as Needle. `audio` is a WAV path or 16 kHz mono float samples in [-1, 1] of any length, one pass up to 30 s and windowed beyond it. Returns `{"text", "language", "ttft_ms", "decode_tps"}`, plus `"words"` with `start`, `end` and `probability` when `word_timestamps` is set. `language` is one of `en de fr es it nl pl`, or `None` to detect it; `keywords` is a list or a newline-separated string of words and phrases to favour. Silence and steady noise return an empty text and language; other sounds, music especially, can still come out as text, so put a voice activity detector or a wake word in front of an always-on microphone. 16 kHz WAV files and raw samples need nothing extra; other sample rates need the `[mic]` extra for soxr. `whistle.cact` is fetched from `Cactus-Compute/whistle` on first use.
- `cactus.stream(chunks, language=None, keywords=None, weights=None)` - live transcription with no limit on length. `chunks` is an iterable of 16 kHz mono float sample buffers, about a second each; the generator yields one dict per chunk with the `"text"` and `"words"` committed by it (times from the start of the stream), the unconfirmed `"pending"` tail, the `"language"`, the seconds `"received"` and `"pass_ms"`, then one more for the tail when the chunks end. Join the texts with a space. Every chunk re-transcribes the last seconds and commits what two passes agree on.
- `cactus.set_threads(threads)` - the CPU thread count for every model loaded after the call, tuned agents included, or `0` (the default) to use the cores the process may run on.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_grounding.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def stub(monkeypatch, tmp_path):
monkeypatch.setattr(runtime, "_base_weights_path", lambda generation: str(base))
monkeypatch.setattr(runtime, "_active", {})
monkeypatch.setattr(runtime, "_loaded_base", {})
monkeypatch.setattr("cactus.agent.text._shared", {})
return engine


Expand Down Expand Up @@ -144,6 +145,16 @@ def test_extract_accepts_a_date_written_with_slashes(stub):
assert invoice.due_date == datetime.date(2024, 12, 31)


def test_extract_reuses_its_agent_across_calls(stub):
import cactus

stub.envelopes = [_call("2024-12-31")]
cactus.extract("Invoice from Acme Corp due 12/31/24", Invoice)
cactus.extract("Invoice from Acme Corp due 12/31/24", Invoice)

assert [call for call in stub.calls if call in ("init", "reset")] == ["init", "reset", "reset"]


def test_extract_licenses_a_relative_date_against_its_date_fact(stub):
import cactus

Expand Down
Loading