Closed
Description
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
Metadata
Metadata
Assignees
Labels
No labels