Generate and execute python code via OpenAI code completion
Project description
akashic_records
akashic_records
is a Python package that dynamically generates functions using OpenAI code completion based on what is imported and how it is used.
Installation
To install the akashic_records package, you can use pip:
pip install akashic_records
Usage
Behaviour of the akashic_records
package is based on what you import from it, and how you use it.
The package generates functions on the fly based on the name you import and the way you call it.
from akashic_records import quick_sort
arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
result = quick_sort(arr)
print(result) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
The package will end up making requests to the OpenAI completations endpoint with the code-davinci-002
model.
The above code will end up generating this prompt:
def quick_sort(arr: list):
Note that the parameter name matches the name of the argument that was passed in. If a constant is passed in instead of an identifier, generic names such as p0
and p1
will be used. To have a useful name with a constant argument, use keyword arguments.
Return type and docstrings
This package (ab)uses type hints to give more information to the completion process.
from typing import Annotated
from akashic_records import merge_sort
unsorted_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
result: Annotated[list, """
Sorts the input list using the mergesort algorithm.
Parameters:
-----------
unsorted_list : list
The input list to be sorted.
Returns:
--------
list
The sorted list.
"""] = merge_sort(unsorted_list)
print(result) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
Generates the following prompt:
def merge_sort(unsorted_list: list) -> list:
"""
Sorts the input list using the mergesort algorithm.
Parameters:
-----------
unsorted_list : list
The input list to be sorted.
Returns:
--------
list
The sorted list.
"""
If you would like to include a docstring but not a return type, only use a string for the type annotation instead of using typing.Annotated
.
Decorator
If you don't like the somewhat magical mechanisms by which the above functionality works (extracting type hints and argument variable names), the packge also supplies a decorator with the same functionality.
from akashic_records import generate
@generate(n=1, temperature=0.1, max_tokens=256)
def merge_sort(unsorted_list: list) -> list:
"""
Sorts the input list using the mergesort algorithm.
Parameters:
-----------
unsorted_list : list
The input list to be sorted.
Returns:
--------
list
The sorted list.
"""
print(merge_sort([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
How does the parameter name/type hint thing work?
The very neat sorcery package by Alex Hall.
Options
The OpenaAI completaions endpoint has many options. Some of these are available for tweaking.
from akashic_records import config
config.n = 3 # https://platform.openai.com/docs/api-reference/completions/create#completions/create-n
config.max_tokens = 512 # https://platform.openai.com/docs/api-reference/completions/create#completions/create-max_tokens
config.temperature = 0.2 # https://platform.openai.com/docs/api-reference/completions/create#completions/create-temperature
Some additional options are available to control the prompt generation process.
from akashic_records import config
config.type_hint = True # Set to False to disable all type hints in prompts
# The package isn't always able to get a working completion from the API.
# In the event that something goes wrong (syntax error in the generated code, runtime error trying to run it, etc)
# The package will try to generate new completions.
# The `attempts` value controls how many times it will try.
config.attempts = 5 # Set to -1 for unlimited tries.
What's with the name?
The name akashic_records
is inspired by the spiritual belief of the Akashic Records. In this belief, the Akashic Records are a repository all universal events, thoughts, words, emotions and intent ever to have occurred in the past, present, or future in terms of all entities and life forms.
This seemed fitting for a package that in some sense contains the implementation of "every function".
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
Built Distribution
File details
Details for the file akashic_records-0.3.0.tar.gz
.
File metadata
- Download URL: akashic_records-0.3.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.9 Linux/6.1.12-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bfa3fd02f27d220b9b9e55ef05bb487f71be5d3c9a8635709da86910f9c66c5 |
|
MD5 | 5fce8b6429c66062a2301337f6a557c1 |
|
BLAKE2b-256 | 491a129abcff45ea91244b616f0517e4ddea76a010bae9f97be1920e0205e87a |
File details
Details for the file akashic_records-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: akashic_records-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.9 Linux/6.1.12-arch1-1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3761598db1ee8ba4838745e29ed2a3b0c21682655aae09f94ebda2b3c477de48 |
|
MD5 | 4a424cf3f9cd19b7c9ccb0e3abcdbb69 |
|
BLAKE2b-256 | 2217904c1007c2c448586738f5865c9bce88eb759d9ec28e669cd8fbfb99c90d |