Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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-3.9.1-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 URLLib3Stream(httpx.SyncByteStream):
- def __init__(self, stream):
- self._stream = stream
- def __iter__(self):
- # print(self._stream)
- # print(dir(self._stream))
- # print(self._stream.__self__)
- # print(dir(self._stream.__self__))
- print(self._stream.__self__.data)
- with httpx._transports.default.map_httpcore_exceptions():
- for part in self._stream:
- yield part
- def close(self):
- if hasattr(self._stream, "close"):
- self._stream.close()
- class URLLib3Transport(httpx.BaseTransport):
- def __init__(self):
- # self._pool = urllib3.PoolManager()
- self._pool = urllib3.connectionpool.ConnectionPool(host="localhost")
- def handle_request(self, request: httpx.Request):
- payload = json.loads(request.content.decode("utf-8").replace("'",'"'))
- urllib3_response = urllib3.request(
- method=request.method,
- url=str(request.url),
- headers=request.headers,
- json=payload,
- # stream=True,
- preload_content=False
- )
- headers = [(b"content-type", b"application/json")]
- return httpx.Response(
- 200,
- headers=headers,
- stream=URLLib3Stream(urllib3_response.stream)
- )
- client = httpx.Client(transport=URLLib3Transport())
- from openai import OpenAI
- from openai import AzureOpenAI
- import openai
- import os
- os.environ['AZURE_OPENAI_API_KEY'] = "xxx"
- openai_client = AzureOpenAI(
- api_version="2023-07-01-preview",
- azure_endpoint="https://xxx.openai.azure.com/",
- http_client=client
- )
- response = openai_client.chat.completions.with_raw_response.create(
- messages=[{
- "role": "user",
- "content": "sing me a song",
- }],
- model="gpt-35-turbo",
- max_tokens=30,
- temperature=0.7,
- # stream=True
- )
- completion = response.parse()
- print(completion)
Advertisement
Add Comment
Please, Sign In to add comment