Skip to main content

💻🔗 LangCode

⚡ Build code-executing autonomous agents ⚡

Documentation

💻🔗 LangCode is a library which provides easy-to-use and reliable interface to a Python code-execution environment, such as Jupyter. LangCode can be used to build autonomous code-executing agents. LangCode supports text, image and code output of Jupyter. Also remote connection feature and jupyter server launcher is planned for the future versions.

Example

pip install langcode

INTERESTING: You can find claude_agent.ipynb in examples which demonstrates a real-world application of LangCode using Claude 3.5 Sonnet

from langcode.jupyter import Jupyter, ExecutionEvent, ExecutionResult

def process_execution_event(x: ExecutionEvent) -> None:
    ...

jupyter = Jupyter.local(
    env="...", # Set the execution environment.
    timeout=None, # Set global timeout (can be overriden later).
    event_handler: lambda x: process_execution_event(x) # Pass event handler if you need.
)

result: ExecutionResult = jupyter.run_cell(code="x = 10; x", timeout=None) # Final result.

for event in jupyter.stream_cell(code="import time; time.sleep(5)", timeout=1000): # Or stream events in real time.
    event: ExecutionEvent

    # This will raise TimeoutError after 1 second.

jupyter.restart() # You can also restart kernel to clear the cell state.

jupyter.close() # Close the notebook and Jupyter kernel.

You can also get images outputted by Jupyter kernel:

code = """
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set(xlabel='x', ylabel='sin(x)',
       title='A simple plot')

plt.show()

"""

result = jupyter.run_cell(code)

result.images
[Base64ImageString(content_format='png', content='iVBORw0KGg...')]

You can also get images in ExecutionEvent data transfer object when you use stream_cell.

Also you can stop the execution in the process if you need:

jupyter.stop_execution()

Here are the data transfer objects which represent execution results:

@dataclass
class ExecutionEvent:
    """
    An execution event which can be outputted in real time into user UI iteratively.
    """

    msg_type: Literal["stream", "error", "display_data", "execute_result"]
    content_type: Literal["console", "image", "code"]
    content_format: Literal["output", "base64/png", "base64/jpeg", "html", "javascript"]
    content: str


@dataclass
class Base64ImageString:
    """Represents an image in `base64` form, either `png` or `jpeg`."""

    content_format: Literal["png", "jpeg"]
    content: str


@dataclass
class ExecutionResult:
    """
    Final result of code execution inside Jupyter notebook.
    """

    events: List[ExecutionEvent]
    """List of all `ExecutionEvent` events outputted during execution."""

    error: bool
    """Signals whether an error has occured during execution, `True` if error occured."""

    text: str
    """Final text, excluding images, which can be injected into LLM."""

    images: List[ImageString]
    """Final list of `base64` images outputted during execution, can be injected into LLM."""

Got inspired and borrowed the code from Open Interpreter and E2B Code Interpreter

Metadata

Release files for langcode 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langcode 0.1.6
File Size Uploaded Provenance Actions
langcode-0.1.6.tar.gz 5.8 kB Without provenance Details for langcode-0.1.6.tar.gz

Built distribution (wheel)

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

Total release size: 13.4 kB

Release files / langcode-0.1.6.tar.gz

Download URL langcode-0.1.6.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a81873a62ba27e3126f0bbe737e1cf31344129c3a60b17962d087c820e7cb915
BLAKE2b-256 checksum
How to use checksums
0fec88ade659db4a46ecd2e31f04bd6c255645999129969d9edb975edbd7809c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.2 Linux/6.1.0-1parrot1-amd64

Release files / langcode-0.1.6-py3-none-any.whl

Download URL langcode-0.1.6-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2750ac2810cf4f6ab65490433e812789466cbfcf2c1a9605c25980a381e12c4
BLAKE2b-256 checksum
How to use checksums
7c98a9cb86265b13066cfcb572a2b2f1459eeff00c6c3039d6d8eeecbf3e5952
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.2 Linux/6.1.0-1parrot1-amd64

Security for langcode 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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