Skip to main content

A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.

Project description

🐍 pytherpreter 🐍

A Python interpreter with built-in safeguards for executing untrusted code, like LLM-generated scripts.

This repository contains the Python interpreter tool extracted from HuggingFace’s smolagents project. Big hug to the HuggingFace team for their initial implementation! 🤗

Some improvements over smolagents:

  • Supports async code execution using the async_evaluate function and AsyncPythonInterpreter class.
  • Improved function call resolution.
  • Supports custom subscriptable objects.
  • No external dependencies.

Installation

pip install pytherpreter

Latest development version:

pip install git+ssh://git@github.com/aremeis/pytherpreter.git

or

pip install git+https://github.com/aremeis/pytherpreter.git

Usage

Using evaluate

This function evaluates Python code and returns the result.

from pytherpreter import evaluate

result, final_answer = evaluate("""
from math import sqrt
sqrt(4)
""")
print(result)
print(final_answer)

# Output:
# 2.0
# False

By default, the evaluate function will return the result of the last expression in the code. However, you can also return a value from the code by using the final_answer function:

result, final_answer = evaluate_python_code("""
from math import sqrt
final_answer(sqrt(4))
""")
print(result)
print(final_answer)

# Output:
# 2.0
# True

Using PythonInterpreter

This class is a wrapper around the evaluate function that keeps the state of the interpreter between calls. Variables and functions defined by the code will be be available in subsequent calls.

from pytherpreter import PythonInterpreter

interpreter = PythonInterpreter()
result, logs, is_final_answer = interpreter("x = 3")
print(result)
print(logs)
print(is_final_answer)

# Output:
# 3
# 
# False

result, logs, is_final_answer = interpreter("""
x += 1
print('x =', x)
final_answer(x);
""")
print(result)
print(logs)
print(is_final_answer)

# Output:
# 4
# x = 4
# True

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

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

pytherpreter-0.1.1.tar.gz (45.5 kB view details)

Uploaded Source

Built Distribution

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

pytherpreter-0.1.1-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file pytherpreter-0.1.1.tar.gz.

File metadata

  • Download URL: pytherpreter-0.1.1.tar.gz
  • Upload date:
  • Size: 45.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for pytherpreter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a601f0480e6ee70a2ad1ddc07dc8a1006aedc05b575cf2e006a4a4ab3c5d0730
MD5 5a00f50682c1bfbbede2d193732bfc9e
BLAKE2b-256 fb98a997ec654d7b3a4f7b52acb255384ad2b60c0798f7bb95a2a4e16011c73d

See more details on using hashes here.

File details

Details for the file pytherpreter-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytherpreter-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 27.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for pytherpreter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ab8106d67301043265e3223843cefd8fa706b6ec2cb3c669f457fd392937e447
MD5 cdfdfa30772ae40a82cb8b4cf03e0530
BLAKE2b-256 1adcecb75774e1071022ab68782e30eac114d42bf8a48392fb8c3c8d1aa16f05

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 Pingdom Monitoring Sentry Error logging StatusPage Status page