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 extracts the Python interpreter tool from HuggingFace’s smolagents project.

One improvement over smolagents is that pytherpreter also supports async code execution using the async_evaluate function and AsyncPythonInterpreter class.

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.0.tar.gz (46.2 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.0-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytherpreter-0.1.0.tar.gz
  • Upload date:
  • Size: 46.2 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.0.tar.gz
Algorithm Hash digest
SHA256 4281db8b8140b29d1aeed7c720cdfc171b60db137aaa49afc12153d68cdc6010
MD5 c53af9ee30c7df9aa9298fb17f295993
BLAKE2b-256 57ae4f6c3e7221b032312aecedb7abc76d1812d75ce56c9d3823587ff26ee8a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytherpreter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5276b8d00aa48ffe1338d16f986f74fd6cdd09babe9fb7428afaf8f510b6e1c6
MD5 2c9d2e56308c5180ba0277e3d85209d4
BLAKE2b-256 b562bd5185d4263fb715b83cd3895e9e0c7d0690770d1e714c9c2cedea0e9916

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