Skip to main content

A library for interacting with Replit's code-exec API

Project description

replit-code-exec

An interface to interact with Replit's code-exec API.

This is an interface for the simpler use cases of https://replit.com/@luisreplit/eval-python, which is a stateless API that is optimized to be called from AI Agents so that they can perform numerical reasoning through the evaluation of generated Python code. In general, this allows the execution of an untrusted snippet of Python code and returns whatever is printed to standard output / standard error as the result. The execution is done inside an ephemeral unprivileged container created on the fly running in Replit Deployments using https://github.com/omegaup/omegajail as code sandbox.

To set up your own copy of the API server, you need to follow these easy 2-3 steps (the second is optional):

  1. Open the https://replit.com/@luisreplit/eval-python in your browser and Fork it to your account.
  2. (Optional): if you want to change the Docker container, run evalctl image ${DOCKER_IMAGE} (e.g. evalctl image python:3 or evalctl image replco/python-kitchen-sink:latest).
    • Open .replit and change the EVAL_FILENAME, EVAL_RUN_COMMAND, EVAL_ENV to suite the new container, if needed.
  3. Deploy the Repl! (just pressing Run is not enough)
    • This is only compatible with the Autoscale Deployments.
    • Make sure you set the EVAL_TOKEN_AUTH Deployments secrets when doing so for authentication.

Sample usage

Here is a small example of this library being used as a small math solver using OpenAI API:

import openai
import replit_code_exec

code_exec = replit_code_exec.build_code_exec(...)

def solve_math(prompt: str, model: str = 'gpt-3.5-turbo-0613') -> str:
    completion = openai.ChatCompletion.create(
        model=model,
        temperature=0.7,
        functions=[code_exec.openai_schema],
        function_call={"name": "code_exec"},
        messages=[
            {
                "role": "system",
                "content": ("You are an assistant that knows how to solve math " +
                            "problems by converting them into Python programs."),
            },
            {
                "role": "user",
                "content": ("Please solve the following problem by creating a Python " +
                            "program that prints the solution to standard output using " +
                            "`print()`: " + prompt),
            },
        ],
    )
    return code_exec.from_response(completion)

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

replit_code_exec-0.0.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

replit_code_exec-0.0.1-py3-none-any.whl (5.5 kB view hashes)

Uploaded Python 3

Supported by

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