There's some efforts to remedy this, specifically https://github.com/encode/httpx/discussions/2994 suggests adding a transport layer based on urllib3 to httpx. This could already be patched in mistralai itself if we detect we are running in a pyodide context.
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)
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 executionTogether with Ace editor it's simple to integrate live code execution
mistralai
pyodide supportCurrently mistralai depends on
pydantic-core==2.20.1
Pydantic does publish
emscripten
wheels :However they are compiled with
emscripten 3.1.46
, current version ofpyodide
are compiled withempscripten 3.1.58
We can patch
react-py
with old version ofpyodide
(0.25.0)mistralai uses
httpx
which is not yet supported in pyodide https://github.com/koenvo/pyodide-http/issues/37mistralai
itself if we detect we are running in a pyodide context.API Token Problem
ToDo
httpx
requests in pyodide to the Python SDKThe patch looks something like this (