Guest User

Streaming response wrapping for custom transport layers (error 3)

a guest
Feb 21st, 2024
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | Source Code | 0 0
  1. import micropip
  2. await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/multidict/multidict-4.7.6-py3-none-any.whl', keep_going=True)
  3. await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/frozenlist/frozenlist-1.4.0-py3-none-any.whl', keep_going=True)
  4. await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/aiohttp/aiohttp-3.9.1-py3-none-any.whl', keep_going=True)
  5. await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/openai/openai-1.3.7-py3-none-any.whl', keep_going=True)
  6. await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/urllib3/urllib3-2.1.0-py3-none-any.whl', keep_going=True)
  7.  
  8. await micropip.install("ssl")
  9. import ssl
  10. await micropip.install("httpx", keep_going=True)
  11. import httpx
  12. await micropip.install('https://raw.githubusercontent.com/psymbio/pyodide_wheels/main/urllib3/urllib3-2.1.0-py3-none-any.whl', keep_going=True)
  13. import urllib3
  14. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  15. import json
  16.  
  17. class URLLib3Stream(httpx.SyncByteStream):
  18.     def __init__(self, stream):
  19.         self._stream = stream
  20.  
  21.     def __iter__(self):
  22.         # print(self._stream)
  23.         # print(dir(self._stream))
  24.         # print(self._stream.__self__)
  25.         # print(dir(self._stream.__self__))
  26.         print(self._stream.__self__.data)
  27.         with httpx._transports.default.map_httpcore_exceptions():
  28.             for part in self._stream:
  29.                 yield part
  30.  
  31.     def close(self):
  32.         if hasattr(self._stream, "close"):
  33.             self._stream.close()
  34.  
  35.  
  36. class URLLib3Transport(httpx.BaseTransport):
  37.     def __init__(self):
  38.         # self._pool = urllib3.PoolManager()
  39.         self._pool = urllib3.connectionpool.ConnectionPool(host="localhost")
  40.  
  41.     def handle_request(self, request: httpx.Request):
  42.         payload = json.loads(request.content.decode("utf-8").replace("'",'"'))
  43.         urllib3_response = urllib3.request(
  44.             method=request.method,
  45.             url=str(request.url),
  46.             headers=request.headers,
  47.             json=payload,
  48.             # stream=True,
  49.             preload_content=False
  50.         )
  51.         headers = [(b"content-type", b"application/json")]
  52.         return httpx.Response(
  53.             200,
  54.             headers=headers,
  55.             stream=URLLib3Stream(urllib3_response.stream)
  56.         )
  57.  
  58. client = httpx.Client(transport=URLLib3Transport())
  59.  
  60. from openai import OpenAI
  61. from openai import AzureOpenAI
  62. import openai
  63. import os
  64.  
  65. os.environ['AZURE_OPENAI_API_KEY'] = "xxx"
  66. openai_client = AzureOpenAI(
  67.     api_version="2023-07-01-preview",
  68.     azure_endpoint="https://xxx.openai.azure.com/",
  69.     http_client=client
  70. )
  71.  
  72. response = openai_client.chat.completions.with_raw_response.create(
  73.     messages=[{
  74.         "role": "user",
  75.         "content": "sing me a song",
  76.     }],
  77.     model="gpt-35-turbo",
  78.     max_tokens=30,
  79.     temperature=0.7,
  80.     # stream=True
  81. )
  82.  
  83. completion = response.parse()
  84. print(completion)
Advertisement
Add Comment
Please, Sign In to add comment