Skip to content

Issue with custom transport #960

New issue

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

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

Already on GitHub? Sign in to your account

Closed
1 task done
psymbio opened this issue Dec 9, 2023 · 2 comments
Closed
1 task done

Issue with custom transport #960

psymbio opened this issue Dec 9, 2023 · 2 comments

Comments

@psymbio
Copy link

psymbio commented Dec 9, 2023

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

So, I'm trying to resolve pyodide/pyodide#4292

With these fixes in place: urllib3/urllib3#3195, encode/httpx#2994, and the custom transport feature in the library the solution is more feasible.

To Reproduce

Testing environment: https://jupyter.org/try-jupyter/lab/

import micropip
await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/multidict/multidict-4.7.6-py3-none-any.whl', keep_going=True)
await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/frozenlist/frozenlist-1.4.0-py3-none-any.whl', keep_going=True)
await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/aiohttp/aiohttp-4.0.0a2.dev0-py3-none-any.whl', keep_going=True)
await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/openai/openai-1.3.7-py3-none-any.whl', keep_going=True)
await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/urllib3/urllib3-2.1.0-py3-none-any.whl', keep_going=True)
await micropip.install("ssl")
import ssl
await micropip.install("httpx", keep_going=True)
import httpx
await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/urllib3/urllib3-2.1.0-py3-none-any.whl', keep_going=True)
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):
        urllib3_response = self.pool.request('GET', str(request.url))  # 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)

client = httpx.Client(transport=URLLib3Transport())
from openai import OpenAI

openai_client = OpenAI(
    base_url="https://api.openai.com/v1",
    api_key="xxx",
    http_client=client
)
response = openai_client.chat.completions.with_raw_response.create(
    messages=[{
        "role": "user",
        "content": "Say this is a test",
    }],
    model="gpt-3.5-turbo",
)
completion = response.parse()
print(completion)

But I get this output:

ChatCompletion(id=None, choices=None, created=None, model=None, object=None, system_fingerprint=None, usage=None, error={'message': "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.", 'type': 'invalid_request_error', 'param': None, 'code': None})

I have provided the API key - so what am I doing wrong here?

Code snippets

No response

OS

Pyodide

Python version

Python v3.11.3

Library version

openai v1.3.7

@psymbio psymbio added the bug Something isn't working label Dec 9, 2023
@rattrayalex rattrayalex removed the bug Something isn't working label Dec 9, 2023
@rattrayalex
Copy link
Collaborator

Hi @psymbio , thanks for sharing your findings. Unfortunately we can't commit to providing support to help debug issues with user code in this way.

In this case, this line looks suspect to me:

urllib3_response = self.pool.request('GET', str(request.url))

You don't seem to be passing the request headers along here (or the body, and the method looks incorrect as well).

If you need further help from here, you may be able to ask fellow developers in the OpenAI Discord server.

@psymbio
Copy link
Author

psymbio commented Dec 10, 2023

You were right, thanks!

The correct way of doing it was:

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

    def handle_request(self, request: httpx.Request):
        payload = json.loads(request.content.decode("utf-8").replace("'",'"'))
        urllib3_response = self.pool.request(request.method, str(request.url), headers=request.headers, json=payload)  # 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)

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

No branches or pull requests

2 participants