Skip to main content

PRPL LLM Utils

LLM utilities from the Princeton Robot Planning and Learning group.

The main feature is the ability to save and load previous responses. There are also some code synthesis utilities.

Usage Examples

Cache to SQLite3 Database (Recommended)

# Make sure OPENAI_API_KEY is set first.
from pathlib import Path
from prpl_llm_utils.models import OpenAIModel
from prpl_llm_utils.cache import SQLite3PretrainedLargeModelCache
cache = SQLite3PretrainedLargeModelCache(Path(".llm_cache.db"))
llm = OpenAIModel("gpt-4o-mini", cache)
response = llm.query("What's a funny one liner?", hyperparameters={"temperature": 1.0})
# Querying again loads from cache.
assert llm.query("What's a funny one liner?", hyperparameters={"temperature": 1.0}).text == response.text
# Querying with different hyperparameters can change the response.
response2 = llm.query("What's a funny one liner?", hyperparameters={"temperature": 0.5})
# Inspect .llm_cache.db, for example, using https://sqliteviewer.app/.

Cache to files

from pathlib import Path
from prpl_llm_utils.models import OpenAIModel
from prpl_llm_utils.cache import FilePretrainedLargeModelCache
cache = FilePretrainedLargeModelCache(Path(".llm_cache"))
llm = OpenAIModel("gpt-4o-mini", cache)
response = llm.query("What's a funny one liner?")
# Inspect the files in .llm_cache.

Synthesize a Python function

from pathlib import Path
from prpl_llm_utils.models import OpenAIModel
from prpl_llm_utils.cache import SQLite3PretrainedLargeModelCache
from prpl_llm_utils.code import (
    FunctionOutputRepromptCheck,
    SyntaxRepromptCheck,
    SynthesizedPythonFunction,
    synthesize_python_function_with_llm,
)
from prpl_llm_utils.structs import Query, Response

# Set up the LLM.
cache = SQLite3PretrainedLargeModelCache(Path(".llm_cache.db"))
llm = OpenAIModel("gpt-4o-mini", cache)

# Give the target function a name.
function_name = "count_vowels"

# Optionally, check syntax and reprompt in case of errors.
reprompt_checks = [SyntaxRepromptCheck()]

# Optionally, define examples to reprompt in case of errors.
inputs = [("books",), ("stormy",), ("farm",)]
output_check_fns = [lambda x: x == 2, lambda x: x in (1, 2), lambda x: x == 1]
reprompt_checks.append(
    FunctionOutputRepromptCheck(
        function_name, inputs, output_check_fns, function_timeout=1.0
    ),
)

# Define the query.
query = Query(
    """Generate a Python function of the form

def count_vowels(s: str) -> int:
    # your code here

Return only the function; do not give example usages.
"""
)

# Run synthesis.
count_vowels = synthesize_python_function_with_llm(
    function_name,
    llm,
    query,
    reprompt_checks=reprompt_checks,
)

# Inspect the synthesized function.
print(count_vowels)

# Use the synthesized function.
assert count_vowels("woohoo") == 4

Requirements

  • Python 3.10+
  • Tested on MacOS Monterey and Ubuntu 22.04

Installation

  1. Recommended: create and source a virtualenv.
  2. pip install -e ".[develop]"

Check Installation

Run ./run_ci_checks.sh. It should complete with all green successes in 5-10 seconds.

Acknowledgements

This code descends from predicators and includes contributions from a number of people, including especially Nishanth Kumar.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prpl_llm_utils-0.1.0.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prpl_llm_utils-0.1.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: prpl_llm_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for prpl_llm_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 75a9afd8635212590c2fd24c96b7f45005ce235568a31a6d12376e8e473c0feb
MD5 99ae60562b08d5a03d330664b78c11c0
BLAKE2b-256 1e3d72745d3377809f6837daf8aedda679dc009fb2b59cd9d999eb2e59491667

See more details on using hashes here.

File details

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

File metadata

  • Download URL: prpl_llm_utils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for prpl_llm_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ce8a37414aae0dbcf796d278fa0559f0428ac482fd59a7279f14af996e30423
MD5 6e39c5be7bc4cacea2c56bbf3bd1c45f
BLAKE2b-256 d82c6a160741716a3d080213fad96873e78f254f3572e7dbc21472a0d786ad73

See more details on using hashes here.

Supported by

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