Skip to main content

llmfn

llmfn is a Python library to approximate a function using OpenAI's API. You can use it to easily train a language model to approximate your own functions with few-shot prompting.

Installation

You can install the package from pip:

pip install llmfn

Usage

First, you need to set your OpenAI API key:

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

Then, you can define a list of examples of the function's behavior:

from llmfn import Arguments
from llmfn import FunctionExample

examples = [
    FunctionExample(arguments=Arguments.call(2, 3), output=5),
    FunctionExample(arguments=Arguments.call(5, 7), output=12),
    # ...
]

Finally, you can use the llmfn decorator to create an approximated version of your function:

from llmfn import llmfn

@llmfn(examples=examples, function_name="my_function")
def my_function(a: int, b: int) -> int:
    return a + b

assert my_function(2, 3) == 5

Alternatively, you can use the make_llmfn function to create an approximated version of your function without using the decorator:

from llmfn import make_llmfn

blackbox = make_llmfn(examples=examples, function_name="my_function")

assert blackbox(2, 3) == 5

Advanced Usage

Changing the Decoder

By default, the decoder is set to lambda x: x, which simply returns the output as a string. You can change the decoder to parse the output into a different data type:

from llmfn import make_llmfn

def decoder(output: str) -> int:
    return int(output)

blackbox = make_llmfn(examples=examples, function_name="my_function", decoder=decoder)

assert blackbox(2, 3) == 5

The most useful decoder (and the most dangerous) is eval, which will evaluate the output as Python code:

from llmfn import make_llmfn

blackbox = make_llmfn(examples=examples, function_name="my_function", decoder=eval)

assert blackbox(2, 3) == 5

Use this with caution - it could be used to execute arbitrary code. (This is why it's not the default decoder.)

Changing the Engine

By default, the engine is set to text-davinci-003. You can change the engine to a different OpenAI engine:

from llmfn import make_llmfn

blackbox = make_llmfn(examples=examples, function_name="my_function", engine="text-curie-001")

assert blackbox(2, 3) == 5

Limitations

  • The function's output must be a string.
  • The approximated function can only handle arguments that are compatible with the examples.
  • The approximated function may not work with complex or large functions.
  • The API usage may be subject to rate limits and other restrictions imposed by OpenAI.

Contributing

We welcome contributions to this project. If you have any ideas or suggestions, please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

Metadata

Release files for llmfn 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmfn 0.1.0
File Size Uploaded
llmfn-0.1.0.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llmfn 0.1.0
File Interpreter ABI Platform
llmfn-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / llmfn-0.1.0.tar.gz

Download URL llmfn-0.1.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c7fed270c5ae65b39d24cc2ea008b3839d14ab32aaa676b036e4c7a32301360c
BLAKE2b-256 checksum
How to use checksums
8fa0d305fcd7d36bfec9ba6ee58a6f9795ac7ed4f8d1f566bc86da2859b61afd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

Release files / llmfn-0.1.0-py3-none-any.whl

Download URL llmfn-0.1.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbac3ad205ac455fb5ab5ce79b209d6afecfd4760b7c5d698e1eef96aad359df
BLAKE2b-256 checksum
How to use checksums
499c76b0b7d7d41d54c5c0a611f24459494f00fea913e23775b584d2d0228ebf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page