Reference#

ai_ghostfunctions#

AI Ghostfunctions.

ai_ghostfunctions.ghostfunction(function=None, /, *, ai_callable=None, prompt_function=<function _default_prompt_creation>, aggregation_function=<function <lambda>>, **kwargs)#

Decorate function to make it a ghostfunction which dispatches logic to the AI.

A ghostfunction is a function that uses OpenAI API to execute the intent of the function, without manually writing (or generating code). The @ghostfunction decorator wraps the function in a sensible prompt, sends the prompt to the OpenAI API, and parses the result into a python object that is returned by the ghostfunction.

Parameters:
  • function (Callable[[...], Any] | None) – The function to decorate

  • ai_callable (Callable[[...], OpenAIObject] | None) – Function to receives output of prompt_function and return result.

  • prompt_function (Callable[[Callable[[...], Any]], List[Message]]) – Function to turn the function into a prompt.

  • aggregation_function (Callable[[...], Any]) – Function to aggregate the n choices from the OpenAI API. Ghostfunctions passes a list of n different results from OpenAI (parsed into python data structures) to this function for aggregation into the output of the ghostfunction.

  • kwargs (Any) – Extra keyword arguments to pass to ai_callable.

Returns:

Decorated function that will dispatch function logic to OpenAI.

Return type:

Callable[[…], Any]

Notes

This function is intended to be used as a decorator. See Example. This function expects the env var OPENAI_API_KEY to be set the OpenAI API key

to be used to make calls to the OpenAI API.

Example

>>> # xdoctest: +SKIP
>>> from ai_ghostfunctions import ghostfunction
>>>
>>> @ghostfunction
>>> def generate_random_words(n: int, startswith: str) -> list:  # xdoctest +SKIP
>>>     """Return a list of `n` random words that start with `startswith`."""
>>>     pass
>>>
>>> generate_random_words(n=4, startswith="goo")
['goofy', 'google', 'goose', 'goodness']
>>> # xdoctest: -SKIP