2023-07-25 02:42:53 +02:00
|
|
|
import os
|
2023-07-25 19:45:21 +02:00
|
|
|
import aiohttp
|
|
|
|
|
|
|
|
import proxies
|
2023-07-25 02:42:53 +02:00
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
2023-07-25 19:45:21 +02:00
|
|
|
from request_manager import Request
|
2023-07-25 02:42:53 +02:00
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2023-07-25 19:45:21 +02:00
|
|
|
async def stream_closedai_request(request: Request):
|
2023-07-25 02:42:53 +02:00
|
|
|
async with aiohttp.ClientSession(
|
2023-07-25 19:45:21 +02:00
|
|
|
connector=await proxies.default_proxy.get_connector(),
|
|
|
|
timeout=aiohttp.ClientTimeout(total=request.timeout),
|
2023-07-25 02:42:53 +02:00
|
|
|
raise_for_status=False
|
2023-07-25 19:45:21 +02:00
|
|
|
) as session:
|
2023-07-25 02:42:53 +02:00
|
|
|
async with session.request(
|
2023-07-25 19:45:21 +02:00
|
|
|
method=request.method,
|
|
|
|
url=request.url,
|
|
|
|
json=request.payload,
|
2023-07-25 02:42:53 +02:00
|
|
|
headers={
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': f'Bearer {os.getenv("CLOSEDAI_KEY")}'
|
|
|
|
}
|
|
|
|
) as response:
|
|
|
|
async for chunk in response.content.iter_any():
|
|
|
|
chunk = f'{chunk.decode("utf8")}\n\n'
|
|
|
|
yield chunk
|
2023-07-25 19:45:21 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
pass
|