Skip to main content

No project description provided

Project description

💻🔗 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

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

langcode-0.1.6.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

langcode-0.1.6-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file langcode-0.1.6.tar.gz.

File metadata

  • Download URL: langcode-0.1.6.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.2 Linux/6.1.0-1parrot1-amd64

File hashes

Hashes for langcode-0.1.6.tar.gz
Algorithm Hash digest
SHA256 a81873a62ba27e3126f0bbe737e1cf31344129c3a60b17962d087c820e7cb915
MD5 d9f8848056da9ae92da83c6b1b615dc1
BLAKE2b-256 0fec88ade659db4a46ecd2e31f04bd6c255645999129969d9edb975edbd7809c

See more details on using hashes here.

File details

Details for the file langcode-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: langcode-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.2 Linux/6.1.0-1parrot1-amd64

File hashes

Hashes for langcode-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e2750ac2810cf4f6ab65490433e812789466cbfcf2c1a9605c25980a381e12c4
MD5 468439be9ae78a8605f51dd398c2af83
BLAKE2b-256 7c98a9cb86265b13066cfcb572a2b2f1459eeff00c6c3039d6d8eeecbf3e5952

See more details on using hashes here.

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