Skip to main content

No project description provided

Project description

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.

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

llmfn-0.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

llmfn-0.1.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file llmfn-0.1.0.tar.gz.

File metadata

  • Download URL: llmfn-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for llmfn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c7fed270c5ae65b39d24cc2ea008b3839d14ab32aaa676b036e4c7a32301360c
MD5 b1865cbe0652d70a8862209ec8067a06
BLAKE2b-256 8fa0d305fcd7d36bfec9ba6ee58a6f9795ac7ed4f8d1f566bc86da2859b61afd

See more details on using hashes here.

File details

Details for the file llmfn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llmfn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.11.0 Darwin/22.2.0

File hashes

Hashes for llmfn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbac3ad205ac455fb5ab5ce79b209d6afecfd4760b7c5d698e1eef96aad359df
MD5 6f8349da0c95fd671b13df960da55812
BLAKE2b-256 499c76b0b7d7d41d54c5c0a611f24459494f00fea913e23775b584d2d0228ebf

See more details on using hashes here.

Supported by

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