Skip to main content

AI Ghostfunctions

Write the docstring, call the function, get the results.

PyPI Status Tests License

Documentation: https://ai-ghostfunctions.readthedocs.io/

A ghostfunction leverages OpenAI GPT-4 to return sensible outputs from functions without defined logic. It packages the the function name, docstring, and signature into a prompt which is sent to OpenAI's GPT-4 API, and transparently handles coercing the api result into the expected return type.

Installation

pip install ai-ghostfunctions

Quickstart

To see it in action, save your OpenAI API Key to the env var OPENAI_API_KEY and run:

>>> import os
>>> from ai_ghostfunctions import ghostfunction

>>> assert os.getenv("OPENAI_API_KEY")

>>> @ghostfunction
>>> def sanitize_messy_string(messy_string: str) -> list[dict]:
>>>     """Return a list of dicts that contain the data from `messy_string`."""
>>>     pass

>>> sanitize_messy_string(messy_string="""name|age|nickname
John Brighton Bradford,  34,  J.B
     Grace B., "24", Grace""")

[{'name': 'John Brighton Bradford', 'age': 34, 'nickname': 'J.B'},
 {'name': 'Grace B.', 'age': 24, 'nickname': 'Grace'}]

###

>>> @ghostfunction
>>> def generate_random_words(n: int, startswith: str) -> list:
>>>     """Return a list of `n` random words that start with `startswith`."""
>>>     pass

>>> generate_random_words(n=4, startswith="goo")
['goofy', 'google', 'goose', 'goodness']

>>> generate_random_words(n=3, startswith="foot")
['football', 'footnote', 'footprint']

By default, a ghostfunction will dispatch a sensible prompt to OpenAI GPT-4 that includes the function name, the docstring, and function arguments, parse the result from OpenAI and return it as the result of the function.

Ghostfunctions will retry and send the data to gpt-3.5-turbo if it looks like the OPENAI_API_KEY does not have access to gpt-4.

Customizations

You can control the prompt:

>>> import os
>>> from ai_ghostfunctions import ghostfunction
>>> from ai_ghostfunctions.keywords import USER
>>> from ai_ghostfunctions.types import Message

>>> assert os.getenv("OPENAI_API_KEY")

>>> @ghostfunction(prompt_function=lambda f, **kwargs: [
>>>     Message(role=USER, content=f"tell me a slightly insulting joke about this function name: {f.__name__}.")
>>> ])
>>> def recursive_function_that_will_recurse():
>>>     """Recurse until you go crazy."""
>>>     pass

>>> recursive_function_that_will_recurse()
# 'Why did the programmer name his function "recursive_function_that_will_recurse"? Because he wanted to make absolutely sure that no one would confuse it for a function that actually does something useful.'

Heh. Not bad.

Prompts to gpt-4 and gpt-3.5-turbo are of type List[ai_ghostfunctions.types.Message].

See ghostfunctions.py for the default prompt.

License

License AI Ghostfunctions is free and open source software.

Requirements

See pyproject.toml for a list of dependencies.

Contributing

Contributions are very welcome. To learn more about setting up a dev environment and contributing back to the project, see the Contributor Guide.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from a fork of @cjolowicz's Hypermodern Python Cookiecutter template.

Metadata

Release files for ai-ghostfunctions 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-ghostfunctions 0.6.1
File Size Uploaded Provenance
ai_ghostfunctions-0.6.1.tar.gz 6.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-ghostfunctions 0.6.1
File Interpreter ABI Platform
ai_ghostfunctions-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.5 kB

Release files / ai_ghostfunctions-0.6.1.tar.gz

Download URL ai_ghostfunctions-0.6.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8a4ce46416d61829468d7431f723072415a498c096dfcb8ae3be94220d702c6d
BLAKE2b-256 checksum
How to use checksums
83f2a7fa498ac17d78ac2189b04a1182be71bd2d899226a70c45b6bd9fe34c5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.8

Release files / ai_ghostfunctions-0.6.1-py3-none-any.whl

Download URL ai_ghostfunctions-0.6.1-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f247549c8fc1a324082e90c85b97ad65d6cb1a7db6727133825b9f8f12e52bd
BLAKE2b-256 checksum
How to use checksums
f209cd7520ee36aeecee066f720e4979a6164f0a74a6e64b81a2000af2540faa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.8

Security for ai-ghostfunctions 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page