Skip to main content

Skill composition for LLMs — like lambda calculus, but the functions talk back

Project description

tk.llmbda

Skill composition for LLM pipelines. Chain deterministic and LLM-powered steps into a skill; the runtime walks them in order until one resolves.

Deterministic skill

from tk.llmbda import Skill, Step, StepContext, StepResult, lm, run_skill

def greet(ctx: StepContext) -> StepResult:
    name = ctx.entry.get("name", "world")
    return StepResult(value=f"hello, {name}")

skill = Skill(name="greeter", steps=[Step("greet", greet)])
result = run_skill(skill, {"name": "λ"})
# SkillResult(skill="greeter", resolved_by="greet", value="hello, λ", ...)

LLM skill

Self-contained with a fake model so the snippet runs as-is:

from tk.llmbda import Skill, Step, StepContext, StepResult, lm, run_skill

def fake_model(*, messages, **_):
    return "2025-01-15"  # pretend the LLM returned an ISO date

@lm(fake_model, system_prompt="Extract a date. Return ISO format.")
def extract_date(ctx: StepContext, call) -> StepResult:
    """Extract a date from natural language."""
    raw = call(messages=[{"role": "user", "content": ctx.entry["text"]}])
    return StepResult(value=raw)

skill = Skill(name="dates", steps=[Step("extract_date", extract_date)])
result = run_skill(skill, {"text": "let's meet on the 15th of January 2025"})
# SkillResult(skill="dates", resolved_by="extract_date", value="2025-01-15", ...)

OpenAI adapter

Any callable matching LMCaller ((*, messages: list[dict], **kwargs) -> str) works as the @lm model. Minimal adapter using the openai SDK:

from openai import OpenAI
client = OpenAI()

def openai_caller(*, messages, **kwargs):
    resp = client.chat.completions.create(
        model="gpt-4o-mini", messages=messages, **kwargs,
    )
    return resp.choices[0].message.content

Drop-in replacement for fake_model in the snippet above.

Concepts

  • @lm(model, system_prompt=...) — binds model (and optional system prompt) at decoration time. Decorated fn signature is (ctx, call); call prepends system_prompt before forwarding to model.
  • Step.description — human-readable summary; falls back to the fn docstring via __post_init__. Separate from @lm system prompts; read those via step.fn.lm_system_prompt.
  • StepResult.value — the step's output: parsed data, extracted values, model responses, or None.
  • StepResult.metadata — auxiliary context: reasons, raw provider output, parse errors, confidence.
  • StepResult.resolved — defaults to True; return resolved=False to fall through. Execution stops after the final step regardless.
  • ctx.steps, ctx.prior — the plan and prior-step outcomes. Serialise both value and metadata when passing to a later LLM step.
  • iter_skill — same execution as run_skill but yields (name, result) per step for live observation or early exit.
  • Test re-bindinglm(fake)(my_step.__wrapped__) re-decorates the original fn body with a different model.

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

tk_llmbda-0.1.0.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

tk_llmbda-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tk_llmbda-0.1.0.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tk_llmbda-0.1.0.tar.gz
Algorithm Hash digest
SHA256 63d47923ad452ac19c4a9a7bd10f933cb90f4ea5e81c5b61a17077307d157eb1
MD5 19fdd845d0744ae26e291019529386ce
BLAKE2b-256 30f366e18fd23dd5180357914c5308def82662d15f69407582243810a683cd5e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tk_llmbda-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tk_llmbda-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 def12b321f301abda80ab72ffec3e8c8896c147b5c8ba60e5883d5a92b9b20ea
MD5 3d651df4047d37cac510fb778de56247
BLAKE2b-256 0fec345ca27ea37edf91274ea3c3cffc873e3a4aa0c4143931a06fc41c9c58aa

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