langcode


Namelangcode JSON
Version 0.1.6 PyPI version JSON
download
home_pagehttps://github.com/keell0renz/langcode
SummaryNone
upload_time2024-07-16 13:01:48
maintainerNone
docs_urlNone
authorYour Name
requires_python<4.0,>=3.7
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 💻🔗 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

```bash
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

```python
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:

```python
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
```

```python
[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:

```python
jupyter.stop_execution()
```

Here are the data transfer objects which represent execution results:

```python
@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](https://github.com/OpenInterpreter/open-interpreter) and [E2B Code Interpreter](https://github.com/e2b-dev/code-interpreter)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/keell0renz/langcode",
    "name": "langcode",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.7",
    "maintainer_email": null,
    "keywords": null,
    "author": "Your Name",
    "author_email": "you@example.com",
    "download_url": "https://files.pythonhosted.org/packages/0f/ec/88ade659db4a46ecd2e31f04bd6c255645999129969d9edb975edbd7809c/langcode-0.1.6.tar.gz",
    "platform": null,
    "description": "# \ud83d\udcbb\ud83d\udd17 LangCode\n\n\u26a1 Build code-executing autonomous agents \u26a1\n\n## Documentation\n\n\ud83d\udcbb\ud83d\udd17 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.\n\n### Example\n\n```bash\npip install langcode\n```\n\n`INTERESTING`: You can find `claude_agent.ipynb` in `examples` which demonstrates a real-world application of LangCode using Claude 3.5 Sonnet\n\n```python\nfrom langcode.jupyter import Jupyter, ExecutionEvent, ExecutionResult\n\ndef process_execution_event(x: ExecutionEvent) -> None:\n    ...\n\njupyter = Jupyter.local(\n    env=\"...\", # Set the execution environment.\n    timeout=None, # Set global timeout (can be overriden later).\n    event_handler: lambda x: process_execution_event(x) # Pass event handler if you need.\n)\n\nresult: ExecutionResult = jupyter.run_cell(code=\"x = 10; x\", timeout=None) # Final result.\n\nfor event in jupyter.stream_cell(code=\"import time; time.sleep(5)\", timeout=1000): # Or stream events in real time.\n    event: ExecutionEvent\n\n    # This will raise TimeoutError after 1 second.\n\njupyter.restart() # You can also restart kernel to clear the cell state.\n\njupyter.close() # Close the notebook and Jupyter kernel.\n```\n\nYou can also get images outputted by Jupyter kernel:\n\n```python\ncode = \"\"\"\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nx = np.linspace(0, 2 * np.pi, 200)\ny = np.sin(x)\n\nfig, ax = plt.subplots()\nax.plot(x, y)\nax.set(xlabel='x', ylabel='sin(x)',\n       title='A simple plot')\n\nplt.show()\n\n\"\"\"\n\nresult = jupyter.run_cell(code)\n\nresult.images\n```\n\n```python\n[Base64ImageString(content_format='png', content='iVBORw0KGg...')]\n```\n\nYou can also get images in `ExecutionEvent` data transfer object when you use `stream_cell`.\n\nAlso you can stop the execution in the process if you need:\n\n```python\njupyter.stop_execution()\n```\n\nHere are the data transfer objects which represent execution results:\n\n```python\n@dataclass\nclass ExecutionEvent:\n    \"\"\"\n    An execution event which can be outputted in real time into user UI iteratively.\n    \"\"\"\n\n    msg_type: Literal[\"stream\", \"error\", \"display_data\", \"execute_result\"]\n    content_type: Literal[\"console\", \"image\", \"code\"]\n    content_format: Literal[\"output\", \"base64/png\", \"base64/jpeg\", \"html\", \"javascript\"]\n    content: str\n\n\n@dataclass\nclass Base64ImageString:\n    \"\"\"Represents an image in `base64` form, either `png` or `jpeg`.\"\"\"\n\n    content_format: Literal[\"png\", \"jpeg\"]\n    content: str\n\n\n@dataclass\nclass ExecutionResult:\n    \"\"\"\n    Final result of code execution inside Jupyter notebook.\n    \"\"\"\n\n    events: List[ExecutionEvent]\n    \"\"\"List of all `ExecutionEvent` events outputted during execution.\"\"\"\n\n    error: bool\n    \"\"\"Signals whether an error has occured during execution, `True` if error occured.\"\"\"\n\n    text: str\n    \"\"\"Final text, excluding images, which can be injected into LLM.\"\"\"\n\n    images: List[ImageString]\n    \"\"\"Final list of `base64` images outputted during execution, can be injected into LLM.\"\"\"\n```\n\nGot inspired and borrowed the code from [Open Interpreter](https://github.com/OpenInterpreter/open-interpreter) and [E2B Code Interpreter](https://github.com/e2b-dev/code-interpreter)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": null,
    "version": "0.1.6",
    "project_urls": {
        "Documentation": "https://github.com/keell0renz/langcode",
        "Homepage": "https://github.com/keell0renz/langcode",
        "Repository": "https://github.com/keell0renz/langcode"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c98a9cb86265b13066cfcb572a2b2f1459eeff00c6c3039d6d8eeecbf3e5952",
                "md5": "468439be9ae78a8605f51dd398c2af83",
                "sha256": "e2750ac2810cf4f6ab65490433e812789466cbfcf2c1a9605c25980a381e12c4"
            },
            "downloads": -1,
            "filename": "langcode-0.1.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "468439be9ae78a8605f51dd398c2af83",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.7",
            "size": 7694,
            "upload_time": "2024-07-16T13:01:45",
            "upload_time_iso_8601": "2024-07-16T13:01:45.560221Z",
            "url": "https://files.pythonhosted.org/packages/7c/98/a9cb86265b13066cfcb572a2b2f1459eeff00c6c3039d6d8eeecbf3e5952/langcode-0.1.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0fec88ade659db4a46ecd2e31f04bd6c255645999129969d9edb975edbd7809c",
                "md5": "d9f8848056da9ae92da83c6b1b615dc1",
                "sha256": "a81873a62ba27e3126f0bbe737e1cf31344129c3a60b17962d087c820e7cb915"
            },
            "downloads": -1,
            "filename": "langcode-0.1.6.tar.gz",
            "has_sig": false,
            "md5_digest": "d9f8848056da9ae92da83c6b1b615dc1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.7",
            "size": 5750,
            "upload_time": "2024-07-16T13:01:48",
            "upload_time_iso_8601": "2024-07-16T13:01:48.324802Z",
            "url": "https://files.pythonhosted.org/packages/0f/ec/88ade659db4a46ecd2e31f04bd6c255645999129969d9edb975edbd7809c/langcode-0.1.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-16 13:01:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "keell0renz",
    "github_project": "langcode",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "langcode"
}
        
Elapsed time: 0.27897s