openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Set `jiter` as optional dependency to support `pyodide` (~3 lines diff)

Open CNSeniorious000 opened this issue 4 months ago • 7 comments

Confirm this is a feature request for the Python library and not the underlying OpenAI API.

  • [X] This is a feature request for the Python library

Describe the feature or improvement you're requesting

Pyodide currently don't support jiter. openai-python use it for partial json parsing. But it is just used in 2 lines.

If we move jiter into optional-dependencies, we will be able to use openai-python in pyodide runtime.

Once upon a time, httpx is blocking openai from pyodide too. But that issue is already resolved. The only barrier is jiter now.

About openai, pyodide and httpx

I've checked these issue:

  • #815
  • #960

At that time, openai is not compatible with pyodide because of httpx. Now there even exist a pyodide-httpx to patch httpx in pyodide

If we can use openai in pyodide, it will be possible to provide interactive python demos in the browser for prompt engineering frameworks, which I think is a great feature to have.

Additional context

Another way is to use another package to parse partial json. There is a package called partial-json-parser which did the almost same job as jiter.from_json, but also providing more flexibility on specifying which types are allowed to be incomplete. And it keep types too. For the latter one, let me present an example:

from jiter import from_json
from_json(b'{"a": [1', partial_mode=True)  # {'a': [1]}
from_json(b'{"a": [1.', partial_mode=True)  # {'a': []}

In the example above, tokens increase but parsed value disappeared.

Plus, partial-json-parser's API is consistent among its Python/JavaScript/Go implementations.

I tried a bit to replace jiter by partial_json_parser:

https://github.com/openai/openai-python/commit/7419b7059f5e024aa9b87942b47ecff80a6b32b5#diff-08dc4c3c3e8e145eec1fd0b6a4577f0bce73567d4da3460e08dd4c2d34b27915

CNSeniorious000 avatar Oct 08 '24 08:10 CNSeniorious000