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():
    ...

Added a decorator for langchain. In this case, it becomes LLMChain.

llm = SomeLLMModels()
@magic_langchain(llm=llm)
def mychain(arg1: int, arg2: str):
    ...

mychain.predict(arg1=2, arg2="hi")

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.8.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

magic_decorator-0.0.8-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for magic_decorator-0.0.8.tar.gz
Algorithm Hash digest
SHA256 afef26705cdb22a4dc3880e734b81dcafd1b5c47118f6823455c611a66358328
MD5 8ae873e57428396484c7b59f6f0ecc5f
BLAKE2b-256 bc893dedeebb6e2529a9ecfac5fcb1081d7c46a5c477887ba01fce61f73f9d08

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for magic_decorator-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 1a7db2b7ed94c57f678fce26e2be9137eff7ba0d4670690319e6d58ba3bc6c61
MD5 1411f259032b81ae77c492260a69cf35
BLAKE2b-256 8fe73470a4245216020629a2d10656bd518f51a6c13e608d8e2d664c4aa7cced

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