Skip to main content

A magic function decorator using OpenAI ChatCompletion

Project description

Simple demo function using OpenAI's ChatComplete

The only function implemented in this module is magic.

magic works as a decorator. It simply sends the function's signature and docstring to the GPT and expects to get a result.

It can be installed with pip install magic-decorator and used with from magic_decorator import magic.

from magic_decorator import magic
@magic(model="gpt-4")
def transpose(matrix: list[list[int]]) -> list[list[int]]:
    """
    This function finds the transpose of the given matrix.
    """
transpose([[1,2,3],[4,5,6]])
[[1, 4], [2, 5], [3, 6]]
from magic_decorator import magic
@magic(model="gpt-4")
def add(a: float, b: float) -> float:
    """
    This function adds a and b.
    """
add(1., 10.)
11.0
from magic_decorator import magic
@magic(model="gpt-4")
def friend(message: str) -> str:
    """
    This function responds like a friend.
    """
friend("Hi, how are you today?")
'Hey! I am doing great, thank you. How about you?'

Since the magic decorator works with OpenAI's API, you need to put the API_KEY in an environment variable or directly in the decorator.

import os
from magic_decorator import magic

os.environ["OPENAI_API_KEY"] = ...
# or
@magic(model="gpt-4", api_key=...)
def func():
    ...

Alternatively, you can pre-declare decorators that will be used repeatedly.

cold_magic = magic(model="gpt-4", temperature=0)
@cold_magic
def func():
    ...

There is no preprocessing, no postprocessing, and no error handling in this module. It's just code to do a simple thing, so there are no prompts to encourage better answers.

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

magic_decorator-0.0.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

magic_decorator-0.0.3-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file magic_decorator-0.0.3.tar.gz.

File metadata

  • Download URL: magic_decorator-0.0.3.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for magic_decorator-0.0.3.tar.gz
Algorithm Hash digest
SHA256 e433dad7a5807706c4abe905637ad1c360a8243a5d08605c115fe60f8aff16e4
MD5 914b7ce7110ef36f50e6708a8b13863b
BLAKE2b-256 d78fffe2cb5519bacc4217d9bc9f76ddbebe0f5cd2451e62932201faa4115a0d

See more details on using hashes here.

File details

Details for the file magic_decorator-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for magic_decorator-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1f13c145a2fc92108e06e3b3cd95889c805c6438f85137aa8622e2c455ed2453
MD5 cc8abd66c0f48410112935f2681e2998
BLAKE2b-256 e82a9918a701145225fd768abfbe5a99be39b16a69e2bd6433d81616f077156b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page