Skip to content

Add pyodide execution support #126

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

philippjfr
Copy link

As discussed in our meeting today, here's my prototype of adding live code execution to the docs along with my notes:

Live Code Execution

  • Since docs are already based on docusaurus there's no point switching to some other solution
  • Docusaurus offers react-py for pyodide based execution
  • Together with Ace editor it's simple to integrate live code execution

mistralai pyodide support

API Token Problem

  • Since pyodide runs in the browser users could access any tokens that are used
  • We could set up a backend proxy server that interacts with Mistral API
  • How do we authenticate with this proxy server?
    • Docs pages currently do not require login or OAuth
    • OAuth tokens probably best and safest way to avoid abuse
    • User authenticates and is given an access_token, we somehow pass the access_token through to the proxy server which checks rate limits and then forwards the request to Mistral API

ToDo

  • Add theme handling to the code editor (i.e. switch the ace theme based on dark/light mode)
  • Add a patch to support httpx requests in pyodide to the Python SDK

The patch looks something like this (

import httpx
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import json

class URLLib3Transport(httpx.BaseTransport):
    def __init__(self):
        self.pool = urllib3.PoolManager()

    def handle_request(self, request: httpx.Request):
        body = json.loads(request.content.decode('utf-8'))  # Decode the data and load as JSON
        urllib3_response = self.pool.request(request.method, str(request.url), body=request.content, headers=dict(request.headers))  # Convert httpx.URL to string
        content = json.loads(urllib3_response.data.decode('utf-8'))  # Decode the data and load as JSON
        stream = httpx.ByteStream(json.dumps(content).encode("utf-8"))  # Convert back to JSON and encode
        headers = [(b"content-type", b"application/json")]
        return httpx.Response(200, headers=headers, stream=stream)

httpx_client = httpx.Client(transport=URLLib3Transport())

client = Mistral(api_key=api_key, client=httpx_client)

@philippjfr
Copy link
Author

Oh, one more thing, you have to manually patch the pyodide version by editing:

/node_modules/react-py/dist/workers/service-worker.js

and changing the pyodide version to 0.25.0.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant