Package: openai-decorator
OpenAI DecoratorThe OpenAI Decorator is a Python package that enriches your Python functions with the capabilities of OpenAI's API. It leverages the function's signature and docstring to generate specifications comprehensible by OpenAI's API, subsequently making a request to the API.Table of ContentsKey FeaturesInstallationUsage GuideBasic UsageType Annotations & OptionalsPassing a Callable PromptUsing the Decorator as a Regular FunctionContributingSetupFuture EnhancementsLicenseKey FeaturesAutomatic generation of specs from the function's signature and docstring.Integration with OpenAI's API using the generated specs and a customizable prompt.Inference of function arguments from the OpenAI API response, negating the need for manual input.Handling of various edge cases related to function signatures, including default and optional values.InstallationInstall the package using pip:pipinstallopenai-decoratorSave your OpenAI key in an environment variable namedOPENAI_API_KEY:exportOPENAI_API_KEY=<yourkey>Usage GuideBasic UsageApply theopenai_funcdecorator to a function with type annotations and a well-formatted Google Style docstring. Then, it will seamlessly interact with OpenAI's API to generate the parameters for your function based on the prompt.fromopenai_decoratorimportopenai_func@openai_func(prompt="Your OpenAI prompt here")defexample_function(arg1:str,arg2:int)->str:"""This is an example function.Args:arg1 (str): Description for arg1arg2 (int): Description for arg2Returns:str: Description for return value"""returnf"Your output here:{arg1}and{arg2}"result=example_function()print(result)Type Annotations & OptionalsThe decorator efficiently handles complex function signatures:Default Values:Parameters with a default value orOptionaltype annotation are marked as optional in the JSON specification.Type Annotations:Supports primitive types (int, str, bool, float) and complex types (List, Dict, Tuple, Set, Optional, custom classes).fromopenai_decoratorimportopenai_func@openai_func(prompt="Perform mathematical operations")defexample_function(a:int,b:int,c:float=0.0,d:List[int]=[]):"""This is an example function.Args:a: The first integer.b: The second integer.c: An optional float.d: An optional list of integers."""# Function body herePassing Callable PromptsIn addition to static strings, you can also pass a callable to thepromptparameter in theopenai_funcdecorator to dynamically generate prompts. This callable should take no arguments and return a string. Here's an example:fromopenai_decoratorimportopenai_funcdefgenerate_prompt():# Dynamically generate a prompt.# In a real-world scenario, this could depend on various factors,# such as the current time or the latest trending topics.return"Your OpenAI prompt here"@openai_func(prompt=generate_prompt)defexample_function(arg1:str,arg2:int)->str:"""This is an example function.Args:arg1 (str): Description for arg1arg2 (int): Description for arg2Returns:str: Description for return value"""returnf"Your output here:{arg1}and{arg2}"result=example_function()print(result)In this example, every timeexample_functionis called, `generate_promptUsing the Decorator as a Regular Functionopenai_funccan also be used as a regular function. First, callopenai_funcwith the prompt and any other arguments to get the actual decorator. Then, apply this decorator to your function.fromopenai_decoratorimportopenai_funcdefexample_function(arg1:str,arg2:int)->str:"""This is an example function.Args:arg1 (str): Description for arg1arg2 (int): Description for arg2Returns:str: Description for return value"""returnf"Your output here:{arg1}and{arg2}"# Use openai_func as a regular functionprompt="Your OpenAI prompt here"example_func_with_generated_params=openai_func(prompt=prompt)(example_function)result=example_func_with_generated_params()print(result)This approach gives you more flexibility in determining the prompt at runtime.ContributingContributions are welcome! Feel free to submit issues and pull requests.SetupInstall Python3.11and the latest version ofpoetrypyenvcan help manage multiple Python versions.Clone the repository:git clone.Set your OpenAI key as an environment variable:exportOPENAI_API_KEY=<insertyouropenaikey>Install dependencies:poetry install --no-rootFuture EnhancementsHandle optional parameters.Handle parameters with default values.Add test to ensure default values are used if OpenAI doesn't return parameters.Publish the package to PyPI.Expand test coverage.Show tests passing & coverage as github badges.Fix CI/CD -- Address the issue where the pre-release deployment to PyPI fails due to the need for version update.Add docstrings.Generate docs (from docstrings).LicenseThis project is under the MIT License. See theLICENSEfile for more details.