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.

Metadata

Release files for prpl_llm_utils 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 prpl_llm_utils 0.1.0
File Size Uploaded
prpl_llm_utils-0.1.0.tar.gz 32.8 kB Details

Built distribution (wheel)

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

Total release size: 52.3 kB

Release files / prpl_llm_utils-0.1.0.tar.gz

Download URL prpl_llm_utils-0.1.0.tar.gz
Size 32.8 kB
Tags Source
SHA-256 checksum
How to use checksums
75a9afd8635212590c2fd24c96b7f45005ce235568a31a6d12376e8e473c0feb
BLAKE2b-256 checksum
How to use checksums
1e3d72745d3377809f6837daf8aedda679dc009fb2b59cd9d999eb2e59491667
Upload date
Uploaded using Trusted Publishing?
What is 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}

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

Download URL prpl_llm_utils-0.1.0-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ce8a37414aae0dbcf796d278fa0559f0428ac482fd59a7279f14af996e30423
BLAKE2b-256 checksum
How to use checksums
d82c6a160741716a3d080213fad96873e78f254f3572e7dbc21472a0d786ad73
Upload date
Uploaded using Trusted Publishing?
What is 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}

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