Skip to main content

Prompt-defined Python functions with lazy or eager LLM-backed generation.

Project description

PyFuncAI

CI GitHub Release PyPI PyPI - Python Version License: Apache-2.0 Code style: black

PyFuncAI is a Python library for defining functions with prompts and generating their implementations with an LLM at runtime.

The library is built around a small API:

  • connect to a model provider
  • describe a function in natural language
  • request lazy or eager generation
  • optionally cache generated source for reuse across runs

PyFuncAI currently supports local Ollama models and remote OpenAI and Gemini providers.

Installation

Install from PyPI:

pip install pyfuncai

For local development:

pip install -e .[dev]

Quick Start

Ollama

from pyfuncai import connect, create_function

connect(
    "ollama",
    model="qwen3.5:latest",
    base_url="http://localhost:11434",
    timeout=180,
)

greet = create_function(
    "Return a short greeting for the provided name.",
    signature="(name: str) -> str",
    function_name="greet",
    mode="eager",
    cache=True,
)

print(greet("Alice"))

OpenAI

from pyfuncai import connect, create_function

connect(
    "openai",
    model="gpt-5-mini",
    api_key="YOUR_OPENAI_API_KEY",
)

slugify = create_function(
    "Convert text into a URL-friendly slug.",
    signature="(text: str) -> str",
    function_name="slugify",
)

Gemini

from pyfuncai import connect, create_function

connect(
    "gemini",
    model="gemini-2.5-flash",
    api_key="YOUR_GEMINI_API_KEY",
)

summarize = create_function(
    "Summarize a short paragraph in one sentence.",
    signature="(text: str) -> str",
    function_name="summarize",
)

Core Concepts

connect(provider, **config)

Registers the default provider used by subsequent create_function() calls.

Supported providers:

  • ollama
  • openai
  • gemini

Common configuration keys:

  • model
  • timeout
  • cache_dir

Provider-specific keys:

  • Ollama: base_url
  • OpenAI: api_key, base_url
  • Gemini: api_key, base_url

create_function(...)

Creates a prompt-defined callable.

Common arguments:

  • prompt
  • signature
  • function_name
  • mode="lazy" or mode="eager"
  • cache=True
  • provider
  • cache_dir
  • system_prompt
  • temperature
  • max_output_tokens
  • allow_modules

The return value is a GeneratedFunction. It behaves like a normal callable and also exposes:

  • .build(force=False)
  • .source
  • .cache_key
  • .is_built

createFunction(...) is available as a compatibility alias.

Generation Modes

  • lazy: generation happens on first invocation
  • eager: generation happens during create_function()

Caching

Generated source can be cached on disk and reused across runs. Cache keys include:

  • prompt
  • signature
  • function name
  • provider identity
  • mode
  • allowed modules

The default cache location is OS-specific. You can override it globally with connect(..., cache_dir=...) or per function with create_function(..., cache_dir=...).

CLI

PyFuncAI ships with a small command-line interface for prebuilding functions and inspecting cached source.

Prebuild every prompt-defined function created by a script:

pyfuncai build path/to/script.py

Force regeneration even when cached source already exists:

pyfuncai build --force path/to/script.py

Inspect the cache directory:

pyfuncai cache path
pyfuncai cache list
pyfuncai cache clear

You can point cache commands at a different directory with --cache-dir.

Safety

PyFuncAI validates generated code before execution. The current implementation includes:

  • AST validation
  • exact signature matching
  • restricted imports
  • restricted builtins at execution time

This is still a code-generation library, not a hardened sandbox. Generated code should be treated as untrusted.

Development

The repository uses three long-lived branches:

  • main: stable development and PyPI releases
  • preview: prerelease validation and release-candidate testing
  • dev: active integration branch

Automation lives under .github/workflows/:

  • ci.yml: formatting, tests, and package build checks on main, preview, dev, and pull requests
  • release.yml: stable releases from main tags such as v0.1.3
  • prerelease.yml: GitHub prereleases from preview tags such as v0.1.4rc1

Run the test suite locally:

pytest

Run the optional live Ollama test:

PYFUNCAI_RUN_OLLAMA_TESTS=1 pytest tests/test_ollama_live.py

PowerShell:

$env:PYFUNCAI_RUN_OLLAMA_TESTS = "1"
pytest tests/test_ollama_live.py

Releasing

Stable releases are published to PyPI from main.

Typical stable release flow:

  1. Update version in pyproject.toml.
  2. Commit and push to main.
  3. Create and push a matching tag.

Example:

git checkout main
git pull
git tag v0.1.3
git push origin main
git push origin v0.1.3

Preview releases are created from preview and published as GitHub prereleases only.

Example:

git checkout preview
git pull
git tag v0.1.4rc1
git push origin preview
git push origin v0.1.4rc1

Contributing

Contributions are welcome. A good contribution usually includes:

  • a clear problem statement or use case
  • tests for behavior changes
  • documentation updates when the public API changes

Before opening a pull request:

python -m black src tests example.py
python -m pytest
python -m build

License

Apache-2.0

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

pyfuncai-0.1.5.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

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

pyfuncai-0.1.5-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file pyfuncai-0.1.5.tar.gz.

File metadata

  • Download URL: pyfuncai-0.1.5.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyfuncai-0.1.5.tar.gz
Algorithm Hash digest
SHA256 b8d91046e71f9089f5891a20d78cf53109bb37f9715b98d71d449513344d2b6d
MD5 610dfbb0cd33fa9a17cc5d24e052158a
BLAKE2b-256 55667e1df1eef38fbc9eb09174b32bf0a22b462bcfa5fcede0e939215f8a3660

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfuncai-0.1.5.tar.gz:

Publisher: release.yml on AaronCreor/PyFuncAI

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

File details

Details for the file pyfuncai-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pyfuncai-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyfuncai-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 95eaa7e854469543fbf207806e73a9f6408f939c44defefce804dde6dbc353b8
MD5 46c6e722842d2732e02209cc6e238886
BLAKE2b-256 fedeed2f69cb21848d3b8a9a55f06439fdf4bb83165a2ef7457f0232e494a024

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfuncai-0.1.5-py3-none-any.whl:

Publisher: release.yml on AaronCreor/PyFuncAI

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 Pingdom Monitoring Sentry Error logging StatusPage Status page