gptfunction


Namegptfunction JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryA decorator for wrapping Python functions to generate an OpenAI GPT function calling schema.
upload_time2024-05-12 16:51:01
maintainerNone
docs_urlNone
authorPandasAreBears
requires_python>=3.5
licenseCopyright (c) 2023 PandasAreBears Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords openai gpt function calling
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Overview

The `GPTFunction` Python module provides a decorator for wrapping Python functions to generate an OpenAI GPT function calling schema. This module is particularly useful for those who need to integrate Python functions with GPT in a structured and efficient manner. The decorator transforms a regular Python function into a GPT function with a JSON schema, which can then be utilized by GPT for various purposes.

## Features

- **Type Hinting and Documentation**: The module enforces type hinting for parameters, supporting types like `str`, `int`, `float`, `typing.Literal`, and subclasses of `Enum`.
- **Docstring Parsing**: It parses the function's docstring to extract descriptions, ensuring a detailed and clear schema.
- **Automatic Schema Generation**: Generates the necessary schema for GPT integration seamlessly.

## Installation

```bash
pip install gptfunction
```

## Usage

### Basic Example

```python
from gptfunction import gptfunction

@gptfunction
def output_user(name: str, age: int) -> None:
    """
    Outputs a user's name and age to the console.

    :param name: The name of the user.
    :param age: The age of the user.
    """
    print(f"Name: {name}, Age: {age}")

gpt_tools = [output_user.schema()]
```

### Advanced Usage with Enums and Literals

```python
from gptfunction import gptfunction
from enum import Enum
from typing import Literal

class Fruit(Enum):
    APPLE = 'apple'
    BANANA = 'banana'

@gptfunction
def favorite_fruit(user_name: str, fruit: Fruit, quantity: Literal[1, 2, 3]) -> str:
    """
    Returns a string stating the user's favorite fruit and quantity.

    :param user_name: Name of the user.
    :param fruit: The preferred fruit.
    :param quantity: The quantity preferred (1, 2, or 3).
    :return: A descriptive string.
    """
    return f"{user_name} likes {quantity} {fruit.value}(s)."

print(favorite_fruit.schema())
```

## Documentation

### Parameter Types

- `str`: For string values.
- `int`: For integer values.
- `float`: For floating-point values.
- `typing.Literal`: For specifying a literal set of values.
- `Enum`: For enumerated types, with string values as Enum members.

### Decorator

- `@gptfunction`: This decorator should be used above the function definition. It processes the function and creates a GPT function schema.

### Methods

- `schema(use_required: bool)`: Returns the JSON schema of the wrapped function.
    - use_required: Indicates whether the schema should specify required parameters (default: True).
- `description()`: Retrieves the function's description from its docstring.
- `name()`: Returns the name of the function.

## Contributing

Contributions to improve `GPTFunction` are welcome. Please follow the standard procedures for submitting issues and pull requests on the project's GitHub repository.

## License

Distributed under the MIT License. See `LICENSE` for more information.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "gptfunction",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": null,
    "keywords": "openai, gpt, function calling",
    "author": "PandasAreBears",
    "author_email": null,
    "download_url": null,
    "platform": null,
    "description": "## Overview\n\nThe `GPTFunction` Python module provides a decorator for wrapping Python functions to generate an OpenAI GPT function calling schema. This module is particularly useful for those who need to integrate Python functions with GPT in a structured and efficient manner. The decorator transforms a regular Python function into a GPT function with a JSON schema, which can then be utilized by GPT for various purposes.\n\n## Features\n\n- **Type Hinting and Documentation**: The module enforces type hinting for parameters, supporting types like `str`, `int`, `float`, `typing.Literal`, and subclasses of `Enum`.\n- **Docstring Parsing**: It parses the function's docstring to extract descriptions, ensuring a detailed and clear schema.\n- **Automatic Schema Generation**: Generates the necessary schema for GPT integration seamlessly.\n\n## Installation\n\n```bash\npip install gptfunction\n```\n\n## Usage\n\n### Basic Example\n\n```python\nfrom gptfunction import gptfunction\n\n@gptfunction\ndef output_user(name: str, age: int) -> None:\n    \"\"\"\n    Outputs a user's name and age to the console.\n\n    :param name: The name of the user.\n    :param age: The age of the user.\n    \"\"\"\n    print(f\"Name: {name}, Age: {age}\")\n\ngpt_tools = [output_user.schema()]\n```\n\n### Advanced Usage with Enums and Literals\n\n```python\nfrom gptfunction import gptfunction\nfrom enum import Enum\nfrom typing import Literal\n\nclass Fruit(Enum):\n    APPLE = 'apple'\n    BANANA = 'banana'\n\n@gptfunction\ndef favorite_fruit(user_name: str, fruit: Fruit, quantity: Literal[1, 2, 3]) -> str:\n    \"\"\"\n    Returns a string stating the user's favorite fruit and quantity.\n\n    :param user_name: Name of the user.\n    :param fruit: The preferred fruit.\n    :param quantity: The quantity preferred (1, 2, or 3).\n    :return: A descriptive string.\n    \"\"\"\n    return f\"{user_name} likes {quantity} {fruit.value}(s).\"\n\nprint(favorite_fruit.schema())\n```\n\n## Documentation\n\n### Parameter Types\n\n- `str`: For string values.\n- `int`: For integer values.\n- `float`: For floating-point values.\n- `typing.Literal`: For specifying a literal set of values.\n- `Enum`: For enumerated types, with string values as Enum members.\n\n### Decorator\n\n- `@gptfunction`: This decorator should be used above the function definition. It processes the function and creates a GPT function schema.\n\n### Methods\n\n- `schema(use_required: bool)`: Returns the JSON schema of the wrapped function.\n    - use_required: Indicates whether the schema should specify required parameters (default: True).\n- `description()`: Retrieves the function's description from its docstring.\n- `name()`: Returns the name of the function.\n\n## Contributing\n\nContributions to improve `GPTFunction` are welcome. Please follow the standard procedures for submitting issues and pull requests on the project's GitHub repository.\n\n## License\n\nDistributed under the MIT License. See `LICENSE` for more information.\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2023 PandasAreBears  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \u201cSoftware\u201d), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \u201cAS IS\u201d, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "A decorator for wrapping Python functions to generate an OpenAI GPT function calling schema.",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/PandasAreBears/gpt_function"
    },
    "split_keywords": [
        "openai",
        " gpt",
        " function calling"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b4a46f789123f279e74393b88f46b89719df4584f4ffcc340e4d36efe79e7772",
                "md5": "8791617d27a781a8de085ec6d0f417d4",
                "sha256": "5ff95a37e99c446736ced4f264ec03d13306b66a865c1ad645ed1ccae923d9e6"
            },
            "downloads": -1,
            "filename": "gptfunction-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8791617d27a781a8de085ec6d0f417d4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 6479,
            "upload_time": "2024-05-12T16:51:01",
            "upload_time_iso_8601": "2024-05-12T16:51:01.695502Z",
            "url": "https://files.pythonhosted.org/packages/b4/a4/6f789123f279e74393b88f46b89719df4584f4ffcc340e4d36efe79e7772/gptfunction-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-12 16:51:01",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PandasAreBears",
    "github_project": "gpt_function",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "gptfunction"
}
        
Elapsed time: 3.80066s